1 /* WAVE sound file writer for recording 16-bit output during program development */ 2 3 #ifndef WAVE_WRITER_H 4 #define WAVE_WRITER_H 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 /* C interface */ 11 void wave_open( long sample_rate, const char* filename ); 12 void wave_enable_stereo( void ); 13 void wave_write( const short* buf, long count ); 14 long wave_sample_count( void ); 15 void wave_close( void ); 16 17 #ifdef __cplusplus 18 } 19 #endif 20 21 #ifdef __cplusplus 22 #include <stddef.h> 23 #include <stdio.h> 24 25 /* C++ interface */ 26 class Wave_Writer { 27 public: 28 typedef short sample_t; 29 30 // Create sound file with given sample rate (in Hz) and filename. 31 // Exits program if there's an error. 32 Wave_Writer( long sample_rate, char const* filename = "out.wav" ); 33 34 // Enable stereo output 35 void enable_stereo(); 36 37 // Append 'count' samples to file. Use every 'skip'th source sample; allows 38 // one channel of stereo sample pairs to be written by specifying a skip of 2. 39 void write( const sample_t*, long count, int skip = 1 ); 40 41 // Append 'count' floating-point samples to file. Use every 'skip'th source sample; 42 // allows one channel of stereo sample pairs to be written by specifying a skip of 2. 43 void write( const float*, long count, int skip = 1 ); 44 45 // Number of samples written so far 46 long sample_count() const; 47 48 // Finish writing sound file and close it 49 void close(); 50 51 ~Wave_Writer(); 52 public: 53 // Deprecated stereo(bool b)54 void stereo( bool b ) { chan_count = b ? 2 : 1; } 55 private: 56 enum { buf_size = 32768 * 2 }; 57 unsigned char* buf; 58 FILE* file; 59 long sample_count_; 60 long rate; 61 long buf_pos; 62 int chan_count; 63 64 void flush(); 65 }; 66 enable_stereo()67inline void Wave_Writer::enable_stereo() { chan_count = 2; } 68 sample_count()69inline long Wave_Writer::sample_count() const { return sample_count_; } 70 71 #endif 72 73 #endif 74