openMSX
SoundDevice.hh
Go to the documentation of this file.
1#ifndef SOUNDDEVICE_HH
2#define SOUNDDEVICE_HH
3
4#include "EmuTime.hh"
5#include "WavWriter.hh"
7#include <array>
8#include <optional>
9#include <string>
10#include <string_view>
11
12namespace openmsx {
13
14class DeviceConfig;
15class DynamicClock;
16class Filename;
17class MSXMixer;
18
20{
21public:
22 static constexpr unsigned MAX_CHANNELS = 24;
23
24 SoundDevice(const SoundDevice&) = delete;
28
32 [[nodiscard]] const std::string& getName() const { return name; }
33
37 [[nodiscard]] std::string_view getDescription() const { return description; }
38
41 [[nodiscard]] unsigned getNumChannels() const { return numChannels; }
42
45 [[nodiscard]] bool hasStereoChannels() const {
46 return stereo == 2;
47 }
48
51 [[nodiscard]] bool isStereo() const {
52 return hasStereoChannels() || !balanceCenter;
53 }
54
68 float left;
69 float right;
70 };
73 return {f * softwareVolumeLeft, f * softwareVolumeRight};
74 }
75
86 void setSoftwareVolume(float volume, EmuTime::param time);
87 void setSoftwareVolume(float left, float right, EmuTime::param time);
88
89 void recordChannel(unsigned channel, const Filename& filename);
90 void muteChannel (unsigned channel, bool muted);
91
107 [[nodiscard]] std::span<const float> getLastBuffer(unsigned channel);
108
111 [[nodiscard]] float getNativeSampleRate() const { return float(inputSampleRate); }
112
117 [[nodiscard]] unsigned getLastMonoBufferSize() const {
118 // Tuned so that both the ImGui waveform and spectrum viewer
119 // look 'nice' (and aren't too expensive to calculate).
120 return inputSampleRate / 7; // ~0.14 seconds
121 }
122 [[nodiscard]] unsigned getLastBufferSize() const {
123 return getLastMonoBufferSize() * stereo;
124 }
125
126protected:
135 SoundDevice(MSXMixer& mixer, std::string_view name, static_string_view description,
136 unsigned numChannels, unsigned inputRate, bool stereo);
138
147 [[nodiscard]] virtual float getAmplificationFactorImpl() const;
148
155 void registerSound(const DeviceConfig& config);
156
161 void unregisterSound();
162
164 void updateStream(EmuTime::param time);
165
166 void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; }
167 [[nodiscard]] unsigned getInputRate() const { return inputSampleRate; }
168
169public: // Will be called by Mixer:
175 virtual void setOutputRate(unsigned hostSampleRate, double speed) = 0;
176
195 [[nodiscard]] virtual bool updateBuffer(size_t length, float* buffer,
196 EmuTime::param time) = 0;
197
198protected:
207 static void addFill(float*& buffer, float value, unsigned num);
208
222 virtual void generateChannels(std::span<float*> buffers, unsigned num) = 0;
223
236 [[nodiscard]] bool mixChannels(float* dataOut, size_t samples);
237
239 [[nodiscard]] const DynamicClock& getHostSampleClock() const;
240 [[nodiscard]] double getEffectiveSpeed() const;
241
242private:
243 MSXMixer& mixer;
244 const std::string name;
245 const static_string_view description;
246
247 std::array<std::optional<Wav16Writer>, MAX_CHANNELS> writer;
248
249 float softwareVolumeLeft = 1.0f;
250 float softwareVolumeRight = 1.0f;
251 unsigned inputSampleRate;
252 const unsigned numChannels;
253 const unsigned stereo;
254 unsigned numRecordChannels = 0;
255 std::array<int, MAX_CHANNELS> channelBalance;
256 std::array<bool, MAX_CHANNELS> channelMuted;
257 bool balanceCenter = true;
258
259 // When channel data needs to be collected separately (e.g. because
260 // we're recording the channel, or because we want to present it in the
261 // GUI), we store that data in this struct.
262 struct PerChannelBuffer {
263 std::vector<float> buffer; // dynamically grows when needed
264 unsigned stopIdx = 0; // buffer only contains valid data at indices [0, stopIdx)
265 unsigned requestCounter = 0; // != 0 means we're interested in collecting this data
266 unsigned silent = 999999; // how many 0 samples are at the end of the buffer
267 };
268 std::array<PerChannelBuffer, MAX_CHANNELS> channelBuffers = {};
269};
270
271} // namespace openmsx
272
273#endif
Represents a clock with a variable frequency.
This class represents a filename.
Definition Filename.hh:20
double getEffectiveSpeed() const
SoundDevice & operator=(const SoundDevice &)=delete
std::string_view getDescription() const
Gets a description of this sound device, to be presented to the user.
virtual bool updateBuffer(size_t length, float *buffer, EmuTime::param time)=0
Generate sample data.
unsigned getNumChannels() const
How many channels does this device have?
void recordChannel(unsigned channel, const Filename &filename)
bool hasStereoChannels() const
Are the individual channels of this device stereo?
void updateStream(EmuTime::param time)
unsigned getInputRate() const
SoundDevice & operator=(SoundDevice &&)=delete
unsigned getLastBufferSize() const
AmplificationFactors getAmplificationFactor() const
const std::string & getName() const
Get the unique name that identifies this sound device.
float getNativeSampleRate() const
The samples returned by 'getLastBuffer()' have this sample rate.
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().
SoundDevice(SoundDevice &&)=delete
void setInputRate(unsigned sampleRate)
virtual void setOutputRate(unsigned hostSampleRate, double speed)=0
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
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.
unsigned getLastMonoBufferSize() const
getLastBuffer() with return buffers containing this many samples.
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.
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:11
Gets this device its 'amplification factor'.