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