OXYGENHUB
◆ main openmsx / src / sound / WavWriter.hh
1.6 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef WAVWRITER_HH
2
#define WAVWRITER_HH
3
4
#include "File.hh"
5
#include "Mixer.hh"
6
7
#include <cassert>
8
#include <cstdint>
9
#include <span>
10
#include <string>
11
12
namespace openmsx {
13
14
/** Base class for writing WAV files.
15
  */
16
class WavWriter
17
{
18
public:
19
	/** Returns false if there has been data written to the wav image.
20
	 */
21
	[[nodiscard]] bool isEmpty() const { return bytes == 0; }
22
23
	/** Returns the number of bytes (not samples) written so far.
24
	 */
25
	[[nodiscard]] uint32_t getBytes() const { return bytes; }
26
27
	/** Flush data to file and update header. Try to make (possibly)
28
	  * incomplete file already usable for external programs.
29
	  */
30
	void flush();
31
32
protected:
33
	WavWriter(const std::string& filename,
34
	          unsigned channels, unsigned bits, unsigned frequency);
35
	~WavWriter();
36
37
protected:
38
	File file;
39
	uint32_t bytes = 0;
40
};
41
42
/** Writes 8-bit WAV files.
43
  */
44
class Wav8Writer : public WavWriter
45
{
46
public:
47
	Wav8Writer(const std::string& filename, unsigned channels, unsigned frequency)
48
		: WavWriter(filename, channels, 8, frequency) {}
49
50
	void write(std::span<const uint8_t> buffer);
51
};
52
53
/** Writes 16-bit WAV files.
54
  */
55
class Wav16Writer : public WavWriter
56
{
57
public:
58
	Wav16Writer(const std::string& filename, unsigned channels, unsigned frequency)
59
		: WavWriter(filename, channels, 16, frequency) {}
60
61
	void write(std::span<const int16_t> buffer);
62
	void write(std::span<const float> buffer, float amp = 1.0f);
63
	void write(std::span<const StereoFloat> buffer, float ampLeft = 1.0f, float ampRight = 1.0f);
64
65
	void writeSilence(uint32_t samples);
66
};
67
68
} // namespace openmsx
69
70
#endif