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