1
/*******************************************/
2
/* Wave file Output Class, */
3
/* by Perry R. Cook, 1995-96 */
4
/* revised by Gary P. Scavone, 1999 */
6
/* This Object opens a DOS/Windows .wav */
7
/* 16bit data (signed integers) file, and */
8
/* poke buffers of samples into it. */
9
/*******************************************/
12
#ifndef __LITTLE_ENDIAN__
16
/******** Wav Soundfile Header Struct *******/
18
char riff[4]; // "RIFF"
19
INT32 file_size; // in bytes
20
char wave[4]; // "WAVE"
21
char fmt[4]; // "fmt "
22
INT32 block_size; // in bytes (16 for PCM)
23
INT16 format_tag; // 1=PCM, 257=Mu-Law, 258=A-Law, 259=ADPCM
24
INT16 num_chans; // 1=mono, 2=stereo
27
INT16 bytes_per_samp; // 2=16-bit mono, 4=16-bit stereo
29
char data[4]; // "data"
30
INT32 dlength; // in bytes
33
FILE *openWAVFile(int chans,char *fileName) {
34
struct wavehdr hdr = {"RIF",44,"WAV","fmt",
35
16,1,1,(INT32) SRATE,(INT32) SRATE*2,2,16,"dat",0};
45
strcpy(tempName,fileName);
46
if (strstr(tempName,".wav") == NULL) strcat(tempName,".wav");
47
hdr.num_chans = chans;
48
hdr.bytes_per_sec = (long) SRATE*2*chans;
49
hdr.bytes_per_samp = 2*chans;
50
hdr.bits_per_samp = 16;
51
fd = fopen(tempName,"wb");
53
sprintf(msg, "WavWvOut: Could not create soundfile: %s\n", tempName);
54
throw StkError(msg, StkError::FILE_ERROR);
57
#ifndef __LITTLE_ENDIAN__
58
swap32((unsigned char *)&hdr.file_size);
59
swap32((unsigned char *)&hdr.block_size);
60
swap16((unsigned char *)&hdr.format_tag);
61
swap16((unsigned char *)&hdr.num_chans);
62
swap32((unsigned char *)&hdr.srate);
63
swap32((unsigned char *)&hdr.bytes_per_sec);
64
swap16((unsigned char *)&hdr.bytes_per_samp);
65
swap16((unsigned char *)&hdr.bits_per_samp);
68
printf("\nCreating soundfile: %s\n", tempName);
73
WavWvOut :: WavWvOut(char *fileName, int chans)
77
sprintf(msg, "WavWvOut: number of channels = %d not supported!\n", chans);
78
throw StkError(msg, StkError::FUNCTION_SYNTAX);
81
fd = openWAVFile(chans,fileName);
82
data_length = FILE_BUFFER_SIZE*channels;
83
data = (INT16 *) new INT16[data_length];
86
WavWvOut :: ~WavWvOut()
91
fwrite(data,2,counter,fd);
92
time = (double) totalCount * ONE_OVER_SRATE;
93
printf("%f Seconds Computed\n\n", time);
95
bytes = totalCount*2*channels;
96
#ifndef __LITTLE_ENDIAN__
97
swap32((unsigned char *)&bytes);
99
fseek(fd,40,SEEK_SET); // jump to data length
100
fwrite(&bytes,4,1,fd);
102
bytes = totalCount*2*channels + 44;
103
#ifndef __LITTLE_ENDIAN__
104
swap32((unsigned char *)&bytes);
106
fseek(fd,4,SEEK_SET); // jump to file size
107
fwrite(&bytes,4,1,fd);
111
void WavWvOut :: tick(MY_FLOAT sample)
113
static INT16 isample;
115
isample = (INT16) (sample * 32000.0);
116
#ifndef __LITTLE_ENDIAN__
117
swap16 ((unsigned char *)&isample);
119
for (int i=0;i<channels;i++)
120
data[counter++] = isample;
123
if (counter == data_length) {
124
fwrite(data,2,data_length,fd);
129
void WavWvOut :: mtick(MY_MULTI samples)
131
for (int i=0;i<channels;i++) {
132
data[counter] = (INT16) (*samples++ * 32000.0);
133
#ifndef __LITTLE_ENDIAN__
134
swap16 ((unsigned char *)&data[counter]);
140
if (counter == data_length) {
141
fwrite(data,2,data_length,fd);