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(string(audioInputFilenameSetting.getString()));
29 }
30 
31 const string& WavAudioInput::getName() const
32 {
33  static const string name("wavinput");
34  return name;
35 }
36 
37 std::string_view WavAudioInput::getDescription() const
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
openmsx::WavData::getSample
int16_t getSample(unsigned pos) const
Definition: WavData.hh:35
openmsx::WavData
Definition: WavData.hh:14
openmsx::Subject::detach
void detach(Observer< T > &observer)
Definition: Subject.hh:56
openmsx::CommandController
Definition: CommandController.hh:17
serialize.hh
openmsx::WavData::getFreq
unsigned getFreq() const
Definition: WavData.hh:33
openmsx::WavAudioInput
Definition: WavAudioInput.hh:14
openmsx::WavData::getSize
unsigned getSize() const
Definition: WavData.hh:34
openmsx::Setting::getCommandController
CommandController & getCommandController() const
Definition: Setting.hh:169
openmsx::MSXException
Definition: MSXException.hh:9
openmsx::PlugException
Thrown when a plug action fails.
Definition: PlugException.hh:10
PlugException.hh
openmsx::Subject::attach
void attach(Observer< T > &observer)
Definition: Subject.hh:50
openmsx::WavAudioInput::serialize
void serialize(Archive &ar, unsigned version)
Definition: WavAudioInput.cc:84
openmsx::Setting
Definition: Setting.hh:119
openmsx::Pluggable
Definition: Pluggable.hh:11
openmsx::WavAudioInput::plugHelper
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: WavAudioInput.cc:43
openmsx::WavAudioInput::getName
const std::string & getName() const override
Name used to identify this pluggable.
Definition: WavAudioInput.cc:31
WavAudioInput.hh
CommandController.hh
openmsx::CliComm::printWarning
void printWarning(std::string_view message)
Definition: CliComm.cc:10
openmsx::CommandController::getCliComm
virtual CliComm & getCliComm()=0
openmsx::WavAudioInput::readSample
int16_t readSample(EmuTime::param time) override
Read wave data.
Definition: WavAudioInput.cc:74
openmsx::FilenameSetting::getString
std::string_view getString() const noexcept
Definition: FilenameSetting.hh:18
openmsx::WavAudioInput::~WavAudioInput
~WavAudioInput() override
Definition: WavAudioInput.cc:21
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::WavAudioInput::getDescription
std::string_view getDescription() const override
Description for this pluggable.
Definition: WavAudioInput.cc:37
openmsx::WavAudioInput::unplugHelper
void unplugHelper(EmuTime::param time) override
Definition: WavAudioInput.cc:55
openmsx::Connector
Represents something you can plug devices into.
Definition: Connector.hh:20
openmsx::Pluggable::isPluggedIn
bool isPluggedIn() const
Returns true if this pluggable is currently plugged into a connector.
Definition: Pluggable.hh:49
CliComm.hh
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::REGISTER_POLYMORPHIC_INITIALIZER
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
openmsx::WavAudioInput::WavAudioInput
WavAudioInput(CommandController &commandController)
Definition: WavAudioInput.cc:11