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 explicit CassettePlayer(const HardwareConfig& hwConf);
33 ~CassettePlayer() override;
34
35 // CassetteDevice
36 void setMotor(bool status, EmuTime::param time) override;
37 int16_t readSample(EmuTime::param time) override;
38 void setSignal(bool output, EmuTime::param time) override;
39
40 // Pluggable
41 std::string_view getName() const override;
42 std::string_view getDescription() const override;
43 void plugHelper(Connector& connector, EmuTime::param time) override;
44 void unplugHelper(EmuTime::param time) override;
45
46 // SoundDevice
47 void generateChannels(std::span<float*> buffers, unsigned num) override;
48 float getAmplificationFactorImpl() const override;
49
50 // MediaInfoProvider
51 void getMediaInfo(TclObject& result) override;
52
53 template<typename Archive>
54 void serialize(Archive& ar, unsigned version);
55
56 enum State { PLAY, RECORD, STOP }; // public for serialization
57
58private:
59 [[nodiscard]] State getState() const { return state; }
60 [[nodiscard]] std::string getStateString() const;
61 void setState(State newState, const Filename& newImage,
62 EmuTime::param time);
63 void setImageName(const Filename& newImage);
64 const Filename& getImageName() const { return casImage; }
65 void checkInvariants() const;
66
69 void playTape(const Filename& filename, EmuTime::param time);
70 void insertTape(const Filename& filename, EmuTime::param time);
71
74 void removeTape(EmuTime::param time);
75
79 void recordTape(const Filename& filename, EmuTime::param time);
80
85 void rewind(EmuTime::param time);
86
89 void setMotorControl(bool status, EmuTime::param time);
90
94 bool isRolling() const;
95
100 void updateLoadingState(EmuTime::param time);
101
104 double getTapePos(EmuTime::param time);
105
110 double getTapeLength(EmuTime::param time);
111
112 void sync(EmuTime::param time);
113 void updateTapePosition(EmuDuration::param duration, EmuTime::param time);
114 void generateRecordOutput(EmuDuration::param duration);
115
116 void fillBuf(size_t length, double x);
117 void flushOutput();
118 void autoRun();
119
120 // EventListener
121 int signalEvent(const Event& event) override;
122
123 // Schedulable
124 struct SyncEndOfTape final : Schedulable {
125 friend class CassettePlayer;
126 explicit SyncEndOfTape(Scheduler& s) : Schedulable(s) {}
127 void executeUntil(EmuTime::param time) override {
128 auto& cp = OUTER(CassettePlayer, syncEndOfTape);
129 cp.execEndOfTape(time);
130 }
131 } syncEndOfTape;
132 struct SyncAudioEmu final : Schedulable {
133 friend class CassettePlayer;
134 explicit SyncAudioEmu(Scheduler& s) : Schedulable(s) {}
135 void executeUntil(EmuTime::param time) override {
136 auto& cp = OUTER(CassettePlayer, syncAudioEmu);
137 cp.execSyncAudioEmu(time);
138 }
139 } syncAudioEmu;
140
141 void execEndOfTape(EmuTime::param time);
142 void execSyncAudioEmu(EmuTime::param time);
143 EmuTime::param getCurrentTime() const { return syncEndOfTape.getCurrentTime(); }
144
145 std::array<uint8_t, 1024> buf;
146
147 double lastX; // last unfiltered output
148 double lastY; // last filtered output
149 double partialOut;
150 double partialInterval;
151
153 EmuTime tapePos = EmuTime::zero();
154
157 EmuTime prevSyncTime = EmuTime::zero();
158
159 // SoundDevice
160 unsigned audioPos = 0;
161 Filename casImage;
162
163 MSXMotherBoard& motherBoard;
164
165 struct TapeCommand final : RecordedCommand {
166 TapeCommand(CommandController& commandController,
167 StateChangeDistributor& stateChangeDistributor,
168 Scheduler& scheduler);
169 void execute(std::span<const TclObject> tokens, TclObject& result,
170 EmuTime::param time) override;
171 [[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
172 void tabCompletion(std::vector<std::string>& tokens) const override;
173 [[nodiscard]] bool needRecord(std::span<const TclObject> tokens) const override;
174 } tapeCommand;
175
176 LoadingIndicator loadingIndicator;
177 BooleanSetting autoRunSetting;
178 std::unique_ptr<Wav8Writer> recordImage;
179 std::unique_ptr<CassetteImage> playImage;
180
181 size_t sampCnt = 0;
182 State state = STOP;
183 bool lastOutput = false;
184 bool motor = false, motorControl = true;
185 bool syncScheduled = false;
186};
188
189} // namespace openmsx
190
191#endif
void plugHelper(Connector &connector, EmuTime::param time) override
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.
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:9
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:454
#define OUTER(type, member)
Definition outer.hh:41
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)