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
42 [[nodiscard]] bool isStereo() const;
43
57 float left;
58 float right;
59 };
62 return {f * softwareVolumeLeft, f * softwareVolumeRight};
63 }
64
75 void setSoftwareVolume(float volume, EmuTime::param time);
76 void setSoftwareVolume(float left, float right, EmuTime::param time);
77
78 void recordChannel(unsigned channel, const Filename& filename);
79 void muteChannel (unsigned channel, bool muted);
80
81protected:
90 SoundDevice(MSXMixer& mixer, std::string_view name, static_string_view description,
91 unsigned numChannels, unsigned inputRate, bool stereo);
93
102 [[nodiscard]] virtual float getAmplificationFactorImpl() const;
103
110 void registerSound(const DeviceConfig& config);
111
116 void unregisterSound();
117
119 void updateStream(EmuTime::param time);
120
121 void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; }
122 [[nodiscard]] unsigned getInputRate() const { return inputSampleRate; }
123
124public: // Will be called by Mixer:
130 virtual void setOutputRate(unsigned hostSampleRate, double speed) = 0;
131
150 [[nodiscard]] virtual bool updateBuffer(size_t length, float* buffer,
151 EmuTime::param time) = 0;
152
153protected:
162 static void addFill(float*& buffer, float value, unsigned num);
163
177 virtual void generateChannels(std::span<float*> buffers, unsigned num) = 0;
178
191 [[nodiscard]] bool mixChannels(float* dataOut, size_t samples);
192
194 [[nodiscard]] const DynamicClock& getHostSampleClock() const;
195 [[nodiscard]] double getEffectiveSpeed() const;
196
197private:
198 MSXMixer& mixer;
199 const std::string name;
200 const static_string_view description;
201
202 std::array<std::optional<Wav16Writer>, MAX_CHANNELS> writer;
203
204 float softwareVolumeLeft = 1.0f;
205 float softwareVolumeRight = 1.0f;
206 unsigned inputSampleRate;
207 const unsigned numChannels;
208 const unsigned stereo;
209 unsigned numRecordChannels = 0;
210 std::array<int, MAX_CHANNELS> channelBalance;
211 std::array<bool, MAX_CHANNELS> channelMuted;
212 bool balanceCenter = true;
213};
214
215} // namespace openmsx
216
217#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.
void recordChannel(unsigned channel, const Filename &filename)
void updateStream(EmuTime::param time)
unsigned getInputRate() const
SoundDevice & operator=(SoundDevice &&)=delete
AmplificationFactors getAmplificationFactor() const
const std::string & getName() const
Get the unique name that identifies this sound device.
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.
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.
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:11
Gets this device its 'amplification factor'.