comparison projects/basic_analog_output/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 // Set range for analog outputs designed for driving LEDs
15 const float kMinimumAmplitude = (1.5 / 5.0) * MATRIX_MAX;
16 const float kAmplitudeRange = MATRIX_MAX - kMinimumAmplitude;
17
18 float gFrequency;
19 float gPhase;
20 float gInverseSampleRate;
21
22 // initialise_render() is called once before the audio rendering starts.
23 // Use it to perform any initialisation and allocation which is dependent
24 // on the period size or sample rate.
25 //
26 // userData holds an opaque pointer to a data structure that was passed
27 // in from the call to initAudio().
28 //
29 // Return true on success; returning false halts the program.
30
31 bool initialise_render(int numChannels, int numMatrixFramesPerPeriod,
32 int numAudioFramesPerPeriod, float matrixSampleRate,
33 float audioSampleRate, void *userData)
34 {
35 // Retrieve a parameter passed in from the initAudio() call
36 gFrequency = *(float *)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 gInverseSampleRate = 1.0 / audioSampleRate;
44 gPhase = 0.0;
45
46 return true;
47 }
48
49 // render() is called regularly at the highest priority by the audio engine.
50 // Input and output are given from the audio hardware and the other
51 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
52 // will be 0.
53
54 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
55 uint16_t *matrixIn, uint16_t *matrixOut)
56 {
57 for(int n = 0; n < numMatrixFrames; n++) {
58 // Set LED to different phase for each matrix channel
59 float relativePhase = 0.0;
60 for(int channel = 0; channel < 8; channel++) {
61 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
62 if(out > MATRIX_MAX)
63 out = MATRIX_MAX;
64
65 matrixOut[n * 8 + channel] = (uint16_t)out;
66
67 // Advance by pi/4 (1/8 of a full rotation) for each channel
68 relativePhase += M_PI * 0.25;
69 }
70
71 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
72 if(gPhase > 2.0 * M_PI)
73 gPhase -= 2.0 * M_PI;
74 }
75 }
76
77 // cleanup_render() is called once at the end, after the audio has stopped.
78 // Release any resources that were allocated in initialise_render().
79
80 void cleanup_render()
81 {
82
83 }