OXYGENHUB
◆ main openmsx / src / sound / WavAudioInput.cc
2.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "WavAudioInput.hh"
2
3
#include "CliComm.hh"
4
#include "CommandController.hh"
5
#include "FileOperations.hh"
6
#include "PlugException.hh"
7
#include "serialize.hh"
8
9
namespace openmsx {
10
11
WavAudioInput::WavAudioInput(CommandController& commandController)
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
20
WavAudioInput::~WavAudioInput()
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
zstring_view WavAudioInput::getName() const
32
{
33
	return "wavinput";
34
}
35
36
zstring_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 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 /*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 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
}
90
INSTANTIATE_SERIALIZE_METHODS(WavAudioInput);
91
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, WavAudioInput, "WavAudioInput");
92
93
} // namespace openmsx