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