22[[nodiscard]]
static std::string makeUnique(
const MSXMixer& mixer, std::string_view name)
24 std::string result(name);
25 if (mixer.findDevice(result)) {
28 result =
strCat(name,
" (", ++n,
')');
29 }
while (mixer.findDevice(result));
48 unsigned numChannels_,
unsigned inputRate,
bool stereo_)
50 , name(makeUnique(mixer, name_))
51 , description(description_)
52 , numChannels(numChannels_)
53 , stereo(stereo_ ? 2 : 1)
56 assert(stereo ==
one_of(1u, 2u));
69 return 1.0f / 32768.0f;
74 const auto& soundConfig = config.
getChild(
"sound");
75 float volume = narrow<float>(soundConfig.getChildDataAsInt(
"volume", 0)) * (1.0f / 32767.0f);
77 std::string_view mode = soundConfig.
getChildData(
"mode",
"mono");
80 }
else if (mode ==
"left") {
82 }
else if (mode ==
"right") {
88 for (
const auto* b : soundConfig.getChildren(
"balance")) {
89 auto balance = StringOp::stringTo<int>(b->getData());
91 throw MSXException(
"balance ", b->getData(),
" illegal");
94 const auto* channel = b->findAttribute(
"channel");
96 devBalance = *balance;
101 if (*balance !=
one_of(0, -100, 100)) {
105 balanceCenter =
false;
109 channels.foreachSetBit([&](
size_t c) {
110 channelBalance[c - 1] = *balance;
114 mixer.registerSound(*
this, volume, devBalance, numChannels);
119 mixer.unregisterSound(*
this);
124 mixer.updateStream(time);
135 softwareVolumeLeft = left;
136 softwareVolumeRight = right;
137 mixer.updateSoftwareVolume(*
this);
142 assert(channel < numChannels);
143 bool wasRecording = writer[channel].has_value();
144 if (!filename.
empty()) {
145 writer[channel].emplace(
146 filename, stereo, inputSampleRate);
148 writer[channel].reset();
150 bool recording = writer[channel].has_value();
151 if (recording != wasRecording) {
153 if (numRecordChannels == 0) {
154 mixer.setSynchronousMode(
true);
157 assert(numRecordChannels <= numChannels);
159 assert(numRecordChannels > 0);
161 if (numRecordChannels == 0) {
162 mixer.setSynchronousMode(
false);
170 assert(channel < numChannels);
171 channelMuted[channel] = muted;
176 assert(channel < numChannels);
177 auto& buf = channelBuffers[channel];
179 buf.requestCounter = inputSampleRate;
182 if (buf.stopIdx < requestedSize)
return {};
183 if (buf.silent >= requestedSize)
return {};
184 return {&buf.buffer[buf.stopIdx - requestedSize], requestedSize};
189 if (samples == 0)
return true;
190 size_t outputStereo =
isStereo() ? 2 : 1;
197 auto needSeparateBuffer = [&](
unsigned channel) {
198 return channelBuffers[channel].requestCounter != 0
199 || channelMuted[channel]
203 bool anySeparateChannel =
false;
204 auto size = narrow<unsigned>(samples * stereo);
205 auto padded = (size + 3) & ~3;
206 for (
auto i :
xrange(numChannels)) {
207 auto& cb = channelBuffers[i];
208 if (!needSeparateBuffer(i)) {
213 anySeparateChannel =
true;
214 cb.requestCounter = (cb.requestCounter < samples) ? 0 : unsigned(cb.requestCounter - samples);
216 if (
auto remainingSize = narrow<unsigned>(cb.buffer.size() - cb.stopIdx);
217 remainingSize < padded) {
220 if (
auto allocateSize = 2 * std::max(lastBufferSize, padded);
221 cb.buffer.size() < allocateSize) [[unlikely]] {
223 cb.buffer.resize(allocateSize);
225 unsigned reuse = lastBufferSize >= size ? lastBufferSize - size : 0;
226 if (cb.stopIdx > reuse) {
228 memmove(&cb.buffer[0], &cb.buffer[cb.stopIdx - reuse], reuse *
sizeof(
float));
232 auto* ptr = &cb.buffer[cb.stopIdx];
239 static_assert(
sizeof(float) ==
sizeof(uint32_t));
240 if ((numChannels != 1) || anySeparateChannel) {
246 ranges::fill(std::span{dataOut, outputStereo * samples}, 0.0f);
251 if (!anySeparateChannel) {
253 [&](
auto i) {
return bufs[i]; });
256 for (
auto i :
xrange(numChannels)) {
259 assert(bufs[i] != dataOut);
264 std::span{bufs[i], samples},
268 std::span{std::bit_cast<const StereoFloat*>(bufs[i]), samples},
269 amp.left, amp.right);
272 writer[i]->writeSilence(narrow<unsigned>(stereo * samples));
276 auto& cb = channelBuffers[i];
280 cb.silent += samples;
285 bool anyUnmuted =
false;
288 for (
auto i :
xrange(numChannels)) {
289 if (bufs[i] && !channelMuted[i]) {
291 if (bufs[i] != dataOut) {
292 bufs[numMix] = bufs[i];
293 mixBalance[numMix] = channelBalance[i];
305 if (!balanceCenter) {
314 if (mixBalance[j] <= 0) {
315 left0 += bufs[j][i + 0];
316 left1 += bufs[j][i + 1];
318 if (mixBalance[j] >= 0) {
319 right0 += bufs[j][i + 0];
320 right1 += bufs[j][i + 1];
323 }
while (j < numMix);
324 dataOut[i * 2 + 0] = left0;
325 dataOut[i * 2 + 1] = right0;
326 dataOut[i * 2 + 2] = left1;
327 dataOut[i * 2 + 3] = right1;
329 }
while (i < samples);
338 size_t num = samples * stereo;
341 auto out0 = dataOut[i + 0];
342 auto out1 = dataOut[i + 1];
343 auto out2 = dataOut[i + 2];
344 auto out3 = dataOut[i + 3];
347 out0 += bufs[j][i + 0];
348 out1 += bufs[j][i + 1];
349 out2 += bufs[j][i + 2];
350 out3 += bufs[j][i + 3];
352 }
while (j < numMix);
353 dataOut[i + 0] = out0;
354 dataOut[i + 1] = out1;
355 dataOut[i + 2] = out2;
356 dataOut[i + 3] = out3;
365 return mixer.getHostSampleClock();
369 return mixer.getEffectiveSpeed();
const XMLElement & getChild(std::string_view name) const
Represents a clock with a variable frequency.
This class represents a filename.
bool empty() const
Convenience method to test for empty filename.
double getEffectiveSpeed() const
void recordChannel(unsigned channel, const Filename &filename)
void updateStream(EmuTime::param time)
unsigned getLastBufferSize() const
AmplificationFactors getAmplificationFactor() const
static void addFill(float *&buffer, float value, unsigned num)
Adds a number of samples that all have the same value.
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
void setInputRate(unsigned sampleRate)
void setSoftwareVolume(float volume, EmuTime::param time)
Change the 'software volume' of this sound device.
bool mixChannels(float *dataOut, size_t samples)
Calls generateChannels() and combines the output to a single channel.
static constexpr unsigned MAX_CHANNELS
void unregisterSound()
Unregisters this sound device with the Mixer.
SoundDevice(const SoundDevice &)=delete
bool isStereo() const
Is the full output of this device stereo?
virtual void generateChannels(std::span< float * > buffers, unsigned num)=0
Abstract method to generate the actual sound data.
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
std::span< const float > getLastBuffer(unsigned channel)
Query the last generated audio signal for a specific channel.
void muteChannel(unsigned channel, bool muted)
virtual float getAmplificationFactorImpl() const
Get amplification/attenuation factor for this device.
std::string_view getChildData(std::string_view childName) const
IterableBitSet< 64 > parseRange(string_view str, unsigned min, unsigned max)
This file implemented 3 utility functions:
constexpr void fill(ForwardRange &&range, const T &value)
constexpr bool any_of(InputRange &&range, UnaryPredicate pred)
constexpr auto xrange(T e)