comparison projects/analogDigitalDemo/render.cpp @ 52:a6d223473ea2 newapi

Updated examples for new API. tank_wars not yet updated; audio_in_FFT and oscillator_bank not working properly yet.
author andrewm
date Sun, 31 May 2015 02:13:39 -0500
parents 83baffda5786
children 3c3a1357657d
comparison
equal deleted inserted replaced
51:4f8db16f17b5 52:a6d223473ea2
1 /* 1 /*
2 *
3 * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover
4 * using the BeagleBone Black.
5 * 2 *
6 * Andrew McPherson and Victor Zappi 3 * Andrew McPherson and Victor Zappi
7 * Queen Mary, University of London 4 * Queen Mary, University of London
8 */ 5 */
9 6
10 #include "../../include/render.h" 7 #include "../../include/BeagleRT.h"
8 #include "../../include/Utilities.h"
11 #include <cmath> 9 #include <cmath>
12 #include <rtdk.h> 10 #include <rtdk.h>
13 11
14 /* TASK: declare any global variables you need here */ 12 /* TASK: declare any global variables you need here */
15 13
19 // 17 //
20 // userData holds an opaque pointer to a data structure that was passed 18 // userData holds an opaque pointer to a data structure that was passed
21 // in from the call to initAudio(). 19 // in from the call to initAudio().
22 // 20 //
23 // Return true on success; returning false halts the program. 21 // Return true on success; returning false halts the program.
24 int gNumDigitalFrames=0; 22
25 bool initialise_render(int numAnalogChannels, int numDigitalChannels, int numAudioChannels, 23 bool initialise_render(BeagleRTContext *context, void *userData)
26 int numAnalogFramesPerPeriod,
27 int numAudioFramesPerPeriod,
28 float analogSampleRate, float audioSampleRate,
29 void *userData, RTAudioSettings* settings)
30 { 24 {
31 gNumAnalogChannels=numAnalogChannels;
32 gNumDigitalChannels=numDigitalChannels;
33 return true; 25 return true;
34 } 26 }
35 27
36 // render() is called regularly at the highest priority by the audio engine. 28 // render() is called regularly at the highest priority by the audio engine.
37 // Input and output are given from the audio hardware and the other 29 // Input and output are given from the audio hardware and the other
38 // ADCs and DACs (if available). If only audio is available, numAnalogFrames 30 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
39 // will be 0. 31 // will be 0.
40 32
41 long int gCountFrames=0; 33 void render(BeagleRTContext *context, void *userData)
42 void render(int numAnalogFrames, int numDigitalFrames, int numAudioFrames, float *audioIn, float *audioOut,
43 float *analogIn, float *analogOut, uint32_t *digital)
44 /* 34 /*
45 we assume that gNumAnalogChannels=8, numAnalogFrames==8 and numDigitalFrames==numAudioFrames 35 we assume that gNumAnalogChannels=8, numAnalogFrames==8 and numDigitalFrames==numAudioFrames
46 * */ 36 * */
47 { 37 {
48 if((gCountFrames&31)==0){ //every 32 frames... 38 if((context->audioSampleCount&31)==0){ //every 32 frames...
49 //ANALOG channels 39 //ANALOG channels
50 analogWrite(0, 0, analogRead(0,0)); // 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 analogWriteFrame(context, 0, 0, analogReadFrame(context, 0,0));
41 // read the input0 at frame0 and write it to output0 frame0. Using analogWrite will fill the rest of the buffer with the same value
51 // The value at the last frame will persist through the successive buffers until is set again. 42 // The value at the last frame will persist through the successive buffers until is set again.
52 // This effectively is a pass-through with downsampling by 32 times 43 // This effectively is a pass-through with downsampling by 32 times
53 analogWrite(3, 0, 1.0); // write 1.0 to channel3 from frame0 to the end of the buffer 44 analogWriteFrame(context, 0, 3, 1.0); // write 1.0 to channel3 from frame0 to the end of the buffer
54 analogWrite(3, 4, 0.1); // write 0.1 to channel3 from frame4 to the end of the buffer 45 analogWriteFrame(context, 4, 3, 0.1); // write 0.1 to channel3 from frame4 to the end of the buffer
55 analogWriteFrame(3,6,0.2); //write 0.2 to channel3 only on frame 6 46 analogWriteFrameOnce(context, 6, 3, 0.2); //write 0.2 to channel3 only on frame 6
56 //this buffer for channel 3 will look like this: 1 1 1 1 0.1 0.1 0.2 0.1 47 //this buffer for channel 3 will look like this: 1 1 1 1 0.1 0.1 0.2 0.1
57 //the next buffers for channel 3 will be filled up with 0.1 .... 48 //the next buffers for channel 3 will be filled up with 0.1 ....
58 //DIGITAL channels 49 //DIGITAL channels
59 digitalWrite(P8_07,0,GPIO_HIGH); //sets all the frames to HIGH for channel 0 50 digitalWriteFrame(context, 0, P8_07, GPIO_HIGH); //sets all the frames to HIGH for channel 0
60 digitalWriteFrame(P8_07,4,GPIO_LOW); //only frame 4 will be LOW for channel 0 51 digitalWriteFrameOnce(context, 4, P8_07, GPIO_LOW); //only frame 4 will be LOW for channel 0
61 // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1 52 // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1
62 // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer) 53 // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer)
63 digitalWrite(P8_08,0,GPIO_HIGH); 54 digitalWriteFrame(context, 0, P8_08, GPIO_HIGH);
64 digitalWrite(P8_08,2,GPIO_LOW); 55 digitalWriteFrame(context, 2, P8_08, GPIO_LOW);
65 digitalWrite(P8_08,4,GPIO_HIGH); 56 digitalWriteFrame(context, 4, P8_08, GPIO_HIGH);
66 digitalWrite(P8_08,5,GPIO_LOW); 57 digitalWriteFrame(context, 5, P8_08, GPIO_LOW);
67 setDigitalDirection(P9_16,0,GPIO_INPUT); // set channel 10 to input 58 pinModeFrame(context, 0, P9_16, GPIO_INPUT); // set channel 10 to input
68 // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0 59 // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0
69 // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer) 60 // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer)
70 } 61 }
71 for(int n=0; n<numAudioFrames; n++){ 62 for(unsigned int n=0; n<context->audioFrames; n++){
72 for(int c=0; c<gNumAudioChannels; c++){ 63 for(unsigned int c=0; c<context->audioChannels; c++){
73 audioOut[n*gNumAudioChannels + c]=audioIn[n*gNumAudioChannels + c]; 64 context->audioOut[n*context->audioChannels + c]=context->audioIn[n*context->audioChannels + c];
74 } 65 }
75 //use digital channels 2-8 to create a 7 bit binary counter 66 //use digital channels 2-8 to create a 7 bit binary counter
76 digital[n]=digital[n] & (~0b111111100); // set to zero (GPIO_OUTPUT) the bits in the lower word 67 context->digital[n]=context->digital[n] & (~0b111111100); // set to zero (GPIO_OUTPUT) the bits in the lower word
77 digital[n]=digital[n] & ((~0b111111100<<16) | 0xffff ); //initialize to zero the bits in the higher word (output value) 68 context->digital[n]=context->digital[n] & ((~0b111111100<<16) | 0xffff ); //initialize to zero the bits in the higher word (output value)
78 digital[n]=digital[n] | ( ((gCountFrames&0b1111111)<<(16+2)) ) ; // set the bits in the higher word to the desired output value, keeping the lower word unchanged 69 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
79 digitalWriteFrame(P8_29,n,digitalRead(P8_30,n)); // echo the input from from channel 15 to channel 14 70 digitalWriteFrame(context, n, P8_29, digitalReadFrame(context, n, P8_30)); // echo the input from from channel 15 to channel 14
80 digitalWriteFrame(P8_28,n,digitalRead(P9_16,n)); // echo the input from from channel 10 to channel 13 71 digitalWriteFrame(context, n, P8_28, digitalReadFrame(context, n, P9_16)); // echo the input from from channel 10 to channel 13
81 setDigitalDirection(P8_30,0,GPIO_INPUT); //set channel 15 to input 72 pinModeFrame(context, 0, P8_30, 0); //set channel 15 to input
82 gCountFrames++;
83 } 73 }
84 74
85 for(int n=0; n<numAnalogFrames; n++){ 75 for(unsigned int n=0; n<context->analogFrames; n++){
86 analogWriteFrame(1,n,(gCountFrames&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames 76 analogWriteFrame(context, n, 1, (context->audioSampleCount&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames
87 analogWriteFrame(2,n,analogRead(2,n)); // writes a single frame. channel2 is just a passthrough 77 analogWriteFrame(context, n, 2, analogReadFrame(context, n, 2)); // writes a single frame. channel2 is just a passthrough
88 // rt_printf("Analog out frame %d :",n); 78 // rt_printf("Analog out frame %d :",n);
89 // for(int c=0; c<gNumAnalogChannels; c++) 79 // for(int c=0; c<gNumAnalogChannels; c++)
90 // rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]); 80 // rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]);
91 // rt_printf("\n"); 81 // rt_printf("\n");
92 } 82 }
94 84
95 } 85 }
96 // cleanup_render() is called once at the end, after the audio has stopped. 86 // cleanup_render() is called once at the end, after the audio has stopped.
97 // Release any resources that were allocated in initialise_render(). 87 // Release any resources that were allocated in initialise_render().
98 88
99 void cleanup_render() 89 void cleanup_render(BeagleRTContext *context, void *userData)
100 { 90 {
101 /* TASK: 91 /* TASK:
102 * If you allocate any memory, be sure to release it here. 92 * If you allocate any memory, be sure to release it here.
103 * You may or may not need anything in this function, depending 93 * You may or may not need anything in this function, depending
104 * on your implementation. 94 * on your implementation.