openMSX
DACSound16S.cc
Go to the documentation of this file.
1#include "DACSound16S.hh"
2#include "DeviceConfig.hh"
3#include "MSXMotherBoard.hh"
4#include "DynamicClock.hh"
5#include "narrow.hh"
6#include "serialize.hh"
7
8namespace openmsx {
9
10static constexpr unsigned DUMMY_INPUT_RATE = 44100; // actual rate depends on frequency setting
11
12DACSound16S::DACSound16S(std::string_view name_, static_string_view desc,
13 const DeviceConfig& config)
14 : SoundDevice(config.getMotherBoard().getMSXMixer(), name_, desc, 1, DUMMY_INPUT_RATE, false)
15{
16 registerSound(config);
17}
18
23
24void DACSound16S::setOutputRate(unsigned hostSampleRate, double /*speed*/)
25{
26 setInputRate(hostSampleRate);
27}
28
29void DACSound16S::reset(EmuTime::param time)
30{
31 writeDAC(0, time);
32}
33
34void DACSound16S::writeDAC(int16_t value, EmuTime::param time)
35{
36 int delta = value - lastWrittenValue;
37 if (delta == 0) return;
38 lastWrittenValue = value;
39
42 blip.addDelta(t, narrow<float>(delta));
43}
44
45void DACSound16S::generateChannels(std::span<float*> bufs, unsigned num)
46{
47 // Note: readSamples() replaces the values in the buffer (it doesn't
48 // add the new values to the existing values in the buffer). That's OK
49 // because this is a single-channel SoundDevice.
50 assert(bufs.size() == 1);
51 if (!blip.readSamples<1>(bufs[0], num)) {
52 bufs[0] = nullptr;
53 }
54}
55
56bool DACSound16S::updateBuffer(size_t length, float* buffer,
57 EmuTime::param /*time*/)
58{
59 return mixChannels(buffer, length);
60}
61
62template<typename Archive>
63void DACSound16S::serialize(Archive& ar, unsigned /*version*/)
64{
65 // Note: It's ok to NOT serialize a DAC object if you call the
66 // writeDAC() method in some other way during de-serialization.
67 // This is for example done in MSXPPI/KeyClick.
68 int16_t lastValue = lastWrittenValue;
69 ar.serialize("lastValue", lastValue);
70 if constexpr (Archive::IS_LOADER) {
71 writeDAC(lastValue, getHostSampleClock().getTime());
72 }
73}
75
76} // namespace openmsx
TclObject t
bool readSamples(float *out, size_t samples)
void addDelta(TimeIndex time, float delta)
Definition BlipBuffer.cc:98
DACSound16S(std::string_view name, static_string_view desc, const DeviceConfig &config)
void reset(EmuTime::param time)
void serialize(Archive &ar, unsigned version)
void writeDAC(int16_t value, EmuTime::param time)
unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
A fixed point number, implemented by a 32-bit signed integer.
Definition FixedPoint.hh:16
const DynamicClock & getHostSampleClock() const
See MSXMixer::getHostSampleClock().
void setInputRate(unsigned sampleRate)
bool mixChannels(float *dataOut, size_t samples)
Calls generateChannels() and combines the output to a single channel.
void unregisterSound()
Unregisters this sound device with the Mixer.
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:9
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)