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