comparison examples/03-Analog/analog-input/render.cpp @ 542:3016638b4da2 prerelease

Analog examples updated
author Robert Jack <robert.h.jack@gmail.com>
date Fri, 24 Jun 2016 13:00:31 +0100
parents bfcbeb437869
children
comparison
equal deleted inserted replaced
541:c301cc07ae11 542:3016638b4da2
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */ 22 */
23 23
24 24
25 #include <Bela.h> 25 #include <Bela.h>
26 #include <rtdk.h>
27 #include <cmath> 26 #include <cmath>
28 27
29 float gPhase; 28 float gPhase;
30 float gInverseSampleRate; 29 float gInverseSampleRate;
31 int gAudioFramesPerAnalogFrame; 30 int gAudioFramesPerAnalogFrame;
32 31
33 // These settings are carried over from main.cpp 32 // Set the analog channels to read from
34 // Setting global variables is an alternative approach 33 int gSensorInputFrequency = 0;
35 // to passing a structure to userData in setup() 34 int gSensorInputAmplitude = 1;
36
37 extern int gSensorInputFrequency;
38 extern int gSensorInputAmplitude;
39 35
40 bool setup(BelaContext *context, void *userData) 36 bool setup(BelaContext *context, void *userData)
41 { 37 {
38
39 // Check if analog channels are enabled
42 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) { 40 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
43 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n"); 41 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
44 return false; 42 return false;
45 } 43 }
46 44
45 // Check that we have the same number of inputs and outputs.
46 if(context->audioInChannels != context->audioOutChannels ||
47 context->analogInChannels != context-> analogOutChannels){
48 printf("Error: for this project, you need the same number of input and output channels.\n");
49 return false;
50 }
51
52 // Useful calculations
47 gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames; 53 gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
48 gInverseSampleRate = 1.0 / context->audioSampleRate; 54 gInverseSampleRate = 1.0 / context->audioSampleRate;
49 gPhase = 0.0; 55 gPhase = 0.0;
50 56
51 return true; 57 return true;
59 // There are twice as many audio frames as matrix frames since audio sample rate 65 // There are twice as many audio frames as matrix frames since audio sample rate
60 // is twice as high 66 // is twice as high
61 67
62 for(unsigned int n = 0; n < context->audioFrames; n++) { 68 for(unsigned int n = 0; n < context->audioFrames; n++) {
63 if(!(n % gAudioFramesPerAnalogFrame)) { 69 if(!(n % gAudioFramesPerAnalogFrame)) {
64 // Even audio samples: update frequency and amplitude from the matrix 70 // On even audio samples: read analog inputs and update frequency and amplitude
65 frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000); 71 frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000);
66 amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude); 72 amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude);
67 } 73 }
68 74
69 float out = amplitude * sinf(gPhase); 75 float out = amplitude * sinf(gPhase);
70 76
71 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) 77 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
72 context->audioOut[n * context->audioChannels + channel] = out; 78 audioWrite(context, n, channel, out);
79 }
73 80
81 // Update and wrap phase of sine tone
74 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; 82 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
75 if(gPhase > 2.0 * M_PI) 83 if(gPhase > 2.0 * M_PI)
76 gPhase -= 2.0 * M_PI; 84 gPhase -= 2.0 * M_PI;
77 } 85 }
78 } 86 }
88 96
89 Connecting potentiometers 97 Connecting potentiometers
90 ------------------------- 98 -------------------------
91 99
92 This sketch produces a sine tone, the frequency and amplitude of which are 100 This sketch produces a sine tone, the frequency and amplitude of which are
93 affected by data received on the analog pins. Before looping through each audio 101 modulated by data received on the analog input pins. Before looping through each audio
94 frame, we declare a value for the frequency and amplitude of our sine tone 102 frame, we declare a value for the `frequency` and `amplitude` of our sine tone;
95 (line 55); we adjust these values by taking in data from analog sensors 103 we adjust these values by taking in data from analog sensors (for example potentiometers)
96 (for example potentiometers) with `analogRead()`. 104 with `analogRead()`.
97 105
98 - connect a 10K pot to 3.3V and GND on its 1st and 3rd pins. 106 - connect a 10K pot to 3.3V and GND on its 1st and 3rd pins.
99 - connect the 2nd middle pin of the pot to analogIn 0. 107 - connect the 2nd middle pin of the pot to analogIn 0.
100 - connect another 10K pot in the same way but with the middle pin connected to analogIn 1. 108 - connect another 10K pot in the same way but with the middle pin connected to analogIn 1.
101 109
111 frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000); 119 frequency = map(analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000);
112 amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude); 120 amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude);
113 } 121 }
114 ```` 122 ````
115 123
116 Note that the pin numbers are stored in the variables `gAnalogInputFrequency` and
117 `gAnalogInputAmplitude`. These are declared in the main.cpp file; if you look in
118 that file you will see that they have the values of 0 and 1. Bear in mind that
119 these are analog input pins which is a specific header!
120 */ 124 */