/* WRAIFF version 0.24, december, 2019. A tiny audiofile writing utility in C. Latest version available at: https://ecomaan.nl/c/wraiff Copyright (c) 2004, 2005, 2006, 2009, 2014, 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. Include this headerfile in your .c file (as is done in example file write_aiff_files.c). Also include sourcefile wraiff.c in your project or makefile. Thanks to Eduard Aylon, Maarten de Boer and Siebe Domeijer for comments. */ typedef struct WRAIFF* WRAIFFp; /* Forward declaration. */ /* To create a new AIFF file (possibly overwriting an existing file) call */ int WRAIFF_open(WRAIFFp* handle, /* Receives ptr to WRAIFF object. */ const char* filename, /* Name copied in WRAIFF object. */ long samplerate, /* Frames/second (1 Hz to 1 Ghz). */ short channels, /* Number of channels (1 to 1000).*/ short bits); /* Number of bits (1 to 56). */ /* It returns zero on success, non-zero on failure. When successful, it also leaves a pointer to the allocated WRAIFF object in *handle. In cases of failure, a NULL pointer is left in *handle. After a successful return from WRAIFF_open(), one can write audio data to the WRAIFF object by (repeatedly) calling one of the following functions: */ int WRAIFF_char (WRAIFFp ptr, const signed char* audio, long frames); int WRAIFF_short (WRAIFFp ptr, const signed short* audio, long frames); int WRAIFF_long (WRAIFFp ptr, const signed long* audio, long frames); int WRAIFF_long_long(WRAIFFp ptr, const long long* audio, long frames); int WRAIFF_float (WRAIFFp ptr, const float* audio, long frames); int WRAIFF_double (WRAIFFp ptr, const double* audio, long frames); /* Where argument ptr = a pointer to the audiofile object. audio = a pointer to an array of channel-interleaved signed chars, shorts, longs, or floats or doubles. frames = the number of sampleframes (not samples!) to write, it may be zero, but negative values cause an error. All functions return zero on success and non-zero on failure: 0 = ok, created new WRAIFF object. 1 = wrong argument(s). 2 = sample underflow. 3 = sample overflow. 4 = write error. 5 = very large file. Errors 2 and 3 may be ignored by the user, the library will take care of clipping (prevent sign-swapping). In case of floats and doubles, the lowest audio value that can be written without being clipped is always -1.0. The highest value, however, depends on the number of bits resolution: 8 bits: 127 / 128 = +0.9921875 16 bits: 32767 / 32768 = +0.999969482421875 24 bits: 8388607 / 8388608 = +0.99999988079071044921875 32 bits: 2147483647 / 2147483648 = +0.9999999995343387126922607421875 In case of writing signed chars, shorts, longs and long longs, the minimum and maximum sample values that can be written are: 8 bits: min = -128; max = +127 16 bits: min = -32768; max = +32767 24 bits: min = -8388608; max = +8388607 32 bits: min = -2147483648; max = +2147483647 All simply a consequence of the 2's complement representation of signed integers. At any time between opening and closing of the AIFF file one may call */ int WRAIFF_info(WRAIFFp ptr, FILE* to); /* 0 = success, 1 = fail. */ const char* WRAIFF_get_filename (WRAIFFp ptr); /* NULL when ptr was NULL.*/ long WRAIFF_get_samplerate(WRAIFFp ptr); /* -1 when ptr was NULL. */ long WRAIFF_get_num_frames(WRAIFFp ptr); /* -1 when ptr was NULL. */ /* to see how many sampleframes were written, the samplerate, etc. No problem when argument(s) are NULL. After use, one should close the file and release memory by calling */ int WRAIFF_close(WRAIFFp* handle); /* which rewrites the AIFF header (the WRAIFF object counts the number of sampleframes written), closes the file and sets object reference *handle to NULL. The function returns zero on success, non-zero on failure. */