andrewm@57: /* andrewm@57: * render.cpp andrewm@57: * andrewm@57: * Created on: Oct 24, 2014 andrewm@57: * Author: parallels andrewm@57: */ andrewm@57: andrewm@57: andrewm@57: #include andrewm@57: #include andrewm@57: #include andrewm@57: #include andrewm@57: andrewm@57: float gPhase; andrewm@57: float gInverseSampleRate; andrewm@57: int gAudioFramesPerAnalogFrame; andrewm@57: andrewm@57: // These settings are carried over from main.cpp andrewm@57: // Setting global variables is an alternative approach andrewm@57: // to passing a structure to userData in setup() andrewm@57: andrewm@57: extern int gSensorInputFrequency; andrewm@57: extern int gSensorInputAmplitude; andrewm@57: andrewm@57: // setup() is called once before the audio rendering starts. andrewm@57: // Use it to perform any initialisation and allocation which is dependent andrewm@57: // on the period size or sample rate. andrewm@57: // andrewm@57: // userData holds an opaque pointer to a data structure that was passed andrewm@57: // in from the call to initAudio(). andrewm@57: // andrewm@57: // Return true on success; returning false halts the program. andrewm@57: andrewm@57: bool setup(BeagleRTContext *context, void *userData) andrewm@57: { andrewm@57: if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) { andrewm@57: rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n"); andrewm@57: return false; andrewm@57: } andrewm@57: andrewm@57: gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames; andrewm@57: gInverseSampleRate = 1.0 / context->audioSampleRate; andrewm@57: gPhase = 0.0; andrewm@57: andrewm@57: return true; andrewm@57: } andrewm@57: andrewm@57: // render() is called regularly at the highest priority by the audio engine. andrewm@57: // Input and output are given from the audio hardware and the other andrewm@57: // ADCs and DACs (if available). If only audio is available, numMatrixFrames andrewm@57: // will be 0. andrewm@57: andrewm@57: void render(BeagleRTContext *context, void *userData) andrewm@57: { andrewm@57: float frequency = 440.0; andrewm@57: float amplitude = 0.8; andrewm@57: andrewm@57: // There are twice as many audio frames as matrix frames since audio sample rate andrewm@57: // is twice as high andrewm@57: andrewm@57: for(unsigned int n = 0; n < context->audioFrames; n++) { andrewm@57: if(!(n % gAudioFramesPerAnalogFrame)) { andrewm@57: // Even audio samples: update frequency and amplitude from the matrix andrewm@57: frequency = map(analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000); andrewm@57: amplitude = analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude); andrewm@57: } andrewm@57: andrewm@57: float out = amplitude * sinf(gPhase); andrewm@57: andrewm@57: for(unsigned int channel = 0; channel < context->audioChannels; channel++) andrewm@57: context->audioOut[n * context->audioChannels + channel] = out; andrewm@57: andrewm@57: gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; andrewm@57: if(gPhase > 2.0 * M_PI) andrewm@57: gPhase -= 2.0 * M_PI; andrewm@57: } andrewm@57: } andrewm@57: andrewm@57: // cleanup() is called once at the end, after the audio has stopped. andrewm@57: // Release any resources that were allocated in setup(). andrewm@57: andrewm@57: void cleanup(BeagleRTContext *context, void *userData) andrewm@57: { andrewm@57: andrewm@57: }