openMSX
ResampledSoundDevice.cc
Go to the documentation of this file.
2 #include "ResampleTrivial.hh"
3 #include "ResampleHQ.hh"
4 #include "ResampleLQ.hh"
5 #include "ResampleBlip.hh"
6 #include "MSXMotherBoard.hh"
7 #include "Reactor.hh"
8 #include "GlobalSettings.hh"
9 #include "EnumSetting.hh"
10 #include "unreachable.hh"
11 #include <cassert>
12 #include <memory>
13 
14 namespace openmsx {
15 
17  MSXMotherBoard& motherBoard, string_view name_,
18  string_view description_, unsigned channels,
19  unsigned inputSampleRate_, bool stereo_)
20  : SoundDevice(motherBoard.getMSXMixer(), name_, description_,
21  channels, inputSampleRate_, stereo_)
22  , resampleSetting(motherBoard.getReactor().getGlobalSettings().getResampleSetting())
23 {
24  resampleSetting.attach(*this);
25 }
26 
28 {
29  resampleSetting.detach(*this);
30 }
31 
32 
33 void ResampledSoundDevice::setOutputRate(unsigned /*sampleRate*/)
34 {
36 }
37 
38 bool ResampledSoundDevice::updateBuffer(unsigned length, float* buffer,
39  EmuTime::param time)
40 {
41  return algo->generateOutput(buffer, length, time);
42 }
43 
44 bool ResampledSoundDevice::generateInput(float* buffer, unsigned num)
45 {
46  return mixChannels(buffer, num);
47 }
48 
49 
51 {
52  (void)setting;
53  assert(&setting == &resampleSetting);
55 }
56 
58 {
59  const DynamicClock& hostClock = getHostSampleClock();
60  unsigned outputRate = hostClock.getFreq();
61  unsigned inputRate = getInputRate() / getEffectiveSpeed();
62 
63  if (outputRate == inputRate) {
64  algo = std::make_unique<ResampleTrivial>(*this);
65  } else {
66  switch (resampleSetting.getEnum()) {
67  case RESAMPLE_HQ:
68  if (!isStereo()) {
69  algo = std::make_unique<ResampleHQ<1>>(
70  *this, hostClock, inputRate);
71  } else {
72  algo = std::make_unique<ResampleHQ<2>>(
73  *this, hostClock, inputRate);
74  }
75  break;
76  case RESAMPLE_LQ:
77  if (!isStereo()) {
78  algo = ResampleLQ<1>::create(
79  *this, hostClock, inputRate);
80  } else {
81  algo = ResampleLQ<2>::create(
82  *this, hostClock, inputRate);
83  }
84  break;
85  case RESAMPLE_BLIP:
86  if (!isStereo()) {
87  algo = std::make_unique<ResampleBlip<1>>(
88  *this, hostClock, inputRate);
89  } else {
90  algo = std::make_unique<ResampleBlip<2>>(
91  *this, hostClock, inputRate);
92  }
93  break;
94  default:
96  }
97  }
98 }
99 
100 } // namespace openmsx
bool updateBuffer(unsigned length, float *buffer, EmuTime::param time) override
Generate sample data.
T getEnum() const noexcept
Definition: EnumSetting.hh:92
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:80
void attach(Observer< T > &observer)
Definition: Subject.hh:50
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:15
unsigned getFreq() const
Returns the frequency (in Hz) at which this clock ticks.
static std::unique_ptr< ResampleLQ< CHANNELS > > create(ResampledSoundDevice &input, const DynamicClock &hostClock, unsigned emuSampleRate)
Definition: ResampleLQ.cc:20
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void setOutputRate(unsigned sampleRate) override
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
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void update(const Setting &setting) override
void detach(Observer< T > &observer)
Definition: Subject.hh:56
double getEffectiveSpeed() const
Definition: SoundDevice.cc:343
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:339
bool generateInput(float *buffer, unsigned num)
Note: To enable various optimizations (like SSE), this method is allowed to generate up to 3 extra sa...
unsigned getInputRate() const
Definition: SoundDevice.hh:109
ResampledSoundDevice(MSXMotherBoard &motherBoard, string_view name, string_view description, unsigned channels, unsigned inputSampleRate, bool stereo)
#define UNREACHABLE
Definition: unreachable.hh:38