Mercurial > hg > beaglert
comparison projects/scope_basic/render.cpp @ 273:0ee6eebb567a prerelease
Added scope_basic and scope_analogue example projects
author | Liam Donovan <l.b.donovan@qmul.ac.uk> |
---|---|
date | Tue, 17 May 2016 16:31:51 +0100 |
parents | |
children | 5433c83ce04e |
comparison
equal
deleted
inserted
replaced
272:733006bdbca2 | 273:0ee6eebb567a |
---|---|
1 #include <BeagleRT.h> | |
2 #include <Scope.h> | |
3 #include <cmath> | |
4 | |
5 // set the frequency of the oscillators | |
6 float gFrequency = 110.0; | |
7 float gPhase; | |
8 float gInverseSampleRate; | |
9 | |
10 // instantiate the scope | |
11 Scope scope; | |
12 | |
13 bool setup(BeagleRTContext *context, void *userData) | |
14 { | |
15 // tell the scope how many channels and the sample rate | |
16 scope.setup(3, context->audioSampleRate); | |
17 | |
18 gPhase = 0; | |
19 gInverseSampleRate = 1.0f/context->audioSampleRate; | |
20 | |
21 return true; | |
22 } | |
23 | |
24 float lastOut = 0.0; | |
25 float lastOut2 = 0.0; | |
26 void render(BeagleRTContext *context, void *userData) | |
27 { | |
28 // iterate over the audio frames and create three oscillators, seperated in phase by PI/2 | |
29 for (unsigned int n=0; n<context->audioFrames; n++){ | |
30 float out = 0.8f * sinf(gPhase); | |
31 float out2 = 0.8f * sinf(gPhase - M_PI/2); | |
32 float out3 = 0.8f * sinf(gPhase + M_PI/2); | |
33 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | |
34 if(gPhase > 2.0 * M_PI) | |
35 gPhase -= 2.0 * M_PI; | |
36 | |
37 // log the three oscillators to the scope | |
38 scope.log(out, out2, out3); | |
39 | |
40 // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2 | |
41 // note this has no effect unless trigger mode is set to custom in the scope UI | |
42 if (lastOut >= lastOut2 && out < out2){ | |
43 scope.trigger(); | |
44 } | |
45 | |
46 lastOut = out; | |
47 lastOut2 = out2; | |
48 } | |
49 } | |
50 | |
51 void cleanup(BeagleRTContext *context, void *userData) | |
52 { | |
53 | |
54 } |