robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: // set the frequency of the oscillators robert@464: float gFrequency = 110.0; robert@464: float gPhase; robert@464: float gInverseSampleRate; robert@464: robert@464: // instantiate the scope robert@464: Scope scope; robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: // tell the scope how many channels and the sample rate robert@464: scope.setup(3, context->audioSampleRate); robert@464: robert@464: gPhase = 0; robert@464: gInverseSampleRate = 1.0f/context->audioSampleRate; robert@464: robert@464: return true; robert@464: } robert@464: robert@464: float lastOut = 0.0; robert@464: float lastOut2 = 0.0; robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: // iterate over the audio frames and create three oscillators, seperated in phase by PI/2 robert@464: for (unsigned int n=0; naudioFrames; n++){ robert@464: float out = 0.8f * sinf(gPhase); robert@464: float out2 = 0.8f * sinf(gPhase - M_PI/2); robert@464: float out3 = 0.8f * sinf(gPhase + M_PI/2); robert@464: gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; robert@464: if(gPhase > 2.0 * M_PI) robert@464: gPhase -= 2.0 * M_PI; robert@464: robert@464: // log the three oscillators to the scope robert@464: scope.log(out, out2, out3); robert@464: robert@464: // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2 robert@464: // note this has no effect unless trigger mode is set to custom in the scope UI robert@464: if (lastOut >= lastOut2 && out < out2){ robert@464: scope.trigger(); robert@464: } robert@464: robert@464: lastOut = out; robert@464: lastOut2 = out2; robert@464: } robert@464: } robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: } robert@464: robert@464: /* ------------ Project Explantation ------------ */ robert@464: robert@464: /** robert@464: \example 01-scope robert@464: robert@464: Oscilloscope in-browser robert@464: ----------------------- robert@464: robert@464: This example demonstrates the scope feature of the IDE. robert@464: robert@464: The scope is instantiated at the top of the file via `Scope scope;` robert@464: robert@464: In `setup()` we define how many channels the scope should have and the sample robert@464: rate that it should run at via `scope.setup(3, context->audioSampleRate)`. robert@464: robert@464: In `render()` we choose what the scope log via `scope.log(out, out2, out3)`. robert@464: In this example the scope is logging three sine waves with different phases. To see robert@464: the output click on the Open Scope button. robert@464: robert@464: An additional option is to set the trigger of the oscilloscope from within `render()`. robert@464: In this example we are triggering the scope when oscillator 1 becomes less than robert@464: oscillator 2 via `scope.trigger()`. Note that this functionality only takes effect robert@464: when the triggering mode is set to custom in the scope UI. robert@464: */