comparison projects/basic_sensor/render.cpp @ 13:6adb088196a7

Fixed ADC bug; added a simple passthrough test
author andrewm
date Fri, 23 Jan 2015 15:17:09 +0000
parents a6beeba3a648
children a6d223473ea2
comparison
equal deleted inserted replaced
12:a6beeba3a648 13:6adb088196a7
11 #include <rtdk.h> 11 #include <rtdk.h>
12 #include <cmath> 12 #include <cmath>
13 13
14 float gPhase; 14 float gPhase;
15 float gInverseSampleRate; 15 float gInverseSampleRate;
16 int gNumChannels; 16 int gMatrixFramesPerAudioFrame;
17 17
18 // These settings are carried over from main.cpp 18 // These settings are carried over from main.cpp
19 // Setting global variables is an alternative approach 19 // Setting global variables is an alternative approach
20 // to passing a structure to userData in initialise_render() 20 // to passing a structure to userData in initialise_render()
21 21
35 int numMatrixFramesPerPeriod, 35 int numMatrixFramesPerPeriod,
36 int numAudioFramesPerPeriod, 36 int numAudioFramesPerPeriod,
37 float matrixSampleRate, float audioSampleRate, 37 float matrixSampleRate, float audioSampleRate,
38 void *userData) 38 void *userData)
39 { 39 {
40 if(numMatrixFramesPerPeriod*2 != numAudioFramesPerPeriod) { 40 if(numMatrixFramesPerPeriod == 0 || numMatrixFramesPerPeriod > numAudioFramesPerPeriod) {
41 rt_printf("Error: this example needs the matrix enabled, running at half audio rate\n"); 41 rt_printf("Error: this example needs the matrix enabled, with 4 or 8 channels\n");
42 return false; 42 return false;
43 } 43 }
44 44
45 gNumChannels = numAudioChannels; 45 gMatrixFramesPerAudioFrame = numAudioFramesPerPeriod / numMatrixFramesPerPeriod;
46 gInverseSampleRate = 1.0 / audioSampleRate; 46 gInverseSampleRate = 1.0 / audioSampleRate;
47 gPhase = 0.0; 47 gPhase = 0.0;
48 48
49 return true; 49 return true;
50 } 50 }
62 62
63 // There are twice as many audio frames as matrix frames since audio sample rate 63 // There are twice as many audio frames as matrix frames since audio sample rate
64 // is twice as high 64 // is twice as high
65 65
66 for(int n = 0; n < numAudioFrames; n++) { 66 for(int n = 0; n < numAudioFrames; n++) {
67 if(!(n % 2)) { 67 if(!(n % gMatrixFramesPerAudioFrame)) {
68 // Even audio samples: update frequency and amplitude from the matrix 68 // Even audio samples: update frequency and amplitude from the matrix
69 frequency = map((float)analogRead(gSensorInputFrequency, n/2), 0, MATRIX_MAX, 100, 1000); 69 frequency = map(analogRead(gSensorInputFrequency, n/gMatrixFramesPerAudioFrame), 0, MATRIX_MAX, 100, 1000);
70 amplitude = (float)analogRead(gSensorInputAmplitude, n/2) / MATRIX_MAX; 70 amplitude = (float)analogRead(gSensorInputAmplitude, n/gMatrixFramesPerAudioFrame) / MATRIX_MAX;
71 } 71 }
72 72
73 float out = amplitude * sinf(gPhase); 73 float out = amplitude * sinf(gPhase);
74 74
75 for(int channel = 0; channel < gNumChannels; channel++) 75 for(int channel = 0; channel < gNumAudioChannels; channel++)
76 audioOut[n * gNumChannels + channel] = out; 76 audioOut[n * gNumAudioChannels + channel] = out;
77 77
78 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; 78 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
79 if(gPhase > 2.0 * M_PI) 79 if(gPhase > 2.0 * M_PI)
80 gPhase -= 2.0 * M_PI; 80 gPhase -= 2.0 * M_PI;
81 } 81 }