openMSX
WavWriter.hh
Go to the documentation of this file.
1 #ifndef WAVWRITER_HH
2 #define WAVWRITER_HH
3 
4 #include "File.hh"
5 #include "one_of.hh"
6 #include <cassert>
7 #include <cstdint>
8 
9 namespace openmsx {
10 
11 class Filename;
12 
15 class WavWriter
16 {
17 public:
20  [[nodiscard]] bool isEmpty() const { return bytes == 0; }
21 
24  [[nodiscard]] unsigned getBytes() const { return bytes; }
25 
29  void flush();
30 
31 protected:
33  unsigned channels, unsigned bits, unsigned frequency);
34  ~WavWriter();
35 
36 protected:
38  unsigned bytes = 0;
39 };
40 
43 class Wav8Writer : public WavWriter
44 {
45 public:
46  Wav8Writer(const Filename& filename, unsigned channels, unsigned frequency)
47  : WavWriter(filename, channels, 8, frequency) {}
48 
49  void write(const uint8_t* buffer, unsigned stereo, unsigned samples) {
50  assert(stereo == one_of(1u, 2u));
51  write(buffer, stereo * samples);
52  }
53 
54 private:
55  void write(const uint8_t* buffer, unsigned samples);
56 };
57 
60 class Wav16Writer : public WavWriter
61 {
62 public:
63  Wav16Writer(const Filename& filename, unsigned channels, unsigned frequency)
64  : WavWriter(filename, channels, 16, frequency) {}
65 
66  void write(const int16_t* buffer, unsigned stereo, unsigned samples) {
67  assert(stereo == one_of(1u, 2u));
68  write(buffer, stereo * samples);
69  }
70  void write(const float* buffer, unsigned stereo, unsigned samples,
71  float ampLeft, float ampRight);
72  void writeSilence(unsigned stereo, unsigned samples) {
73  assert(stereo == one_of(1u, 2u));
74  writeSilence(stereo * samples);
75  }
76 
77 private:
78  void write(const int16_t* buffer, unsigned samples);
79  void writeSilence(unsigned samples);
80 };
81 
82 } // namespace openmsx
83 
84 #endif
Definition: one_of.hh:7
This class represents a filename.
Definition: Filename.hh:18
Writes 16-bit WAV files.
Definition: WavWriter.hh:61
void writeSilence(unsigned stereo, unsigned samples)
Definition: WavWriter.hh:72
Wav16Writer(const Filename &filename, unsigned channels, unsigned frequency)
Definition: WavWriter.hh:63
void write(const int16_t *buffer, unsigned stereo, unsigned samples)
Definition: WavWriter.hh:66
Writes 8-bit WAV files.
Definition: WavWriter.hh:44
Wav8Writer(const Filename &filename, unsigned channels, unsigned frequency)
Definition: WavWriter.hh:46
void write(const uint8_t *buffer, unsigned stereo, unsigned samples)
Definition: WavWriter.hh:49
Base class for writing WAV files.
Definition: WavWriter.hh:16
unsigned getBytes() const
Returns the number of bytes (not samples) written so far.
Definition: WavWriter.hh:24
void flush()
Flush data to file and update header.
Definition: WavWriter.cc:66
bool isEmpty() const
Returns false if there has been data written to the wav image.
Definition: WavWriter.hh:20
WavWriter(const Filename &filename, unsigned channels, unsigned bits, unsigned frequency)
Definition: WavWriter.cc:13
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename