openMSX
WavAudioInput.cc
Go to the documentation of this file.
1#include "WavAudioInput.hh"
2
3#include "CliComm.hh"
5#include "FileOperations.hh"
6#include "PlugException.hh"
7#include "serialize.hh"
8
9namespace openmsx {
10
12 : audioInputFilenameSetting(
13 commandController, "audio-inputfilename",
14 "filename of the file where the sampler reads data from",
15 "audio-input.wav")
16{
17 audioInputFilenameSetting.attach(*this);
18}
19
21{
22 audioInputFilenameSetting.detach(*this);
23}
24
25void WavAudioInput::loadWave()
26{
27 wav = WavData(FileOperations::expandTilde(std::string(
28 audioInputFilenameSetting.getString())));
29}
30
31std::string_view WavAudioInput::getName() const
32{
33 return "wavinput";
34}
35
36std::string_view WavAudioInput::getDescription() const
37{
38 return "Read .wav files. Can for example be used as input for "
39 "samplers.";
40}
41
42void WavAudioInput::plugHelper(Connector& /*connector*/, EmuTime::param time)
43{
44 try {
45 if (wav.getSize() == 0) {
46 loadWave();
47 }
48 } catch (MSXException& e) {
49 throw PlugException("Load of wave file failed: ", e.getMessage());
50 }
51 reference = time;
52}
53
54void WavAudioInput::unplugHelper(EmuTime::param /*time*/)
55{
56}
57
58void WavAudioInput::update(const Setting& setting) noexcept
59{
60 (void)setting;
61 assert(&setting == &audioInputFilenameSetting);
62 try {
63 if (isPluggedIn()) {
64 loadWave();
65 }
66 } catch (MSXException& e) {
67 // TODO proper error handling, message should go to console
68 setting.getCommandController().getCliComm().printWarning(
69 "Load of wave file failed: ", e.getMessage());
70 }
71}
72
73int16_t WavAudioInput::readSample(EmuTime::param time)
74{
75 if (wav.getSize()) {
76 unsigned pos = (time - reference).getTicksAt(wav.getFreq());
77 return wav.getSample(pos);
78 }
79 return 0;
80}
81
82template<typename Archive>
83void WavAudioInput::serialize(Archive& ar, unsigned /*version*/)
84{
85 ar.serialize("reference", reference);
86 if constexpr (Archive::IS_LOADER) {
87 update(audioInputFilenameSetting);
88 }
89}
92
93} // namespace openmsx
BaseSetting * setting
Represents something you can plug devices into.
Definition Connector.hh:21
zstring_view getString() const noexcept
Thrown when a plug action fails.
void detach(Observer< T > &observer)
Definition Subject.hh:60
void attach(Observer< T > &observer)
Definition Subject.hh:54
void serialize(Archive &ar, unsigned version)
void unplugHelper(EmuTime::param time) override
void plugHelper(Connector &connector, EmuTime::param time) override
WavAudioInput(CommandController &commandController)
std::string_view getDescription() const override
Description for this pluggable.
std::string_view getName() const override
Name used to identify this pluggable.
int16_t readSample(EmuTime::param time) override
Read wave data.
size_t getSize() const
Definition WavData.hh:40
int16_t getSample(size_t pos) const
Definition WavData.hh:41
unsigned getFreq() const
Definition WavData.hh:39
constexpr double e
Definition Math.hh:21
string expandTilde(string path)
Expand the '~' character to the users home directory.
This file implemented 3 utility functions:
Definition Autofire.cc:11
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
#define REGISTER_POLYMORPHIC_INITIALIZER(BASE, CLASS, NAME)