annotate examples/01-Basics/passthrough/render.cpp @ 528:5c8f46fcd4d0 API-update

Updated BelaContext to use separate values for in/ou channels
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 23 Jun 2016 18:17:35 +0100
parents 1cec96845a23
children bfcbeb437869
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24 #include <Bela.h>
giuliomoro@528 25 #include <Scope.h>
giuliomoro@528 26
giuliomoro@528 27 Scope scope;
robert@464 28
robert@464 29 bool setup(BelaContext *context, void *userData)
robert@464 30 {
robert@464 31 // Nothing to do here...
giuliomoro@528 32 printf("%d %d\n", context->audioInChannels, context->audioOutChannels);
giuliomoro@528 33 scope.setup(8, 44100);
robert@464 34 return true;
robert@464 35 }
robert@464 36
robert@464 37 void render(BelaContext *context, void *userData)
robert@464 38 {
giuliomoro@528 39 static const unsigned int audioChannels = min(context->audioInChannels, context->audioOutChannels);
giuliomoro@528 40 static const unsigned int analogChannels = min(context->analogInChannels, context->analogOutChannels);
giuliomoro@528 41
robert@464 42 // Simplest possible case: pass inputs through to outputs
robert@464 43 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@528 44 for(unsigned int ch = 0; ch < audioChannels; ch++){
robert@464 45 // Two equivalent ways to write this code
robert@464 46
robert@464 47 // The long way, using the buffers directly:
giuliomoro@528 48 // context->audioOut[n * context->audioOutChannels + ch] =
giuliomoro@528 49 // context->audioIn[n * context->audioInChannels + ch];
robert@464 50
robert@464 51 // Or using the macros:
robert@464 52 audioWrite(context, n, ch, audioRead(context, n, ch));
robert@464 53 }
robert@464 54 }
robert@464 55
giuliomoro@528 56 // Same with analog channels
robert@464 57 for(unsigned int n = 0; n < context->analogFrames; n++) {
giuliomoro@528 58 for(unsigned int ch = 0; ch < analogChannels; ch++) {
robert@464 59 // Two equivalent ways to write this code
robert@464 60
robert@464 61 // The long way, using the buffers directly:
giuliomoro@528 62 // context->analogOut[n * context->analogOutChannels + ch] =
giuliomoro@528 63 // context->analogIn[n * context->analogInChannels + ch];
robert@464 64
robert@464 65 // Or using the macros:
robert@464 66 analogWrite(context, n, ch, analogRead(context, n, ch));
robert@464 67 }
robert@464 68 }
robert@464 69 }
robert@464 70
robert@464 71 void cleanup(BelaContext *context, void *userData)
robert@464 72 {
robert@464 73
robert@464 74 }
robert@464 75
robert@464 76
robert@464 77 /**
robert@500 78 \example passthrough/render.cpp
robert@464 79
robert@464 80 Audio and analog passthrough: input to output
robert@464 81 -----------------------------------------
robert@464 82
robert@464 83 This sketch demonstrates how to read from and write to the audio and analog input and output buffers.
robert@464 84
robert@464 85 In `render()` you'll see a nested for loop structure. You'll see this in all Bela projects.
robert@464 86 The first for loop cycles through `audioFrames`, the second through
robert@464 87 `audioChannels` (in this case left 0 and right 1).
robert@464 88
robert@464 89 You can access any information about current audio and sensor settings you can do the following:
robert@464 90 `context->name_of_item`. For example `context->audioChannels` returns current number of channels,
robert@464 91 `context->audioFrames` returns the current number of audio frames,
robert@464 92 `context->audioSampleRate` returns the audio sample rate.
robert@464 93
robert@500 94 You can look at all the information you can access in ::BelaContext.
robert@464 95
robert@464 96 Reading and writing from the audio buffers
robert@464 97 ------------------------------------------
robert@464 98
robert@464 99 The simplest way to read samples from the audio input buffer is with
robert@464 100 `audioRead()` which we pass three arguments: context, current audio
robert@464 101 frame and current channel. In this example we have
robert@464 102 `audioRead(context, n, ch)` where both `n` and `ch` are provided by
robert@464 103 the nested for loop structure.
robert@464 104
robert@464 105 We can write samples to the audio output buffer in a similar way using
robert@464 106 `audioWrite()`. This has a fourth argument which is the value of the output.
robert@464 107 For example `audioWrite(context, n, ch, value_to_output)`.
robert@464 108
robert@464 109 Reading and writing from the analog buffers
robert@464 110 -------------------------------------------
robert@464 111
robert@464 112 The same is true for `analogRead()` and `analogWrite()`.
robert@464 113
robert@464 114 Note that for the analog channels we write to and read from the buffers in a separate set
robert@464 115 of nested for loops. This is because the they are sampled at half audio rate by default.
robert@464 116 The first of these for loops cycles through `analogFrames`, the second through
robert@464 117 `analogChannels`.
robert@464 118
robert@464 119 By setting `audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch))` and
robert@464 120 `analogWrite(context, n, ch, analogReadFrame(context, n, ch))` we have a simple
robert@464 121 passthrough of audio input to output and analog input to output.
robert@464 122
robert@464 123
robert@464 124 It is also possible to address the buffers directly, for example:
robert@464 125 `context->audioOut[n * context->audioChannels + ch]`.
robert@464 126 */