- //ForwardBackward.c
- //gcc MKAiff.c ForwardBackward.c -o ForwardBackward
- #include "MKAiff.h"
- #define AUDIO_SAMPLE_PATH "demo.aif"
- #define NUM_SECONDS 3
- int main()
- {
- MKAiff* audioSample = aiffWithContentsOfFile(AUDIO_SAMPLE_PATH);
- if(audioSample == NULL) return 1;
- const int NUM_CHANNELS = aiffNumChannels (audioSample);
- const int SAMPLE_RATE = aiffSampleRate (audioSample);
- const int BITS_PER_SAMPLE = aiffBitsPerSample(audioSample);
- int WAVETABLE_NUM_SAMPLES, WAVETABLE_NUM_FRAMES, sampleLocation;
- if(aiffHasInstrumentInfo(audioSample))
- {
- uint32_t beginning, end;
- aiffPositionInFramesOfMarkerWithID(audioSample, aiffInstrumentSustainLoopStartMarkerID(audioSample), &beginning);
- aiffPositionInFramesOfMarkerWithID(audioSample, aiffInstrumentSustainLoopEndMarkerID (audioSample), &end);
- WAVETABLE_NUM_FRAMES = end-beginning;
- sampleLocation = beginning;
- }
- else
- {
- WAVETABLE_NUM_FRAMES = aiffDurationInSamples(audioSample) / 10;
- srandom(time(NULL));
- sampleLocation = (random() / (double) RAND_MAX) * (aiffDurationInFrames(audioSample) - WAVETABLE_NUM_FRAMES);
- }
- WAVETABLE_NUM_SAMPLES = WAVETABLE_NUM_FRAMES * NUM_CHANNELS;
- const int NUM_BUFFERS = (NUM_SECONDS * SAMPLE_RATE) / (2 * WAVETABLE_NUM_FRAMES);
- MKAiff* aiff = aiffWithDurationInSeconds(NUM_CHANNELS, SAMPLE_RATE, BITS_PER_SAMPLE, NUM_SECONDS);
- if(aiff == NULL) return 1;
- float wavetable[WAVETABLE_NUM_SAMPLES * 2];
- aiffSetPlayheadToFrames(audioSample, sampleLocation);
- aiffReadFloatingPointSamplesAtPlayhead(audioSample, wavetable, WAVETABLE_NUM_SAMPLES);
- int i, j;
- for(i=0; i<WAVETABLE_NUM_SAMPLES; i+=NUM_CHANNELS)
- for(j=0; j<NUM_CHANNELS; j++)
- wavetable[2*WAVETABLE_NUM_SAMPLES - (i+NUM_CHANNELS) + j] = wavetable[i+j];
- for(i=0; i<NUM_BUFFERS; i++)
- aiffAppendFloatingPointSamples(aiff, wavetable, WAVETABLE_NUM_SAMPLES*2, aiffFloatSampleType);
- aiffSaveWithFilename(aiff, "ForwardBackward.aif");
- aiffDestroy(aiff);
- aiffDestroy(audioSample);
- return 0;
- }
Output:
Explanation of the Concepts
This example reads the "Sustain Loop" from the audio-sample "demo.aif" and copies it alternately forwards and backwards into a new audio file.
It is possible to create a forward-backward loop by making a wavetable that is just big enough to hold the forward version of the audio, and using some sort of play-head that scans the wavetable and switches direction when it reaches the beginning or end of the table. This possibility is implemented in the ASR example, later in this chapter. It will, however, be easier to create a wavetable that is twice as big as the sound to be looped, and of course, put the forward version in the first half of the table, and copy that backwards into the second half of the table, so that the table can always be read in the forward direction. This is the approach taken here. In either case, the difficult part is that, because the channels are interleaved, if we just reverse all of the samples in a stereo buffer, the channels will get switched, so that the right channel goes to the left ear, and vice-versa. This is not usually the desired effect, so care must be taken to only reverse the order of the frames, without reversing the order of the samples within those frames.
This example checks to see if the pre-recorded audio-sample already has 'instrument info' embedded within it, according to the previous example. If it does, then the wavetable is made from the 'sustain loop', otherwise, some samples are taken at random and used for the wavetable.
In this example, the wavetable is simply copied into a new audio file as many times as it will fit in its entirety, before the end of the new file draws too nigh. In real-time applications you would keep a pointer to the next sample in the wavetable, which would be incremented by 1 (and wrapped around to the beginning after the last sample) each time one sample is given to the output stream.
Explanation of the Code
Code Explanation