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