Mercurial > hg > beaglert
comparison projects/samples/render.cpp @ 108:3068421c0737 ultra-staging
Merged default into ultra-staging
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 18 Aug 2015 00:35:15 +0100 |
parents | 3c3a1357657d |
children | ac8eb07afcf5 |
comparison
equal
deleted
inserted
replaced
54:d3f869b98147 | 108:3068421c0737 |
---|---|
4 * Created on: Oct 24, 2014 | 4 * Created on: Oct 24, 2014 |
5 * Author: Andrew McPherson and Victor Zappi | 5 * Author: Andrew McPherson and Victor Zappi |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #include "../../include/render.h" | 9 #include <BeagleRT.h> |
10 #include "../../include/RTAudio.h" // to schedule lower prio parallel process | |
11 #include <rtdk.h> | |
12 #include <cmath> | 10 #include <cmath> |
13 #include <stdio.h> | |
14 #include "SampleData.h" | 11 #include "SampleData.h" |
15 | 12 |
16 SampleData gSampleData; // User defined structure to get complex data from main | 13 SampleData gSampleData; // User defined structure to get complex data from main |
17 int gReadPtr; // Position of last read sample from file | 14 int gReadPtr; // Position of last read sample from file |
18 | 15 |
20 AuxiliaryTask gTriggerSamplesTask; | 17 AuxiliaryTask gTriggerSamplesTask; |
21 | 18 |
22 bool initialise_trigger(); | 19 bool initialise_trigger(); |
23 void trigger_samples(); | 20 void trigger_samples(); |
24 | 21 |
25 // initialise_render() is called once before the audio rendering starts. | 22 // setup() is called once before the audio rendering starts. |
26 // Use it to perform any initialisation and allocation which is dependent | 23 // Use it to perform any initialisation and allocation which is dependent |
27 // on the period size or sample rate. | 24 // on the period size or sample rate. |
28 // | 25 // |
29 // userData holds an opaque pointer to a data structure that was passed | 26 // userData holds an opaque pointer to a data structure that was passed |
30 // in from the call to initAudio(). | 27 // in from the call to initAudio(). |
31 // | 28 // |
32 // Return true on success; returning false halts the program. | 29 // Return true on success; returning false halts the program. |
33 | 30 |
34 bool initialise_render(int numMatrixChannels, int numAudioChannels, | 31 bool setup(BeagleRTContext *context, void *userData) |
35 int numMatrixFramesPerPeriod, | |
36 int numAudioFramesPerPeriod, | |
37 float matrixSampleRate, float audioSampleRate, | |
38 void *userData) | |
39 { | 32 { |
40 | 33 |
41 // Retrieve a parameter passed in from the initAudio() call | 34 // Retrieve a parameter passed in from the initAudio() call |
42 gSampleData = *(SampleData *)userData; | 35 gSampleData = *(SampleData *)userData; |
43 | 36 |
53 // render() is called regularly at the highest priority by the audio engine. | 46 // render() is called regularly at the highest priority by the audio engine. |
54 // Input and output are given from the audio hardware and the other | 47 // Input and output are given from the audio hardware and the other |
55 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 48 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
56 // will be 0. | 49 // will be 0. |
57 | 50 |
58 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | 51 void render(BeagleRTContext *context, void *userData) |
59 uint16_t *matrixIn, uint16_t *matrixOut) | |
60 { | 52 { |
61 for(int n = 0; n < numAudioFrames; n++) { | 53 for(unsigned int n = 0; n < context->audioFrames; n++) { |
62 float out = 0; | 54 float out = 0; |
63 | 55 |
64 // If triggered... | 56 // If triggered... |
65 if(gReadPtr != -1) | 57 if(gReadPtr != -1) |
66 out += gSampleData.samples[gReadPtr++]; // ...read each sample... | 58 out += gSampleData.samples[gReadPtr++]; // ...read each sample... |
67 | 59 |
68 if(gReadPtr >= gSampleData.sampleLen) | 60 if(gReadPtr >= gSampleData.sampleLen) |
69 gReadPtr = -1; | 61 gReadPtr = -1; |
70 | 62 |
71 for(int channel = 0; channel < gNumAudioChannels; channel++) | 63 for(unsigned int channel = 0; channel < context->audioChannels; channel++) |
72 audioOut[n * gNumAudioChannels + channel] = out; // ...and put it in both left and right channel | 64 context->audioOut[n * context->audioChannels + channel] = out; // ...and put it in both left and right channel |
73 } | 65 } |
74 | 66 |
75 // Request that the lower-priority task run at next opportunity | 67 // Request that the lower-priority task run at next opportunity |
76 scheduleAuxiliaryTask(gTriggerSamplesTask); | 68 BeagleRT_scheduleAuxiliaryTask(gTriggerSamplesTask); |
77 } | 69 } |
78 | 70 |
79 // Initialise the auxiliary task | 71 // Initialise the auxiliary task |
80 // and print info | 72 // and print info |
81 | 73 |
82 bool initialise_trigger() | 74 bool initialise_trigger() |
83 { | 75 { |
84 if((gTriggerSamplesTask = createAuxiliaryTaskLoop(&trigger_samples, 50, "beaglert-trigger-samples")) == 0) | 76 if((gTriggerSamplesTask = BeagleRT_createAuxiliaryTask(&trigger_samples, 50, "beaglert-trigger-samples")) == 0) |
85 return false; | 77 return false; |
86 | 78 |
87 rt_printf("Press 'a' to trigger sample, 's' to stop\n"); | 79 rt_printf("Press 'a' to trigger sample, 's' to stop\n"); |
88 rt_printf("Press 'q' to quit\n"); | 80 rt_printf("Press 'q' to quit\n"); |
89 | 81 |
122 } | 114 } |
123 } | 115 } |
124 | 116 |
125 | 117 |
126 | 118 |
127 // cleanup_render() is called once at the end, after the audio has stopped. | 119 // cleanup() is called once at the end, after the audio has stopped. |
128 // Release any resources that were allocated in initialise_render(). | 120 // Release any resources that were allocated in setup(). |
129 | 121 |
130 void cleanup_render() | 122 void cleanup(BeagleRTContext *context, void *userData) |
131 { | 123 { |
132 delete[] gSampleData.samples; | 124 delete[] gSampleData.samples; |
133 } | 125 } |