openMSX
CassettePlayer.hh
Go to the documentation of this file.
1 #ifndef CASSETTEPLAYER_HH
2 #define CASSETTEPLAYER_HH
3 
4 #include "EventListener.hh"
5 #include "CassetteDevice.hh"
7 #include "RecordedCommand.hh"
8 #include "Schedulable.hh"
9 #include "ThrottleManager.hh"
10 #include "Filename.hh"
11 #include "EmuTime.hh"
12 #include "BooleanSetting.hh"
13 #include "outer.hh"
14 #include "serialize_meta.hh"
15 #include <string>
16 #include <memory>
17 
18 namespace openmsx {
19 
20 class CassetteImage;
21 class HardwareConfig;
22 class MSXMotherBoard;
23 class Wav8Writer;
24 
26  , private EventListener
27 {
28 public:
29  explicit CassettePlayer(const HardwareConfig& hwConf);
30  ~CassettePlayer() override;
31 
32  // CassetteDevice
33  void setMotor(bool status, EmuTime::param time) override;
34  int16_t readSample(EmuTime::param time) override;
35  void setSignal(bool output, EmuTime::param time) override;
36 
37  // Pluggable
38  std::string_view getName() const override;
39  std::string_view getDescription() const override;
40  void plugHelper(Connector& connector, EmuTime::param time) override;
41  void unplugHelper(EmuTime::param time) override;
42 
43  // SoundDevice
44  void generateChannels(float** buffers, unsigned num) override;
45  float getAmplificationFactorImpl() const override;
46 
47  template<typename Archive>
48  void serialize(Archive& ar, unsigned version);
49 
50  enum State { PLAY, RECORD, STOP }; // public for serialization
51 
52 private:
53  State getState() const { return state; }
54  std::string getStateString() const;
55  void setState(State newState, const Filename& newImage,
56  EmuTime::param time);
57  void setImageName(const Filename& newImage);
58  const Filename& getImageName() const { return casImage; }
59  void checkInvariants() const;
60 
63  void playTape(const Filename& filename, EmuTime::param time);
64  void insertTape(const Filename& filename, EmuTime::param time);
65 
68  void removeTape(EmuTime::param time);
69 
73  void recordTape(const Filename& filename, EmuTime::param time);
74 
79  void rewind(EmuTime::param time);
80 
83  void setMotorControl(bool status, EmuTime::param time);
84 
88  bool isRolling() const;
89 
94  void updateLoadingState(EmuTime::param time);
95 
98  double getTapePos(EmuTime::param time);
99 
104  double getTapeLength(EmuTime::param time);
105 
106  void sync(EmuTime::param time);
107  void updateTapePosition(EmuDuration::param duration, EmuTime::param time);
108  void generateRecordOutput(EmuDuration::param duration);
109 
110  void fillBuf(size_t length, double x);
111  void flushOutput();
112  void autoRun();
113 
114  // EventListener
115  int signalEvent(const std::shared_ptr<const Event>& event) noexcept override;
116 
117  // Schedulable
118  struct SyncEndOfTape final : Schedulable {
119  friend class CassettePlayer;
120  explicit SyncEndOfTape(Scheduler& s) : Schedulable(s) {}
121  void executeUntil(EmuTime::param time) override {
122  auto& cp = OUTER(CassettePlayer, syncEndOfTape);
123  cp.execEndOfTape(time);
124  }
125  } syncEndOfTape;
126  struct SyncAudioEmu final : Schedulable {
127  friend class CassettePlayer;
128  explicit SyncAudioEmu(Scheduler& s) : Schedulable(s) {}
129  void executeUntil(EmuTime::param time) override {
130  auto& cp = OUTER(CassettePlayer, syncAudioEmu);
131  cp.execSyncAudioEmu(time);
132  }
133  } syncAudioEmu;
134 
135  void execEndOfTape(EmuTime::param time);
136  void execSyncAudioEmu(EmuTime::param time);
137  EmuTime::param getCurrentTime() const { return syncEndOfTape.getCurrentTime(); }
138 
139  static constexpr size_t BUF_SIZE = 1024;
140  unsigned char buf[BUF_SIZE];
141 
142  double lastX; // last unfiltered output
143  double lastY; // last filtered output
144  double partialOut;
145  double partialInterval;
146 
148  EmuTime tapePos;
149 
152  EmuTime prevSyncTime;
153 
154  // SoundDevice
155  unsigned audioPos;
156  Filename casImage;
157 
158  MSXMotherBoard& motherBoard;
159 
160  struct TapeCommand final : RecordedCommand {
161  TapeCommand(CommandController& commandController,
162  StateChangeDistributor& stateChangeDistributor,
163  Scheduler& scheduler);
164  void execute(span<const TclObject> tokens, TclObject& result,
165  EmuTime::param time) override;
166  [[nodiscard]] std::string help(const std::vector<std::string>& tokens) const override;
167  void tabCompletion(std::vector<std::string>& tokens) const override;
168  [[nodiscard]] bool needRecord(span<const TclObject> tokens) const override;
169  } tapeCommand;
170 
171  LoadingIndicator loadingIndicator;
172  BooleanSetting autoRunSetting;
173  std::unique_ptr<Wav8Writer> recordImage;
174  std::unique_ptr<CassetteImage> playImage;
175 
176  size_t sampcnt;
177  State state;
178  bool lastOutput;
179  bool motor, motorControl;
180  bool syncScheduled;
181 };
183 
184 } // namespace openmsx
185 
186 #endif
void plugHelper(Connector &connector, EmuTime::param time) override
void generateChannels(float **buffers, unsigned num) override
Abstract method to generate the actual sound data.
float getAmplificationFactorImpl() const override
Get amplification/attenuation factor for this device.
std::string_view getName() const override
Name used to identify this pluggable.
std::string_view getDescription() const override
Description for this pluggable.
void setSignal(bool output, EmuTime::param time) override
Sets the cassette output signal false = low true = high.
void unplugHelper(EmuTime::param time) override
void setMotor(bool status, EmuTime::param time) override
Sets the cassette motor relay false = off true = on.
CassettePlayer(const HardwareConfig &hwConf)
void serialize(Archive &ar, unsigned version)
int16_t readSample(EmuTime::param time) override
Read wave data from cassette device.
Represents something you can plug devices into.
Definition: Connector.hh:21
const EmuDuration & param
Definition: EmuDuration.hh:27
Definition: span.hh:126
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
This file implemented 3 utility functions:
Definition: Autofire.cc:5
constexpr const char *const filename
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:124
constexpr int BUF_SIZE
#define OUTER(type, member)
Definition: outer.hh:41