22 unsigned wantedFreq,
unsigned wantedSamples)
25 SDL_AudioSpec desired;
26 desired.freq = narrow<int>(wantedFreq);
27 desired.samples = narrow<Uint16>(std::bit_ceil(wantedSamples));
29 desired.format = AUDIO_F32SYS;
30 desired.callback = audioCallbackHelper;
31 desired.userdata =
this;
33 SDL_AudioSpec obtained;
34 deviceID = SDL_OpenAudioDevice(
nullptr, SDL_FALSE, &desired, &obtained,
35 SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
37 throw MSXException(
"Unable to open SDL audio: ", SDL_GetError());
40 frequency = obtained.freq;
41 fragmentSize = obtained.samples;
43 mixBufferSize = narrow<unsigned>(3 * (obtained.size /
sizeof(
StereoFloat)) + 1);
44 mixBuffer.resize(mixBufferSize);
50 SDL_CloseAudioDevice(deviceID);
53void SDLSoundDriver::reInit()
55 SDL_LockAudioDevice(deviceID);
58 SDL_UnlockAudioDevice(deviceID);
65 SDL_PauseAudioDevice(deviceID, 1);
74 SDL_PauseAudioDevice(deviceID, 0);
88void SDLSoundDriver::audioCallbackHelper(
void* userdata, uint8_t* strm,
int len)
90 assert((len & 7) == 0);
92 audioCallback(std::span{std::bit_cast<StereoFloat*>(strm),
93 len / (2 *
sizeof(float))});
96unsigned SDLSoundDriver::getBufferFilled()
const
98 int result = narrow_cast<int>(writeIdx - readIdx);
99 if (result < 0) result += narrow<int>(mixBufferSize);
100 assert((0 <= result) && (narrow<unsigned>(result) < mixBufferSize));
104unsigned SDLSoundDriver::getBufferFree()
const
109 unsigned result = mixBufferSize - 1 - getBufferFilled();
110 assert(narrow_cast<int>(result) >= 0);
111 assert(result < mixBufferSize);
115void SDLSoundDriver::audioCallback(std::span<StereoFloat> stream)
117 auto len = stream.size();
119 size_t available = getBufferFilled();
120 auto num = narrow<unsigned>(std::min(len, available));
121 if ((readIdx + num) < mixBufferSize) {
122 ranges::copy(std::span{&mixBuffer[readIdx], num}, stream);
125 unsigned len1 = mixBufferSize - readIdx;
126 ranges::copy(std::span{&mixBuffer[readIdx], len1}, stream);
127 unsigned len2 = num - len1;
128 ranges::copy(std::span{&mixBuffer[0], len2}, stream.subspan(len1));
131 auto missing = narrow_cast<ptrdiff_t>(len - available);
140 SDL_LockAudioDevice(deviceID);
141 unsigned free = getBufferFree();
142 if (buffer.size() > free) {
144 if (board && !board->getMSXMixer().isSynchronousMode() &&
147 SDL_UnlockAudioDevice(deviceID);
149 SDL_LockAudioDevice(deviceID);
150 board->getRealTime().resync();
151 free = getBufferFree();
152 }
while (buffer.size() > free);
155 buffer = buffer.subspan(0, free);
158 assert(buffer.size() <= free);
159 if ((writeIdx + buffer.size()) < mixBufferSize) {
161 writeIdx += narrow<unsigned>(buffer.size());
163 unsigned len1 = mixBufferSize - writeIdx;
164 ranges::copy(buffer.subspan(0, len1), &mixBuffer[writeIdx]);
165 unsigned len2 = narrow<unsigned>(buffer.size()) - len1;
166 ranges::copy(buffer.subspan(len1, len2), &mixBuffer[0]);
170 SDL_UnlockAudioDevice(deviceID);
ThrottleManager & getThrottleManager()
Contains the main loop of openMSX.
GlobalSettings & getGlobalSettings()
MSXMotherBoard * getMotherBoard() const
void uploadBuffer(std::span< const StereoFloat > buffer) override
void unmute() override
Unmute the sound system.
void mute() override
Mute the sound system.
~SDLSoundDriver() override
unsigned getFrequency() const override
Returns the actual sample frequency.
SDLSoundDriver(Reactor &reactor, unsigned wantedFreq, unsigned samples)
unsigned getSamples() const override
Get the number of samples that should be created 'per fragment'.
bool isThrottled() const
Ask if throttling is enabled.
void sleep(uint64_t us)
Sleep for the specified amount of time (in us).
This file implemented 3 utility functions:
constexpr void fill(ForwardRange &&range, const T &value)
constexpr auto copy(InputRange &&range, OutputIter out)
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)