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 using std::string;
9 
10 namespace openmsx {
11 
13  : audioInputFilenameSetting(
14  commandController, "audio-inputfilename",
15  "filename of the file where the sampler reads data from",
16  "audio-input.wav")
17  , reference(EmuTime::zero())
18 {
19  audioInputFilenameSetting.attach(*this);
20 }
21 
23 {
24  audioInputFilenameSetting.detach(*this);
25 }
26 
27 void WavAudioInput::loadWave()
28 {
30  audioInputFilenameSetting.getString())));
31 }
32 
33 std::string_view WavAudioInput::getName() const
34 {
35  return "wavinput";
36 }
37 
38 std::string_view WavAudioInput::getDescription() const
39 {
40  return "Read .wav files. Can for example be used as input for "
41  "samplers.";
42 }
43 
44 void WavAudioInput::plugHelper(Connector& /*connector*/, EmuTime::param time)
45 {
46  try {
47  if (wav.getSize() == 0) {
48  loadWave();
49  }
50  } catch (MSXException& e) {
51  throw PlugException("Load of wave file failed: ", e.getMessage());
52  }
53  reference = time;
54 }
55 
56 void WavAudioInput::unplugHelper(EmuTime::param /*time*/)
57 {
58 }
59 
60 void WavAudioInput::update(const Setting& setting) noexcept
61 {
62  (void)setting;
63  assert(&setting == &audioInputFilenameSetting);
64  try {
65  if (isPluggedIn()) {
66  loadWave();
67  }
68  } catch (MSXException& e) {
69  // TODO proper error handling, message should go to console
70  setting.getCommandController().getCliComm().printWarning(
71  "Load of wave file failed: ", e.getMessage());
72  }
73 }
74 
75 int16_t WavAudioInput::readSample(EmuTime::param time)
76 {
77  if (wav.getSize()) {
78  unsigned pos = (time - reference).getTicksAt(wav.getFreq());
79  return wav.getSample(pos);
80  }
81  return 0;
82 }
83 
84 template<typename Archive>
85 void WavAudioInput::serialize(Archive& ar, unsigned /*version*/)
86 {
87  ar.serialize("reference", reference);
88  if (ar.isLoader()) {
89  update(audioInputFilenameSetting);
90  }
91 }
94 
95 } // namespace openmsx
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:5
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:983