Home | History | Annotate | Download | only in sonic

Lines Matching refs:file

4    This file is part of the Sonic Library.
6 This file is licensed under the Apache 2.0 license.
10 This file supports read/write wave files.
22 FILE *soundFile;
28 /* Write a string to a file. */
30 waveFile file,
36 if(file->failed) {
39 bytesWritten = fwrite(bytes, sizeof(char), length, file->soundFile);
41 fprintf(stderr, "Unable to write to output file");
42 file->failed = 1;
44 file->bytesWritten += bytesWritten;
47 /* Write a string to a file. */
49 waveFile file,
52 writeBytes(file, string, strlen(string));
55 /* Write an integer to a file in little endian order. */
57 waveFile file,
67 writeBytes(file, bytes, 4);
70 /* Write a short integer to a file in little endian order. */
72 waveFile file,
82 writeBytes(file, bytes, 2);
85 /* Read bytes from the input file. Return the number of bytes actually read. */
87 waveFile file,
91 if(file->failed) {
94 return fread(bytes, sizeof(char), length, file->soundFile);
97 /* Read an exact number of bytes from the input file. */
99 waveFile file,
105 if(file->failed) {
108 numRead = fread(bytes, sizeof(char), length, file->soundFile);
110 fprintf(stderr, "Failed to read requested bytes from input file\n");
111 file->failed = 1;
115 /* Read an integer from the input file */
117 waveFile file)
122 readExactBytes(file, bytes, 4);
130 /* Read a short from the input file */
132 waveFile file)
137 readExactBytes(file, bytes, 2);
147 waveFile file,
155 file->failed = 1;
157 readExactBytes(file, buf, length);
160 fprintf(stderr, "Unsupported wave file format\n");
161 file->failed = 1;
166 /* Write the header of the wave file. */
168 waveFile file,
171 /* write the wav file per the wav file format */
172 writeString(file, "RIFF"); /* 00 - RIFF */
173 /* We have to fseek and overwrite this later when we close the file because */
175 writeInt(file, 36 /* + dataLength */); /* 04 - how big is the rest of this file? */
176 writeString(file, "WAVE"); /* 08 - WAVE */
177 writeString(file, "fmt "); /* 12 - fmt */
178 writeInt(file, 16); /* 16 - size of this chunk */
179 writeShort(file, 1); /* 20 - what is the audio format? 1 for PCM = Pulse Code Modulation */
180 writeShort(file, 1); /* 22 - mono or stereo? 1 or 2? (or 5 or ???) */
181 writeInt(file, sampleRate); /* 24 - samples per second (numbers per second) */
182 writeInt(file, sampleRate * 2); /* 28 - bytes per second */
183 writeShort(file, 2); /* 32 - # of bytes in one sample, for all channels */
184 writeShort(file, 16); /* 34 - how many bits in a sample(number)? usually 16 or 24 */
185 writeString(file, "data"); /* 36 - data */
186 writeInt(file, 0); /* 40 - how big is this data chunk */
189 /* Read the header of the wave file. */
191 waveFile file)
195 expectString(file, "RIFF");
196 data = readInt(file); /* 04 - how big is the rest of this file? */
197 expectString(file, "WAVE"); /* 08 - WAVE */
198 expectString(file, "fmt "); /* 12 - fmt */
199 int chunkSize = readInt(file); /* 16 or 18 - size of this chunk */
204 data = readShort(file); /* 20 - what is the audio format? 1 for PCM = Pulse Code Modulation */
209 file->numChannels = readShort(file); /* 22 - mono or stereo? 1 or 2? (or 5 or ???) */
210 file->sampleRate = readInt(file); /* 24 - samples per second (numbers per second) */
211 readInt(file); /* 28 - bytes per second */
212 readShort(file); /* 32 - # of bytes in one sample, for all channels */
213 data = readShort(file); /* 34 - how many bits in a sample(number)? usually 16 or 24 */
219 data = readShort(file);
221 expectString(file, "data"); /* 36 - data */
222 readInt(file); /* 40 - how big is this data chunk */
226 /* Close the input or output file and free the waveFile. */
228 waveFile file)
230 FILE *soundFile = file->soundFile;
234 file->soundFile = NULL;
236 free(file);
239 /* Open a 16-bit little-endian wav file for reading. It may be mono or stereo. */
245 waveFile file;
246 FILE *soundFile = fopen(fileName, "rb");
249 fprintf(stderr, "Unable to open wave file %s for reading\n", fileName);
252 file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
253 file->soundFile = soundFile;
254 file->isInput = 1;
255 if(!readHeader(file)) {
256 closeFile(file);
259 *sampleRate = file->sampleRate;
260 *numChannels = file->numChannels;
261 return file;
264 /* Open a 16-bit little-endian wav file for writing. It may be mono or stereo. */
270 waveFile file;
271 FILE *soundFile = fopen(fileName, "wb");
274 fprintf(stderr, "Unable to open wave file %s for writing\n", fileName);
277 file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
278 file->soundFile = soundFile;
279 file->sampleRate = sampleRate;
280 file->numChannels = numChannels;
281 writeHeader(file, sampleRate);
282 if(file->failed) {
283 closeFile(file);
286 return file;
289 /* Close the sound file. */
291 waveFile file)
293 FILE *soundFile = file->soundFile;
296 if(!file->isInput) {
298 fprintf(stderr, "Failed to seek on input file.\n");
301 /* Now update the file to have the correct size. */
302 writeInt(file, file->bytesWritten - 8);
303 if(file->failed) {
304 fprintf(stderr, "Failed to write wave file size.\n");
308 fprintf(stderr, "Failed to seek on input file.\n");
311 /* Now update the file to have the correct size. */
312 writeInt(file, file->bytesWritten - 48);
313 if(file->failed) {
314 fprintf(stderr, "Failed to write wave file size.\n");
320 closeFile(file);
324 /* Read from the wave file. Return the number of samples read. */
326 waveFile file,
335 if(maxSamples*file->numChannels*2 > WAVE_BUF_LEN) {
336 maxSamples = WAVE_BUF_LEN/(file->numChannels*2);
338 bytesRead = readBytes(file, bytes, maxSamples*file->numChannels*2);
339 samplesRead = bytesRead/(file->numChannels*2);
340 for(i = 0; i < samplesRead*file->numChannels; i++) {
348 /* Write to the wave file. */
350 waveFile file,
358 int total = numSamples*file->numChannels;
362 writeBytes(file, bytes, bytePos);
370 writeBytes(file, bytes, bytePos);
372 return file->failed;