Mercurial > hg > beaglert
comparison projects/basic_sensor/render.cpp @ 52:a6d223473ea2 newapi
Updated examples for new API. tank_wars not yet updated; audio_in_FFT and oscillator_bank not working properly yet.
author | andrewm |
---|---|
date | Sun, 31 May 2015 02:13:39 -0500 |
parents | 6adb088196a7 |
children |
comparison
equal
deleted
inserted
replaced
51:4f8db16f17b5 | 52:a6d223473ea2 |
---|---|
4 * Created on: Oct 24, 2014 | 4 * Created on: Oct 24, 2014 |
5 * Author: parallels | 5 * Author: parallels |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #include "../../include/render.h" | 9 #include "../../include/BeagleRT.h" |
10 #include "../../include/Utilities.h" | 10 #include "../../include/Utilities.h" |
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 gMatrixFramesPerAudioFrame; | 16 int gAudioFramesPerAnalogFrame; |
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 |
29 // userData holds an opaque pointer to a data structure that was passed | 29 // userData holds an opaque pointer to a data structure that was passed |
30 // in from the call to initAudio(). | 30 // in from the call to initAudio(). |
31 // | 31 // |
32 // Return true on success; returning false halts the program. | 32 // Return true on success; returning false halts the program. |
33 | 33 |
34 bool initialise_render(int numMatrixChannels, int numAudioChannels, | 34 bool initialise_render(BeagleRTContext *context, void *userData) |
35 int numMatrixFramesPerPeriod, | |
36 int numAudioFramesPerPeriod, | |
37 float matrixSampleRate, float audioSampleRate, | |
38 void *userData) | |
39 { | 35 { |
40 if(numMatrixFramesPerPeriod == 0 || numMatrixFramesPerPeriod > numAudioFramesPerPeriod) { | 36 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) { |
41 rt_printf("Error: this example needs the matrix enabled, with 4 or 8 channels\n"); | 37 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n"); |
42 return false; | 38 return false; |
43 } | 39 } |
44 | 40 |
45 gMatrixFramesPerAudioFrame = numAudioFramesPerPeriod / numMatrixFramesPerPeriod; | 41 gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames; |
46 gInverseSampleRate = 1.0 / audioSampleRate; | 42 gInverseSampleRate = 1.0 / context->audioSampleRate; |
47 gPhase = 0.0; | 43 gPhase = 0.0; |
48 | 44 |
49 return true; | 45 return true; |
50 } | 46 } |
51 | 47 |
52 // render() is called regularly at the highest priority by the audio engine. | 48 // render() is called regularly at the highest priority by the audio engine. |
53 // Input and output are given from the audio hardware and the other | 49 // Input and output are given from the audio hardware and the other |
54 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 50 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
55 // will be 0. | 51 // will be 0. |
56 | 52 |
57 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | 53 void render(BeagleRTContext *context, void *userData) |
58 uint16_t *matrixIn, uint16_t *matrixOut) | |
59 { | 54 { |
60 float frequency = 0; | 55 float frequency = 440.0; |
61 float amplitude = 0; | 56 float amplitude = 0.8; |
62 | 57 |
63 // There are twice as many audio frames as matrix frames since audio sample rate | 58 // There are twice as many audio frames as matrix frames since audio sample rate |
64 // is twice as high | 59 // is twice as high |
65 | 60 |
66 for(int n = 0; n < numAudioFrames; n++) { | 61 for(unsigned int n = 0; n < context->audioFrames; n++) { |
67 if(!(n % gMatrixFramesPerAudioFrame)) { | 62 if(!(n % gAudioFramesPerAnalogFrame)) { |
68 // Even audio samples: update frequency and amplitude from the matrix | 63 // Even audio samples: update frequency and amplitude from the matrix |
69 frequency = map(analogRead(gSensorInputFrequency, n/gMatrixFramesPerAudioFrame), 0, MATRIX_MAX, 100, 1000); | 64 frequency = map(analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000); |
70 amplitude = (float)analogRead(gSensorInputAmplitude, n/gMatrixFramesPerAudioFrame) / MATRIX_MAX; | 65 amplitude = analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude); |
71 } | 66 } |
72 | 67 |
73 float out = amplitude * sinf(gPhase); | 68 float out = amplitude * sinf(gPhase); |
74 | 69 |
75 for(int channel = 0; channel < gNumAudioChannels; channel++) | 70 for(unsigned int channel = 0; channel < context->audioChannels; channel++) |
76 audioOut[n * gNumAudioChannels + channel] = out; | 71 context->audioOut[n * context->audioChannels + channel] = out; |
77 | 72 |
78 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; | 73 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; |
79 if(gPhase > 2.0 * M_PI) | 74 if(gPhase > 2.0 * M_PI) |
80 gPhase -= 2.0 * M_PI; | 75 gPhase -= 2.0 * M_PI; |
81 } | 76 } |
82 } | 77 } |
83 | 78 |
84 // cleanup_render() is called once at the end, after the audio has stopped. | 79 // cleanup_render() is called once at the end, after the audio has stopped. |
85 // Release any resources that were allocated in initialise_render(). | 80 // Release any resources that were allocated in initialise_render(). |
86 | 81 |
87 void cleanup_render() | 82 void cleanup_render(BeagleRTContext *context, void *userData) |
88 { | 83 { |
89 | 84 |
90 } | 85 } |