openMSX
WavAudioInput.cc
Go to the documentation of this file.
1 #include "WavAudioInput.hh"
2 #include "CommandController.hh"
3 #include "PlugException.hh"
4 #include "CliComm.hh"
5 #include "serialize.hh"
6 
7 using std::string;
8 
9 namespace openmsx {
10 
12  : audioInputFilenameSetting(
13  commandController, "audio-inputfilename",
14  "filename of the file where the sampler reads data from",
15  "audio-input.wav")
16  , reference(EmuTime::zero)
17 {
18  audioInputFilenameSetting.attach(*this);
19 }
20 
22 {
23  audioInputFilenameSetting.detach(*this);
24 }
25 
26 void WavAudioInput::loadWave()
27 {
28  wav = WavData(audioInputFilenameSetting.getString().str());
29 }
30 
31 const string& WavAudioInput::getName() const
32 {
33  static const string name("wavinput");
34  return name;
35 }
36 
38 {
39  return "Read .wav files. Can for example be used as input for "
40  "samplers.";
41 }
42 
43 void WavAudioInput::plugHelper(Connector& /*connector*/, EmuTime::param time)
44 {
45  try {
46  if (wav.getSize() == 0) {
47  loadWave();
48  }
49  } catch (MSXException& e) {
50  throw PlugException("Load of wave file failed: ", e.getMessage());
51  }
52  reference = time;
53 }
54 
55 void WavAudioInput::unplugHelper(EmuTime::param /*time*/)
56 {
57 }
58 
59 void WavAudioInput::update(const Setting& setting)
60 {
61  (void)setting;
62  assert(&setting == &audioInputFilenameSetting);
63  try {
64  if (isPluggedIn()) {
65  loadWave();
66  }
67  } catch (MSXException& e) {
68  // TODO proper error handling, message should go to console
70  "Load of wave file failed: ", e.getMessage());
71  }
72 }
73 
74 int16_t WavAudioInput::readSample(EmuTime::param time)
75 {
76  if (wav.getSize()) {
77  unsigned pos = (time - reference).getTicksAt(wav.getFreq());
78  return wav.getSample(pos);
79  }
80  return 0;
81 }
82 
83 template<typename Archive>
84 void WavAudioInput::serialize(Archive& ar, unsigned /*version*/)
85 {
86  ar.serialize("reference", reference);
87  if (ar.isLoader()) {
88  update(audioInputFilenameSetting);
89  }
90 }
93 
94 } // namespace openmsx
void serialize(Archive &ar, unsigned version)
string_view getDescription() const override
Description for this pluggable.
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
unsigned getSize() const
Definition: WavData.hh:33
Represents something you can plug devices into.
Definition: Connector.hh:20
const std::string & getMessage() const &
Definition: MSXException.hh:23
unsigned getFreq() const
Definition: WavData.hh:32
const std::string & getName() const override
Name used to identify this pluggable.
bool isPluggedIn() const
Returns true if this pluggable is currently plugged into a connector.
Definition: Pluggable.hh:49
void unplugHelper(EmuTime::param time) override
void attach(Observer< T > &observer)
Definition: Subject.hh:49
WavAudioInput(CommandController &commandController)
void plugHelper(Connector &connector, EmuTime::param time) override
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
string_view getString() const noexcept
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1006
std::string str() const
Definition: string_view.cc:12
virtual CliComm & getCliComm()=0
void detach(Observer< T > &observer)
Definition: Subject.hh:55
int16_t readSample(EmuTime::param time) override
Read wave data.
Thrown when a plug action fails.
CommandController & getCommandController() const
Definition: Setting.hh:168
int16_t getSample(unsigned pos) const
Definition: WavData.hh:34
void printWarning(string_view message)
Definition: CliComm.cc:20