annotate examples/01-Basics/passthrough/render.cpp @ 543:8f8809c77dda prerelease

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