comparison examples/01-Basics/scope/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 #include <Bela.h>
25 #include <Scope.h>
26 #include <cmath>
27
28 // set the frequency of the oscillators
29 float gFrequency = 110.0;
30 float gPhase;
31 float gInverseSampleRate;
32
33 // instantiate the scope
34 Scope scope;
35
36 bool setup(BelaContext *context, void *userData)
37 {
38 // tell the scope how many channels and the sample rate
39 scope.setup(3, context->audioSampleRate);
40
41 gPhase = 0;
42 gInverseSampleRate = 1.0f/context->audioSampleRate;
43
44 return true;
45 }
46
47 float lastOut = 0.0;
48 float lastOut2 = 0.0;
49 void render(BelaContext *context, void *userData)
50 {
51 // iterate over the audio frames and create three oscillators, seperated in phase by PI/2
52 for (unsigned int n=0; n<context->audioFrames; n++){
53 float out = 0.8f * sinf(gPhase);
54 float out2 = 0.8f * sinf(gPhase - M_PI/2);
55 float out3 = 0.8f * sinf(gPhase + M_PI/2);
56 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
57 if(gPhase > 2.0 * M_PI)
58 gPhase -= 2.0 * M_PI;
59
60 // log the three oscillators to the scope
61 scope.log(out, out2, out3);
62
63 // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2
64 // note this has no effect unless trigger mode is set to custom in the scope UI
65 if (lastOut >= lastOut2 && out < out2){
66 scope.trigger();
67 }
68
69 lastOut = out;
70 lastOut2 = out2;
71 }
72 }
73
74 void cleanup(BelaContext *context, void *userData)
75 {
76
77 }
78
79 /* ------------ Project Explantation ------------ */
80
81 /**
82 \example 01-scope
83
84 Oscilloscope in-browser
85 -----------------------
86
87 This example demonstrates the scope feature of the IDE.
88
89 The scope is instantiated at the top of the file via `Scope scope;`
90
91 In `setup()` we define how many channels the scope should have and the sample
92 rate that it should run at via `scope.setup(3, context->audioSampleRate)`.
93
94 In `render()` we choose what the scope log via `scope.log(out, out2, out3)`.
95 In this example the scope is logging three sine waves with different phases. To see
96 the output click on the <b>Open Scope</b> button.
97
98 An additional option is to set the trigger of the oscilloscope from within `render()`.
99 In this example we are triggering the scope when oscillator 1 becomes less than
100 oscillator 2 via `scope.trigger()`. Note that this functionality only takes effect
101 when the triggering mode is set to custom in the scope UI.
102 */