-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wave_Writer.h
61 lines (43 loc) · 1.45 KB
/
Wave_Writer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// WAVE sound file writer for recording 16-bit output during program development
// Copyright (C) 2003-2004 Shay Green. MIT license.
#ifndef WAVE_WRITER_HPP
#define WAVE_WRITER_HPP
#include <stddef.h>
#include <stdio.h>
class Wave_Writer {
public:
typedef short sample_t;
// Create sound file with given sample rate (in Hz) and filename.
// Exit program if there's an error.
Wave_Writer( long sample_rate, char const* filename = "out.wav" );
// Enable stereo output
void stereo( int );
// Append 'count' samples to file. Use every 'skip'th source sample; allows
// one channel of stereo sample pairs to be written by specifying a skip of 2.
void write( const sample_t*, long count, int skip = 1 );
// Append 'count' floating-point samples to file. Use every 'skip'th source sample;
// allows one channel of stereo sample pairs to be written by specifying a skip of 2.
void write( const float*, long count, int skip = 1 );
// Number of samples written so far
long sample_count() const;
// Write sound file header and close file. If no samples were written,
// delete file.
~Wave_Writer();
// End of public interface
private:
enum { buf_size = 32768 * 2 };
unsigned char* buf;
FILE* file;
long sample_count_;
long rate;
long buf_pos;
int chan_count;
void flush();
};
inline void Wave_Writer::stereo( int s ) {
chan_count = s ? 2 : 1;
}
inline long Wave_Writer::sample_count() const {
return sample_count_;
}
#endif