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, std::string_view name_,
18  std::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  , emuClock(EmuTime::zero())
24 {
25  resampleSetting.attach(*this);
26 }
27 
29 {
30  resampleSetting.detach(*this);
31 }
32 
33 
34 void ResampledSoundDevice::setOutputRate(unsigned /*sampleRate*/)
35 {
37 }
38 
39 bool ResampledSoundDevice::updateBuffer(unsigned length, float* buffer,
40  EmuTime::param time)
41 {
42  return algo->generateOutput(buffer, length, time);
43 }
44 
45 bool ResampledSoundDevice::generateInput(float* buffer, unsigned num)
46 {
47  return mixChannels(buffer, num);
48 }
49 
50 
52 {
53  (void)setting;
54  assert(&setting == &resampleSetting);
56 }
57 
59 {
60  const DynamicClock& hostClock = getHostSampleClock();
61  unsigned outputRate = hostClock.getFreq();
62  unsigned inputRate = getInputRate() / getEffectiveSpeed();
63  emuClock.reset(hostClock.getTime());
64  emuClock.setFreq(inputRate);
65 
66  if (outputRate == inputRate) {
67  algo = std::make_unique<ResampleTrivial>(*this);
68  } else {
69  switch (resampleSetting.getEnum()) {
70  case RESAMPLE_HQ:
71  if (!isStereo()) {
72  algo = std::make_unique<ResampleHQ<1>>(
73  *this, hostClock);
74  } else {
75  algo = std::make_unique<ResampleHQ<2>>(
76  *this, hostClock);
77  }
78  break;
79  case RESAMPLE_LQ:
80  if (!isStereo()) {
81  algo = ResampleLQ<1>::create(
82  *this, hostClock, inputRate);
83  } else {
84  algo = ResampleLQ<2>::create(
85  *this, hostClock, inputRate);
86  }
87  break;
88  case RESAMPLE_BLIP:
89  if (!isStereo()) {
90  algo = std::make_unique<ResampleBlip<1>>(
91  *this, hostClock, inputRate);
92  } else {
93  algo = std::make_unique<ResampleBlip<2>>(
94  *this, hostClock, inputRate);
95  }
96  break;
97  default:
99  }
100  }
101 }
102 
103 } // namespace openmsx
openmsx::ResampledSoundDevice::RESAMPLE_HQ
@ RESAMPLE_HQ
Definition: ResampledSoundDevice.hh:19
ResampleLQ.hh
openmsx::DynamicClock::setFreq
void setFreq(unsigned freq)
Change the frequency at which this clock ticks.
Definition: DynamicClock.hh:103
openmsx::ResampledSoundDevice::setOutputRate
void setOutputRate(unsigned sampleRate) override
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
Definition: ResampledSoundDevice.cc:34
ResampleBlip.hh
ResampleHQ.hh
openmsx::ResampledSoundDevice::RESAMPLE_LQ
@ RESAMPLE_LQ
Definition: ResampledSoundDevice.hh:19
openmsx::ResampledSoundDevice::~ResampledSoundDevice
~ResampledSoundDevice()
Definition: ResampledSoundDevice.cc:28
openmsx::SoundDevice::mixChannels
bool mixChannels(float *dataOut, unsigned samples)
Calls generateChannels() and combines the output to a single channel.
Definition: SoundDevice.cc:189
openmsx::DynamicClock::getFreq
unsigned getFreq() const
Returns the frequency (in Hz) at which this clock ticks.
Definition: DynamicClock.hh:123
openmsx::ResampledSoundDevice::generateInput
bool generateInput(float *buffer, unsigned num)
Note: To enable various optimizations (like SSE), this method is allowed to generate up to 3 extra sa...
Definition: ResampledSoundDevice.cc:45
gl::length
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:348
openmsx::EnumSetting::getEnum
T getEnum() const noexcept
Definition: EnumSetting.hh:92
openmsx::ResampledSoundDevice::updateBuffer
bool updateBuffer(unsigned length, float *buffer, EmuTime::param time) override
Generate sample data.
Definition: ResampledSoundDevice.cc:39
openmsx::Setting
Definition: Setting.hh:119
ResampleTrivial.hh
openmsx::SoundDevice::getHostSampleClock
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:340
openmsx::SoundDevice::getEffectiveSpeed
double getEffectiveSpeed() const
Definition: SoundDevice.cc:344
openmsx::DynamicClock
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:16
Reactor.hh
openmsx::ResampledSoundDevice::ResampledSoundDevice
ResampledSoundDevice(MSXMotherBoard &motherBoard, std::string_view name, std::string_view description, unsigned channels, unsigned inputSampleRate, bool stereo)
Definition: ResampledSoundDevice.cc:16
openmsx::SoundDevice::getInputRate
unsigned getInputRate() const
Definition: SoundDevice.hh:109
UNREACHABLE
#define UNREACHABLE
Definition: unreachable.hh:38
openmsx::SoundDevice
Definition: SoundDevice.hh:16
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:59
openmsx::ResampleLQ::create
static std::unique_ptr< ResampleLQ< CHANNELS > > create(ResampledSoundDevice &input, const DynamicClock &hostClock, unsigned emuSampleRate)
Definition: ResampleLQ.cc:20
EnumSetting.hh
GlobalSettings.hh
openmsx::ResampledSoundDevice::RESAMPLE_BLIP
@ RESAMPLE_BLIP
Definition: ResampledSoundDevice.hh:19
openmsx::DynamicClock::getTime
EmuTime::param getTime() const
Gets the time at which the last clock tick occurred.
Definition: DynamicClock.hh:38
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:81
openmsx::ResampledSoundDevice::update
void update(const Setting &setting) override
Definition: ResampledSoundDevice.cc:51
unreachable.hh
openmsx::DynamicClock::reset
void reset(EmuTime::param e)
Reset the clock to start ticking at the given time.
Definition: DynamicClock.hh:136
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
MSXMotherBoard.hh
openmsx::ResampledSoundDevice::createResampler
void createResampler()
Definition: ResampledSoundDevice.cc:58
ResampledSoundDevice.hh