l@273
|
1 // this example reads the analogue inputs 0 and 1
|
l@273
|
2 // and generates a sine wave with an amplitude and
|
l@273
|
3 // frequency determined by their values
|
l@273
|
4 // it then plots these on the oscilloscope
|
l@273
|
5
|
giuliomoro@301
|
6 #include <Bela.h>
|
l@273
|
7 #include <cmath>
|
l@273
|
8 #include <Scope.h>
|
l@273
|
9
|
l@273
|
10 Scope scope;
|
l@273
|
11
|
l@273
|
12 float gInverseSampleRate;
|
l@273
|
13 float gPhase;
|
l@273
|
14
|
giuliomoro@301
|
15 bool setup(BelaContext *context, void *userData)
|
l@273
|
16 {
|
l@273
|
17
|
l@273
|
18 // setup the scope with 3 channels at the audio sample rate
|
l@273
|
19 scope.setup(3, context->audioSampleRate);
|
l@273
|
20
|
l@273
|
21 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
l@273
|
22 gPhase = 0.0;
|
l@273
|
23
|
l@273
|
24 return true;
|
l@273
|
25 }
|
l@273
|
26
|
giuliomoro@301
|
27 void render(BelaContext *context, void *userData)
|
l@273
|
28 {
|
l@273
|
29
|
l@273
|
30 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
l@273
|
31
|
l@273
|
32 // read analogIn channels 0 and 1
|
andrewm@308
|
33 float in1 = analogRead(context, n, 0);
|
andrewm@308
|
34 float in2 = analogRead(context, n, 1);
|
l@273
|
35
|
l@273
|
36 // map in1 to amplitude and in2 to frequency
|
l@273
|
37 float amplitude = in1 * 0.8f;
|
l@273
|
38 float frequency = map(in2, 0, 1, 100, 1000);
|
l@273
|
39
|
l@273
|
40 // generate a sine wave with the amplitude and frequency
|
l@273
|
41 float out = amplitude * sinf(gPhase);
|
l@273
|
42 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
|
l@273
|
43 if(gPhase > 2.0 * M_PI)
|
l@273
|
44 gPhase -= 2.0 * M_PI;
|
l@273
|
45
|
l@273
|
46 // log the sine wave and sensor values on the scope
|
l@273
|
47 scope.log(out, in1, in2);
|
l@273
|
48
|
l@273
|
49 // pass the sine wave to the audio outputs
|
l@273
|
50 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
l@273
|
51 context->audioOut[n * context->audioChannels + channel] = out;
|
l@273
|
52
|
l@273
|
53 }
|
l@273
|
54 }
|
l@273
|
55
|
giuliomoro@301
|
56 void cleanup(BelaContext *context, void *userData)
|
l@273
|
57 {
|
l@273
|
58
|
l@273
|
59 }
|