annotate examples/01-Basics/scope/render.cpp @ 507:1cec96845a23 prerelease

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