Mercurial > hg > beaglert
comparison projects/analogDigitalDemo/render.cpp @ 67:472e892c6e41
Merge newapi into default
author | Andrew McPherson <a.mcpherson@qmul.ac.uk> |
---|---|
date | Fri, 17 Jul 2015 15:28:18 +0100 |
parents | 3c3a1357657d |
children | 567bd8f76714 |
comparison
equal
deleted
inserted
replaced
21:0d80ff9e2227 | 67:472e892c6e41 |
---|---|
1 /* | |
2 * | |
3 * Andrew McPherson and Victor Zappi | |
4 * Queen Mary, University of London | |
5 */ | |
6 | |
7 #include <BeagleRT.h> | |
8 #include <Utilities.h> | |
9 #include <cmath> | |
10 #include <rtdk.h> | |
11 | |
12 // setup() is called once before the audio rendering starts. | |
13 // Use it to perform any initialisation and allocation which is dependent | |
14 // on the period size or sample rate. | |
15 // | |
16 // userData holds an opaque pointer to a data structure that was passed | |
17 // in from the call to initAudio(). | |
18 // | |
19 // Return true on success; returning false halts the program. | |
20 | |
21 bool setup(BeagleRTContext *context, void *userData) | |
22 { | |
23 return true; | |
24 } | |
25 | |
26 // render() is called regularly at the highest priority by the audio engine. | |
27 // Input and output are given from the audio hardware and the other | |
28 // ADCs and DACs (if available). If only audio is available, numAnalogFrames | |
29 // will be 0. | |
30 | |
31 void render(BeagleRTContext *context, void *userData) | |
32 /* | |
33 we assume that gNumAnalogChannels=8, numAnalogFrames==8 and numDigitalFrames==numAudioFrames | |
34 * */ | |
35 { | |
36 if((context->audioSampleCount&31)==0){ //every 32 frames... | |
37 //ANALOG channels | |
38 analogWriteFrame(context, 0, 0, analogReadFrame(context, 0,0)); | |
39 // read the input0 at frame0 and write it to output0 frame0. Using analogWrite will fill the rest of the buffer with the same value | |
40 // The value at the last frame will persist through the successive buffers until is set again. | |
41 // This effectively is a pass-through with downsampling by 32 times | |
42 analogWriteFrame(context, 0, 3, 1.0); // write 1.0 to channel3 from frame0 to the end of the buffer | |
43 analogWriteFrame(context, 4, 3, 0.1); // write 0.1 to channel3 from frame4 to the end of the buffer | |
44 analogWriteFrameOnce(context, 6, 3, 0.2); //write 0.2 to channel3 only on frame 6 | |
45 //this buffer for channel 3 will look like this: 1 1 1 1 0.1 0.1 0.2 0.1 | |
46 //the next buffers for channel 3 will be filled up with 0.1 .... | |
47 //DIGITAL channels | |
48 digitalWriteFrame(context, 0, P8_07, GPIO_HIGH); //sets all the frames to HIGH for channel 0 | |
49 digitalWriteFrameOnce(context, 4, P8_07, GPIO_LOW); //only frame 4 will be LOW for channel 0 | |
50 // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1 | |
51 // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer) | |
52 digitalWriteFrame(context, 0, P8_08, GPIO_HIGH); | |
53 digitalWriteFrame(context, 2, P8_08, GPIO_LOW); | |
54 digitalWriteFrame(context, 4, P8_08, GPIO_HIGH); | |
55 digitalWriteFrame(context, 5, P8_08, GPIO_LOW); | |
56 pinModeFrame(context, 0, P9_16, GPIO_INPUT); // set channel 10 to input | |
57 // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0 | |
58 // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer) | |
59 } | |
60 for(unsigned int n=0; n<context->audioFrames; n++){ | |
61 for(unsigned int c=0; c<context->audioChannels; c++){ | |
62 context->audioOut[n*context->audioChannels + c]=context->audioIn[n*context->audioChannels + c]; | |
63 } | |
64 //use digital channels 2-8 to create a 7 bit binary counter | |
65 context->digital[n]=context->digital[n] & (~0b111111100); // set to zero (GPIO_OUTPUT) the bits in the lower word | |
66 context->digital[n]=context->digital[n] & ((~0b111111100<<16) | 0xffff ); //initialize to zero the bits in the higher word (output value) | |
67 context->digital[n]=context->digital[n] | ( ((context->audioSampleCount&0b1111111)<<(16+2)) ) ; // set the bits in the higher word to the desired output value, keeping the lower word unchanged | |
68 digitalWriteFrame(context, n, P8_29, digitalReadFrame(context, n, P8_30)); // echo the input from from channel 15 to channel 14 | |
69 digitalWriteFrame(context, n, P8_28, digitalReadFrame(context, n, P9_16)); // echo the input from from channel 10 to channel 13 | |
70 pinModeFrame(context, 0, P8_30, 0); //set channel 15 to input | |
71 } | |
72 | |
73 for(unsigned int n=0; n<context->analogFrames; n++){ | |
74 analogWriteFrame(context, n, 1, (context->audioSampleCount&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames | |
75 analogWriteFrame(context, n, 2, analogReadFrame(context, n, 2)); // writes a single frame. channel2 is just a passthrough | |
76 // rt_printf("Analog out frame %d :",n); | |
77 // for(int c=0; c<gNumAnalogChannels; c++) | |
78 // rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]); | |
79 // rt_printf("\n"); | |
80 } | |
81 return; | |
82 | |
83 } | |
84 | |
85 // cleanup() is called once at the end, after the audio has stopped. | |
86 // Release any resources that were allocated in setup(). | |
87 | |
88 void cleanup(BeagleRTContext *context, void *userData) | |
89 { | |
90 // Nothing to do here | |
91 } |