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