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