openMSX
SDLSoundDriver.cc
Go to the documentation of this file.
1#include "SDLSoundDriver.hh"
2
3#include "Mixer.hh"
4#include "Reactor.hh"
5#include "MSXMixer.hh"
6#include "MSXMotherBoard.hh"
7#include "RealTime.hh"
8#include "GlobalSettings.hh"
9#include "ThrottleManager.hh"
10#include "MSXException.hh"
11#include "Timer.hh"
12
13#include "narrow.hh"
14
15#include <algorithm>
16#include <bit>
17#include <cassert>
18
19namespace openmsx {
20
22 unsigned wantedFreq, unsigned wantedSamples)
23 : reactor(reactor_)
24{
25 SDL_AudioSpec desired;
26 desired.freq = narrow<int>(wantedFreq);
27 desired.samples = narrow<Uint16>(std::bit_ceil(wantedSamples));
28 desired.channels = 2; // stereo
29 desired.format = AUDIO_F32SYS;
30 desired.callback = audioCallbackHelper; // must be a static method
31 desired.userdata = this;
32
33 SDL_AudioSpec obtained;
34 deviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &desired, &obtained,
35 SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
36 if (!deviceID) {
37 throw MSXException("Unable to open SDL audio: ", SDL_GetError());
38 }
39
40 frequency = obtained.freq;
41 fragmentSize = obtained.samples;
42
43 mixBuffer.resize(3 * (obtained.size / sizeof(StereoFloat)) + 1);
44 reInit();
45}
46
48{
49 SDL_CloseAudioDevice(deviceID);
50}
51
52void SDLSoundDriver::reInit()
53{
54 SDL_LockAudioDevice(deviceID);
55 readIdx = 0;
56 writeIdx = 0;
57 SDL_UnlockAudioDevice(deviceID);
58}
59
61{
62 if (!muted) {
63 muted = true;
64 SDL_PauseAudioDevice(deviceID, 1);
65 }
66}
67
69{
70 if (muted) {
71 muted = false;
72 reInit();
73 SDL_PauseAudioDevice(deviceID, 0);
74 }
75}
76
78{
79 return frequency;
80}
81
83{
84 return fragmentSize;
85}
86
87void SDLSoundDriver::audioCallbackHelper(void* userdata, uint8_t* strm, int len)
88{
89 assert((len & 7) == 0); // stereo, 32 bit float
90 static_cast<SDLSoundDriver*>(userdata)->
91 audioCallback(std::span{std::bit_cast<StereoFloat*>(strm),
92 len / (2 * sizeof(float))});
93}
94
95unsigned SDLSoundDriver::getBufferFilled() const
96{
97 int result = narrow_cast<int>(writeIdx - readIdx);
98 if (result < 0) result += narrow<int>(mixBuffer.size());
99 assert((0 <= result) && (narrow<unsigned>(result) < mixBuffer.size()));
100 return result;
101}
102
103unsigned SDLSoundDriver::getBufferFree() const
104{
105 // we can't distinguish completely filled from completely empty
106 // (in both cases readIx would be equal to writeIdx), so instead
107 // we define full as '(writeIdx + 1) == readIdx'.
108 auto result = narrow<unsigned>(mixBuffer.size() - 1 - getBufferFilled());
109 assert(narrow_cast<int>(result) >= 0);
110 assert(result < mixBuffer.size());
111 return result;
112}
113
114void SDLSoundDriver::audioCallback(std::span<StereoFloat> stream)
115{
116 auto len = stream.size();
117
118 size_t available = getBufferFilled();
119 auto num = std::min(len, available);
120 if ((readIdx + num) < mixBuffer.size()) {
121 ranges::copy(mixBuffer.subspan(readIdx, num), stream);
122 readIdx += num;
123 } else {
124 auto len1 = mixBuffer.size() - readIdx;
125 ranges::copy(mixBuffer.subspan(readIdx, len1), stream);
126 auto len2 = num - len1;
127 ranges::copy(mixBuffer.first(len2), stream.subspan(len1));
128 readIdx = narrow<unsigned>(len2);
129 }
130 auto missing = narrow_cast<ptrdiff_t>(len - available);
131 if (missing > 0) {
132 // buffer underrun
133 ranges::fill(subspan(stream, available, missing), StereoFloat{});
134 }
135}
136
137void SDLSoundDriver::uploadBuffer(std::span<const StereoFloat> buffer)
138{
139 SDL_LockAudioDevice(deviceID);
140 unsigned free = getBufferFree();
141 if (buffer.size() > free) {
142 auto* board = reactor.getMotherBoard();
143 if (board && !board->getMSXMixer().isSynchronousMode() && // when not recording
145 do {
146 SDL_UnlockAudioDevice(deviceID);
147 Timer::sleep(5000); // 5ms
148 SDL_LockAudioDevice(deviceID);
149 board->getRealTime().resync();
150 free = getBufferFree();
151 } while (buffer.size() > free);
152 } else {
153 // drop excess samples
154 buffer = buffer.subspan(0, free);
155 }
156 }
157 assert(buffer.size() <= free);
158 if ((writeIdx + buffer.size()) < mixBuffer.size()) {
159 ranges::copy(buffer, mixBuffer.subspan(writeIdx));
160 writeIdx += narrow<unsigned>(buffer.size());
161 } else {
162 auto len1 = mixBuffer.size() - writeIdx;
163 ranges::copy(buffer.subspan(0, len1), mixBuffer.subspan(writeIdx));
164 auto len2 = buffer.size() - len1;
165 ranges::copy(buffer.subspan(len1, len2), std::span{mixBuffer});
166 writeIdx = narrow<unsigned>(len2);
167 }
168
169 SDL_UnlockAudioDevice(deviceID);
170}
171
172} // namespace openmsx
ThrottleManager & getThrottleManager()
Contains the main loop of openMSX.
Definition Reactor.hh:75
GlobalSettings & getGlobalSettings()
Definition Reactor.hh:117
MSXMotherBoard * getMotherBoard() const
Definition Reactor.cc:409
void uploadBuffer(std::span< const StereoFloat > buffer) override
void unmute() override
Unmute the sound system.
void mute() override
Mute the sound system.
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).
Definition Timer.cc:27
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:315
constexpr auto copy(InputRange &&range, OutputIter out)
Definition ranges.hh:252
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:481