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
|
robert@464
|
25 #include <Bela.h>
|
robert@464
|
26 #include <rtdk.h>
|
robert@464
|
27 #include <cmath>
|
robert@464
|
28
|
robert@464
|
29 float gPhase;
|
robert@464
|
30 float gInverseSampleRate;
|
robert@464
|
31 int gAudioFramesPerAnalogFrame;
|
robert@464
|
32
|
robert@464
|
33 // These settings are carried over from main.cpp
|
robert@464
|
34 // Setting global variables is an alternative approach
|
robert@464
|
35 // to passing a structure to userData in setup()
|
robert@464
|
36
|
robert@464
|
37 extern int gSensorInputFrequency;
|
robert@464
|
38 extern int gSensorInputAmplitude;
|
robert@464
|
39
|
robert@464
|
40 bool setup(BelaContext *context, void *userData)
|
robert@464
|
41 {
|
robert@464
|
42 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
|
robert@464
|
43 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
|
robert@464
|
44 return false;
|
robert@464
|
45 }
|
robert@464
|
46
|
robert@464
|
47 gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
|
robert@464
|
48 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
robert@464
|
49 gPhase = 0.0;
|
robert@464
|
50
|
robert@464
|
51 return true;
|
robert@464
|
52 }
|
robert@464
|
53
|
robert@464
|
54 void render(BelaContext *context, void *userData)
|
robert@464
|
55 {
|
robert@464
|
56 float frequency = 440.0;
|
robert@464
|
57 float amplitude = 0.8;
|
robert@464
|
58
|
robert@464
|
59 // There are twice as many audio frames as matrix frames since audio sample rate
|
robert@464
|
60 // is twice as high
|
robert@464
|
61
|
robert@464
|
62 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
63 if(!(n % gAudioFramesPerAnalogFrame)) {
|
robert@464
|
64 // Even audio samples: update frequency and amplitude from the matrix
|
robert@464
|
65 frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000);
|
robert@464
|
66 amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude);
|
robert@464
|
67 }
|
robert@464
|
68
|
robert@464
|
69 float out = amplitude * sinf(gPhase);
|
robert@464
|
70
|
robert@464
|
71 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
robert@464
|
72 context->audioOut[n * context->audioChannels + channel] = out;
|
robert@464
|
73
|
robert@464
|
74 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
|
robert@464
|
75 if(gPhase > 2.0 * M_PI)
|
robert@464
|
76 gPhase -= 2.0 * M_PI;
|
robert@464
|
77 }
|
robert@464
|
78 }
|
robert@464
|
79
|
robert@464
|
80 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
81 {
|
robert@464
|
82
|
robert@464
|
83 }
|
robert@464
|
84
|
robert@464
|
85 /* ------------ Project Explantation ------------ */
|
robert@464
|
86
|
robert@464
|
87 /**
|
robert@500
|
88 \example analog-input/render.cpp
|
robert@464
|
89
|
robert@464
|
90 Connecting potentiometers
|
robert@464
|
91 -------------------------
|
robert@464
|
92
|
robert@464
|
93 This sketch produces a sine tone, the frequency and amplitude of which are
|
robert@464
|
94 affected by data received on the analog pins. Before looping through each audio
|
robert@464
|
95 frame, we declare a value for the frequency and amplitude of our sine tone
|
robert@464
|
96 (line 55); we adjust these values by taking in data from analog sensors
|
robert@464
|
97 (for example potentiometers) with `analogRead()`.
|
robert@464
|
98
|
robert@464
|
99 - connect a 10K pot to 3.3V and GND on its 1st and 3rd pins.
|
robert@464
|
100 - connect the 2nd middle pin of the pot to analogIn 0.
|
robert@464
|
101 - connect another 10K pot in the same way but with the middle pin connected to analogIn 1.
|
robert@464
|
102
|
robert@464
|
103 The important thing to notice is that audio is sampled twice as often as analog
|
robert@464
|
104 data. The audio sampling rate is 44.1kHz (44100 frames per second) and the
|
robert@464
|
105 analog sampling rate is 22.05kHz (22050 frames per second). On line 62 you might
|
robert@464
|
106 notice that we are processing the analog data and updating frequency and
|
robert@464
|
107 amplitude only on every second audio sample, since the analog sampling rate is
|
robert@464
|
108 half that of the audio.
|
robert@464
|
109
|
robert@464
|
110 Note that the pin numbers are stored in the variables `gAnalogInputFrequency` and
|
robert@464
|
111 `gAnalogInputAmplitude`. These are declared in the main.cpp file; if you look in
|
robert@464
|
112 that file you will see that they have the values of 0 and 1. Bear in mind that
|
robert@464
|
113 these are analog input pins which is a specific header!
|
robert@464
|
114 */
|