giuliomoro@301
|
1 #include <Bela.h>
|
l@273
|
2 #include <Scope.h>
|
l@273
|
3 #include <cmath>
|
l@273
|
4
|
l@273
|
5 // set the frequency of the oscillators
|
l@273
|
6 float gFrequency = 110.0;
|
l@273
|
7 float gPhase;
|
l@273
|
8 float gInverseSampleRate;
|
l@273
|
9
|
l@273
|
10 // instantiate the scope
|
l@273
|
11 Scope scope;
|
l@273
|
12
|
giuliomoro@301
|
13 bool setup(BelaContext *context, void *userData)
|
l@273
|
14 {
|
l@273
|
15 // tell the scope how many channels and the sample rate
|
l@273
|
16 scope.setup(3, context->audioSampleRate);
|
l@273
|
17
|
l@273
|
18 gPhase = 0;
|
l@273
|
19 gInverseSampleRate = 1.0f/context->audioSampleRate;
|
l@273
|
20
|
l@273
|
21 return true;
|
l@273
|
22 }
|
l@273
|
23
|
l@273
|
24 float lastOut = 0.0;
|
l@273
|
25 float lastOut2 = 0.0;
|
giuliomoro@301
|
26 void render(BelaContext *context, void *userData)
|
l@273
|
27 {
|
l@273
|
28 // iterate over the audio frames and create three oscillators, seperated in phase by PI/2
|
l@273
|
29 for (unsigned int n=0; n<context->audioFrames; n++){
|
l@273
|
30 float out = 0.8f * sinf(gPhase);
|
l@273
|
31 float out2 = 0.8f * sinf(gPhase - M_PI/2);
|
l@273
|
32 float out3 = 0.8f * sinf(gPhase + M_PI/2);
|
l@273
|
33 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
l@273
|
34 if(gPhase > 2.0 * M_PI)
|
l@273
|
35 gPhase -= 2.0 * M_PI;
|
l@273
|
36
|
l@273
|
37 // log the three oscillators to the scope
|
l@273
|
38 scope.log(out, out2, out3);
|
l@273
|
39
|
l@273
|
40 // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2
|
l@273
|
41 // note this has no effect unless trigger mode is set to custom in the scope UI
|
l@273
|
42 if (lastOut >= lastOut2 && out < out2){
|
l@273
|
43 scope.trigger();
|
l@273
|
44 }
|
l@273
|
45
|
l@273
|
46 lastOut = out;
|
l@273
|
47 lastOut2 = out2;
|
l@273
|
48 }
|
l@273
|
49 }
|
l@273
|
50
|
giuliomoro@301
|
51 void cleanup(BelaContext *context, void *userData)
|
l@273
|
52 {
|
l@273
|
53
|
l@273
|
54 }
|