andrewm@13
|
1 /*
|
robert@269
|
2 ____ _____ _ _
|
robert@269
|
3 | __ )| ____| | / \
|
robert@269
|
4 | _ \| _| | | / _ \
|
robert@269
|
5 | |_) | |___| |___ / ___ \
|
robert@269
|
6 |____/|_____|_____/_/ \_\.io
|
robert@269
|
7
|
robert@269
|
8 */
|
robert@269
|
9
|
robert@269
|
10 /*
|
andrewm@13
|
11 * render.cpp
|
andrewm@13
|
12 *
|
andrewm@13
|
13 * Created on: Oct 24, 2014
|
andrewm@13
|
14 * Author: parallels
|
andrewm@13
|
15 */
|
andrewm@13
|
16
|
robert@269
|
17 /**
|
robert@285
|
18 \example 1_basic_audio_analog_passthrough
|
robert@269
|
19
|
robert@285
|
20 Audio and analog passthrough: input to output
|
robert@269
|
21 -----------------------------------------
|
robert@269
|
22
|
robert@285
|
23 This sketch demonstrates how to read from and write to the audio and analog input and output buffers.
|
robert@269
|
24
|
robert@285
|
25 In `render()` you'll see a nested for loop structure. You'll see this in all Bela projects.
|
robert@285
|
26 The first for loop cycles through `audioFrames`, the second through
|
robert@285
|
27 `audioChannels` (in this case left 0 and right 1).
|
robert@269
|
28
|
robert@285
|
29 You can access any information about current audio and sensor settings you can do the following:
|
robert@285
|
30 `context->name_of_item`. For example `context->audioChannels` returns current number of channels,
|
robert@285
|
31 `context->audioFrames` returns the current number of audio frames,
|
robert@285
|
32 `context->audioSampleRate` returns the audio sample rate.
|
robert@269
|
33
|
robert@285
|
34 You can look at all the information you can access in ::BeagleRTContext.
|
robert@269
|
35
|
robert@285
|
36 Reading and writing from the audio buffers
|
robert@285
|
37 ------------------------------------------
|
robert@269
|
38
|
robert@285
|
39 The simplest way to read samples from the audio input buffer is with
|
robert@285
|
40 `audioReadFrame()` which we pass three arguments context, current audio
|
robert@285
|
41 frame and current channel. In this example we have
|
robert@285
|
42 `audioReadFrame(context, n, ch)` where both `n` and `ch` are provided by
|
robert@285
|
43 the nested for loop structure.
|
robert@285
|
44
|
robert@285
|
45 We can write samples to the audio output buffer in a similar way using
|
robert@285
|
46 `audioWriteFrame()`. This has a fourth argument which is the value of the output.
|
robert@285
|
47 For example `audioWriteFrame(context, n, ch, value_to_output)`.
|
robert@285
|
48
|
robert@285
|
49 Reading and writing from the analog buffers
|
robert@285
|
50 -------------------------------------------
|
robert@285
|
51
|
robert@285
|
52 The same is true for `analogReadFrame()` and `analogWriteFrame()`.
|
robert@285
|
53
|
robert@285
|
54 Note that for the analog channels we write to and read from the buffers in a separate set
|
robert@285
|
55 of nested for loops. This is because the they are sampled at half audio rate by default.
|
robert@285
|
56 The first of these for loops cycles through `analogFrames`, the second through
|
robert@285
|
57 `analogChannels`.
|
robert@285
|
58
|
robert@285
|
59 By setting `audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch))` and
|
robert@285
|
60 `analogWriteFrame(context, n, ch, analogReadFrame(context, n, ch))` we have a simple
|
robert@285
|
61 passthrough of audio input to output and analog input to output.
|
robert@285
|
62
|
robert@285
|
63
|
robert@285
|
64 It is also possible to address the buffers directly like this:
|
robert@285
|
65 `context->audioOut[n * context->audioChannels + ch]`.
|
robert@269
|
66
|
robert@269
|
67
|
robert@269
|
68 */
|
robert@269
|
69
|
robert@269
|
70
|
robert@269
|
71
|
robert@269
|
72
|
andrewm@13
|
73
|
andrewm@56
|
74 #include <BeagleRT.h>
|
andrewm@56
|
75 #include <Utilities.h>
|
andrewm@13
|
76 #include <rtdk.h>
|
andrewm@13
|
77
|
andrewm@56
|
78 // setup() is called once before the audio rendering starts.
|
andrewm@13
|
79 // Use it to perform any initialisation and allocation which is dependent
|
andrewm@13
|
80 // on the period size or sample rate.
|
andrewm@13
|
81 //
|
andrewm@13
|
82 // userData holds an opaque pointer to a data structure that was passed
|
andrewm@13
|
83 // in from the call to initAudio().
|
andrewm@13
|
84 //
|
andrewm@13
|
85 // Return true on success; returning false halts the program.
|
andrewm@13
|
86
|
andrewm@56
|
87 bool setup(BeagleRTContext *context, void *userData)
|
andrewm@13
|
88 {
|
andrewm@13
|
89 // Nothing to do here...
|
andrewm@13
|
90 return true;
|
andrewm@13
|
91 }
|
andrewm@13
|
92
|
andrewm@13
|
93 // render() is called regularly at the highest priority by the audio engine.
|
andrewm@13
|
94 // Input and output are given from the audio hardware and the other
|
andrewm@13
|
95 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
andrewm@13
|
96 // will be 0.
|
andrewm@13
|
97
|
andrewm@52
|
98 void render(BeagleRTContext *context, void *userData)
|
andrewm@13
|
99 {
|
andrewm@13
|
100 // Simplest possible case: pass inputs through to outputs
|
andrewm@52
|
101 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
giuliomoro@180
|
102 for(unsigned int ch = 0; ch < context->audioChannels; ch++){
|
giuliomoro@180
|
103 // Two equivalent ways to write this code
|
giuliomoro@180
|
104
|
giuliomoro@180
|
105 // The long way, using the buffers directly:
|
giuliomoro@180
|
106 // context->audioOut[n * context->audioChannels + ch] =
|
giuliomoro@180
|
107 // context->audioIn[n * context->audioChannels + ch];
|
giuliomoro@180
|
108
|
giuliomoro@180
|
109 // Or using the macros:
|
giuliomoro@180
|
110 audioWriteFrame(context, n, ch, audioReadFrame(context, n, ch));
|
giuliomoro@180
|
111 }
|
andrewm@13
|
112 }
|
andrewm@13
|
113
|
robert@285
|
114 // Same with analog channels
|
giuliomoro@180
|
115 for(unsigned int n = 0; n < context->analogFrames; n++) {
|
giuliomoro@180
|
116 for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
|
giuliomoro@180
|
117 // Two equivalent ways to write this code
|
giuliomoro@180
|
118
|
giuliomoro@180
|
119 // The long way, using the buffers directly:
|
giuliomoro@180
|
120 // context->analogOut[n * context->analogChannels + ch] = context->analogIn[n * context->analogChannels + ch];
|
giuliomoro@180
|
121
|
giuliomoro@180
|
122 // Or using the macros:
|
giuliomoro@180
|
123 analogWriteFrame(context, n, ch, analogReadFrame(context, n, ch));
|
giuliomoro@180
|
124 }
|
giuliomoro@180
|
125 }
|
andrewm@13
|
126 }
|
andrewm@13
|
127
|
andrewm@56
|
128 // cleanup() is called once at the end, after the audio has stopped.
|
andrewm@56
|
129 // Release any resources that were allocated in setup().
|
andrewm@13
|
130
|
andrewm@56
|
131 void cleanup(BeagleRTContext *context, void *userData)
|
andrewm@13
|
132 {
|
andrewm@13
|
133
|
andrewm@13
|
134 }
|