-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathrender.cpp
121 lines (94 loc) · 3.3 KB
/
render.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
/**
\example Analog/scope-analog/render.cpp
Scoping sensor input
-------------------------
This example reads from analogue inputs 0 and 1 via `analogRead()` and
generates a sine wave with amplitude and frequency determined by their values.
It's best to connect a 10K potentiometer to each of these analog inputs. Far
left and far right pins of the pot go to 3.3V and GND, the middle should be
connected to the analog in pins.
The sine wave is then plotted on the oscilloscope. Click the Open Scope button to
view the results. As you turn the potentiometers you will see the amplitude and
frequency of the sine wave change. You can also see the two sensor readings plotted
on the oscilloscope.
The scope is initialised in `setup()` where the number of channels and sampling rate
are set.
`````
scope.setup(3, context->audioSampleRate);
`````
We can then pass signals to the scope in `render()` using:
``````
scope.log(out, gIn1, gIn2);
``````
This project also shows as example of `map()` which allows you to re-scale a number
from one range to another. Note that `map()` does not constrain your variable
within the upper and lower limits. If you want to do this use the `constrain()`
function.
*/
#include <Bela.h>
#include <cmath>
#include <libraries/Scope/Scope.h>
Scope scope;
int gAudioFramesPerAnalogFrame = 0;
float gInverseSampleRate;
float gPhase;
float gAmplitude;
float gFrequency;
float gIn1;
float gIn2;
// For this example you need to set the Analog Sample Rate to
// 44.1 KHz which you can do in the settings tab.
bool setup(BelaContext *context, void *userData)
{
// setup the scope with 3 channels at the audio sample rate
scope.setup(3, context->audioSampleRate);
if(context->analogSampleRate > context->audioSampleRate)
{
fprintf(stderr, "Error: for this project the sampling rate of the analog inputs has to be <= the audio sample rate\n");
return false;
}
if(context->analogInChannels < 2)
{
fprintf(stderr, "Error: for this project you need at least two analog inputs\n");
return false;
}
if(context->analogFrames)
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++) {
if(gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame)) {
// read analog inputs and update frequency and amplitude
gIn1 = analogRead(context, n/gAudioFramesPerAnalogFrame, 0);
gIn2 = analogRead(context, n/gAudioFramesPerAnalogFrame, 1);
gAmplitude = gIn1 * 0.8f;
gFrequency = map(gIn2, 0, 1, 100, 1000);
}
// generate a sine wave with the amplitude and frequency
float out = gAmplitude * sinf(gPhase);
gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
if(gPhase > M_PI)
gPhase -= 2.0f * (float)M_PI;
// log the sine wave and sensor values on the scope
scope.log(out, gIn1, gIn2);
// pass the sine wave to the audio outputs
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
audioWrite(context, n, channel, out);
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}