annotate projects/samples/main.cpp @ 2:021ac8a1a4f9

_new FIR filter example
author Victor Zappi <victor.zappi@qmul.ac.uk>
date Thu, 06 Nov 2014 15:59:16 +0000
parents 24fc8026ae8e
children 09f03ac40fcc
rev   line source
victor@1 1 /*
victor@1 2 * main.cpp
victor@1 3 *
victor@1 4 * Created on: Oct 24, 2014
victor@1 5 * Author: Andrew McPherson and Victor Zappi
victor@1 6 */
victor@1 7
victor@1 8 #include <iostream>
victor@1 9 #include <cstdlib>
victor@1 10 #include <libgen.h>
victor@1 11 #include <signal.h>
victor@1 12 #include <string>
victor@1 13 #include <sndfile.h> // to load audio files
victor@1 14 #include "../../include/RTAudio.h"
victor@1 15 #include "SampleData.h"
victor@1 16
victor@1 17 using namespace std;
victor@1 18
victor@1 19 // Load samples from file
victor@1 20 int initFile(string file, SampleData *smp)//float *& smp)
victor@1 21 {
victor@1 22 SNDFILE *sndfile ;
victor@1 23 SF_INFO sfinfo ;
victor@1 24
victor@1 25 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
victor@1 26 cout << "Couldn't open file " << file << endl;
victor@1 27 return 1;
victor@1 28 }
victor@1 29
victor@1 30 int numChan = sfinfo.channels;
victor@1 31 if(numChan != 1)
victor@1 32 {
victor@1 33 cout << "Error: " << file << " is not a mono file" << endl;
victor@1 34 return 1;
victor@1 35 }
victor@1 36
victor@1 37 smp->sampleLen = sfinfo.frames * numChan;
victor@1 38 smp->samples = new float[smp->sampleLen];
victor@1 39 if(smp == NULL){
victor@1 40 cout << "Could not allocate buffer" << endl;
victor@1 41 return 1;
victor@1 42 }
victor@1 43
victor@1 44 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
victor@1 45 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
victor@1 46
victor@1 47 // Pad with zeros in case we couldn't read whole file
victor@1 48 for(int k = readcount; k <smp->sampleLen; k++)
victor@1 49 smp->samples[k] = 0;
victor@1 50
victor@1 51 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
victor@1 52 double scale ;
victor@1 53 int m ;
victor@1 54
victor@1 55 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
victor@1 56 if (scale < 1e-10)
victor@1 57 scale = 1.0 ;
victor@1 58 else
victor@1 59 scale = 32700.0 / scale ;
victor@1 60 cout << "File samples scale = " << scale << endl;
victor@1 61
victor@1 62 for (m = 0; m < smp->sampleLen; m++)
victor@1 63 smp->samples[m] *= scale;
victor@1 64 }
victor@1 65
victor@1 66 sf_close(sndfile);
victor@1 67
victor@1 68 return 0;
victor@1 69 }
victor@1 70
victor@1 71
victor@1 72 // Handle Ctrl-C by requesting that the audio rendering stop
victor@1 73 void interrupt_handler(int var)
victor@1 74 {
victor@1 75 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask);
victor@1 76 gShouldStop = true;
victor@1 77 }
victor@1 78
victor@1 79 // Print usage information
victor@1 80 void usage(const char * processName)
victor@1 81 {
victor@1 82 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl;
victor@1 83 cerr << " -h: Print this menu\n";
victor@1 84 cerr << " -v: Enable verbose messages\n";
victor@1 85 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n";
victor@1 86 cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n";
victor@1 87 cerr << " -f filename: Name of the file to load (default is \"sample.wav\")\n";
victor@1 88 }
victor@1 89
victor@1 90 int main(int argc, char *argv[])
victor@1 91 {
victor@1 92 int periodSize = 8; // Period size in sensor frames
victor@1 93 int verbose = 0; // Verbose printing level
victor@1 94 int useMatrix = 0; // Whether to use the matrix or just audio
victor@1 95 string fileName; // Name of the sample to load
victor@1 96
victor@1 97 SampleData sampleData; // User define structure to pass data retrieved from file to render function
victor@1 98 sampleData.samples = 0;
victor@1 99 sampleData.sampleLen = -1;
victor@1 100
victor@1 101 // Parse command-line arguments
victor@1 102 while (1) {
victor@1 103 int c;
victor@1 104 if ((c = getopt(argc, argv, "hp:vms:")) < 0)
victor@1 105 break;
victor@1 106 switch (c) {
victor@1 107 case 'h':
victor@1 108 usage(basename(argv[0]));
victor@1 109 exit(0);
victor@1 110 case 'p':
victor@1 111 periodSize = atoi(optarg);
victor@1 112 if(periodSize < 1)
victor@1 113 periodSize = 1;
victor@1 114 break;
victor@1 115 case 'v':
victor@1 116 verbose = 1;
victor@1 117 break;
victor@1 118 case 'm':
victor@1 119 useMatrix = 1;
victor@1 120 break;
victor@1 121 case 'f':
victor@1 122 fileName = string((char *)optarg);
victor@1 123 break;
victor@1 124 case '?':
victor@1 125 default:
victor@1 126 usage(basename(argv[0]));
victor@1 127 exit(1);
victor@1 128 }
victor@1 129 }
victor@1 130
victor@1 131 if(fileName.empty()){
victor@1 132 fileName = "samples/sample.wav";
victor@1 133 }
victor@1 134
victor@1 135 // Set verbose logging information (optional by using value > 0; default is 0)
victor@1 136 setVerboseLevel(verbose);
victor@1 137
victor@1 138 if(verbose) {
victor@1 139 cout << "Starting with period size " << periodSize << endl;
victor@1 140 if(useMatrix)
victor@1 141 cout << "Matrix enabled\n";
victor@1 142 else
victor@1 143 cout << "Matrix disabled\n";
victor@1 144 cout << "Loading file " << fileName << endl;
victor@1 145 }
victor@1 146
victor@1 147 // Load file
victor@1 148 if(initFile(fileName, &sampleData) != 0)
victor@1 149 {
victor@1 150 cout << "Error: unable to load samples " << endl;
victor@1 151 return -1;
victor@1 152 }
victor@1 153
victor@1 154 if(verbose)
victor@1 155 cout << "File contains " << sampleData.sampleLen << " samples" << endl;
victor@1 156
victor@1 157 // Initialise the PRU audio device
victor@1 158 if(initAudio(periodSize, useMatrix, &sampleData) != 0) {
victor@1 159 cout << "Error: unable to initialise audio" << endl;
victor@1 160 return -1;
victor@1 161 }
victor@1 162
victor@1 163 // Start the audio device running
victor@1 164 if(startAudio()) {
victor@1 165 cout << "Error: unable to start real-time audio" << endl;
victor@1 166 return -1;
victor@1 167 }
victor@1 168
victor@1 169 // Set up interrupt handler to catch Control-C
victor@1 170 signal(SIGINT, interrupt_handler);
victor@1 171
victor@1 172 // Run until told to stop
victor@1 173 while(!gShouldStop) {
victor@1 174 usleep(100000);
victor@1 175 }
victor@1 176
victor@1 177 // Stop the audio device
victor@1 178 stopAudio();
victor@1 179
victor@1 180 if(verbose) {
victor@1 181 cout << "Cleaning up..." << endl;
victor@1 182 }
victor@1 183
victor@1 184 // Clean up any resources allocated for audio
victor@1 185 cleanupAudio();
victor@1 186
victor@1 187 // All done!
victor@1 188 return 0;
victor@1 189 }