Mercurial > hg > beaglert
comparison projects/basic_sensor/render.cpp @ 0:8a575ba3ab52
Initial commit.
author | andrewm |
---|---|
date | Fri, 31 Oct 2014 19:10:17 +0100 |
parents | |
children | 09f03ac40fcc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a575ba3ab52 |
---|---|
1 /* | |
2 * render.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 | |
9 #include "../../include/render.h" | |
10 #include "../../include/Utilities.h" | |
11 #include <rtdk.h> | |
12 #include <cmath> | |
13 | |
14 float gPhase; | |
15 float gInverseSampleRate; | |
16 int gNumChannels; | |
17 | |
18 // These settings are carried over from main.cpp | |
19 // Setting global variables is an alternative approach | |
20 // to passing a structure to userData in initialise_render() | |
21 | |
22 extern int gSensorInputFrequency; | |
23 extern int gSensorInputAmplitude; | |
24 | |
25 // initialise_render() is called once before the audio rendering starts. | |
26 // Use it to perform any initialisation and allocation which is dependent | |
27 // on the period size or sample rate. | |
28 // | |
29 // userData holds an opaque pointer to a data structure that was passed | |
30 // in from the call to initAudio(). | |
31 // | |
32 // Return true on success; returning false halts the program. | |
33 | |
34 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod, | |
35 int numAudioFramesPerPeriod, float matrixSampleRate, | |
36 float audioSampleRate, void *userData) | |
37 { | |
38 if(numMatrixFramesPerPeriod*2 != numAudioFramesPerPeriod) { | |
39 rt_printf("Error: this example needs the matrix enabled, running at half audio rate\n"); | |
40 return false; | |
41 } | |
42 | |
43 gNumChannels = numChannels; | |
44 gInverseSampleRate = 1.0 / audioSampleRate; | |
45 gPhase = 0.0; | |
46 | |
47 return true; | |
48 } | |
49 | |
50 // render() is called regularly at the highest priority by the audio engine. | |
51 // Input and output are given from the audio hardware and the other | |
52 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
53 // will be 0. | |
54 | |
55 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | |
56 uint16_t *matrixIn, uint16_t *matrixOut) | |
57 { | |
58 float frequency = 0; | |
59 float amplitude = 0; | |
60 | |
61 // There are twice as many audio frames as matrix frames since audio sample rate | |
62 // is twice as high | |
63 | |
64 for(int n = 0; n < numAudioFrames; n++) { | |
65 if(!(n % 2)) { | |
66 // Even audio samples: update frequency and amplitude from the matrix | |
67 frequency = map((float)matrixIn[gSensorInputFrequency], 0, MATRIX_MAX, 100, 1000); | |
68 amplitude = (float)matrixIn[gSensorInputAmplitude] / MATRIX_MAX; | |
69 } | |
70 | |
71 float out = amplitude * sinf(gPhase); | |
72 | |
73 for(int channel = 0; channel < gNumChannels; channel++) | |
74 audioOut[n * gNumChannels + channel] = out; | |
75 | |
76 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; | |
77 if(gPhase > 2.0 * M_PI) | |
78 gPhase -= 2.0 * M_PI; | |
79 } | |
80 } | |
81 | |
82 // cleanup_render() is called once at the end, after the audio has stopped. | |
83 // Release any resources that were allocated in initialise_render(). | |
84 | |
85 void cleanup_render() | |
86 { | |
87 | |
88 } |