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 <cmath>
|
andrewm@0
|
11
|
andrewm@0
|
12 float gFrequency;
|
andrewm@0
|
13 float gPhase;
|
andrewm@0
|
14 float gInverseSampleRate;
|
andrewm@0
|
15 int gNumChannels;
|
andrewm@0
|
16
|
andrewm@0
|
17 // initialise_render() is called once before the audio rendering starts.
|
andrewm@0
|
18 // Use it to perform any initialisation and allocation which is dependent
|
andrewm@0
|
19 // on the period size or sample rate.
|
andrewm@0
|
20 //
|
andrewm@0
|
21 // userData holds an opaque pointer to a data structure that was passed
|
andrewm@0
|
22 // in from the call to initAudio().
|
andrewm@0
|
23 //
|
andrewm@0
|
24 // Return true on success; returning false halts the program.
|
andrewm@0
|
25
|
andrewm@12
|
26 bool initialise_render(int numMatrixChannels, int numAudioChannels,
|
andrewm@12
|
27 int numMatrixFramesPerPeriod,
|
andrewm@12
|
28 int numAudioFramesPerPeriod,
|
andrewm@12
|
29 float matrixSampleRate, float audioSampleRate,
|
andrewm@12
|
30 void *userData)
|
andrewm@0
|
31 {
|
andrewm@0
|
32 // Retrieve a parameter passed in from the initAudio() call
|
andrewm@0
|
33 gFrequency = *(float *)userData;
|
andrewm@0
|
34
|
andrewm@12
|
35 gNumChannels = numAudioChannels;
|
andrewm@0
|
36 gInverseSampleRate = 1.0 / audioSampleRate;
|
andrewm@0
|
37 gPhase = 0.0;
|
andrewm@0
|
38
|
andrewm@0
|
39 return true;
|
andrewm@0
|
40 }
|
andrewm@0
|
41
|
andrewm@0
|
42 // render() is called regularly at the highest priority by the audio engine.
|
andrewm@0
|
43 // Input and output are given from the audio hardware and the other
|
andrewm@0
|
44 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
andrewm@0
|
45 // will be 0.
|
andrewm@0
|
46
|
andrewm@0
|
47 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
|
andrewm@0
|
48 uint16_t *matrixIn, uint16_t *matrixOut)
|
andrewm@0
|
49 {
|
andrewm@0
|
50 for(int n = 0; n < numAudioFrames; n++) {
|
andrewm@0
|
51 float out = 0.8f * sinf(gPhase);
|
andrewm@0
|
52 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
andrewm@0
|
53 if(gPhase > 2.0 * M_PI)
|
andrewm@0
|
54 gPhase -= 2.0 * M_PI;
|
andrewm@0
|
55
|
andrewm@0
|
56 for(int channel = 0; channel < gNumChannels; channel++)
|
andrewm@0
|
57 audioOut[n * gNumChannels + channel] = out;
|
andrewm@0
|
58 }
|
andrewm@0
|
59 }
|
andrewm@0
|
60
|
andrewm@0
|
61 // cleanup_render() is called once at the end, after the audio has stopped.
|
andrewm@0
|
62 // Release any resources that were allocated in initialise_render().
|
andrewm@0
|
63
|
andrewm@0
|
64 void cleanup_render()
|
andrewm@0
|
65 {
|
andrewm@0
|
66
|
andrewm@0
|
67 }
|