Mercurial > hg > beaglert
comparison projects/basic_analog_output/render.cpp @ 108:3068421c0737 ultra-staging
Merged default into ultra-staging
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 18 Aug 2015 00:35:15 +0100 |
parents | 3c3a1357657d |
children | ac8eb07afcf5 |
comparison
equal
deleted
inserted
replaced
54:d3f869b98147 | 108:3068421c0737 |
---|---|
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 <BeagleRT.h> |
10 #include "../../include/Utilities.h" | 10 #include <Utilities.h> |
11 #include <rtdk.h> | 11 #include <rtdk.h> |
12 #include <cmath> | 12 #include <cmath> |
13 | 13 |
14 // Set range for analog outputs designed for driving LEDs | 14 // Set range for analog outputs designed for driving LEDs |
15 const float kMinimumAmplitude = (1.5 / 5.0) * MATRIX_MAX; | 15 const float kMinimumAmplitude = (1.5 / 5.0); |
16 const float kAmplitudeRange = MATRIX_MAX - kMinimumAmplitude; | 16 const float kAmplitudeRange = 1.0 - kMinimumAmplitude; |
17 | 17 |
18 float gFrequency; | 18 float gFrequency; |
19 float gPhase; | 19 float gPhase; |
20 float gInverseSampleRate; | 20 float gInverseSampleRate; |
21 | 21 |
22 // initialise_render() is called once before the audio rendering starts. | 22 // setup() is called once before the audio rendering starts. |
23 // Use it to perform any initialisation and allocation which is dependent | 23 // Use it to perform any initialisation and allocation which is dependent |
24 // on the period size or sample rate. | 24 // on the period size or sample rate. |
25 // | 25 // |
26 // userData holds an opaque pointer to a data structure that was passed | 26 // userData holds an opaque pointer to a data structure that was passed |
27 // in from the call to initAudio(). | 27 // in from the call to initAudio(). |
28 // | 28 // |
29 // Return true on success; returning false halts the program. | 29 // Return true on success; returning false halts the program. |
30 | 30 |
31 bool initialise_render(int numMatrixChannels, int numAudioChannels, | 31 bool setup(BeagleRTContext *context, void *userData) |
32 int numMatrixFramesPerPeriod, | |
33 int numAudioFramesPerPeriod, | |
34 float matrixSampleRate, float audioSampleRate, | |
35 void *userData) | |
36 { | 32 { |
37 // Retrieve a parameter passed in from the initAudio() call | 33 // Retrieve a parameter passed in from the initAudio() call |
38 gFrequency = *(float *)userData; | 34 gFrequency = *(float *)userData; |
39 | 35 |
40 if(numMatrixFramesPerPeriod == 0) { | 36 if(context->analogFrames == 0) { |
41 rt_printf("Error: this example needs the matrix enabled\n"); | 37 rt_printf("Error: this example needs the matrix enabled\n"); |
42 return false; | 38 return false; |
43 } | 39 } |
44 | 40 |
45 gInverseSampleRate = 1.0 / matrixSampleRate; | 41 gInverseSampleRate = 1.0 / context->analogSampleRate; |
46 gPhase = 0.0; | 42 gPhase = 0.0; |
47 | 43 |
48 return true; | 44 return true; |
49 } | 45 } |
50 | 46 |
51 // render() is called regularly at the highest priority by the audio engine. | 47 // render() is called regularly at the highest priority by the audio engine. |
52 // Input and output are given from the audio hardware and the other | 48 // Input and output are given from the audio hardware and the other |
53 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 49 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
54 // will be 0. | 50 // will be 0. |
55 | 51 |
56 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | 52 void render(BeagleRTContext *context, void *userData) |
57 uint16_t *matrixIn, uint16_t *matrixOut) | |
58 { | 53 { |
59 for(int n = 0; n < numMatrixFrames; n++) { | 54 for(unsigned int n = 0; n < context->analogFrames; n++) { |
60 // Set LED to different phase for each matrix channel | 55 // Set LED to different phase for each matrix channel |
61 float relativePhase = 0.0; | 56 float relativePhase = 0.0; |
62 for(int channel = 0; channel < gNumMatrixChannels; channel++) { | 57 for(unsigned int channel = 0; channel < context->analogChannels; channel++) { |
63 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase)); | 58 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase)); |
64 if(out > MATRIX_MAX) | |
65 out = MATRIX_MAX; | |
66 | 59 |
67 analogWrite(channel, n, out); | 60 analogWriteFrame(context, n, channel, out); |
68 | 61 |
69 // Advance by pi/4 (1/8 of a full rotation) for each channel | 62 // Advance by pi/4 (1/8 of a full rotation) for each channel |
70 relativePhase += M_PI * 0.25; | 63 relativePhase += M_PI * 0.25; |
71 } | 64 } |
72 | 65 |
74 if(gPhase > 2.0 * M_PI) | 67 if(gPhase > 2.0 * M_PI) |
75 gPhase -= 2.0 * M_PI; | 68 gPhase -= 2.0 * M_PI; |
76 } | 69 } |
77 } | 70 } |
78 | 71 |
79 // cleanup_render() is called once at the end, after the audio has stopped. | 72 // cleanup() is called once at the end, after the audio has stopped. |
80 // Release any resources that were allocated in initialise_render(). | 73 // Release any resources that were allocated in setup(). |
81 | 74 |
82 void cleanup_render() | 75 void cleanup(BeagleRTContext *context, void *userData) |
83 { | 76 { |
84 | 77 |
85 } | 78 } |