comparison examples/basic_analog_output/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_analog_output/render.cpp@3c3a1357657d
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 <Utilities.h>
11 #include <rtdk.h>
12 #include <cmath>
13
14 // Set range for analog outputs designed for driving LEDs
15 const float kMinimumAmplitude = (1.5 / 5.0);
16 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
17
18 float gFrequency;
19 float gPhase;
20 float gInverseSampleRate;
21
22 // setup() is called once before the audio rendering starts.
23 // Use it to perform any initialisation and allocation which is dependent
24 // on the period size or sample rate.
25 //
26 // userData holds an opaque pointer to a data structure that was passed
27 // in from the call to initAudio().
28 //
29 // Return true on success; returning false halts the program.
30
31 bool setup(BeagleRTContext *context, void *userData)
32 {
33 // Retrieve a parameter passed in from the initAudio() call
34 gFrequency = *(float *)userData;
35
36 if(context->analogFrames == 0) {
37 rt_printf("Error: this example needs the matrix enabled\n");
38 return false;
39 }
40
41 gInverseSampleRate = 1.0 / context->analogSampleRate;
42 gPhase = 0.0;
43
44 return true;
45 }
46
47 // render() is called regularly at the highest priority by the audio engine.
48 // Input and output are given from the audio hardware and the other
49 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
50 // will be 0.
51
52 void render(BeagleRTContext *context, void *userData)
53 {
54 for(unsigned int n = 0; n < context->analogFrames; n++) {
55 // Set LED to different phase for each matrix channel
56 float relativePhase = 0.0;
57 for(unsigned int channel = 0; channel < context->analogChannels; channel++) {
58 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
59
60 analogWriteFrame(context, n, channel, out);
61
62 // Advance by pi/4 (1/8 of a full rotation) for each channel
63 relativePhase += M_PI * 0.25;
64 }
65
66 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
67 if(gPhase > 2.0 * M_PI)
68 gPhase -= 2.0 * M_PI;
69 }
70 }
71
72 // cleanup() is called once at the end, after the audio has stopped.
73 // Release any resources that were allocated in setup().
74
75 void cleanup(BeagleRTContext *context, void *userData)
76 {
77
78 }