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 "MSXMotherBoard.hh"
8#include "RecordedCommand.hh"
9#include "Schedulable.hh"
10#include "ThrottleManager.hh"
11#include "Filename.hh"
12#include "EmuTime.hh"
13#include "BooleanSetting.hh"
14#include "outer.hh"
15#include "serialize_meta.hh"
16#include <array>
17#include <cstdint>
18#include <memory>
19#include <string>
20
21namespace openmsx {
22
23class CassetteImage;
24class HardwareConfig;
25class Wav8Writer;
26
28 , public MediaInfoProvider
29 , private EventListener
30{
31public:
32 static constexpr std::string_view TAPE_RECORDING_DIR = "taperecordings";
33 static constexpr std::string_view TAPE_RECORDING_EXTENSION = ".wav";
34
35public:
36 explicit CassettePlayer(const HardwareConfig& hwConf);
37 ~CassettePlayer() override;
38
39 // CassetteDevice
40 void setMotor(bool status, EmuTime::param time) override;
41 int16_t readSample(EmuTime::param time) override;
42 void setSignal(bool output, EmuTime::param time) override;
43
44 // Pluggable
45 std::string_view getName() const override;
46 std::string_view getDescription() const override;
47 void plugHelper(Connector& connector, EmuTime::param time) override;
48 void unplugHelper(EmuTime::param time) override;
49
50 // SoundDevice
51 void generateChannels(std::span<float*> buffers, unsigned num) override;
52 float getAmplificationFactorImpl() const override;
53
54 // MediaInfoProvider
55 void getMediaInfo(TclObject& result) override;
56
57 template<typename Archive>
58 void serialize(Archive& ar, unsigned version);
59
60 enum State { PLAY, RECORD, STOP }; // public for serialization
61
62private:
63 [[nodiscard]] State getState() const { return state; }
64 [[nodiscard]] std::string getStateString() const;
65 void setState(State newState, const Filename& newImage,
66 EmuTime::param time);
67 void setImageName(const Filename& newImage);
68 const Filename& getImageName() const { return casImage; }
69 void checkInvariants() const;
70
73 void playTape(const Filename& filename, EmuTime::param time);
74 void insertTape(const Filename& filename, EmuTime::param time);
75
78 void removeTape(EmuTime::param time);
79
83 void recordTape(const Filename& filename, EmuTime::param time);
84
89 void rewind(EmuTime::param time);
90 void wind(EmuTime::param time);
91
94 void setMotorControl(bool status, EmuTime::param time);
95
99 bool isRolling() const;
100
105 void updateLoadingState(EmuTime::param time);
106
109 double getTapePos(EmuTime::param time);
110
113 void setTapePos(EmuTime::param time, double newPos);
114
119 double getTapeLength(EmuTime::param time);
120
121 void sync(EmuTime::param time);
122 void updateTapePosition(EmuDuration::param duration, EmuTime::param time);
123 void generateRecordOutput(EmuDuration::param duration);
124
125 void fillBuf(size_t length, double x);
126 void flushOutput();
127 void autoRun();
128
129 // EventListener
130 bool signalEvent(const Event& event) override;
131
132 // Schedulable
133 struct SyncEndOfTape final : Schedulable {
134 friend class CassettePlayer;
135 explicit SyncEndOfTape(Scheduler& s) : Schedulable(s) {}
136 void executeUntil(EmuTime::param time) override {
137 auto& cp = OUTER(CassettePlayer, syncEndOfTape);
138 cp.execEndOfTape(time);
139 }
140 } syncEndOfTape;
141 struct SyncAudioEmu final : Schedulable {
142 friend class CassettePlayer;
143 explicit SyncAudioEmu(Scheduler& s) : Schedulable(s) {}
144 void executeUntil(EmuTime::param time) override {
145 auto& cp = OUTER(CassettePlayer, syncAudioEmu);
146 cp.execSyncAudioEmu(time);
147 }
148 } syncAudioEmu;
149
150 void execEndOfTape(EmuTime::param time);
151 void execSyncAudioEmu(EmuTime::param time);
152 EmuTime::param getCurrentTime() const { return syncEndOfTape.getCurrentTime(); }
153
154 std::array<uint8_t, 1024> buf;
155
156 double lastX; // last unfiltered output
157 double lastY; // last filtered output
158 double partialOut;
159 double partialInterval;
160
162 EmuTime tapePos = EmuTime::zero();
163
166 EmuTime prevSyncTime = EmuTime::zero();
167
168 // SoundDevice
169 unsigned audioPos = 0;
170 Filename casImage;
171
172 MSXMotherBoard& motherBoard;
173
174 struct TapeCommand final : RecordedCommand {
175 TapeCommand(CommandController& commandController,
176 StateChangeDistributor& stateChangeDistributor,
177 Scheduler& scheduler);
178 void execute(std::span<const TclObject> tokens, TclObject& result,
179 EmuTime::param time) override;
180 [[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
181 void tabCompletion(std::vector<std::string>& tokens) const override;
182 [[nodiscard]] bool needRecord(std::span<const TclObject> tokens) const override;
183 } tapeCommand;
184
185 LoadingIndicator loadingIndicator;
186 BooleanSetting autoRunSetting;
187 std::unique_ptr<Wav8Writer> recordImage;
188 std::unique_ptr<CassetteImage> playImage;
189
190 size_t sampCnt = 0;
191 State state = STOP;
192 bool lastOutput = false;
193 bool motor = false, motorControl = true;
194 bool syncScheduled = false;
195};
197
198} // namespace openmsx
199
200#endif
void plugHelper(Connector &connector, EmuTime::param time) override
static constexpr std::string_view TAPE_RECORDING_DIR
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 generateChannels(std::span< float * > buffers, unsigned num) override
Abstract method to generate the actual sound data.
void setMotor(bool status, EmuTime::param time) override
Sets the cassette motor relay false = off true = on.
void serialize(Archive &ar, unsigned version)
int16_t readSample(EmuTime::param time) override
Read wave data from cassette device.
static constexpr std::string_view TAPE_RECORDING_EXTENSION
void getMediaInfo(TclObject &result) override
This method gets called when information is required on the media inserted in the media slot of the p...
Represents something you can plug devices into.
Definition Connector.hh:21
const EmuDuration & param
Schedulable(const Schedulable &)=delete
This file implemented 3 utility functions:
Definition Autofire.cc:11
std::variant< KeyUpEvent, KeyDownEvent, MouseMotionEvent, MouseButtonUpEvent, MouseButtonDownEvent, MouseWheelEvent, JoystickAxisMotionEvent, JoystickHatEvent, JoystickButtonUpEvent, JoystickButtonDownEvent, OsdControlReleaseEvent, OsdControlPressEvent, WindowEvent, TextEvent, FileDropEvent, QuitEvent, FinishFrameEvent, CliCommandEvent, GroupEvent, BootEvent, FrameDrawnEvent, BreakEvent, SwitchRendererEvent, TakeReverseSnapshotEvent, AfterTimedEvent, MachineLoadedEvent, MachineActivatedEvent, MachineDeactivatedEvent, MidiInReaderEvent, MidiInWindowsEvent, MidiInCoreMidiEvent, MidiInCoreMidiVirtualEvent, MidiInALSAEvent, Rs232TesterEvent, Rs232NetEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:445
#define OUTER(type, member)
Definition outer.hh:42
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)