Mercurial > hg > beaglert
comparison examples/10-Instruments/tank_wars/main.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Mon, 20 Jun 2016 16:20:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
463:c47709e8b5c9 | 464:8fcfbfb32aa0 |
---|---|
1 /* | |
2 * main.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 #include <iostream> | |
9 #include <cstdlib> | |
10 #include <libgen.h> | |
11 #include <signal.h> | |
12 #include <getopt.h> | |
13 #include <sndfile.h> | |
14 #include <Bela.h> | |
15 | |
16 extern int gScreenFramesPerSecond; | |
17 | |
18 float *gMusicBuffer = 0; | |
19 int gMusicBufferLength = 0; | |
20 float *gSoundBoomBuffer = 0; | |
21 int gSoundBoomBufferLength = 0; | |
22 float *gSoundHitBuffer = 0; | |
23 int gSoundHitBufferLength = 0; | |
24 | |
25 | |
26 using namespace std; | |
27 | |
28 // Load a sound sample from file | |
29 int loadSoundFile(const string& path, float **buffer, int *bufferLength) | |
30 { | |
31 SNDFILE *sndfile ; | |
32 SF_INFO sfinfo ; | |
33 | |
34 if (!(sndfile = sf_open (path.c_str(), SFM_READ, &sfinfo))) { | |
35 cout << "Couldn't open file " << path << endl; | |
36 return 1; | |
37 } | |
38 | |
39 int numChan = sfinfo.channels; | |
40 if(numChan != 1) | |
41 { | |
42 cout << "Error: " << path << " is not a mono file" << endl; | |
43 return 1; | |
44 } | |
45 | |
46 *bufferLength = sfinfo.frames * numChan; | |
47 *buffer = new float[*bufferLength]; | |
48 if(*buffer == 0){ | |
49 cout << "Could not allocate buffer" << endl; | |
50 return 1; | |
51 } | |
52 | |
53 int subformat = sfinfo.format & SF_FORMAT_SUBMASK; | |
54 int readcount = sf_read_float(sndfile, *buffer, *bufferLength); | |
55 | |
56 // Pad with zeros in case we couldn't read whole file | |
57 for(int k = readcount; k < *bufferLength; k++) | |
58 (*buffer)[k] = 0; | |
59 | |
60 sf_close(sndfile); | |
61 return 0; | |
62 } | |
63 | |
64 // Handle Ctrl-C by requesting that the audio rendering stop | |
65 void interrupt_handler(int var) | |
66 { | |
67 gShouldStop = true; | |
68 } | |
69 | |
70 // Print usage information | |
71 void usage(const char * processName) | |
72 { | |
73 cerr << "Usage: " << processName << " [options]" << endl; | |
74 | |
75 Bela_usage(); | |
76 | |
77 cerr << " --fps [-f] value: Set target frames per second\n"; | |
78 cerr << " --help [-h]: Print this menu\n"; | |
79 } | |
80 | |
81 int main(int argc, char *argv[]) | |
82 { | |
83 BelaInitSettings settings; // Standard audio settings | |
84 string musicFileName = "music.wav"; | |
85 string soundBoomFileName = "boom.wav"; | |
86 string soundHitFileName = "hit.wav"; | |
87 | |
88 struct option customOptions[] = | |
89 { | |
90 {"help", 0, NULL, 'h'}, | |
91 {"fps", 1, NULL, 'f'}, | |
92 {NULL, 0, NULL, 0} | |
93 }; | |
94 | |
95 // Set default settings | |
96 Bela_defaultSettings(&settings); | |
97 | |
98 // Parse command-line arguments | |
99 while (1) { | |
100 int c; | |
101 if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) | |
102 break; | |
103 switch (c) { | |
104 case 'f': | |
105 gScreenFramesPerSecond = atoi(optarg); | |
106 if(gScreenFramesPerSecond < 1) | |
107 gScreenFramesPerSecond = 1; | |
108 if(gScreenFramesPerSecond > 100) | |
109 gScreenFramesPerSecond = 100; | |
110 break; | |
111 case 'h': | |
112 usage(basename(argv[0])); | |
113 exit(0); | |
114 case '?': | |
115 default: | |
116 usage(basename(argv[0])); | |
117 exit(1); | |
118 } | |
119 } | |
120 | |
121 // Load the sound files | |
122 if(loadSoundFile(musicFileName, &gMusicBuffer, &gMusicBufferLength) != 0) { | |
123 cout << "Warning: unable to load sound file " << musicFileName << endl; | |
124 } | |
125 if(loadSoundFile(soundBoomFileName, &gSoundBoomBuffer, &gSoundBoomBufferLength) != 0) { | |
126 cout << "Warning: unable to load sound file " << soundBoomFileName << endl; | |
127 } | |
128 if(loadSoundFile(soundHitFileName, &gSoundHitBuffer, &gSoundHitBufferLength) != 0) { | |
129 cout << "Warning: unable to load sound file " << soundHitFileName << endl; | |
130 } | |
131 | |
132 // Initialise the PRU audio device | |
133 if(Bela_initAudio(&settings, 0) != 0) { | |
134 cout << "Error: unable to initialise audio" << endl; | |
135 return -1; | |
136 } | |
137 | |
138 // Start the audio device running | |
139 if(Bela_startAudio()) { | |
140 cout << "Error: unable to start real-time audio" << endl; | |
141 return -1; | |
142 } | |
143 | |
144 // Set up interrupt handler to catch Control-C and SIGTERM | |
145 signal(SIGINT, interrupt_handler); | |
146 signal(SIGTERM, interrupt_handler); | |
147 | |
148 // Run until told to stop | |
149 while(!gShouldStop) { | |
150 usleep(100000); | |
151 } | |
152 | |
153 // Stop the audio device | |
154 Bela_stopAudio(); | |
155 | |
156 // Clean up any resources allocated for audio | |
157 Bela_cleanupAudio(); | |
158 | |
159 // Release sound files | |
160 if(gMusicBuffer != 0) | |
161 free(gMusicBuffer); | |
162 if(gSoundBoomBuffer != 0) | |
163 free(gSoundBoomBuffer); | |
164 if(gSoundHitBuffer != 0) | |
165 free(gSoundHitBuffer); | |
166 | |
167 // All done! | |
168 return 0; | |
169 } |