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