Mercurial > hg > beaglert
annotate 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 |
rev | line source |
---|---|
l@273 | 1 #include <BeagleRT.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 |
l@273 | 13 bool setup(BeagleRTContext *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; |
l@273 | 26 void render(BeagleRTContext *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 |
l@273 | 51 void cleanup(BeagleRTContext *context, void *userData) |
l@273 | 52 { |
l@273 | 53 |
l@273 | 54 } |