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 <memory>
7 #include <string_view>
8 
9 namespace openmsx {
10 
11 class DeviceConfig;
12 class Wav16Writer;
13 class Filename;
14 class DynamicClock;
15 
17 {
18 public:
19  static constexpr unsigned MAX_CHANNELS = 24;
20 
24  const std::string& getName() const { return name; }
25 
29  const std::string& getDescription() const { return description; }
30 
34  bool isStereo() const;
35 
48  std::pair<float, float> getAmplificationFactor() const {
49  auto f = getAmplificationFactorImpl();
50  return {f * softwareVolumeLeft, f * softwareVolumeRight};
51  }
52 
63  void setSoftwareVolume(float volume, EmuTime::param time);
64  void setSoftwareVolume(float left, float right, EmuTime::param time);
65 
66  void recordChannel(unsigned channel, const Filename& filename);
67  void muteChannel (unsigned channel, bool muted);
68 
69 protected:
78  SoundDevice(MSXMixer& mixer, std::string_view name, std::string_view description,
79  unsigned numChannels, unsigned inputRate, bool stereo);
81 
90  virtual float getAmplificationFactorImpl() const;
91 
98  void registerSound(const DeviceConfig& config);
99 
104  void unregisterSound();
105 
107  void updateStream(EmuTime::param time);
108 
109  void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; }
110  unsigned getInputRate() const { return inputSampleRate; }
111 
112 public: // Will be called by Mixer:
118  virtual void setOutputRate(unsigned sampleRate) = 0;
119 
138  virtual bool updateBuffer(unsigned length, float* buffer,
139  EmuTime::param time) = 0;
140 
141 protected:
150  static void addFill(float*& buffer, float value, unsigned num);
151 
165  virtual void generateChannels(float** buffers, unsigned num) = 0;
166 
179  bool mixChannels(float* dataOut, unsigned samples);
180 
182  const DynamicClock& getHostSampleClock() const;
183  double getEffectiveSpeed() const;
184 
185 private:
186  MSXMixer& mixer;
187  const std::string name;
188  const std::string description;
189 
190  std::unique_ptr<Wav16Writer> writer[MAX_CHANNELS];
191 
192  float softwareVolumeLeft = 1.0f;
193  float softwareVolumeRight = 1.0f;
194  unsigned inputSampleRate;
195  const unsigned numChannels;
196  const unsigned stereo;
197  unsigned numRecordChannels;
198  int channelBalance[MAX_CHANNELS];
199  bool channelMuted[MAX_CHANNELS];
200  bool balanceCenter;
201 };
202 
203 } // namespace openmsx
204 
205 #endif
openmsx::MSXMixer
Definition: MSXMixer.hh:30
openmsx::SoundDevice::getDescription
const std::string & getDescription() const
Gets a description of this sound device, to be presented to the user.
Definition: SoundDevice.hh:29
openmsx::SoundDevice::setInputRate
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:109
openmsx::SoundDevice::~SoundDevice
~SoundDevice()
openmsx::SoundDevice::setOutputRate
virtual void setOutputRate(unsigned sampleRate)=0
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
openmsx::DeviceConfig
Definition: DeviceConfig.hh:20
openmsx::SoundDevice::mixChannels
bool mixChannels(float *dataOut, unsigned samples)
Calls generateChannels() and combines the output to a single channel.
Definition: SoundDevice.cc:190
gl::length
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:348
openmsx::SoundDevice::getHostSampleClock
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:341
openmsx::SoundDevice::getEffectiveSpeed
double getEffectiveSpeed() const
Definition: SoundDevice.cc:345
openmsx::DynamicClock
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:17
openmsx::SoundDevice::recordChannel
void recordChannel(unsigned channel, const Filename &filename)
Definition: SoundDevice.cc:156
openmsx::SoundDevice::SoundDevice
SoundDevice(MSXMixer &mixer, std::string_view name, std::string_view description, unsigned numChannels, unsigned inputRate, bool stereo)
Constructor.
Definition: SoundDevice.cc:60
openmsx::SoundDevice::updateBuffer
virtual bool updateBuffer(unsigned length, float *buffer, EmuTime::param time)=0
Generate sample data.
openmsx::SoundDevice::getInputRate
unsigned getInputRate() const
Definition: SoundDevice.hh:110
openmsx::SoundDevice::registerSound
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:92
openmsx::SoundDevice
Definition: SoundDevice.hh:17
openmsx::SoundDevice::muteChannel
void muteChannel(unsigned channel, bool muted)
Definition: SoundDevice.cc:184
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::SoundDevice::setSoftwareVolume
void setSoftwareVolume(float volume, EmuTime::param time)
Change the 'software volume' of this sound device.
Definition: SoundDevice.cc:143
openmsx::SoundDevice::MAX_CHANNELS
static constexpr unsigned MAX_CHANNELS
Definition: SoundDevice.hh:19
openmsx::SoundDevice::unregisterSound
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:133
openmsx::SoundDevice::getName
const std::string & getName() const
Get the unique name that identifies this sound device.
Definition: SoundDevice.hh:24
openmsx::SoundDevice::addFill
static void addFill(float *&buffer, float value, unsigned num)
Adds a number of samples that all have the same value.
Definition: SoundDevice.cc:47
EmuTime.hh
openmsx::Filename
Filename
Definition: Filename.cc:50
openmsx::SoundDevice::getAmplificationFactorImpl
virtual float getAmplificationFactorImpl() const
Get amplification/attenuation factor for this device.
Definition: SoundDevice.cc:87
openmsx::SoundDevice::isStereo
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:82
openmsx::Filename
This class represents a filename.
Definition: Filename.hh:18
MSXMixer.hh
openmsx::SoundDevice::generateChannels
virtual void generateChannels(float **buffers, unsigned num)=0
Abstract method to generate the actual sound data.
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::DynamicClock
DynamicClock
Definition: DynamicClock.cc:26
openmsx::SoundDevice::updateStream
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:138
openmsx::SoundDevice::getAmplificationFactor
std::pair< float, float > getAmplificationFactor() const
Gets this device its 'amplification factor'.
Definition: SoundDevice.hh:48