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@56
|
9 #include <BeagleRT.h>
|
andrewm@0
|
10 #include <cmath>
|
giuliomoro@180
|
11 #include <Utilities.h>
|
andrewm@0
|
12
|
andrewm@56
|
13 float gFrequency = 440.0;
|
andrewm@0
|
14 float gPhase;
|
andrewm@0
|
15 float gInverseSampleRate;
|
andrewm@0
|
16
|
andrewm@56
|
17 // setup() 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@56
|
26 bool setup(BeagleRTContext *context, void *userData)
|
andrewm@0
|
27 {
|
andrewm@0
|
28 // Retrieve a parameter passed in from the initAudio() call
|
andrewm@56
|
29 if(userData != 0)
|
andrewm@56
|
30 gFrequency = *(float *)userData;
|
andrewm@0
|
31
|
andrewm@45
|
32 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
andrewm@0
|
33 gPhase = 0.0;
|
andrewm@0
|
34
|
andrewm@0
|
35 return true;
|
andrewm@0
|
36 }
|
andrewm@0
|
37
|
andrewm@0
|
38 // render() is called regularly at the highest priority by the audio engine.
|
andrewm@0
|
39 // Input and output are given from the audio hardware and the other
|
andrewm@0
|
40 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
andrewm@0
|
41 // will be 0.
|
andrewm@0
|
42
|
andrewm@45
|
43 void render(BeagleRTContext *context, void *userData)
|
andrewm@0
|
44 {
|
andrewm@45
|
45 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
andrewm@0
|
46 float out = 0.8f * sinf(gPhase);
|
andrewm@0
|
47 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
andrewm@0
|
48 if(gPhase > 2.0 * M_PI)
|
andrewm@0
|
49 gPhase -= 2.0 * M_PI;
|
andrewm@0
|
50
|
giuliomoro@180
|
51 for(unsigned int channel = 0; channel < context->audioChannels; channel++) {
|
giuliomoro@180
|
52 // Two equivalent ways to write this code
|
giuliomoro@180
|
53
|
giuliomoro@180
|
54 // The long way, using the buffers directly:
|
giuliomoro@180
|
55 // context->audioOut[n * context->audioChannels + channel] = out;
|
giuliomoro@180
|
56
|
giuliomoro@180
|
57 // Or using the macros:
|
giuliomoro@180
|
58 audioWriteFrame(context, n, channel, out);
|
giuliomoro@180
|
59 }
|
andrewm@0
|
60 }
|
andrewm@0
|
61 }
|
andrewm@0
|
62
|
andrewm@56
|
63 // cleanup() is called once at the end, after the audio has stopped.
|
andrewm@56
|
64 // Release any resources that were allocated in setup().
|
andrewm@0
|
65
|
andrewm@56
|
66 void cleanup(BeagleRTContext *context, void *userData)
|
andrewm@0
|
67 {
|
andrewm@0
|
68
|
andrewm@0
|
69 }
|