- //Scrubbing.c
- //gcc /usr/include/MKAudio/MKAiff.c Scrubbing.c -o Scrubbing
- #include <MKAudio/MKAiff.h>
- #include <math.h>
- #define PRERECORDED_AUDIO_PATH "/Users/michaelkrzyzaniak/Desktop/demo.aif"
- #define NUM_CHANNELS aiffNumChannels(preRecordedAudio)
- #define SAMPLE_RATE 44100
- #define BITS_PER_SAMPLE 16
- #define NUM_SECONDS 20
- #define PI 3.141592653589793
- #define GRAIN_DURATION 0.1
- #define GRAIN_OVERLAP 0.3
- const int framesPerGrain = GRAIN_DURATION * SAMPLE_RATE;
- const int numGrains = NUM_SECONDS / (GRAIN_DURATION * GRAIN_OVERLAP);
- #define GAIN 0.5
- int main()
- {
- MKAiff* preRecordedAudio = aiffWithContentsOfFile(PRERECORDED_AUDIO_PATH);
- if(preRecordedAudio == NULL) return 1;
- MKAiff* aiff = aiffWithDurationInSeconds(NUM_CHANNELS, SAMPLE_RATE, BITS_PER_SAMPLE, NUM_SECONDS);
- if(aiff == NULL) return 1;
- const int samplesPerGrain = NUM_CHANNELS * framesPerGrain;
- float grainBuffer [samplesPerGrain];
- float windowFunction[samplesPerGrain];
- int i, j;
- for(i=0; i<samplesPerGrain; i+=NUM_CHANNELS)
- for(j=0; j<NUM_CHANNELS; j++)
- windowFunction[i+j] = GAIN * 0.5 * (1-cos(2*PI*i/samplesPerGrain));
- //windowFunction[i+j] = sin( PI * (double)i / (double)samplesPerGrain );
- for(i=0; i<numGrains; i++)
- {
- aiffSetPlayheadToFrames(preRecordedAudio, i*aiffDurationInFrames(preRecordedAudio)/numGrains);
- aiffReadFloatingPointSamplesAtPlayhead(preRecordedAudio, grainBuffer, samplesPerGrain);
- for(j=0; j<samplesPerGrain; grainBuffer[j] *= windowFunction[j++]);
- aiffSetPlayheadToFrames(aiff, i * framesPerGrain * GRAIN_OVERLAP);
- aiffAddFloatingPointSamplesAtPlayhead(aiff, grainBuffer, samplesPerGrain, aiffFloatSampleType, aiffNo);
- }
- aiffSaveWithFilename(aiff, "Scrubbing.aif");
- aiffDestroy(aiff);
- aiffDestroy(preRecordedAudio);
- return 0;
- }
Output:
Explanation of the Concepts
This example uses granular synthesis to slowly scan or scrub over the contents of an audio-file.
Here, individual grains are pulled out of successive, overlapping locations in a single audio file. The following is a visual example of how the grains are made: the grey line represents the waveform of the original audio, red lines are individual window functions, and the yellow line represents the resulting individual grains.
The grains are then spread out into a new audio file. This is similar to slicing a loaf of bread, and evenly fanning out the slices so that the loaf takes up more space on the platter. Notice that spreading the slices onto an even larger platter results in each slice overlapping the next by less distance. This can, of course be corrected by cutting the bread into more slices to begin with. In granular synthesis, by analogy, the number of grains on average that are sounding at any given time (the average number of overlapping slices of bread, if several cross-sections were taken) plays a large role in the final musical texture. This quantity is known as 'grain density', and is sometimes expressed in terms of 'grain overlap', which is defined as 1/grainDensity. Qualitatively, this means that if there are to be 2 grains sounding at any given time, then each will have to overlap the previous by 0.5 (50%). In granular synthesis, since we are usually are most interested in and the grain density (or grain overlap) and the final duration (the size of the platter), the number of grains to be taken from the original audio file (the number of slices) is usually calculated from these data.
Explanation of the Code
Code Explanation