- //gcc /usr/include/MKAudio/MKAiff.c Template.c -o Template
- #include <MKAudio/MKAiff.h>
- #define SAMPLE_RATE 44100
- #define NUM_CHANNELS 1
- #define BITS_PER_SAMPLE 16
- #define NUM_SECONDS 3
- const int numSamples = NUM_SECONDS * NUM_CHANNELS * SAMPLE_RATE;
- int main()
- {
- MKAiff* aiff = aiffWithDurationInSeconds(NUM_CHANNELS, SAMPLE_RATE, BITS_PER_SAMPLE, NUM_SECONDS);
- if(aiff == NULL) return 0;
- float audioBuffer[numSamples];
- int i;
- for(i=0; i<numSamples; i+=NUM_CHANNELS)
- {
- //write code that fills the buffer with data
- //...
- //...
- }
- aiffAppendFloatingPointSamples(aiff, audioBuffer, numSamples, aiffFloatSampleType);
- aiffSaveWithFilename(aiff, "MyGreatAiff.aif");
- aiff = aiffDestroy(aiff);
- return 1;
- }
Builds On
noneExplanation of the Concepts
As has been discussed in detail in the previous section, to create audio on a computer is merely to fill a buffer with numbers that represent the displacement of a speaker cone with respect to rest. Actually sending those numbers to the speaker to produce actual audible sound is a separate and complicated issue. A programmer usually does not have direct programatic access to the hardware that controls the speakers. Usually the operating system alone can communicate directly with the speakers, so to get sound out in real-time a programmer must give his precious buffer to the operating system. How this happens, of course, depends on the operating system. Details on how this works for different systems will be given in the appendix. For the most part, however, this book strives to be as platform-independent as possible. For this reason, we will content ourselves to write our sound buffers to disk in the form of Audio-IFF files, and let someone else's software be responsible for sending it to the speakers. While the aiff specification was designed by Apple Computers, the format is popular enough that nearly all computers can play them. Furthermore, the file format has not changed significantly since its creation over 20 years ago, and does not show signs of needing major amendments any time soon.
This file creates a 3 second audio buffer and an empty AIFF file. It then iterates over the contents of the buffer, one frame at a time. As this is just a template, the program does not actually do anything as it iterates over the buffer. An actual program would fill the buffer with valid samples as it iterates. The buffer is then attached to the AIFF file, which is subsequently saved. This is the basic pattern for almost all of the examples in this book, so a clear understanding of this template will facilitate understanding of the more complex examples.
Explanation of the Code
Actually writing your buffer to disk as an AIFF file is somewhat complicated, and is beyond the scope of this book. Therefore, code has been provided that handles this for you. In order compile the examples in this book, you must first download MKAiff.h and MKAiff.c (available in the "downloads" section above), and put them in the same directory as the code to be compiled. This will allow you to use such functions as "aiffSaveWithFilename()" without actually worrying about how it works. Curious parties may find precisely which functions are provided by MKAiff by examining the "prototypes" section of MKAiff.h. This, however, is not necessary, as the functions will be explained as they are used.
The first line of code