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