annotate examples/04-Audio/samples/render.cpp @ 507:1cec96845a23 prerelease

Explanted explantation
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 22 Jun 2016 01:51:17 +0100
parents b935f890e512
children cdabbaf3a252
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <cmath>
robert@464 27 #include "SampleData.h"
robert@464 28
robert@464 29 SampleData gSampleData; // User defined structure to get complex data from main
robert@464 30 int gReadPtr; // Position of last read sample from file
robert@464 31
robert@464 32 // Task for handling the update of the frequencies using the matrix
robert@464 33 AuxiliaryTask gTriggerSamplesTask;
robert@464 34
robert@464 35 bool initialise_trigger();
robert@464 36 void trigger_samples();
robert@464 37
robert@464 38 bool setup(BelaContext *context, void *userData)
robert@464 39 {
robert@464 40
robert@464 41 // Retrieve a parameter passed in from the initAudio() call
robert@464 42 gSampleData = *(SampleData *)userData;
robert@464 43
robert@464 44 gReadPtr = -1;
robert@464 45
robert@464 46 // Initialise auxiliary tasks
robert@464 47 if(!initialise_trigger())
robert@464 48 return false;
robert@464 49
robert@464 50 return true;
robert@464 51 }
robert@464 52
robert@464 53 void render(BelaContext *context, void *userData)
robert@464 54 {
robert@464 55 for(unsigned int n = 0; n < context->audioFrames; n++) {
robert@464 56 float out = 0;
robert@464 57
robert@464 58 // If triggered...
robert@464 59 if(gReadPtr != -1)
robert@464 60 out += gSampleData.samples[gReadPtr++]; // ...read each sample...
robert@464 61
robert@464 62 if(gReadPtr >= gSampleData.sampleLen)
robert@464 63 gReadPtr = -1;
robert@464 64
robert@464 65 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
robert@464 66 context->audioOut[n * context->audioChannels + channel] = out; // ...and put it in both left and right channel
robert@464 67 }
robert@464 68
robert@464 69 // Request that the lower-priority task run at next opportunity
robert@464 70 Bela_scheduleAuxiliaryTask(gTriggerSamplesTask);
robert@464 71 }
robert@464 72
robert@464 73 // Initialise the auxiliary task
robert@464 74 // and print info
robert@464 75
robert@464 76 bool initialise_trigger()
robert@464 77 {
robert@464 78 if((gTriggerSamplesTask = Bela_createAuxiliaryTask(&trigger_samples, 50, "bela-trigger-samples")) == 0)
robert@464 79 return false;
robert@464 80
robert@464 81 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
robert@464 82 rt_printf("Press 'q' to quit\n");
robert@464 83
robert@464 84 return true;
robert@464 85 }
robert@464 86
robert@464 87 // This is a lower-priority call to periodically read keyboard input
robert@464 88 // and trigger samples. By placing it at a lower priority,
robert@464 89 // it has minimal effect on the audio performance but it will take longer to
robert@464 90 // complete if the system is under heavy audio load.
robert@464 91
robert@464 92 void trigger_samples()
robert@464 93 {
robert@464 94 // This is not a real-time task!
robert@464 95 // Cos getchar is a system call, not handled by Xenomai.
robert@464 96 // This task will be automatically down graded.
robert@464 97
robert@464 98 char keyStroke = '.';
robert@464 99
robert@464 100 keyStroke = getchar();
robert@464 101 while(getchar()!='\n'); // to read the first stroke
robert@464 102
robert@464 103 switch (keyStroke)
robert@464 104 {
robert@464 105 case 'a':
robert@464 106 gReadPtr = 0;
robert@464 107 break;
robert@464 108 case 's':
robert@464 109 gReadPtr = -1;
robert@464 110 break;
robert@464 111 case 'q':
robert@464 112 gShouldStop = true;
robert@464 113 break;
robert@464 114 default:
robert@464 115 break;
robert@464 116 }
robert@464 117 }
robert@464 118
robert@464 119
robert@464 120 void cleanup(BelaContext *context, void *userData)
robert@464 121 {
robert@464 122 delete[] gSampleData.samples;
robert@464 123 }
robert@464 124
robert@464 125
robert@464 126 /**
robert@500 127 \example samples/render.cpp
robert@464 128
robert@464 129 Playback WAV files
robert@464 130 ------------------
robert@464 131
robert@464 132 This sketch shows how to playback audio samples from a buffer.
robert@464 133
robert@464 134 An audio file is loaded into a buffer `SampleData` as `gSampleData`. This is
robert@464 135 accessed with a read pointer that is incremented at audio rate within the render
robert@464 136 function: `out += gSampleData.samples[gReadPtr++]`.
robert@464 137
robert@464 138 Note that the read pointer is stopped from incrementing past the length of the
robert@464 139 `gSampleData`. This is achieved by comparing the read pointer value against the
robert@464 140 sample length which we can access as follows: `gSampleData.sampleLen`.
robert@464 141
robert@464 142 The sample is triggered by keyboard input: (a) starts sample playback, (s)
robert@464 143 stops sample playback. The triggering is treated as a lower priority task than
robert@464 144 the audio. You can see this at the bottom of the render function:
robert@464 145 `Bela_scheduleAuxiliaryTask(gTriggerSamplesTask)`;
robert@464 146 */