robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: #include giuliomoro@528: #include giuliomoro@528: giuliomoro@528: Scope scope; robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: // Nothing to do here... giuliomoro@528: printf("%d %d\n", context->audioInChannels, context->audioOutChannels); giuliomoro@528: scope.setup(8, 44100); robert@464: return true; robert@464: } robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { giuliomoro@528: static const unsigned int audioChannels = min(context->audioInChannels, context->audioOutChannels); giuliomoro@528: static const unsigned int analogChannels = min(context->analogInChannels, context->analogOutChannels); giuliomoro@528: robert@464: // Simplest possible case: pass inputs through to outputs robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { giuliomoro@528: for(unsigned int ch = 0; ch < audioChannels; ch++){ robert@464: // Two equivalent ways to write this code robert@464: robert@464: // The long way, using the buffers directly: giuliomoro@528: // context->audioOut[n * context->audioOutChannels + ch] = giuliomoro@528: // context->audioIn[n * context->audioInChannels + ch]; robert@464: robert@464: // Or using the macros: robert@464: audioWrite(context, n, ch, audioRead(context, n, ch)); robert@464: } robert@464: } robert@464: giuliomoro@528: // Same with analog channels robert@464: for(unsigned int n = 0; n < context->analogFrames; n++) { giuliomoro@528: for(unsigned int ch = 0; ch < analogChannels; ch++) { robert@464: // Two equivalent ways to write this code robert@464: robert@464: // The long way, using the buffers directly: giuliomoro@528: // context->analogOut[n * context->analogOutChannels + ch] = giuliomoro@528: // context->analogIn[n * context->analogInChannels + ch]; robert@464: robert@464: // Or using the macros: robert@464: analogWrite(context, n, ch, analogRead(context, n, ch)); robert@464: } robert@464: } robert@464: } robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: } robert@464: robert@464: robert@464: /** robert@500: \example passthrough/render.cpp robert@464: robert@464: Audio and analog passthrough: input to output robert@464: ----------------------------------------- robert@464: robert@464: This sketch demonstrates how to read from and write to the audio and analog input and output buffers. robert@464: robert@464: In `render()` you'll see a nested for loop structure. You'll see this in all Bela projects. robert@464: The first for loop cycles through `audioFrames`, the second through robert@464: `audioChannels` (in this case left 0 and right 1). robert@464: robert@464: You can access any information about current audio and sensor settings you can do the following: robert@464: `context->name_of_item`. For example `context->audioChannels` returns current number of channels, robert@464: `context->audioFrames` returns the current number of audio frames, robert@464: `context->audioSampleRate` returns the audio sample rate. robert@464: robert@500: You can look at all the information you can access in ::BelaContext. robert@464: robert@464: Reading and writing from the audio buffers robert@464: ------------------------------------------ robert@464: robert@464: The simplest way to read samples from the audio input buffer is with robert@464: `audioRead()` which we pass three arguments: context, current audio robert@464: frame and current channel. In this example we have robert@464: `audioRead(context, n, ch)` where both `n` and `ch` are provided by robert@464: the nested for loop structure. robert@464: robert@464: We can write samples to the audio output buffer in a similar way using robert@464: `audioWrite()`. This has a fourth argument which is the value of the output. robert@464: For example `audioWrite(context, n, ch, value_to_output)`. robert@464: robert@464: Reading and writing from the analog buffers robert@464: ------------------------------------------- robert@464: robert@464: The same is true for `analogRead()` and `analogWrite()`. robert@464: robert@464: Note that for the analog channels we write to and read from the buffers in a separate set robert@464: of nested for loops. This is because the they are sampled at half audio rate by default. robert@464: The first of these for loops cycles through `analogFrames`, the second through robert@464: `analogChannels`. robert@464: robert@464: By setting `audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch))` and robert@464: `analogWrite(context, n, ch, analogReadFrame(context, n, ch))` we have a simple robert@464: passthrough of audio input to output and analog input to output. robert@464: robert@464: robert@464: It is also possible to address the buffers directly, for example: robert@464: `context->audioOut[n * context->audioChannels + ch]`. robert@464: */