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:
77  SoundDevice(MSXMixer& mixer, std::string_view name, std::string_view description,
78  unsigned numChannels, unsigned inputRate, bool stereo);
79  ~SoundDevice();
80 
89  virtual float getAmplificationFactorImpl() const;
90 
97  void registerSound(const DeviceConfig& config);
98 
103  void unregisterSound();
104 
106  void updateStream(EmuTime::param time);
107 
108  void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; }
109  unsigned getInputRate() const { return inputSampleRate; }
110 
111 public: // Will be called by Mixer:
117  virtual void setOutputRate(unsigned sampleRate) = 0;
118 
137  virtual bool updateBuffer(unsigned length, float* buffer,
138  EmuTime::param time) = 0;
139 
140 protected:
149  static void addFill(float*& buffer, float value, unsigned num);
150 
164  virtual void generateChannels(float** buffers, unsigned num) = 0;
165 
178  bool mixChannels(float* dataOut, unsigned samples);
179 
181  const DynamicClock& getHostSampleClock() const;
182  double getEffectiveSpeed() const;
183 
184 private:
185  MSXMixer& mixer;
186  const std::string name;
187  const std::string description;
188 
189  std::unique_ptr<Wav16Writer> writer[MAX_CHANNELS];
190 
191  float softwareVolumeLeft = 1.0f;
192  float softwareVolumeRight = 1.0f;
193  unsigned inputSampleRate;
194  const unsigned numChannels;
195  const unsigned stereo;
196  unsigned numRecordChannels;
197  int channelBalance[MAX_CHANNELS];
198  bool channelMuted[MAX_CHANNELS];
199  bool balanceCenter;
200 };
201 
202 } // namespace openmsx
203 
204 #endif
void muteChannel(unsigned channel, bool muted)
Definition: SoundDevice.cc:182
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
void setSoftwareVolume(float volume, EmuTime::param time)
Change the &#39;software volume&#39; of this sound device.
Definition: SoundDevice.cc:141
static constexpr unsigned MAX_CHANNELS
Definition: SoundDevice.hh:19
static void addFill(float *&buffer, float value, unsigned num)
Adds a number of samples that all have the same value.
Definition: SoundDevice.cc:45
const std::string & getName() const
Get the unique name that identifies this sound device.
Definition: SoundDevice.hh:24
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:131
virtual float getAmplificationFactorImpl() const
Get amplification/attenuation factor for this device.
Definition: SoundDevice.cc:85
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:80
virtual void generateChannels(float **buffers, unsigned num)=0
Abstract method to generate the actual sound data.
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:136
const std::string & getDescription() const
Gets a description of this sound device, to be presented to the user.
Definition: SoundDevice.hh:29
constexpr const char *const filename
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:108
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:15
std::pair< float, float > getAmplificationFactor() const
Gets this device its &#39;amplification factor&#39;.
Definition: SoundDevice.hh:48
This class represents a filename.
Definition: Filename.hh:17
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
virtual void setOutputRate(unsigned sampleRate)=0
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
bool mixChannels(float *dataOut, unsigned samples)
Calls generateChannels() and combines the output to a single channel.
Definition: SoundDevice.cc:188
virtual bool updateBuffer(unsigned length, float *buffer, EmuTime::param time)=0
Generate sample data.
double getEffectiveSpeed() const
Definition: SoundDevice.cc:343
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:339
void recordChannel(unsigned channel, const Filename &filename)
Definition: SoundDevice.cc:154
unsigned getInputRate() const
Definition: SoundDevice.hh:109
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:90
SoundDevice(MSXMixer &mixer, std::string_view name, std::string_view description, unsigned numChannels, unsigned inputRate, bool stereo)
Constructor.
Definition: SoundDevice.cc:58