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
14namespace openmsx {
15
17 MSXMotherBoard& motherBoard, std::string_view name_,
18 static_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
34void ResampledSoundDevice::setOutputRate(unsigned /*hostSampleRate*/, double /*speed*/)
35{
37}
38
39bool ResampledSoundDevice::updateBuffer(size_t length, float* buffer,
40 EmuTime::param time)
41{
42 return algo->generateOutput(buffer, length, time);
43}
44
45bool ResampledSoundDevice::generateInput(float* buffer, size_t num)
46{
47 return mixChannels(buffer, num);
48}
49
50
52{
53 (void)setting;
54 assert(&setting == &resampleSetting);
55 createResampler();
56}
57
59{
60 const DynamicClock& hostClock = getHostSampleClock();
61 EmuDuration outputPeriod = hostClock.getPeriod();
62 EmuDuration inputPeriod(getEffectiveSpeed() / double(getInputRate()));
63 emuClock.reset(hostClock.getTime());
64 emuClock.setPeriod(inputPeriod);
65
66 if (outputPeriod == inputPeriod) {
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>>(*this, hostClock);
73 } else {
74 algo = std::make_unique<ResampleHQ<2>>(*this, hostClock);
75 }
76 break;
77 case RESAMPLE_LQ:
78 if (!isStereo()) {
79 algo = ResampleLQ<1>::create(*this, hostClock);
80 } else {
81 algo = ResampleLQ<2>::create(*this, hostClock);
82 }
83 break;
84 case RESAMPLE_BLIP:
85 if (!isStereo()) {
86 algo = std::make_unique<ResampleBlip<1>>(*this, hostClock);
87 } else {
88 algo = std::make_unique<ResampleBlip<2>>(*this, hostClock);
89 }
90 break;
91 default:
93 }
94 }
95}
96
97} // namespace openmsx
BaseSetting * setting
Definition: Interpreter.cc:28
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:16
EmuDuration getPeriod() const
Returns the length of one clock-cycle.
void setPeriod(EmuDuration period)
Set the duration of a clock tick.
void reset(EmuTime::param e)
Reset the clock to start ticking at the given time.
EmuTime::param getTime() const
Gets the time at which the last clock tick occurred.
Definition: DynamicClock.hh:37
T getEnum() const noexcept
Definition: EnumSetting.hh:112
static std::unique_ptr< ResampleLQ< CHANNELS > > create(ResampledSoundDevice &input, const DynamicClock &hostClock)
Definition: ResampleLQ.cc:19
bool generateInput(float *buffer, size_t num)
Note: To enable various optimizations (like SSE), this method is allowed to generate up to 3 extra sa...
ResampledSoundDevice(MSXMotherBoard &motherBoard, std::string_view name, static_string_view description, unsigned channels, unsigned inputSampleRate, bool stereo)
void update(const Setting &setting) noexcept override
void setOutputRate(unsigned hostSampleRate, double speed) override
When a SoundDevice registers itself with the Mixer, the Mixer sets the required sampleRate through th...
bool updateBuffer(size_t length, float *buffer, EmuTime::param time) override
Generate sample data.
double getEffectiveSpeed() const
Definition: SoundDevice.cc:351
unsigned getInputRate() const
Definition: SoundDevice.hh:120
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
Definition: SoundDevice.cc:347
bool mixChannels(float *dataOut, size_t samples)
Calls generateChannels() and combines the output to a single channel.
Definition: SoundDevice.cc:190
bool isStereo() const
Is this a stereo device? This is set in the constructor and cannot be changed anymore.
Definition: SoundDevice.cc:78
void detach(Observer< T > &observer)
Definition: Subject.hh:56
void attach(Observer< T > &observer)
Definition: Subject.hh:50
static_string_view
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:340
This file implemented 3 utility functions:
Definition: Autofire.cc:9
#define UNREACHABLE
Definition: unreachable.hh:38