Mercurial > hg > beaglert
comparison examples/basic_FFT_phase_vocoder/main.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/basic_FFT_phase_vocoder/main.cpp@aec268b5d1b4 |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
1 /* | |
2 * main.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 #include <iostream> | |
9 #include <cstdlib> | |
10 #include <cstdio> | |
11 #include <libgen.h> | |
12 #include <signal.h> | |
13 #include <getopt.h> | |
14 #include <unistd.h> | |
15 #include <sys/time.h> | |
16 #include <sndfile.h> // to load audio files | |
17 #include "SampleData.h" | |
18 #include <BeagleRT.h> | |
19 | |
20 using namespace std; | |
21 | |
22 // Global variables used by getCurrentTime() | |
23 unsigned long long gFirstSeconds, gFirstMicroseconds; | |
24 | |
25 // Load samples from file | |
26 int initFile(string file, SampleData *smp)//float *& smp) | |
27 { | |
28 SNDFILE *sndfile ; | |
29 SF_INFO sfinfo ; | |
30 | |
31 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) { | |
32 cout << "Couldn't open file " << file << endl; | |
33 return 1; | |
34 } | |
35 | |
36 int numChan = sfinfo.channels; | |
37 if(numChan != 1) | |
38 { | |
39 cout << "Error: " << file << " is not a mono file" << endl; | |
40 return 1; | |
41 } | |
42 | |
43 smp->sampleLen = sfinfo.frames * numChan; | |
44 smp->samples = new float[smp->sampleLen]; | |
45 if(smp == NULL){ | |
46 cout << "Could not allocate buffer" << endl; | |
47 return 1; | |
48 } | |
49 | |
50 int subformat = sfinfo.format & SF_FORMAT_SUBMASK; | |
51 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen); | |
52 | |
53 // Pad with zeros in case we couldn't read whole file | |
54 for(int k = readcount; k <smp->sampleLen; k++) | |
55 smp->samples[k] = 0; | |
56 | |
57 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { | |
58 double scale ; | |
59 int m ; | |
60 | |
61 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; | |
62 if (scale < 1e-10) | |
63 scale = 1.0 ; | |
64 else | |
65 scale = 32700.0 / scale ; | |
66 cout << "File samples scale = " << scale << endl; | |
67 | |
68 for (m = 0; m < smp->sampleLen; m++) | |
69 smp->samples[m] *= scale; | |
70 } | |
71 | |
72 sf_close(sndfile); | |
73 | |
74 return 0; | |
75 } | |
76 | |
77 | |
78 // Handle Ctrl-C by requesting that the audio rendering stop | |
79 void interrupt_handler(int var) | |
80 { | |
81 gShouldStop = true; | |
82 } | |
83 | |
84 // Print usage information | |
85 void usage(const char * processName) | |
86 { | |
87 cerr << "Usage: " << processName << " [options]" << endl; | |
88 | |
89 BeagleRT_usage(); | |
90 | |
91 cerr << " --help [-h]: Print this menu\n"; | |
92 } | |
93 | |
94 /* Function which returns the time since start of the program | |
95 * in (fractional) seconds. | |
96 */ | |
97 double getCurrentTime(void) { | |
98 unsigned long long result; | |
99 struct timeval tv; | |
100 | |
101 gettimeofday(&tv, NULL); | |
102 result = (tv.tv_sec - gFirstSeconds) * 1000000ULL + (tv.tv_usec - gFirstMicroseconds); | |
103 return (double)result / 1000000.0; | |
104 } | |
105 extern SampleData gSampleData; | |
106 int main(int argc, char *argv[]) | |
107 { | |
108 BeagleRTInitSettings settings; // Standard audio settings | |
109 struct timeval tv; | |
110 string fileName; // Name of the sample to load | |
111 | |
112 struct option customOptions[] = | |
113 { | |
114 {"help", 0, NULL, 'h'}, | |
115 {"file", 1, NULL, 'f'}, | |
116 {NULL, 0, NULL, 0} | |
117 }; | |
118 | |
119 gSampleData.samples = 0; | |
120 gSampleData.sampleLen = -1; | |
121 | |
122 // Set default settings | |
123 BeagleRT_defaultSettings(&settings); | |
124 | |
125 settings.periodSize = 32; // Larger period size by default, for testing | |
126 | |
127 // Parse command-line arguments | |
128 while (1) { | |
129 int c; | |
130 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) | |
131 break; | |
132 switch (c) { | |
133 case 'h': | |
134 usage(basename(argv[0])); | |
135 exit(0); | |
136 case 'f': | |
137 fileName = string((char *)optarg); | |
138 break; | |
139 case '?': | |
140 default: | |
141 usage(basename(argv[0])); | |
142 exit(1); | |
143 } | |
144 } | |
145 | |
146 if(fileName.empty()){ | |
147 fileName = "sample.wav"; | |
148 } | |
149 | |
150 | |
151 // Load file | |
152 if(initFile(fileName, &gSampleData) != 0) | |
153 { | |
154 cout << "Error: unable to load samples " << endl; | |
155 return -1; | |
156 } | |
157 | |
158 if(settings.verbose) | |
159 cout << "File contains " << gSampleData.sampleLen << " samples" << endl; | |
160 | |
161 | |
162 // Initialise the PRU audio device | |
163 if(BeagleRT_initAudio(&settings, &gSampleData) != 0) { | |
164 cout << "Error: unable to initialise audio" << endl; | |
165 return -1; | |
166 } | |
167 | |
168 // Initialise time | |
169 gettimeofday(&tv, NULL); | |
170 gFirstSeconds = tv.tv_sec; | |
171 gFirstMicroseconds = tv.tv_usec; | |
172 | |
173 // Start the audio device running | |
174 if(BeagleRT_startAudio()) { | |
175 cout << "Error: unable to start real-time audio" << endl; | |
176 return -1; | |
177 } | |
178 | |
179 // Set up interrupt handler to catch Control-C | |
180 signal(SIGINT, interrupt_handler); | |
181 signal(SIGTERM, interrupt_handler); | |
182 | |
183 // Run until told to stop | |
184 while(!gShouldStop) { | |
185 usleep(100000); | |
186 } | |
187 | |
188 // Stop the audio device | |
189 BeagleRT_stopAudio(); | |
190 | |
191 // Clean up any resources allocated for audio | |
192 BeagleRT_cleanupAudio(); | |
193 | |
194 // All done! | |
195 return 0; | |
196 } |