l@273: // this example reads the analogue inputs 0 and 1 l@273: // and generates a sine wave with an amplitude and l@273: // frequency determined by their values l@273: // it then plots these on the oscilloscope l@273: giuliomoro@301: #include l@273: #include l@273: #include l@273: l@273: Scope scope; l@273: l@273: float gInverseSampleRate; l@273: float gPhase; l@273: giuliomoro@301: bool setup(BelaContext *context, void *userData) l@273: { l@273: l@273: // setup the scope with 3 channels at the audio sample rate l@273: scope.setup(3, context->audioSampleRate); l@273: l@273: gInverseSampleRate = 1.0 / context->audioSampleRate; l@273: gPhase = 0.0; l@273: l@273: return true; l@273: } l@273: giuliomoro@301: void render(BelaContext *context, void *userData) l@273: { l@273: l@273: for(unsigned int n = 0; n < context->audioFrames; n++) { l@273: l@273: // read analogIn channels 0 and 1 andrewm@308: float in1 = analogRead(context, n, 0); andrewm@308: float in2 = analogRead(context, n, 1); l@273: l@273: // map in1 to amplitude and in2 to frequency l@273: float amplitude = in1 * 0.8f; l@273: float frequency = map(in2, 0, 1, 100, 1000); l@273: l@273: // generate a sine wave with the amplitude and frequency l@273: float out = amplitude * sinf(gPhase); l@273: gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; l@273: if(gPhase > 2.0 * M_PI) l@273: gPhase -= 2.0 * M_PI; l@273: l@273: // log the sine wave and sensor values on the scope l@273: scope.log(out, in1, in2); l@273: l@273: // pass the sine wave to the audio outputs l@273: for(unsigned int channel = 0; channel < context->audioChannels; channel++) l@273: context->audioOut[n * context->audioChannels + channel] = out; l@273: l@273: } l@273: } l@273: giuliomoro@301: void cleanup(BelaContext *context, void *userData) l@273: { l@273: l@273: }