54 void setup_to_write(
const Sound& sound, FILE *fp);
55 bool parse_from_file(FILE *fp);
61 printf(
"bool PcmWavHeader::parse_from_file(FILE *fp)\n");
65 ret = fread(&wavHeader,
sizeof(wavHeader), 1, fp);
67 printf(
"failed to read wav file");
71 ret = fread(&wavLength,
sizeof(wavLength), 1, fp);
73 printf(
"failed to read wav file");
77 ret = fread(&formatHeader1,
sizeof(formatHeader1), 1, fp);
79 printf(
"failed to read wav file");
83 ret = fread(&formatHeader2,
sizeof(formatHeader2), 1, fp);
85 printf(
"failed to read wav file");
89 ret = fread(&formatLength,
sizeof(formatLength), 1, fp);
91 printf(
"failed to read wav file");
95 ret = fread(&pcm.pcmFormatTag,
sizeof(pcm.pcmFormatTag), 1, fp);
97 printf(
"failed to read wav file");
101 ret = fread(&pcm.pcmChannels,
sizeof(pcm.pcmChannels), 1, fp);
103 printf(
"failed to read wav file");
107 ret = fread(&pcm.pcmSamplesPerSecond,
sizeof(pcm.pcmSamplesPerSecond), 1, fp);
109 printf(
"failed to read wav file");
113 ret = fread(&pcm.pcmBytesPerSecond,
sizeof(pcm.pcmBytesPerSecond), 1, fp);
115 printf(
"failed to read wav file");
119 ret = fread(&pcm.pcmBlockAlign,
sizeof(pcm.pcmBlockAlign), 1, fp);
121 printf(
"failed to read wav file");
125 ret = fread(&pcm.pcmBitsPerSample,
sizeof(pcm.pcmBitsPerSample), 1, fp);
127 printf(
"failed to read wav file");
130 if (pcm.pcmBitsPerSample != 16)
132 printf(
"sorry, lousy wav read code only does 16-bit ints\n");
137 int extra_size = formatLength -
sizeof(pcm);
138 if (extra_size != 0) {
139 printf(
"extra_size = %d\n", extra_size);
140 pcmExtraData.allocate(extra_size);
141 ret = fread(&pcmExtraData, extra_size, 1, fp);
143 printf(
"failed to read wav file");
149 ret = fread(&dummyHeader,
sizeof(dummyHeader), 1, fp);
151 printf(
"failed to read wav file");
157 fread(&dummyLength,
sizeof(dummyLength), 1, fp);
159 printf(
"failed to read wav file");
163 dummyData.allocate(dummyLength);
164 fread(&dummyData, dummyLength, 1, fp);
166 printf(
"failed to read wav file");
169 fread(&dummyHeader,
sizeof(dummyHeader), 1, fp);
171 printf(
"failed to read wav file");
176 dataHeader = dummyHeader;
177 fread(&dataLength,
sizeof(dataLength), 1, fp);
179 printf(
"failed to read wav file");
188 int bitsPerSample = 16;
191 int align = channels*((bitsPerSample+7)/8);
197 formatLength =
sizeof(pcm);
199 pcm.pcmFormatTag = 1;
200 pcm.pcmChannels = channels;
202 pcm.pcmBytesPerSecond = align*pcm.pcmSamplesPerSecond;
203 pcm.pcmBlockAlign = align;
204 pcm.pcmBitsPerSample = bitsPerSample;
210 fwrite(&wavHeader,
sizeof(wavHeader),1,fp);
211 fwrite(&wavLength,
sizeof(wavLength),1,fp);
212 fwrite(&formatHeader1,
sizeof(formatHeader1),1,fp);
214 fwrite(&formatHeader2,
sizeof(formatHeader2),1,fp);
215 fwrite(&formatLength,
sizeof(formatLength),1,fp);
217 fwrite(&pcm.pcmFormatTag,
sizeof(pcm.pcmFormatTag),1,fp);
218 fwrite(&pcm.pcmChannels,
sizeof(pcm.pcmChannels),1,fp);
219 fwrite(&pcm.pcmSamplesPerSecond,
sizeof(pcm.pcmSamplesPerSecond),1,fp);
220 fwrite(&pcm.pcmBytesPerSecond,
sizeof(pcm.pcmBytesPerSecond),1,fp);
221 fwrite(&pcm.pcmBlockAlign,
sizeof(pcm.pcmBlockAlign),1,fp);
222 fwrite(&pcm.pcmBitsPerSample,
sizeof(pcm.pcmBitsPerSample),1,fp);
224 fwrite(&dataHeader,
sizeof(dataHeader),1,fp);
225 fwrite(&dataLength,
sizeof(dataLength),1,fp);
231 FILE *fp = fopen(src,
"rb");
233 printf(
"cannot open file %s for reading\n", src);
240 printf(
"error parsing header of file %s\n", src);
253 printf(
"%d channels %d samples %d frequency\n", channels,
samples, freq);
256 result = fread(bytes.
get(),bytes.
length(),1,fp);
259 auto* data =
reinterpret_cast<NetInt16*
>(bytes.
get());
261 for (
int i=0; i<
samples; i++) {
262 for (
int j=0; j<channels; j++) {
263 dest.
set(data[ct],i,j);
275 FILE *fp = fopen(dest,
"wb");
277 printf(
"cannot open file %s for writing\n", dest);
285 auto* data =
reinterpret_cast<NetInt16*
>(bytes.
get());
289 for (
size_t i=0; i<
samples; i++) {
290 for (
size_t j=0; j<channels; j++) {
291 int v = src.
get(i,j);
306 printf(
"file %s is already open\n", fname);
310 fp = fopen(filename,
"rb");
313 printf(
"cannot open file %s for reading\n", filename);
316 strcpy(fname,filename);
320 printf(
"error parsing header of file %s\n", fname);
327 this->soundInfo.samples = header.
dataLength/(this->soundInfo.bits/8)/this->soundInfo.channels;
328 this->soundInfo.data_start_offset = ftell(fp);
337 printf(
"no files open\n");
349 int expected_bytes = (int)(block_size*(soundInfo.bits/8)*soundInfo.channels);
352 int expected_words=expected_bytes/(soundInfo.bits/8);
353 auto* data =
new NetInt16 [expected_words];
355 size_t bytes_read = fread(data,1,expected_bytes,fp);
356 size_t samples_read = bytes_read/(soundInfo.bits/8)/soundInfo.channels;
358 dest.
resize((
int)samples_read,soundInfo.channels);
362 for (
size_t i=0; i<samples_read; i++) {
363 for (
size_t j=0; j< (size_t) soundInfo.channels; j++) {
364 dest.
set(data[ct],i,j);
378 printf(
"no files open\n");
382 if ((
int)sample_offset>this->soundInfo.samples)
384 printf(
"invalid sample_offset\n");
388 fseek(fp,(
long int)(this->soundInfo.data_start_offset+(sample_offset*this->soundInfo.channels*this->soundInfo.bits/2)),SEEK_SET);