andrewm@0
|
1 /*
|
andrewm@0
|
2 * render.cpp
|
andrewm@0
|
3 *
|
andrewm@0
|
4 * Created on: Oct 24, 2014
|
andrewm@0
|
5 * Author: parallels
|
andrewm@0
|
6 */
|
andrewm@0
|
7
|
andrewm@0
|
8
|
andrewm@0
|
9 #include "../../include/render.h"
|
andrewm@0
|
10 #include "../../include/Utilities.h"
|
andrewm@0
|
11 #include <rtdk.h>
|
andrewm@0
|
12 #include <cmath>
|
andrewm@0
|
13
|
andrewm@0
|
14 // Set range for analog outputs designed for driving LEDs
|
andrewm@0
|
15 const float kMinimumAmplitude = (1.5 / 5.0) * MATRIX_MAX;
|
andrewm@0
|
16 const float kAmplitudeRange = MATRIX_MAX - kMinimumAmplitude;
|
andrewm@0
|
17
|
andrewm@0
|
18 float gFrequency;
|
andrewm@0
|
19 float gPhase;
|
andrewm@0
|
20 float gInverseSampleRate;
|
andrewm@0
|
21
|
andrewm@12
|
22 int gMatrixChannels;
|
andrewm@12
|
23
|
andrewm@0
|
24 // initialise_render() is called once before the audio rendering starts.
|
andrewm@0
|
25 // Use it to perform any initialisation and allocation which is dependent
|
andrewm@0
|
26 // on the period size or sample rate.
|
andrewm@0
|
27 //
|
andrewm@0
|
28 // userData holds an opaque pointer to a data structure that was passed
|
andrewm@0
|
29 // in from the call to initAudio().
|
andrewm@0
|
30 //
|
andrewm@0
|
31 // Return true on success; returning false halts the program.
|
andrewm@0
|
32
|
andrewm@12
|
33 bool initialise_render(int numMatrixChannels, int numAudioChannels,
|
andrewm@12
|
34 int numMatrixFramesPerPeriod,
|
andrewm@12
|
35 int numAudioFramesPerPeriod,
|
andrewm@12
|
36 float matrixSampleRate, float audioSampleRate,
|
andrewm@12
|
37 void *userData)
|
andrewm@0
|
38 {
|
andrewm@0
|
39 // Retrieve a parameter passed in from the initAudio() call
|
andrewm@0
|
40 gFrequency = *(float *)userData;
|
andrewm@0
|
41
|
andrewm@12
|
42 if(numMatrixFramesPerPeriod == 0) {
|
andrewm@12
|
43 rt_printf("Error: this example needs the matrix enabled\n");
|
andrewm@0
|
44 return false;
|
andrewm@0
|
45 }
|
andrewm@0
|
46
|
andrewm@12
|
47 gMatrixChannels = numMatrixChannels;
|
andrewm@12
|
48 gInverseSampleRate = 1.0 / matrixSampleRate;
|
andrewm@0
|
49 gPhase = 0.0;
|
andrewm@0
|
50
|
andrewm@0
|
51 return true;
|
andrewm@0
|
52 }
|
andrewm@0
|
53
|
andrewm@0
|
54 // render() is called regularly at the highest priority by the audio engine.
|
andrewm@0
|
55 // Input and output are given from the audio hardware and the other
|
andrewm@0
|
56 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
andrewm@0
|
57 // will be 0.
|
andrewm@0
|
58
|
andrewm@0
|
59 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
|
andrewm@0
|
60 uint16_t *matrixIn, uint16_t *matrixOut)
|
andrewm@0
|
61 {
|
andrewm@0
|
62 for(int n = 0; n < numMatrixFrames; n++) {
|
andrewm@0
|
63 // Set LED to different phase for each matrix channel
|
andrewm@0
|
64 float relativePhase = 0.0;
|
andrewm@12
|
65 for(int channel = 0; channel < gMatrixChannels; channel++) {
|
andrewm@0
|
66 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
|
andrewm@0
|
67 if(out > MATRIX_MAX)
|
andrewm@0
|
68 out = MATRIX_MAX;
|
andrewm@0
|
69
|
andrewm@12
|
70 matrixOut[n * gMatrixChannels + channel] = (uint16_t)out;
|
andrewm@12
|
71 //analogWrite(channel, n, out);
|
andrewm@0
|
72
|
andrewm@0
|
73 // Advance by pi/4 (1/8 of a full rotation) for each channel
|
andrewm@0
|
74 relativePhase += M_PI * 0.25;
|
andrewm@0
|
75 }
|
andrewm@0
|
76
|
andrewm@0
|
77 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
andrewm@0
|
78 if(gPhase > 2.0 * M_PI)
|
andrewm@0
|
79 gPhase -= 2.0 * M_PI;
|
andrewm@0
|
80 }
|
andrewm@0
|
81 }
|
andrewm@0
|
82
|
andrewm@0
|
83 // cleanup_render() is called once at the end, after the audio has stopped.
|
andrewm@0
|
84 // Release any resources that were allocated in initialise_render().
|
andrewm@0
|
85
|
andrewm@0
|
86 void cleanup_render()
|
andrewm@0
|
87 {
|
andrewm@0
|
88
|
andrewm@0
|
89 }
|