OXYGENHUB
◆ main openmsx / src / sound / SamplePlayer.hh
1.6 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef SAMPLEPLAYER_HH
2
#define SAMPLEPLAYER_HH
3
4
#include "ResampledSoundDevice.hh"
5
#include "WavData.hh"
6
#include "dynarray.hh"
7
8
namespace openmsx {
9
10
class SamplePlayer final : public ResampledSoundDevice
11
{
12
public:
13
	SamplePlayer(const std::string& name, static_string_view desc,
14
	             const DeviceConfig& config,
15
	             std::string_view samplesBaseName, unsigned numSamples,
16
	             std::string_view alternativeName = {});
17
	~SamplePlayer();
18
19
	void reset();
20
21
	/** Start playing a (new) sample.
22
	 */
23
	void play(unsigned sampleNum);
24
25
	/** Keep on repeating the given sample data.
26
	 * If there is already a sample playing, that sample is still
27
	 * finished. If there was no sample playing, the given sample
28
	 * immediately starts playing.
29
	 * Parameters are the same as for the play() method.
30
	 * @see stopRepeat()
31
	 */
32
	void repeat(unsigned sampleNum);
33
34
	/** Stop repeat mode.
35
	 * The currently playing sample will still be finished, but won't
36
	 * be started.
37
	 * @see repeat()
38
	 */
39
	void stopRepeat() { nextSampleNum = unsigned(-1); }
40
41
	/** Is there currently playing a sample. */
42
	[[nodiscard]] bool isPlaying() const { return currentSampleNum != unsigned(-1); }
43
44
	template<typename Archive>
45
	void serialize(Archive& ar, unsigned version);
46
47
private:
48
	void setWavParams();
49
	void doRepeat();
50
51
	// SoundDevice
52
	void generateChannels(std::span<float*> bufs, unsigned num) override;
53
54
private:
55
	const dynarray<WavData> samples;
56
57
	unsigned index = 0; // avoid UMR on serialize
58
	unsigned bufferSize;
59
	unsigned currentSampleNum;
60
	unsigned nextSampleNum;
61
};
62
63
} // namespace openmsx
64
65
#endif