annotate examples/01-Basics/passthrough/render.cpp @ 507:1cec96845a23 prerelease

Explanted explantation
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 22 Jun 2016 01:51:17 +0100
parents b935f890e512
children 5c8f46fcd4d0
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>
robert@464 25 #include <rtdk.h>
robert@464 26
robert@464 27 bool setup(BelaContext *context, void *userData)
robert@464 28 {
robert@464 29 // Nothing to do here...
robert@464 30 return true;
robert@464 31 }
robert@464 32
robert@464 33 void render(BelaContext *context, void *userData)
robert@464 34 {
robert@464 35 // Simplest possible case: pass inputs through to outputs
robert@464 36 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 37 for(unsigned int ch = 0; ch < context->audioChannels; ch++){
robert@464 38 // Two equivalent ways to write this code
robert@464 39
robert@464 40 // The long way, using the buffers directly:
robert@464 41 // context->audioOut[n * context->audioChannels + ch] =
robert@464 42 // context->audioIn[n * context->audioChannels + ch];
robert@464 43
robert@464 44 // Or using the macros:
robert@464 45 audioWrite(context, n, ch, audioRead(context, n, ch));
robert@464 46 }
robert@464 47 }
robert@464 48
robert@464 49 // Same with analog channelss
robert@464 50 for(unsigned int n = 0; n < context->analogFrames; n++) {
robert@464 51 for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
robert@464 52 // Two equivalent ways to write this code
robert@464 53
robert@464 54 // The long way, using the buffers directly:
robert@464 55 // context->analogOut[n * context->analogChannels + ch] = context->analogIn[n * context->analogChannels + ch];
robert@464 56
robert@464 57 // Or using the macros:
robert@464 58 analogWrite(context, n, ch, analogRead(context, n, ch));
robert@464 59 }
robert@464 60 }
robert@464 61 }
robert@464 62
robert@464 63 void cleanup(BelaContext *context, void *userData)
robert@464 64 {
robert@464 65
robert@464 66 }
robert@464 67
robert@464 68
robert@464 69 /**
robert@500 70 \example passthrough/render.cpp
robert@464 71
robert@464 72 Audio and analog passthrough: input to output
robert@464 73 -----------------------------------------
robert@464 74
robert@464 75 This sketch demonstrates how to read from and write to the audio and analog input and output buffers.
robert@464 76
robert@464 77 In `render()` you'll see a nested for loop structure. You'll see this in all Bela projects.
robert@464 78 The first for loop cycles through `audioFrames`, the second through
robert@464 79 `audioChannels` (in this case left 0 and right 1).
robert@464 80
robert@464 81 You can access any information about current audio and sensor settings you can do the following:
robert@464 82 `context->name_of_item`. For example `context->audioChannels` returns current number of channels,
robert@464 83 `context->audioFrames` returns the current number of audio frames,
robert@464 84 `context->audioSampleRate` returns the audio sample rate.
robert@464 85
robert@500 86 You can look at all the information you can access in ::BelaContext.
robert@464 87
robert@464 88 Reading and writing from the audio buffers
robert@464 89 ------------------------------------------
robert@464 90
robert@464 91 The simplest way to read samples from the audio input buffer is with
robert@464 92 `audioRead()` which we pass three arguments: context, current audio
robert@464 93 frame and current channel. In this example we have
robert@464 94 `audioRead(context, n, ch)` where both `n` and `ch` are provided by
robert@464 95 the nested for loop structure.
robert@464 96
robert@464 97 We can write samples to the audio output buffer in a similar way using
robert@464 98 `audioWrite()`. This has a fourth argument which is the value of the output.
robert@464 99 For example `audioWrite(context, n, ch, value_to_output)`.
robert@464 100
robert@464 101 Reading and writing from the analog buffers
robert@464 102 -------------------------------------------
robert@464 103
robert@464 104 The same is true for `analogRead()` and `analogWrite()`.
robert@464 105
robert@464 106 Note that for the analog channels we write to and read from the buffers in a separate set
robert@464 107 of nested for loops. This is because the they are sampled at half audio rate by default.
robert@464 108 The first of these for loops cycles through `analogFrames`, the second through
robert@464 109 `analogChannels`.
robert@464 110
robert@464 111 By setting `audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch))` and
robert@464 112 `analogWrite(context, n, ch, analogReadFrame(context, n, ch))` we have a simple
robert@464 113 passthrough of audio input to output and analog input to output.
robert@464 114
robert@464 115
robert@464 116 It is also possible to address the buffers directly, for example:
robert@464 117 `context->audioOut[n * context->audioChannels + ch]`.
robert@464 118 */