andrewm@13: /* robert@372: ____ _____ _ _ robert@372: | __ )| ____| | / \ robert@372: | _ \| _| | | / _ \ robert@372: | |_) | |___| |___ / ___ \ robert@372: |____/|_____|_____/_/ \_\.io robert@372: robert@372: */ robert@372: robert@372: /* andrewm@13: * render.cpp andrewm@13: * andrewm@13: * Created on: Oct 24, 2014 andrewm@13: * Author: parallels andrewm@13: */ andrewm@13: robert@372: /** robert@372: \example 1_basic_audio_analog_passthrough robert@372: robert@372: Audio and analog passthrough: input to output robert@372: ----------------------------------------- robert@372: robert@372: This sketch demonstrates how to read from and write to the audio and analog input and output buffers. robert@372: robert@372: In `render()` you'll see a nested for loop structure. You'll see this in all Bela projects. robert@372: The first for loop cycles through `audioFrames`, the second through robert@372: `audioChannels` (in this case left 0 and right 1). robert@372: robert@372: You can access any information about current audio and sensor settings you can do the following: robert@372: `context->name_of_item`. For example `context->audioChannels` returns current number of channels, robert@372: `context->audioFrames` returns the current number of audio frames, robert@372: `context->audioSampleRate` returns the audio sample rate. robert@372: robert@372: You can look at all the information you can access in ::BeagleRTContext. robert@372: robert@372: Reading and writing from the audio buffers robert@372: ------------------------------------------ robert@372: robert@372: The simplest way to read samples from the audio input buffer is with robert@372: `audioRead()` which we pass three arguments: context, current audio robert@372: frame and current channel. In this example we have robert@372: `audioRead(context, n, ch)` where both `n` and `ch` are provided by robert@372: the nested for loop structure. robert@372: robert@372: We can write samples to the audio output buffer in a similar way using robert@372: `audioWrite()`. This has a fourth argument which is the value of the output. robert@372: For example `audioWrite(context, n, ch, value_to_output)`. robert@372: robert@372: Reading and writing from the analog buffers robert@372: ------------------------------------------- robert@372: robert@372: The same is true for `analogRead()` and `analogWrite()`. robert@372: robert@372: Note that for the analog channels we write to and read from the buffers in a separate set robert@372: of nested for loops. This is because the they are sampled at half audio rate by default. robert@372: The first of these for loops cycles through `analogFrames`, the second through robert@372: `analogChannels`. robert@372: robert@372: By setting `audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch))` and robert@372: `analogWrite(context, n, ch, analogReadFrame(context, n, ch))` we have a simple robert@372: passthrough of audio input to output and analog input to output. robert@372: robert@372: robert@372: It is also possible to address the buffers directly, for example: robert@372: `context->audioOut[n * context->audioChannels + ch]`. robert@372: */ andrewm@13: giuliomoro@301: #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: giuliomoro@301: bool setup(BelaContext *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: giuliomoro@301: void render(BelaContext *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: andrewm@308: audioWrite(context, n, ch, audioRead(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: andrewm@308: analogWrite(context, n, ch, analogRead(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: giuliomoro@301: void cleanup(BelaContext *context, void *userData) andrewm@13: { andrewm@13: andrewm@13: }