openMSX
WavAudioInput.cc
Go to the documentation of this file.
1 #include "WavAudioInput.hh"
2 #include "CommandController.hh"
3 #include "FileOperations.hh"
4 #include "PlugException.hh"
5 #include "CliComm.hh"
6 #include "serialize.hh"
7 
8 namespace openmsx {
9 
11  : audioInputFilenameSetting(
12  commandController, "audio-inputfilename",
13  "filename of the file where the sampler reads data from",
14  "audio-input.wav")
15  , reference(EmuTime::zero())
16 {
17  audioInputFilenameSetting.attach(*this);
18 }
19 
21 {
22  audioInputFilenameSetting.detach(*this);
23 }
24 
25 void WavAudioInput::loadWave()
26 {
27  wav = WavData(FileOperations::expandTilde(std::string(
28  audioInputFilenameSetting.getString())));
29 }
30 
31 std::string_view WavAudioInput::getName() const
32 {
33  return "wavinput";
34 }
35 
36 std::string_view WavAudioInput::getDescription() const
37 {
38  return "Read .wav files. Can for example be used as input for "
39  "samplers.";
40 }
41 
42 void 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 
54 void WavAudioInput::unplugHelper(EmuTime::param /*time*/)
55 {
56 }
57 
58 void 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 
73 int16_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 
82 template<typename Archive>
83 void 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
Definition: Interpreter.cc:27
Represents something you can plug devices into.
Definition: Connector.hh:21
zstring_view getString() const noexcept
const std::string & getMessage() const &
Definition: MSXException.hh:23
Thrown when a plug action fails.
void detach(Observer< T > &observer)
Definition: Subject.hh:56
void attach(Observer< T > &observer)
Definition: Subject.hh:50
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.
int16_t getSample(unsigned pos) const
Definition: WavData.hh:36
unsigned getSize() const
Definition: WavData.hh:35
unsigned getFreq() const
Definition: WavData.hh:34
string expandTilde(string path)
Expand the '~' character to the users home directory.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:998