Wave_Writer.h 1.73 KB
Newer Older
Libretro-Admin's avatar
Libretro-Admin committed
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
61
62
63
64
65
66
67
68
69
70
71
72
73
/* WAVE sound file writer for recording 16-bit output during program development */

#ifndef WAVE_WRITER_H
#define WAVE_WRITER_H

#ifdef __cplusplus
	extern "C" {
#endif

/* C interface */
void wave_open( long sample_rate, const char* filename );
void wave_enable_stereo( void );
void wave_write( const short* buf, long count );
long wave_sample_count( void );
void wave_close( void );

#ifdef __cplusplus
	}
#endif

#ifdef __cplusplus
#include <stddef.h>
#include <stdio.h>

/* C++ interface */
class Wave_Writer {
public:
	typedef short sample_t;
	
	// Create sound file with given sample rate (in Hz) and filename.
	// Exits program if there's an error.
	Wave_Writer( long sample_rate, char const* filename = "out.wav" );
	
	// Enable stereo output
	void enable_stereo();
	
	// 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;
	
	// Finish writing sound file and close it
	void close();
	
	~Wave_Writer();
public:
	// Deprecated
	void stereo( bool b ) { chan_count = b ? 2 : 1; }
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::enable_stereo() { chan_count = 2; }

inline long Wave_Writer::sample_count() const { return sample_count_; }

#endif

#endif