annotate projects/filter_FIR/main.cpp @ 93:8c7f537d0a21

edited Scope.h with ifdefs to be compatible with udpio plugin
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 21 Jul 2015 21:18:45 +0100
parents 3c3a1357657d
children
rev   line source
victor@2 1 /*
victor@2 2 * main.cpp
victor@2 3 *
victor@2 4 * Created on: Oct 24, 2014
victor@2 5 * Author: Andrew McPherson and Victor Zappi
victor@2 6 */
victor@2 7
victor@2 8 #include <iostream>
victor@2 9 #include <cstdlib>
victor@2 10 #include <libgen.h>
victor@2 11 #include <signal.h>
victor@2 12 #include <string>
andrewm@5 13 #include <getopt.h>
victor@2 14 #include <sndfile.h> // to load audio files
andrewm@56 15
andrewm@56 16 #include <BeagleRT.h>
victor@2 17 #include "SampleData.h"
victor@2 18
victor@2 19 using namespace std;
victor@2 20
victor@2 21 // Load samples from file
victor@2 22 int initFile(string file, SampleData *smp)//float *& smp)
victor@2 23 {
victor@2 24 SNDFILE *sndfile ;
victor@2 25 SF_INFO sfinfo ;
victor@2 26
victor@2 27 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
victor@2 28 cout << "Couldn't open file " << file << endl;
victor@2 29 return 1;
victor@2 30 }
victor@2 31
victor@2 32 int numChan = sfinfo.channels;
victor@2 33 if(numChan != 1)
victor@2 34 {
victor@2 35 cout << "Error: " << file << " is not a mono file" << endl;
victor@2 36 return 1;
victor@2 37 }
victor@2 38
victor@2 39 smp->sampleLen = sfinfo.frames * numChan;
victor@2 40 smp->samples = new float[smp->sampleLen];
victor@2 41 if(smp == NULL){
victor@2 42 cout << "Could not allocate buffer" << endl;
victor@2 43 return 1;
victor@2 44 }
victor@2 45
victor@2 46 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
victor@2 47 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
victor@2 48
victor@2 49 // Pad with zeros in case we couldn't read whole file
victor@2 50 for(int k = readcount; k <smp->sampleLen; k++)
victor@2 51 smp->samples[k] = 0;
victor@2 52
victor@2 53 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
victor@2 54 double scale ;
victor@2 55 int m ;
victor@2 56
victor@2 57 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
victor@2 58 if (scale < 1e-10)
victor@2 59 scale = 1.0 ;
victor@2 60 else
victor@2 61 scale = 32700.0 / scale ;
victor@2 62 cout << "File samples scale = " << scale << endl;
victor@2 63
victor@2 64 for (m = 0; m < smp->sampleLen; m++)
victor@2 65 smp->samples[m] *= scale;
victor@2 66 }
victor@2 67
victor@2 68 sf_close(sndfile);
victor@2 69
victor@2 70 return 0;
victor@2 71 }
victor@2 72
victor@2 73
victor@2 74 // Handle Ctrl-C by requesting that the audio rendering stop
victor@2 75 void interrupt_handler(int var)
victor@2 76 {
victor@2 77 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask);
victor@2 78 gShouldStop = true;
victor@2 79 }
victor@2 80
victor@2 81 // Print usage information
victor@2 82 void usage(const char * processName)
victor@2 83 {
andrewm@5 84 cerr << "Usage: " << processName << " [options]" << endl;
andrewm@5 85
andrewm@5 86 BeagleRT_usage();
andrewm@5 87
andrewm@5 88 cerr << " --file [-f] filename: Name of the file to load (default is \"longsample.wav\")\n";
andrewm@5 89 cerr << " --help [-h]: Print this menu\n";
victor@2 90 }
victor@2 91
victor@2 92 int main(int argc, char *argv[])
victor@2 93 {
andrewm@52 94 BeagleRTInitSettings settings; // Standard audio settings
victor@2 95 string fileName; // Name of the sample to load
victor@2 96
victor@2 97 SampleData sampleData; // User define structure to pass data retrieved from file to render function
victor@2 98 sampleData.samples = 0;
victor@2 99 sampleData.sampleLen = -1;
victor@2 100
andrewm@5 101
andrewm@5 102 struct option customOptions[] =
andrewm@5 103 {
andrewm@5 104 {"help", 0, NULL, 'h'},
andrewm@5 105 {"file", 1, NULL, 'f'},
andrewm@5 106 {NULL, 0, NULL, 0}
andrewm@5 107 };
andrewm@5 108
andrewm@5 109 // Set default settings
andrewm@5 110 BeagleRT_defaultSettings(&settings);
andrewm@5 111
victor@2 112 // Parse command-line arguments
victor@2 113 while (1) {
victor@2 114 int c;
andrewm@5 115 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
victor@2 116 break;
victor@2 117 switch (c) {
victor@2 118 case 'h':
victor@2 119 usage(basename(argv[0]));
victor@2 120 exit(0);
victor@2 121 case 'f':
victor@2 122 fileName = string((char *)optarg);
victor@2 123 break;
victor@2 124 case '?':
victor@2 125 default:
victor@2 126 usage(basename(argv[0]));
victor@2 127 exit(1);
victor@2 128 }
victor@2 129 }
victor@2 130
victor@2 131 if(fileName.empty()){
victor@9 132 fileName = "filter/longsample.wav";
victor@2 133 }
victor@2 134
andrewm@5 135 if(settings.verbose) {
victor@2 136 cout << "Loading file " << fileName << endl;
victor@2 137 }
victor@2 138
victor@2 139 // Load file
victor@2 140 if(initFile(fileName, &sampleData) != 0)
victor@2 141 {
victor@2 142 cout << "Error: unable to load samples " << endl;
victor@2 143 return -1;
victor@2 144 }
victor@2 145
andrewm@5 146 if(settings.verbose)
victor@2 147 cout << "File contains " << sampleData.sampleLen << " samples" << endl;
victor@2 148
andrewm@5 149
victor@2 150 // Initialise the PRU audio device
andrewm@5 151 if(BeagleRT_initAudio(&settings, &sampleData) != 0) {
victor@2 152 cout << "Error: unable to initialise audio" << endl;
victor@2 153 return -1;
victor@2 154 }
victor@2 155
victor@2 156 // Start the audio device running
andrewm@5 157 if(BeagleRT_startAudio()) {
victor@2 158 cout << "Error: unable to start real-time audio" << endl;
victor@2 159 return -1;
victor@2 160 }
victor@2 161
andrewm@15 162 // Set up interrupt handler to catch Control-C and SIGTERM
victor@2 163 signal(SIGINT, interrupt_handler);
andrewm@15 164 signal(SIGTERM, interrupt_handler);
victor@2 165
victor@2 166 // Run until told to stop
victor@2 167 while(!gShouldStop) {
victor@2 168 usleep(100000);
victor@2 169 }
victor@2 170
victor@2 171 // Stop the audio device
andrewm@5 172 BeagleRT_stopAudio();
victor@2 173
victor@2 174 // Clean up any resources allocated for audio
andrewm@5 175 BeagleRT_cleanupAudio();
victor@2 176
victor@2 177 // All done!
victor@2 178 return 0;
victor@2 179 }
andrewm@5 180