comparison examples/basic_analog_input/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_input/render.cpp@72726dd4f66c
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 float gPhase;
15 float gInverseSampleRate;
16 int gAudioFramesPerAnalogFrame;
17
18 // These settings are carried over from main.cpp
19 // Setting global variables is an alternative approach
20 // to passing a structure to userData in setup()
21
22 extern int gSensorInputFrequency;
23 extern int gSensorInputAmplitude;
24
25 // setup() is called once before the audio rendering starts.
26 // Use it to perform any initialisation and allocation which is dependent
27 // on the period size or sample rate.
28 //
29 // userData holds an opaque pointer to a data structure that was passed
30 // in from the call to initAudio().
31 //
32 // Return true on success; returning false halts the program.
33
34 bool setup(BeagleRTContext *context, void *userData)
35 {
36 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
37 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
38 return false;
39 }
40
41 gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
42 gInverseSampleRate = 1.0 / context->audioSampleRate;
43 gPhase = 0.0;
44
45 return true;
46 }
47
48 // render() is called regularly at the highest priority by the audio engine.
49 // Input and output are given from the audio hardware and the other
50 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
51 // will be 0.
52
53 void render(BeagleRTContext *context, void *userData)
54 {
55 float frequency = 440.0;
56 float amplitude = 0.8;
57
58 // There are twice as many audio frames as matrix frames since audio sample rate
59 // is twice as high
60
61 for(unsigned int n = 0; n < context->audioFrames; n++) {
62 if(!(n % gAudioFramesPerAnalogFrame)) {
63 // Even audio samples: update frequency and amplitude from the matrix
64 frequency = map(analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputFrequency), 0, 1, 100, 1000);
65 amplitude = analogReadFrame(context, n/gAudioFramesPerAnalogFrame, gSensorInputAmplitude);
66 }
67
68 float out = amplitude * sinf(gPhase);
69
70 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
71 context->audioOut[n * context->audioChannels + channel] = out;
72
73 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
74 if(gPhase > 2.0 * M_PI)
75 gPhase -= 2.0 * M_PI;
76 }
77 }
78
79 // cleanup() is called once at the end, after the audio has stopped.
80 // Release any resources that were allocated in setup().
81
82 void cleanup(BeagleRTContext *context, void *userData)
83 {
84
85 }