/* RDAIFF version 0.09, a tiny audiofile reading utility in C by Pieter Suurmond, december 30, 2019. Latest version available at: https://ecomaan.nl/c/rdaiff Include this headerfile in your .c file (as is done in example program read_aiff_files.c). Also include sourcefile rdaiff.c in your project or makefile. Copyright (c) 2004, 2005, 2006, 2019 - Pieter Suurmond Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ typedef struct RDAIFF* RDAIFFp; /* Forward declaration. */ /* To open an existing 8-, 16- or 24-bit AIFF file for reading, call */ int RDAIFF_open(RDAIFFp* handle, const char* filename); /* Apart from opening, it also reads in the first part of the file: the AIFF- header, containing information about length, samplerate, etc. It returns zero, and leaves a valid RDAIFF pointer in *handle, on success. It returns non-zero, and leaves a NULL pointer in *handle, in case of failure. The filename is copied into the dynamic RDAIFF object so, after return, the caller is not obliged to keep it in memory. After opening, AIFF-header parameters can be retreived with by calling (in any order, as many times as you like) */ long RDAIFF_frames (RDAIFFp ptr); /* Total number of sampleframes. */ long RDAIFF_samplerate(RDAIFFp ptr); short RDAIFF_channels (RDAIFFp ptr); /* Pass pointer received from */ short RDAIFF_bits (RDAIFFp ptr); /* RDAIFF_open() as argument. */ /* A routine that prints all header information to stdout, a textfile or some other filestream is */ void RDAIFF_info(RDAIFFp ptr, FILE* to); /* If 'to' equals NULL, RDAIFF_info() won't try to print at all. After successfully opening, you can read audio data by (repeatedly) calling (one of) the following functions: */ int RDAIFF_float (RDAIFFp ptr, float* audio, long frames); int RDAIFF_double(RDAIFFp ptr, double* audio, long frames); int RDAIFF_char (RDAIFFp ptr, signed char* audio, long frames); int RDAIFF_short (RDAIFFp ptr, signed short* audio, long frames); int RDAIFF_int (RDAIFFp ptr, signed int* audio, long frames); int RDAIFF_long (RDAIFFp ptr, signed long* audio, long frames); int RDAIFF_llong (RDAIFFp ptr, signed long long* audio, long frames); /* All 7 functions try to read, without any loss of information, the next 'frames' number of sampleframes from file and write them into an array that was allocated earlier by the caller. Rather than losing only the smallest bit, RDAIFF prefers to return a 'read-error'. All functions return zero on succes and non-zero on failure: 0 = Ok 1 = Data does not fit in destination datatype! 2 = NULL pointer(s) supplied! 3 = Negative number of frames! 4 = Requested too many frames! 5 = Low level read-error! In case of reading floats or doubles, sample-values are scaled, without loss of information, to range from -1.0 (incl.) to +1.0 (excl.). Assuming any float's mantissa can at least hold 24 bits, the scaling of floating point numbers can never 'fail': error 1 can never occur. In case of reading signed integers, samples are never upscaled, downscaled, or clipped to the requested output datatype. Instead, we keep audio-data right-justified, and the reading function returns error 1 when the data doesn't fit in the output datatype. Calling: RDAIFF_char(): RDAIFF_short(): RDAIFF_long(): Output range: after opening: 8-bit file: Ok Ok Ok -128..+127 16-bit file: Error=1 Ok Ok -32768..+32767 24-bit file: Error=1 Error=1 Ok -8388608..+8388607 For example, after opening an 8-bit file, it doesn't matter which function is called: you'll always read sample-values ranging from -128 to +127. Whether RDAIFF_short() behaves like RDAIFF_short() or like RDAIFF_long() may differ per computer, but, most likely, reading ints is just the same as reading longs. You can read the same audio data again, without closing and re-opening, by calling */ int RDAIFF_rewind(RDAIFFp ptr); /* To see how many sampleframes are still available for reading, call */ long RDAIFF_frames_togo(RDAIFFp ptr); /* This number decrements during reading, and function RDAIFF_rewind() sets it back again to RDAIFF_frames() (the total number of frames in the AIFF file). After use, one should close the file and release memory by calling */ int RDAIFF_close(RDAIFFp* handle); /* which also clears object reference *handle for the caller. */