openMSX
SoundDevice.hh
Go to the documentation of this file.
1 #ifndef SOUNDDEVICE_HH
2 #define SOUNDDEVICE_HH
3 
4 #include "MSXMixer.hh"
5 #include "EmuTime.hh"
6 #include "static_string_view.hh"
7 #include <memory>
8 #include <string>
9 #include <string_view>
10 
11 namespace openmsx {
12 
13 class DeviceConfig;
14 class Wav16Writer;
15 class Filename;
16 class DynamicClock;
17 
19 {
20 public:
21  static constexpr unsigned MAX_CHANNELS = 24;
22 
26  [[nodiscard]] const std::string& getName() const { return name; }
27 
31  [[nodiscard]] std::string_view getDescription() const { return description; }
32 
36  [[nodiscard]] bool isStereo() const;
37 
50  [[nodiscard]] std::pair<float, float> getAmplificationFactor() const {
51  auto f = getAmplificationFactorImpl();
52  return {f * softwareVolumeLeft, f * softwareVolumeRight};
53  }
54 
65  void setSoftwareVolume(float volume, EmuTime::param time);
66  void setSoftwareVolume(float left, float right, EmuTime::param time);
67 
68  void recordChannel(unsigned channel, const Filename& filename);
69  void muteChannel (unsigned channel, bool muted);
70 
71 protected:
80  SoundDevice(MSXMixer& mixer, std::string_view name, static_string_view description,
81  unsigned numChannels, unsigned inputRate, bool stereo);
83 
92  [[nodiscard]] virtual float getAmplificationFactorImpl() const;
93 
100  void registerSound(const DeviceConfig& config);
101 
106  void unregisterSound();
107 
109  void updateStream(EmuTime::param time);
110 
111  void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; }
112  [[nodiscard]] unsigned getInputRate() const { return inputSampleRate; }
113 
114 public: // Will be called by Mixer:
120  virtual void setOutputRate(unsigned sampleRate) = 0;
121 
140  [[nodiscard]] virtual bool updateBuffer(unsigned length, float* buffer,
141  EmuTime::param time) = 0;
142 
143 protected:
152  static void addFill(float*& buffer, float value, unsigned num);
153 
167  virtual void generateChannels(float** buffers, unsigned num) = 0;
168 
181  [[nodiscard]] bool mixChannels(float* dataOut, unsigned samples);
182 
184  [[nodiscard]] const DynamicClock& getHostSampleClock() const;
185  [[nodiscard]] double getEffectiveSpeed() const;
186 
187 private:
188  MSXMixer& mixer;
189  const std::string name;
190  const static_string_view description;
191 
192  std::unique_ptr<Wav16Writer> writer[MAX_CHANNELS];
193 
194  float softwareVolumeLeft = 1.0f;
195  float softwareVolumeRight = 1.0f;
196  unsigned inputSampleRate;
197  const unsigned numChannels;
198  const unsigned stereo;
199  unsigned numRecordChannels;
200  int channelBalance[MAX_CHANNELS];
201  bool channelMuted[MAX_CHANNELS];
202  bool balanceCenter;
203 };
204 
205 } // namespace openmsx
206 
207 #endif
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:17
This class represents a filename.
Definition: Filename.hh:18
double getEffectiveSpeed() const
Definition: SoundDevice.cc:348
std::string_view getDescription() const
Gets a description of this sound device, to be presented to the user.
Definition: SoundDevice.hh:31
virtual void setOutputRate(unsigned sampleRate)=0
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
std::pair< float, float > getAmplificationFactor() const
Gets this device its 'amplification factor'.
Definition: SoundDevice.hh:50
void recordChannel(unsigned channel, const Filename &filename)
Definition: SoundDevice.cc:159
virtual void generateChannels(float **buffers, unsigned num)=0
Abstract method to generate the actual sound data.
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:141
unsigned getInputRate() const
Definition: SoundDevice.hh:112
virtual bool updateBuffer(unsigned length, float *buffer, EmuTime::param time)=0
Generate sample data.
bool mixChannels(float *dataOut, unsigned samples)
Calls generateChannels() and combines the output to a single channel.
Definition: SoundDevice.cc:193
static void addFill(float *&buffer, float value, unsigned num)
Adds a number of samples that all have the same value.
Definition: SoundDevice.cc:47
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:344
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:111
void setSoftwareVolume(float volume, EmuTime::param time)
Change the 'software volume' of this sound device.
Definition: SoundDevice.cc:146
static constexpr unsigned MAX_CHANNELS
Definition: SoundDevice.hh:21
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:136
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:82
const std::string & getName() const
Get the unique name that identifies this sound device.
Definition: SoundDevice.hh:26
SoundDevice(MSXMixer &mixer, std::string_view name, static_string_view description, unsigned numChannels, unsigned inputRate, bool stereo)
Constructor.
Definition: SoundDevice.cc:60
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:92
void muteChannel(unsigned channel, bool muted)
Definition: SoundDevice.cc:187
virtual float getAmplificationFactorImpl() const
Get amplification/attenuation factor for this device.
Definition: SoundDevice.cc:87
static_string_view
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
This file implemented 3 utility functions:
Definition: Autofire.cc:5
constexpr const char *const filename