annotate examples/04-Audio/filter-IIR/main.cpp @ 544:cdabbaf3a252 prerelease

Updated Audio examples for audioOutChannels etc.
author Robert Jack <robert.h.jack@gmail.com>
date Fri, 24 Jun 2016 13:32:07 +0100
parents 8fcfbfb32aa0
children
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 #include <iostream>
robert@464 25 #include <cstdlib>
robert@464 26 #include <libgen.h>
robert@464 27 #include <signal.h>
robert@464 28 #include <string>
robert@464 29 #include <getopt.h>
robert@464 30 #include <sndfile.h> // to load audio files
robert@464 31
robert@464 32 #include <Bela.h>
robert@464 33 #include "SampleData.h"
robert@464 34
robert@464 35 using namespace std;
robert@464 36
robert@464 37 float gCutFreq = 100;
robert@464 38
robert@464 39 // Load samples from file
robert@464 40 int initFile(string file, SampleData *smp)//float *& smp)
robert@464 41 {
robert@464 42 SNDFILE *sndfile ;
robert@464 43 SF_INFO sfinfo ;
robert@464 44
robert@464 45 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
robert@464 46 cout << "Couldn't open file " << file << endl;
robert@464 47 return 1;
robert@464 48 }
robert@464 49
robert@464 50 int numChan = sfinfo.channels;
robert@464 51 if(numChan != 1)
robert@464 52 {
robert@464 53 cout << "Error: " << file << " is not a mono file" << endl;
robert@464 54 return 1;
robert@464 55 }
robert@464 56
robert@464 57 smp->sampleLen = sfinfo.frames * numChan;
robert@464 58 smp->samples = new float[smp->sampleLen];
robert@464 59 if(smp == NULL){
robert@464 60 cout << "Could not allocate buffer" << endl;
robert@464 61 return 1;
robert@464 62 }
robert@464 63
robert@464 64 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
robert@464 65 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
robert@464 66
robert@464 67 // Pad with zeros in case we couldn't read whole file
robert@464 68 for(int k = readcount; k <smp->sampleLen; k++)
robert@464 69 smp->samples[k] = 0;
robert@464 70
robert@464 71 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
robert@464 72 double scale ;
robert@464 73 int m ;
robert@464 74
robert@464 75 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
robert@464 76 if (scale < 1e-10)
robert@464 77 scale = 1.0 ;
robert@464 78 else
robert@464 79 scale = 32700.0 / scale ;
robert@464 80 cout << "File samples scale = " << scale << endl;
robert@464 81
robert@464 82 for (m = 0; m < smp->sampleLen; m++)
robert@464 83 smp->samples[m] *= scale;
robert@464 84 }
robert@464 85
robert@464 86 sf_close(sndfile);
robert@464 87
robert@464 88 return 0;
robert@464 89 }
robert@464 90
robert@464 91 // Handle Ctrl-C by requesting that the audio rendering stop
robert@464 92 void interrupt_handler(int var)
robert@464 93 {
robert@464 94 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask);
robert@464 95 gShouldStop = true;
robert@464 96 }
robert@464 97
robert@464 98 // Print usage information
robert@464 99 void usage(const char * processName)
robert@464 100 {
robert@464 101 cerr << "Usage: " << processName << " [options]" << endl;
robert@464 102
robert@464 103 Bela_usage();
robert@464 104
robert@464 105 cerr << " --file [-f] filename: Name of the file to load (default is \"longsample.wav\")\n";
robert@464 106 cerr << " --cutfreq [-c] freq: Set the cut off frequency of the filter in Hz\n";
robert@464 107 cerr << " --help [-h]: Print this menu\n";
robert@464 108 }
robert@464 109
robert@464 110 int main(int argc, char *argv[])
robert@464 111 {
robert@464 112 BelaInitSettings settings; // Standard audio settings
robert@464 113 string fileName; // Name of the sample to load
robert@464 114
robert@464 115 SampleData sampleData; // User define structure to pass data retrieved from file to render function
robert@464 116 sampleData.samples = 0;
robert@464 117 sampleData.sampleLen = -1;
robert@464 118
robert@464 119
robert@464 120 struct option customOptions[] =
robert@464 121 {
robert@464 122 {"help", 0, NULL, 'h'},
robert@464 123 {"cutfreq", 1, NULL, 'c'},
robert@464 124 {"file", 1, NULL, 'f'},
robert@464 125 {NULL, 0, NULL, 0}
robert@464 126 };
robert@464 127
robert@464 128 // Set default settings
robert@464 129 Bela_defaultSettings(&settings);
robert@464 130
robert@464 131 // Parse command-line arguments
robert@464 132 while (1) {
robert@464 133 int c;
robert@464 134 if ((c = Bela_getopt_long(argc, argv, "hf:c:", customOptions, &settings)) < 0)
robert@464 135 break;
robert@464 136 switch (c) {
robert@464 137 case 'h':
robert@464 138 usage(basename(argv[0]));
robert@464 139 exit(0);
robert@464 140 case 'f':
robert@464 141 fileName = string((char *)optarg);
robert@464 142 break;
robert@464 143 case 'c':
robert@464 144 gCutFreq = atof(optarg);
robert@464 145 break;
robert@464 146 case '?':
robert@464 147 default:
robert@464 148 usage(basename(argv[0]));
robert@464 149 exit(1);
robert@464 150 }
robert@464 151 }
robert@464 152
robert@464 153 if(fileName.empty()){
robert@544 154 fileName = "longsample.wav";
robert@464 155 }
robert@464 156
robert@464 157 if(settings.verbose) {
robert@464 158 cout << "Loading file " << fileName << endl;
robert@464 159 }
robert@464 160
robert@464 161 // Load file
robert@464 162 if(initFile(fileName, &sampleData) != 0)
robert@464 163 {
robert@464 164 cout << "Error: unable to load samples " << endl;
robert@464 165 return -1;
robert@464 166 }
robert@464 167
robert@464 168 if(settings.verbose)
robert@464 169 cout << "File contains " << sampleData.sampleLen << " samples" << endl;
robert@464 170
robert@464 171
robert@464 172 // Initialise the PRU audio device
robert@464 173 if(Bela_initAudio(&settings, &sampleData) != 0) {
robert@464 174 cout << "Error: unable to initialise audio" << endl;
robert@464 175 return -1;
robert@464 176 }
robert@464 177
robert@464 178 // Start the audio device running
robert@464 179 if(Bela_startAudio()) {
robert@464 180 cout << "Error: unable to start real-time audio" << endl;
robert@464 181 return -1;
robert@464 182 }
robert@464 183
robert@464 184 // Set up interrupt handler to catch Control-C and SIGTERM
robert@464 185 signal(SIGINT, interrupt_handler);
robert@464 186 signal(SIGTERM, interrupt_handler);
robert@464 187
robert@464 188 // Run until told to stop
robert@464 189 while(!gShouldStop) {
robert@464 190 usleep(100000);
robert@464 191 }
robert@464 192
robert@464 193 // Stop the audio device
robert@464 194 Bela_stopAudio();
robert@464 195
robert@464 196 // Clean up any resources allocated for audio
robert@464 197 Bela_cleanupAudio();
robert@464 198
robert@464 199 // All done!
robert@464 200 return 0;
robert@464 201 }
robert@464 202