Created
February 26, 2016 05:32
-
-
Save AlexanderBollbach/1c931df3c4ebf571e473 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OSStatus playbackCallback(void * inRefCon, | |
AudioUnitRenderActionFlags * ioActionFlags, | |
const AudioTimeStamp * inTimeStamp, | |
UInt32 inBusNumber, | |
UInt32 inNumberFrames, | |
AudioBufferList * ioData) { | |
AudioController *THIS = (__bridge AudioController *)inRefCon; | |
// test | |
static int a = 0; | |
a++; | |
printf("playback %i \n", a); | |
// temp reference to buffer to fill | |
float * bufferToFill = ioData->mBuffers[0].mData; | |
int start = THIS->_waveStart; | |
int end = THIS->_waveEnd; | |
float lfoRate = THIS->_lfoRate; | |
float lfoAmount = THIS->_lfoAmount; | |
if (end <= 0 || start <= 0) { | |
return noErr; | |
} | |
// clear outside of loop points | |
for (int x = 0; x < start; x++) { | |
THIS->samplesBuffer[x] = 0; | |
} | |
for (int x = end; x < 1000; x++) { | |
THIS->samplesBuffer[x] = 0; | |
} | |
// write samples to buffer | |
for (int x = 0; x < inNumberFrames; x++) { | |
THIS->timeIndex++; | |
// reset to loop begin at loop end | |
if (THIS.bufferIndex >= end) THIS.bufferIndex = start; | |
THIS->samplesBuffer[THIS.bufferIndex] += lfoAmount; | |
if (THIS->timeIndex % (int)lfoRate == 0) lfoAmount = -lfoAmount; | |
// keep values within range -1 - 1 | |
if (THIS->samplesBuffer[THIS.bufferIndex] > 1) THIS->samplesBuffer[THIS.bufferIndex] = 1; | |
if (THIS->samplesBuffer[THIS.bufferIndex] < -1) THIS->samplesBuffer[THIS.bufferIndex] = -1; | |
printf("val: %f \n", THIS->samplesBuffer[x]); | |
bufferToFill[x] = THIS->samplesBuffer[THIS.bufferIndex]; | |
THIS.bufferIndex++; | |
} | |
// assign custom buffer to playback buffer | |
ioData->mBuffers[0].mData = THIS->samplesBuffer; | |
return noErr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment