22 #ifndef SCUMM_PLAYERS_PLAYER_APPLEII_H 23 #define SCUMM_PLAYERS_PLAYER_APPLEII_H 25 #include "common/mutex.h" 26 #include "common/scummsys.h" 27 #include "scumm/music.h" 28 #include "audio/audiostream.h" 29 #include "audio/mixer.h" 60 void ensureFree(uint32 needed) {
62 if ((_writePos != 0) && (_writePos == _readPos)) {
68 uint32 freeEndCnt = _capacity - _writePos;
69 if (needed <= freeEndCnt)
72 uint32 avail = availableSize();
75 if (needed <= _readPos + freeEndCnt) {
77 memmove(_data, _data + _readPos, avail);
81 byte *old_data = _data;
82 uint32 new_len = avail + needed;
84 _capacity = new_len + 2048;
85 _data = (byte *)malloc(_capacity);
89 memcpy(_data, old_data + _readPos, avail);
97 uint32 availableSize()
const {
98 if (_readPos >= _writePos)
100 return _writePos - _readPos;
103 uint32 write(
const void *dataPtr, uint32 dataSize) {
104 ensureFree(dataSize);
105 memcpy(_data + _writePos, dataPtr, dataSize);
106 _writePos += dataSize;
110 uint32 read(byte *dataPtr, uint32 dataSize) {
111 uint32 avail = availableSize();
114 if (dataSize > avail)
116 memcpy(dataPtr, _data + _readPos, dataSize);
117 _readPos += dataSize;
129 static const double APPLEII_CPU_CLOCK = 1020484.5;
138 void addSampleToBuffer(
int sample) {
139 int16 value = sample * _volume / _maxVolume;
140 _buffer.write(&value,
sizeof(value));
145 _cyclesPerSampleFP(0),
147 _sampleCyclesSumFP(0),
154 _missingCyclesFP = 0;
155 _sampleCyclesSumFP = 0;
159 uint32 availableSize()
const {
160 return _buffer.availableSize();
163 void setMusicVolume(
int vol) {
164 assert(vol >= 0 && vol <= _maxVolume);
168 void setSampleRate(
int rate) {
170 _cyclesPerSampleFP = int(APPLEII_CPU_CLOCK * (1 << PREC_SHIFT) / rate);
174 void addCycles(byte level,
const int cycles) {
176 int cyclesFP = cycles << PREC_SHIFT;
179 if (_missingCyclesFP > 0) {
180 int n = (_missingCyclesFP < cyclesFP) ? _missingCyclesFP : cyclesFP;
182 _sampleCyclesSumFP += n;
184 _missingCyclesFP -= n;
185 if (_missingCyclesFP == 0) {
186 addSampleToBuffer(2*32767 * _sampleCyclesSumFP / _cyclesPerSampleFP - 32767);
192 _sampleCyclesSumFP = 0;
195 while (cyclesFP >= _cyclesPerSampleFP) {
196 addSampleToBuffer(level ? 32767 : -32767);
197 cyclesFP -= _cyclesPerSampleFP;
202 _missingCyclesFP = _cyclesPerSampleFP - cyclesFP;
204 _sampleCyclesSumFP = cyclesFP;
208 uint32 readSamples(
void *buffer,
int numSamples) {
209 return _buffer.read((byte *)buffer, numSamples * 2) / 2;
213 static const int PREC_SHIFT = 7;
216 int _cyclesPerSampleFP;
217 int _missingCyclesFP;
218 int _sampleCyclesSumFP;
220 static const int _maxVolume = 256;
230 virtual void init(
Player_AppleII *player,
const byte *params) = 0;
232 virtual bool update() = 0;
243 void setSampleRate(
int rate) {
245 _sampleConverter.setSampleRate(rate);
247 void startSound(
int sound)
override;
248 void stopSound(
int sound)
override;
249 void stopAllSounds()
override;
250 int getSoundStatus(
int sound)
const override;
251 int getMusicTimer()
override;
254 int readBuffer(int16 *buffer,
const int numSamples)
override;
257 int getRate()
const override {
return _sampleRate; }
260 void speakerToggle();
261 void generateSamples(
int cycles);
262 void wait(
int interval,
int count);
272 const byte *_params =
nullptr;
274 byte _speakerState = 0;
Definition: player_apple2.h:237
bool isStereo() const override
Definition: player_apple2.h:255
bool endOfData() const override
Definition: player_apple2.h:256
void setMusicVolume(int vol) override
Definition: player_apple2.h:242
Definition: player_apple2.h:42
Definition: player_apple2.h:226
Definition: audiostream.h:50
int getRate() const override
Definition: player_apple2.h:257
Definition: player_apple2.h:136