andrewm@13: /* robert@269: ____ _____ _ _ robert@269: | __ )| ____| | / \ robert@269: | _ \| _| | | / _ \ robert@269: | |_) | |___| |___ / ___ \ robert@269: |____/|_____|_____/_/ \_\.io robert@269: robert@269: */ robert@269: robert@269: /* andrewm@13: * render.cpp andrewm@13: * andrewm@13: * Created on: Oct 24, 2014 andrewm@13: * Author: parallels andrewm@13: */ andrewm@13: robert@269: /** robert@269: \example 1_basic_audio_passthrough robert@269: robert@269: Audio passthrough: input to output robert@269: ----------------------------------------- robert@269: robert@269: This sketch demonstrates the simplest possible case of using audio: it passes robert@269: audio input straight to audio output. robert@269: robert@269: Note the nested `for` loop structure. You will see this in all Bela projects. The robert@269: first `for` loop cycles through the audio frames, the second through each of the robert@269: audio channels (in this case left 0 and right 1). robert@269: robert@269: robert@269: We write samples to the audio output buffer like this: robert@269: `context->audioOut[n * context->audioChannels + ch]` where `n` is the current audio robert@269: frame and `ch` is the current channel, both provided by the nested for loop structure. robert@269: robert@269: We can access samples in the audio input buffer in a similar way, like this: robert@269: `context->audioIn[n * context->audioChannels + ch]`. robert@269: robert@269: So a simple audio pass through is achieved by setting output buffer equal to robert@269: input buffer: robert@269: `context->audioOut[n * context->audioChannels + ch] = context->audioIn[n * context->audioChannels + ch]`. robert@269: robert@269: robert@269: */ robert@269: robert@269: robert@269: robert@269: andrewm@13: andrewm@56: #include andrewm@56: #include andrewm@13: #include andrewm@13: andrewm@56: // setup() is called once before the audio rendering starts. andrewm@13: // Use it to perform any initialisation and allocation which is dependent andrewm@13: // on the period size or sample rate. andrewm@13: // andrewm@13: // userData holds an opaque pointer to a data structure that was passed andrewm@13: // in from the call to initAudio(). andrewm@13: // andrewm@13: // Return true on success; returning false halts the program. andrewm@13: andrewm@56: bool setup(BeagleRTContext *context, void *userData) andrewm@13: { andrewm@13: // Nothing to do here... andrewm@13: return true; andrewm@13: } andrewm@13: andrewm@13: // render() is called regularly at the highest priority by the audio engine. andrewm@13: // Input and output are given from the audio hardware and the other andrewm@13: // ADCs and DACs (if available). If only audio is available, numMatrixFrames andrewm@13: // will be 0. andrewm@13: andrewm@52: void render(BeagleRTContext *context, void *userData) andrewm@13: { andrewm@13: // Simplest possible case: pass inputs through to outputs andrewm@52: for(unsigned int n = 0; n < context->audioFrames; n++) { giuliomoro@180: for(unsigned int ch = 0; ch < context->audioChannels; ch++){ giuliomoro@180: // Two equivalent ways to write this code giuliomoro@180: giuliomoro@180: // The long way, using the buffers directly: giuliomoro@180: // context->audioOut[n * context->audioChannels + ch] = giuliomoro@180: // context->audioIn[n * context->audioChannels + ch]; giuliomoro@180: giuliomoro@180: // Or using the macros: giuliomoro@180: audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch)); giuliomoro@180: } andrewm@13: } andrewm@13: giuliomoro@180: // Same with analog channelss giuliomoro@180: for(unsigned int n = 0; n < context->analogFrames; n++) { giuliomoro@180: for(unsigned int ch = 0; ch < context->analogChannels; ch++) { giuliomoro@180: // Two equivalent ways to write this code giuliomoro@180: giuliomoro@180: // The long way, using the buffers directly: giuliomoro@180: // context->analogOut[n * context->analogChannels + ch] = context->analogIn[n * context->analogChannels + ch]; giuliomoro@180: giuliomoro@180: // Or using the macros: giuliomoro@180: analogWriteFrame(context, n, ch, analogReadFrame(context, n, ch)); giuliomoro@180: } giuliomoro@180: } andrewm@13: } andrewm@13: andrewm@56: // cleanup() is called once at the end, after the audio has stopped. andrewm@56: // Release any resources that were allocated in setup(). andrewm@13: andrewm@56: void cleanup(BeagleRTContext *context, void *userData) andrewm@13: { andrewm@13: andrewm@13: }