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 "FixedPoint.hh"
7 #include "string_view.hh"
8 #include <memory>
9 
10 namespace openmsx {
11 
12 class DeviceConfig;
13 class Wav16Writer;
14 class Filename;
15 class DynamicClock;
16 
18 {
19 public:
21  static const unsigned MAX_CHANNELS = 24;
22 
26  const std::string& getName() const { return name; }
27 
31  const std::string& getDescription() const { return description; }
32 
36  bool isStereo() const;
37 
50  std::pair<VolumeType, VolumeType> getAmplificationFactor() const {
51  auto f = getAmplificationFactorImpl();
52  return std::make_pair(f * softwareVolumeLeft,
53  f * softwareVolumeRight);
54  }
55 
66  void setSoftwareVolume(VolumeType volume, EmuTime::param time);
67  void setSoftwareVolume(VolumeType left, VolumeType right, EmuTime::param time);
68 
69  void recordChannel(unsigned channel, const Filename& filename);
70  void muteChannel (unsigned channel, bool muted);
71 
72 protected:
80  SoundDevice(MSXMixer& mixer, string_view name, string_view description,
81  unsigned numChannels, bool stereo = false);
82  ~SoundDevice();
83 
92  virtual int 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  unsigned getInputRate() const { return inputSampleRate; }
113 
114 public: // Will be called by Mixer:
120  virtual void setOutputRate(unsigned sampleRate) = 0;
121 
140  virtual bool updateBuffer(unsigned length, int* buffer,
141  EmuTime::param time) = 0;
142 
143 protected:
152  static void addFill(int*& buffer, int value, unsigned num);
153 
167  virtual void generateChannels(int** buffers, unsigned num) = 0;
168 
181  bool mixChannels(int* dataOut, unsigned num);
182 
184  const DynamicClock& getHostSampleClock() const;
185  double getEffectiveSpeed() const;
186 
187 private:
188  MSXMixer& mixer;
189  const std::string name;
190  const std::string description;
191 
192  std::unique_ptr<Wav16Writer> writer[MAX_CHANNELS];
193 
194  VolumeType softwareVolumeLeft{1};
195  VolumeType softwareVolumeRight{1};
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
void muteChannel(unsigned channel, bool muted)
Definition: SoundDevice.cc:217
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:333
const std::string & getName() const
Get the unique name that identifies this sound device.
Definition: SoundDevice.hh:26
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:166
virtual bool updateBuffer(unsigned length, int *buffer, EmuTime::param time)=0
Generate sample data.
void setSoftwareVolume(VolumeType volume, EmuTime::param time)
Change the &#39;software volume&#39; of this sound device.
Definition: SoundDevice.cc:176
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:115
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:171
bool mixChannels(int *dataOut, unsigned num)
Calls generateChannels() and combines the output to a single channel.
Definition: SoundDevice.cc:223
const std::string & getDescription() const
Gets a description of this sound device, to be presented to the user.
Definition: SoundDevice.hh:31
void setInputRate(unsigned sampleRate)
Definition: SoundDevice.hh:111
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:15
virtual int getAmplificationFactorImpl() const
Get extra amplification factor for this device.
Definition: SoundDevice.cc:120
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...
SoundDevice(MSXMixer &mixer, string_view name, string_view description, unsigned numChannels, bool stereo=false)
Constructor.
Definition: SoundDevice.cc:92
std::pair< VolumeType, VolumeType > getAmplificationFactor() const
Gets this device its &#39;amplification factor&#39;.
Definition: SoundDevice.hh:50
virtual void generateChannels(int **buffers, unsigned num)=0
Abstract method to generate the actual sound data.
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
static void addFill(int *&buffer, int value, unsigned num)
Adds a number of samples that all have the same value.
Definition: SoundDevice.cc:43
double getEffectiveSpeed() const
Definition: SoundDevice.cc:381
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:377
void recordChannel(unsigned channel, const Filename &filename)
Definition: SoundDevice.cc:189
unsigned getInputRate() const
Definition: SoundDevice.hh:112
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:125
static const unsigned MAX_CHANNELS
Definition: SoundDevice.hh:21