robert@372: /*
robert@372: ____ _____ _ _
robert@372: | __ )| ____| | / \
robert@372: | _ \| _| | | / _ \
robert@372: | |_) | |___| |___ / ___ \
robert@372: |____/|_____|_____/_/ \_\.io
robert@372:
robert@372: */
robert@372:
robert@372: /**
robert@372: \example 1_scope_basic
robert@372:
robert@372: Oscilloscope in-browser
robert@372: -----------------------
robert@372:
robert@372: This example demonstrates the scope feature of the IDE.
robert@372:
robert@372: The scope is instantiated at the top of the file via `Scope scope;`
robert@372:
robert@372: In `setup()` we define how many channels the scope should have and the sample
robert@372: rate that it should run at via `scope.setup(3, context->audioSampleRate)`.
robert@372:
robert@372: In `render()` we choose what the scope log via `scope.log(out, out2, out3)`.
robert@372: In this example the scope is logging three sine waves with different phases. To see
robert@372: the output click on the Open Scope button.
robert@372:
robert@372: An additional option is to set the trigger of the oscilloscope from within `render()`.
robert@372: In this example we are triggering the scope when oscillator 1 becomes less than
robert@372: oscillator 2 via `scope.trigger()`. Note that this functionality only takes effect
robert@372: when the triggering mode is set to custom in the scope UI.
robert@372: */
robert@372:
giuliomoro@301: #include
l@273: #include
l@273: #include
l@273:
l@273: // set the frequency of the oscillators
l@273: float gFrequency = 110.0;
l@273: float gPhase;
l@273: float gInverseSampleRate;
l@273:
l@273: // instantiate the scope
l@273: Scope scope;
l@273:
giuliomoro@301: bool setup(BelaContext *context, void *userData)
l@273: {
l@273: // tell the scope how many channels and the sample rate
l@273: scope.setup(3, context->audioSampleRate);
l@273:
l@273: gPhase = 0;
l@273: gInverseSampleRate = 1.0f/context->audioSampleRate;
l@273:
l@273: return true;
l@273: }
l@273:
l@273: float lastOut = 0.0;
l@273: float lastOut2 = 0.0;
giuliomoro@301: void render(BelaContext *context, void *userData)
l@273: {
l@273: // iterate over the audio frames and create three oscillators, seperated in phase by PI/2
l@273: for (unsigned int n=0; naudioFrames; n++){
l@273: float out = 0.8f * sinf(gPhase);
l@273: float out2 = 0.8f * sinf(gPhase - M_PI/2);
l@273: float out3 = 0.8f * sinf(gPhase + M_PI/2);
l@273: gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
l@273: if(gPhase > 2.0 * M_PI)
l@273: gPhase -= 2.0 * M_PI;
l@273:
l@273: // log the three oscillators to the scope
l@273: scope.log(out, out2, out3);
l@273:
l@273: // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2
l@273: // note this has no effect unless trigger mode is set to custom in the scope UI
l@273: if (lastOut >= lastOut2 && out < out2){
l@273: scope.trigger();
l@273: }
l@273:
l@273: lastOut = out;
l@273: lastOut2 = out2;
l@273: }
l@273: }
l@273:
giuliomoro@301: void cleanup(BelaContext *context, void *userData)
l@273: {
l@273:
l@273: }