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 <cmath>
|
robert@464
|
27 #include <Scope.h>
|
robert@464
|
28
|
robert@464
|
29 Scope scope;
|
robert@464
|
30
|
robert@464
|
31 float gInverseSampleRate;
|
robert@464
|
32 float gPhase;
|
robert@464
|
33
|
robert@464
|
34 bool setup(BelaContext *context, void *userData)
|
robert@464
|
35 {
|
robert@464
|
36
|
robert@464
|
37 // setup the scope with 3 channels at the audio sample rate
|
robert@464
|
38 scope.setup(3, context->audioSampleRate);
|
robert@464
|
39
|
robert@464
|
40 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
robert@464
|
41 gPhase = 0.0;
|
robert@464
|
42
|
robert@464
|
43 return true;
|
robert@464
|
44 }
|
robert@464
|
45
|
robert@464
|
46 void render(BelaContext *context, void *userData)
|
robert@464
|
47 {
|
robert@464
|
48
|
robert@464
|
49 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
robert@464
|
50
|
robert@464
|
51 // read analogIn channels 0 and 1
|
robert@464
|
52 float in1 = analogRead(context, n, 0);
|
robert@464
|
53 float in2 = analogRead(context, n, 1);
|
robert@464
|
54
|
robert@464
|
55 // map in1 to amplitude and in2 to frequency
|
robert@464
|
56 float amplitude = in1 * 0.8f;
|
robert@464
|
57 float frequency = map(in2, 0, 1, 100, 1000);
|
robert@464
|
58
|
robert@464
|
59 // generate a sine wave with the amplitude and frequency
|
robert@464
|
60 float out = amplitude * sinf(gPhase);
|
robert@464
|
61 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
|
robert@464
|
62 if(gPhase > 2.0 * M_PI)
|
robert@464
|
63 gPhase -= 2.0 * M_PI;
|
robert@464
|
64
|
robert@464
|
65 // log the sine wave and sensor values on the scope
|
robert@464
|
66 scope.log(out, in1, in2);
|
robert@464
|
67
|
robert@464
|
68 // pass the sine wave to the audio outputs
|
robert@464
|
69 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
robert@464
|
70 context->audioOut[n * context->audioChannels + channel] = out;
|
robert@464
|
71
|
robert@464
|
72 }
|
robert@464
|
73 }
|
robert@464
|
74
|
robert@464
|
75 void cleanup(BelaContext *context, void *userData)
|
robert@464
|
76 {
|
robert@464
|
77
|
robert@464
|
78 }
|
robert@464
|
79
|
robert@464
|
80 /* ------------ Project Explantation ------------ */
|
robert@464
|
81
|
robert@464
|
82 /**
|
robert@500
|
83 \example scope-analog/render.cpp
|
robert@464
|
84
|
robert@464
|
85 Connecting potentiometers
|
robert@464
|
86 -------------------------
|
robert@464
|
87
|
robert@464
|
88 This example reads from analogue inputs 0 and 1 via `analogReadFrame()` and
|
robert@464
|
89 generates a sine wave with amplitude and frequency determined by their values.
|
robert@464
|
90 It's best to connect a 10K potentiometer to each of these analog inputs. Far
|
robert@464
|
91 left and far right pins of the pot go to 3.3V and GND, the middle should be
|
robert@464
|
92 connected to the analog in pins.
|
robert@464
|
93
|
robert@464
|
94 The sine wave is then plotted on the oscilloscope. Click the Open Scope button to
|
robert@464
|
95 view the results. As you turn the potentiometers you will see the amplitude and
|
robert@464
|
96 frequency of the sine wave change.
|
robert@464
|
97
|
robert@464
|
98 This project also shows as example of `map()` which allows you to re-scale a number
|
robert@464
|
99 from one range to another. Note that `map()` does not constrain your variable
|
robert@464
|
100 within the upper and lower limits. If you want to do this use the `constrain()`
|
robert@464
|
101 function.
|
robert@464
|
102 */
|