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  const std::string& 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) 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
openmsx::HardwareConfig
HardwareConfig
Definition: HardwareConfig.cc:476
openmsx::CassettePlayer::CassettePlayer
CassettePlayer(const HardwareConfig &hwConf)
Definition: CassettePlayer.cc:69
openmsx::ResampledSoundDevice
Definition: ResampledSoundDevice.hh:17
openmsx::CassettePlayer::getAmplificationFactorImpl
float getAmplificationFactorImpl() const override
Get amplification/attenuation factor for this device.
Definition: CassettePlayer.cc:574
openmsx::CassettePlayer::serialize
void serialize(Archive &ar, unsigned version)
Definition: CassettePlayer.cc:852
openmsx::CassettePlayer::generateChannels
void generateChannels(float **buffers, unsigned num) override
Abstract method to generate the actual sound data.
Definition: CassettePlayer.cc:563
serialize_meta.hh
EventListener.hh
gl::length
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:348
Schedulable.hh
CassetteDevice.hh
Filename.hh
openmsx::CassettePlayer::getName
const std::string & getName() const override
Name used to identify this pluggable.
Definition: CassettePlayer.cc:535
openmsx::CassettePlayer::setSignal
void setSignal(bool output, EmuTime::param time) override
Sets the cassette output signal false = low true = high.
Definition: CassettePlayer.cc:439
openmsx::CassettePlayer::setMotor
void setMotor(bool status, EmuTime::param time) override
Sets the cassette motor relay false = off true = on.
Definition: CassettePlayer.cc:409
openmsx::CassettePlayer::PLAY
@ PLAY
Definition: CassettePlayer.hh:50
openmsx::CassettePlayer::~CassettePlayer
~CassettePlayer() override
Definition: CassettePlayer.cc:102
BooleanSetting.hh
openmsx::CassettePlayer::State
State
Definition: CassettePlayer.hh:50
span
Definition: span.hh:126
openmsx::SERIALIZE_CLASS_VERSION
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
openmsx::CassettePlayer::plugHelper
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: CassettePlayer.cc:550
openmsx::CassettePlayer::RECORD
@ RECORD
Definition: CassettePlayer.hh:50
OUTER
#define OUTER(type, member)
Definition: outer.hh:41
RecordedCommand.hh
openmsx::CassettePlayer::unplugHelper
void unplugHelper(EmuTime::param time) override
Definition: CassettePlayer.cc:556
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::CassetteDevice
Definition: CassetteDevice.hh:10
openmsx::EventListener
Definition: EventListener.hh:11
ThrottleManager.hh
openmsx::CassettePlayer::getDescription
std::string_view getDescription() const override
Description for this pluggable.
Definition: CassettePlayer.cc:541
openmsx::CassettePlayer::readSample
int16_t readSample(EmuTime::param time) override
Read wave data from cassette device.
Definition: CassettePlayer.cc:427
outer.hh
openmsx::x
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1416
openmsx::Schedulable
Schedulable
Definition: Schedulable.cc:69
EmuTime.hh
openmsx::Filename
Filename
Definition: Filename.cc:50
openmsx::Connector
Represents something you can plug devices into.
Definition: Connector.hh:21
openmsx::HardwareConfig
Definition: HardwareConfig.hh:22
openmsx::Scheduler
Scheduler
Definition: Scheduler.cc:132
openmsx::CassettePlayer
Definition: CassettePlayer.hh:27
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::BUF_SIZE
constexpr int BUF_SIZE
Definition: CliConnection.cc:161
openmsx::CassettePlayer::STOP
@ STOP
Definition: CassettePlayer.hh:50
openmsx::EmuDuration::param
const EmuDuration & param
Definition: EmuDuration.hh:27
ResampledSoundDevice.hh