26static MemBuffer<float, SSE_ALIGNMENT> mixBuffer;
27static size_t mixBufferSize = 0;
29static void allocateMixBuffer(
size_t size)
31 if (mixBufferSize < size) [[unlikely]] {
33 mixBuffer.
resize(mixBufferSize);
37[[nodiscard]]
static std::string makeUnique(
const MSXMixer& mixer, std::string_view name)
39 std::string result(name);
40 if (mixer.findDevice(result)) {
43 result =
strCat(name,
" (", ++n,
')');
44 }
while (mixer.findDevice(result));
63 unsigned numChannels_,
unsigned inputRate,
bool stereo_)
65 , name(makeUnique(mixer, name_))
66 , description(description_)
67 , numChannels(numChannels_)
68 , stereo(stereo_ ? 2 : 1)
71 assert(stereo ==
one_of(1u, 2u));
84 return stereo == 2 || !balanceCenter;
89 return 1.0f / 32768.0f;
94 const auto& soundConfig = config.
getChild(
"sound");
95 float volume = narrow<float>(soundConfig.getChildDataAsInt(
"volume", 0)) * (1.0f / 32767.0f);
97 std::string_view mode = soundConfig.
getChildData(
"mode",
"mono");
100 }
else if (mode ==
"left") {
102 }
else if (mode ==
"right") {
108 for (
const auto* b : soundConfig.getChildren(
"balance")) {
109 auto balance = StringOp::stringTo<int>(b->getData());
111 throw MSXException(
"balance ", b->getData(),
" illegal");
114 const auto* channel = b->findAttribute(
"channel");
116 devBalance = *balance;
121 if (*balance !=
one_of(0, -100, 100)) {
125 balanceCenter =
false;
129 channels.foreachSetBit([&](
size_t c) {
130 channelBalance[c - 1] = *balance;
134 mixer.registerSound(*
this, volume, devBalance, numChannels);
139 mixer.unregisterSound(*
this);
144 mixer.updateStream(time);
155 softwareVolumeLeft = left;
156 softwareVolumeRight = right;
157 mixer.updateSoftwareVolume(*
this);
162 assert(channel < numChannels);
163 bool wasRecording = writer[channel].has_value();
164 if (!filename.
empty()) {
165 writer[channel].emplace(
166 filename, stereo, inputSampleRate);
168 writer[channel].reset();
170 bool recording = writer[channel].has_value();
171 if (recording != wasRecording) {
173 if (numRecordChannels == 0) {
174 mixer.setSynchronousMode(
true);
177 assert(numRecordChannels <= numChannels);
179 assert(numRecordChannels > 0);
181 if (numRecordChannels == 0) {
182 mixer.setSynchronousMode(
false);
190 assert(channel < numChannels);
191 channelMuted[channel] = muted;
197 assert((uintptr_t(dataOut) & 15) == 0);
199 if (samples == 0)
return true;
200 size_t outputStereo =
isStereo() ? 2 : 1;
202 std::array<float*, MAX_CHANNELS> bufs_;
203 auto bufs =
subspan(bufs_, 0, numChannels);
205 unsigned separateChannels = 0;
206 size_t pitch = (samples * stereo + 3) & ~3;
210 for (
auto i :
xrange(numChannels)) {
211 if (!channelMuted[i] && !writer[i] && balanceCenter) {
221 static_assert(
sizeof(float) ==
sizeof(uint32_t));
222 if ((numChannels != 1) || separateChannels) {
228 ranges::fill(std::span{dataOut, outputStereo * samples}, 0.0f);
231 if (separateChannels) {
232 allocateMixBuffer(pitch * separateChannels);
236 for (
auto i :
xrange(numChannels)) {
237 if (channelMuted[i] || writer[i] || !balanceCenter) {
238 bufs[i] = &mixBuffer[pitch * count++];
241 assert(count == separateChannels);
246 if (separateChannels == 0) {
248 [&](
auto i) {
return bufs[i]; });
252 for (
auto i :
xrange(numChannels)) {
254 assert(bufs[i] != dataOut);
259 std::span{bufs[i], samples},
263 std::span{std::bit_cast<const StereoFloat*>(bufs[i]), samples},
264 amp.left, amp.right);
267 writer[i]->writeSilence(narrow<unsigned>(stereo * samples));
273 bool anyUnmuted =
false;
275 VLA(
int, mixBalance, numChannels);
276 for (
auto i :
xrange(numChannels)) {
277 if (bufs[i] && !channelMuted[i]) {
279 if (bufs[i] != dataOut) {
280 bufs[numMix] = bufs[i];
281 mixBalance[numMix] = channelBalance[i];
293 if (!balanceCenter) {
302 if (mixBalance[j] <= 0) {
303 left0 += bufs[j][i + 0];
304 left1 += bufs[j][i + 1];
306 if (mixBalance[j] >= 0) {
307 right0 += bufs[j][i + 0];
308 right1 += bufs[j][i + 1];
311 }
while (j < numMix);
312 dataOut[i * 2 + 0] = left0;
313 dataOut[i * 2 + 1] = right0;
314 dataOut[i * 2 + 2] = left1;
315 dataOut[i * 2 + 3] = right1;
317 }
while (i < samples);
326 size_t num = samples * stereo;
329 auto out0 = dataOut[i + 0];
330 auto out1 = dataOut[i + 1];
331 auto out2 = dataOut[i + 2];
332 auto out3 = dataOut[i + 3];
335 out0 += bufs[j][i + 0];
336 out1 += bufs[j][i + 1];
337 out2 += bufs[j][i + 2];
338 out3 += bufs[j][i + 3];
340 }
while (j < numMix);
341 dataOut[i + 0] = out0;
342 dataOut[i + 1] = out1;
343 dataOut[i + 2] = out2;
344 dataOut[i + 3] = out3;
353 return mixer.getHostSampleClock();
357 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.
void resize(size_t size)
Grow or shrink the memory block.
const T * data() const
Returns pointer to the start of the memory buffer.
double getEffectiveSpeed() const
void recordChannel(unsigned channel, const Filename &filename)
void updateStream(EmuTime::param time)
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 this a stereo device? This is set in the constructor and cannot be changed anymore.
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.
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:
bool any_of(InputRange &&range, UnaryPredicate pred)
constexpr void fill(ForwardRange &&range, const T &value)
size_t size(std::string_view utf8)
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
#define VLA(TYPE, NAME, LENGTH)
constexpr auto xrange(T e)