23[[nodiscard]]
static std::string makeUnique(
const MSXMixer& mixer, std::string_view name)
25 std::string result(name);
26 if (mixer.findDevice(result)) {
29 result =
strCat(name,
" (", ++n,
')');
30 }
while (mixer.findDevice(result));
49 unsigned numChannels_,
unsigned inputRate,
bool stereo_)
51 , name(makeUnique(mixer, name_))
52 , description(description_)
53 , numChannels(numChannels_)
54 , stereo(stereo_ ? 2 : 1)
57 assert(stereo ==
one_of(1u, 2u));
70 return 1.0f / 32768.0f;
75 const auto& soundConfig = config.
getChild(
"sound");
76 float volume = narrow<float>(soundConfig.getChildDataAsInt(
"volume", 0)) * (1.0f / 32767.0f);
78 std::string_view mode = soundConfig.
getChildData(
"mode",
"mono");
81 }
else if (mode ==
"left") {
83 }
else if (mode ==
"right") {
89 for (
const auto* b : soundConfig.getChildren(
"balance")) {
90 auto balance = StringOp::stringTo<int>(b->getData());
92 throw MSXException(
"balance ", b->getData(),
" illegal");
95 const auto* channel = b->findAttribute(
"channel");
97 devBalance = *balance;
102 if (*balance !=
one_of(0, -100, 100)) {
106 balanceCenter =
false;
110 channels.foreachSetBit([&](
size_t c) {
111 channelBalance[c - 1] = *balance;
115 mixer.registerSound(*
this, volume, devBalance, numChannels);
120 mixer.unregisterSound(*
this);
125 mixer.updateStream(time);
136 softwareVolumeLeft = left;
137 softwareVolumeRight = right;
138 mixer.updateSoftwareVolume(*
this);
143 assert(channel < numChannels);
144 bool wasRecording = writer[channel].has_value();
145 if (!filename.
empty()) {
146 writer[channel].emplace(
147 filename, stereo, inputSampleRate);
149 writer[channel].reset();
151 bool recording = writer[channel].has_value();
152 if (recording != wasRecording) {
154 if (numRecordChannels == 0) {
155 mixer.setSynchronousMode(
true);
158 assert(numRecordChannels <= numChannels);
160 assert(numRecordChannels > 0);
162 if (numRecordChannels == 0) {
163 mixer.setSynchronousMode(
false);
171 assert(channel < numChannels);
172 channelMuted[channel] = muted;
177 assert(channel < numChannels);
178 auto& buf = channelBuffers[channel];
180 buf.requestCounter = inputSampleRate;
183 if (buf.stopIdx < requestedSize)
return {};
184 if (buf.silent >= requestedSize)
return {};
185 return {&buf.buffer[buf.stopIdx - requestedSize], requestedSize};
191 assert((uintptr_t(dataOut) & 15) == 0);
193 if (samples == 0)
return true;
194 size_t outputStereo =
isStereo() ? 2 : 1;
196 std::array<float*, MAX_CHANNELS> bufs_;
197 auto bufs =
subspan(bufs_, 0, numChannels);
202 auto needSeparateBuffer = [&](
unsigned channel) {
203 return channelBuffers[channel].requestCounter != 0
204 || channelMuted[channel]
208 bool anySeparateChannel =
false;
209 unsigned size = samples * stereo;
210 unsigned padded = (size + 3) & ~3;
211 for (
auto i :
xrange(numChannels)) {
212 auto& cb = channelBuffers[i];
213 if (!needSeparateBuffer(i)) {
218 anySeparateChannel =
true;
219 cb.requestCounter = std::max<int>(0, cb.requestCounter - samples);
221 unsigned remainingSize = cb.buffer.size() - cb.stopIdx;
222 if (remainingSize < padded) {
225 auto allocateSize = 2 * std::max(lastBufferSize, padded);
226 if (cb.buffer.size() < allocateSize) [[unlikely]] {
228 cb.buffer.resize(allocateSize);
230 unsigned reuse = lastBufferSize >= size ? lastBufferSize - size : 0;
231 if (cb.stopIdx > reuse) {
233 memmove(&cb.buffer[0], &cb.buffer[cb.stopIdx - reuse], reuse *
sizeof(
float));
237 auto* ptr = &cb.buffer[cb.stopIdx];
244 static_assert(
sizeof(float) ==
sizeof(uint32_t));
245 if ((numChannels != 1) || anySeparateChannel) {
251 ranges::fill(std::span{dataOut, outputStereo * samples}, 0.0f);
256 if (!anySeparateChannel) {
258 [&](
auto i) {
return bufs[i]; });
261 for (
auto i :
xrange(numChannels)) {
264 assert(bufs[i] != dataOut);
269 std::span{bufs[i], samples},
273 std::span{std::bit_cast<const StereoFloat*>(bufs[i]), samples},
274 amp.left, amp.right);
277 writer[i]->writeSilence(narrow<unsigned>(stereo * samples));
281 auto& cb = channelBuffers[i];
285 cb.silent += samples;
290 bool anyUnmuted =
false;
292 VLA(
int, mixBalance, numChannels);
293 for (
auto i :
xrange(numChannels)) {
294 if (bufs[i] && !channelMuted[i]) {
296 if (bufs[i] != dataOut) {
297 bufs[numMix] = bufs[i];
298 mixBalance[numMix] = channelBalance[i];
310 if (!balanceCenter) {
319 if (mixBalance[j] <= 0) {
320 left0 += bufs[j][i + 0];
321 left1 += bufs[j][i + 1];
323 if (mixBalance[j] >= 0) {
324 right0 += bufs[j][i + 0];
325 right1 += bufs[j][i + 1];
328 }
while (j < numMix);
329 dataOut[i * 2 + 0] = left0;
330 dataOut[i * 2 + 1] = right0;
331 dataOut[i * 2 + 2] = left1;
332 dataOut[i * 2 + 3] = right1;
334 }
while (i < samples);
343 size_t num = samples * stereo;
346 auto out0 = dataOut[i + 0];
347 auto out1 = dataOut[i + 1];
348 auto out2 = dataOut[i + 2];
349 auto out3 = dataOut[i + 3];
352 out0 += bufs[j][i + 0];
353 out1 += bufs[j][i + 1];
354 out2 += bufs[j][i + 2];
355 out3 += bufs[j][i + 3];
357 }
while (j < numMix);
358 dataOut[i + 0] = out0;
359 dataOut[i + 1] = out1;
360 dataOut[i + 2] = out2;
361 dataOut[i + 3] = out3;
370 return mixer.getHostSampleClock();
374 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 subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
#define VLA(TYPE, NAME, LENGTH)
constexpr auto xrange(T e)