comparison projects/basic_analog_output/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 3c3a1357657d
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 // 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
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 initialise_render(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(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(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
77 } 70 }
78 71
79 // cleanup_render() is called once at the end, after the audio has stopped. 72 // cleanup_render() 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 initialise_render().
81 74
82 void cleanup_render() 75 void cleanup_render(BeagleRTContext *context, void *userData)
83 { 76 {
84 77
85 } 78 }