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