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"
6 #include "static_string_view.hh"
7 #include <optional>
8 #include <string>
9 #include <string_view>
10 
11 namespace openmsx {
12 
13 class DeviceConfig;
14 class DynamicClock;
15 class Filename;
16 class MSXMixer;
17 
19 {
20 public:
21  static constexpr unsigned MAX_CHANNELS = 24;
22 
23  SoundDevice(const SoundDevice&) = delete;
24  SoundDevice& operator=(const SoundDevice&) = delete;
25 
29  [[nodiscard]] const std::string& getName() const { return name; }
30 
34  [[nodiscard]] std::string_view getDescription() const { return description; }
35 
39  [[nodiscard]] bool isStereo() const;
40 
54  float left;
55  float right;
56  };
58  auto f = getAmplificationFactorImpl();
59  return {f * softwareVolumeLeft, f * softwareVolumeRight};
60  }
61 
72  void setSoftwareVolume(float volume, EmuTime::param time);
73  void setSoftwareVolume(float left, float right, EmuTime::param time);
74 
75  void recordChannel(unsigned channel, const Filename& filename);
76  void muteChannel (unsigned channel, bool muted);
77 
78 protected:
87  SoundDevice(MSXMixer& mixer, std::string_view name, static_string_view description,
88  unsigned numChannels, unsigned inputRate, bool stereo);
90 
99  [[nodiscard]] virtual float getAmplificationFactorImpl() const;
100 
107  void registerSound(const DeviceConfig& config);
108 
113  void unregisterSound();
114 
116  void updateStream(EmuTime::param time);
117 
118  void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; }
119  [[nodiscard]] unsigned getInputRate() const { return inputSampleRate; }
120 
121 public: // Will be called by Mixer:
127  virtual void setOutputRate(unsigned sampleRate) = 0;
128 
147  [[nodiscard]] virtual bool updateBuffer(unsigned length, float* buffer,
148  EmuTime::param time) = 0;
149 
150 protected:
159  static void addFill(float*& buffer, float value, unsigned num);
160 
174  virtual void generateChannels(float** buffers, unsigned num) = 0;
175 
188  [[nodiscard]] bool mixChannels(float* dataOut, unsigned samples);
189 
191  [[nodiscard]] const DynamicClock& getHostSampleClock() const;
192  [[nodiscard]] double getEffectiveSpeed() const;
193 
194 private:
195  MSXMixer& mixer;
196  const std::string name;
197  const static_string_view description;
198 
199  std::optional<Wav16Writer> writer[MAX_CHANNELS];
200 
201  float softwareVolumeLeft = 1.0f;
202  float softwareVolumeRight = 1.0f;
203  unsigned inputSampleRate;
204  const unsigned numChannels;
205  const unsigned stereo;
206  unsigned numRecordChannels;
207  int channelBalance[MAX_CHANNELS];
208  bool channelMuted[MAX_CHANNELS];
209  bool balanceCenter;
210 };
211 
212 } // namespace openmsx
213 
214 #endif
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:16
This class represents a filename.
Definition: Filename.hh:18
double getEffectiveSpeed() const
Definition: SoundDevice.cc:345
std::string_view getDescription() const
Gets a description of this sound device, to be presented to the user.
Definition: SoundDevice.hh:34
virtual void setOutputRate(unsigned sampleRate)=0
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
void recordChannel(unsigned channel, const Filename &filename)
Definition: SoundDevice.cc:157
virtual void generateChannels(float **buffers, unsigned num)=0
Abstract method to generate the actual sound data.
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:139
unsigned getInputRate() const
Definition: SoundDevice.hh:119
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:191
AmplificationFactors getAmplificationFactor() const
Definition: SoundDevice.hh:57
static void addFill(float *&buffer, float value, unsigned num)
Adds a number of samples that all have the same value.
Definition: SoundDevice.cc:44
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:341
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:118
void setSoftwareVolume(float volume, EmuTime::param time)
Change the 'software volume' of this sound device.
Definition: SoundDevice.cc:144
static constexpr unsigned MAX_CHANNELS
Definition: SoundDevice.hh:21
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:134
SoundDevice(const SoundDevice &)=delete
SoundDevice & operator=(const SoundDevice &)=delete
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:79
const std::string & getName() const
Get the unique name that identifies this sound device.
Definition: SoundDevice.hh:29
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:89
void muteChannel(unsigned channel, bool muted)
Definition: SoundDevice.cc:185
virtual float getAmplificationFactorImpl() const
Get amplification/attenuation factor for this device.
Definition: SoundDevice.cc:84
static_string_view
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
Gets this device its 'amplification factor'.
Definition: SoundDevice.hh:53