Mercurial > hg > beaglert
comparison examples/basic/render.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/basic/render.cpp@07cfd337ad18 |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
1 /* | |
2 * render.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 | |
9 #include <BeagleRT.h> | |
10 #include <cmath> | |
11 #include <Utilities.h> | |
12 | |
13 float gFrequency = 440.0; | |
14 float gPhase; | |
15 float gInverseSampleRate; | |
16 | |
17 // setup() 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 setup(BeagleRTContext *context, void *userData) | |
27 { | |
28 // Retrieve a parameter passed in from the initAudio() call | |
29 if(userData != 0) | |
30 gFrequency = *(float *)userData; | |
31 | |
32 gInverseSampleRate = 1.0 / context->audioSampleRate; | |
33 gPhase = 0.0; | |
34 | |
35 return true; | |
36 } | |
37 | |
38 // render() is called regularly at the highest priority by the audio engine. | |
39 // Input and output are given from the audio hardware and the other | |
40 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
41 // will be 0. | |
42 | |
43 void render(BeagleRTContext *context, void *userData) | |
44 { | |
45 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
46 float out = 0.8f * sinf(gPhase); | |
47 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | |
48 if(gPhase > 2.0 * M_PI) | |
49 gPhase -= 2.0 * M_PI; | |
50 | |
51 for(unsigned int channel = 0; channel < context->audioChannels; channel++) { | |
52 // Two equivalent ways to write this code | |
53 | |
54 // The long way, using the buffers directly: | |
55 // context->audioOut[n * context->audioChannels + channel] = out; | |
56 | |
57 // Or using the macros: | |
58 audioWriteFrame(context, n, channel, out); | |
59 } | |
60 } | |
61 } | |
62 | |
63 // cleanup() is called once at the end, after the audio has stopped. | |
64 // Release any resources that were allocated in setup(). | |
65 | |
66 void cleanup(BeagleRTContext *context, void *userData) | |
67 { | |
68 | |
69 } |