Mercurial > hg > beaglert
comparison projects/tank_wars/main.cpp @ 22:fbfeb5895efd matrix_gpio
Updated tank wars demo for new API
author | andrewm |
---|---|
date | Sun, 03 May 2015 01:10:17 +0100 |
parents | 901d205d1a3c |
children | 3c3a1357657d |
comparison
equal
deleted
inserted
replaced
20:58eb99dac921 | 22:fbfeb5895efd |
---|---|
8 #include <iostream> | 8 #include <iostream> |
9 #include <cstdlib> | 9 #include <cstdlib> |
10 #include <libgen.h> | 10 #include <libgen.h> |
11 #include <signal.h> | 11 #include <signal.h> |
12 #include <getopt.h> | 12 #include <getopt.h> |
13 #include <sndfile.h> | |
13 #include "../../include/RTAudio.h" | 14 #include "../../include/RTAudio.h" |
14 | 15 |
15 extern int gScreenFramesPerSecond; | 16 extern int gScreenFramesPerSecond; |
16 | 17 |
18 float *gMusicBuffer = 0; | |
19 int gMusicBufferLength = 0; | |
20 float *gSoundBoomBuffer = 0; | |
21 int gSoundBoomBufferLength = 0; | |
22 | |
17 using namespace std; | 23 using namespace std; |
24 | |
25 // Load a sound sample from file | |
26 int loadSoundFile(const string& path, float **buffer, int *bufferLength) | |
27 { | |
28 SNDFILE *sndfile ; | |
29 SF_INFO sfinfo ; | |
30 | |
31 if (!(sndfile = sf_open (path.c_str(), SFM_READ, &sfinfo))) { | |
32 cout << "Couldn't open file " << path << endl; | |
33 return 1; | |
34 } | |
35 | |
36 int numChan = sfinfo.channels; | |
37 if(numChan != 1) | |
38 { | |
39 cout << "Error: " << path << " is not a mono file" << endl; | |
40 return 1; | |
41 } | |
42 | |
43 *bufferLength = sfinfo.frames * numChan; | |
44 *buffer = new float[*bufferLength]; | |
45 if(*buffer == 0){ | |
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, *buffer, *bufferLength); | |
52 | |
53 // Pad with zeros in case we couldn't read whole file | |
54 for(int k = readcount; k < *bufferLength; k++) | |
55 (*buffer)[k] = 0; | |
56 | |
57 sf_close(sndfile); | |
58 return 0; | |
59 } | |
18 | 60 |
19 // Handle Ctrl-C by requesting that the audio rendering stop | 61 // Handle Ctrl-C by requesting that the audio rendering stop |
20 void interrupt_handler(int var) | 62 void interrupt_handler(int var) |
21 { | 63 { |
22 gShouldStop = true; | 64 gShouldStop = true; |
34 } | 76 } |
35 | 77 |
36 int main(int argc, char *argv[]) | 78 int main(int argc, char *argv[]) |
37 { | 79 { |
38 RTAudioSettings settings; // Standard audio settings | 80 RTAudioSettings settings; // Standard audio settings |
81 string musicFileName = "music.wav"; | |
82 string soundBoomFileName = "boom.wav"; | |
39 | 83 |
40 struct option customOptions[] = | 84 struct option customOptions[] = |
41 { | 85 { |
42 {"help", 0, NULL, 'h'}, | 86 {"help", 0, NULL, 'h'}, |
43 {"fps", 1, NULL, 'f'}, | 87 {"fps", 1, NULL, 'f'}, |
68 usage(basename(argv[0])); | 112 usage(basename(argv[0])); |
69 exit(1); | 113 exit(1); |
70 } | 114 } |
71 } | 115 } |
72 | 116 |
117 // Load the sound files | |
118 if(loadSoundFile(musicFileName, &gMusicBuffer, &gMusicBufferLength) != 0) { | |
119 cout << "Warning: unable to load sound file " << musicFileName << endl; | |
120 } | |
121 if(loadSoundFile(soundBoomFileName, &gSoundBoomBuffer, &gSoundBoomBufferLength) != 0) { | |
122 cout << "Warning: unable to load sound file " << soundBoomFileName << endl; | |
123 } | |
124 | |
73 // Initialise the PRU audio device | 125 // Initialise the PRU audio device |
74 if(BeagleRT_initAudio(&settings, 0) != 0) { | 126 if(BeagleRT_initAudio(&settings, 0) != 0) { |
75 cout << "Error: unable to initialise audio" << endl; | 127 cout << "Error: unable to initialise audio" << endl; |
76 return -1; | 128 return -1; |
77 } | 129 } |
95 BeagleRT_stopAudio(); | 147 BeagleRT_stopAudio(); |
96 | 148 |
97 // Clean up any resources allocated for audio | 149 // Clean up any resources allocated for audio |
98 BeagleRT_cleanupAudio(); | 150 BeagleRT_cleanupAudio(); |
99 | 151 |
152 // Release sound files | |
153 if(gMusicBuffer != 0) | |
154 free(gMusicBuffer); | |
155 if(gSoundBoomBuffer != 0) | |
156 free(gSoundBoomBuffer); | |
157 | |
100 // All done! | 158 // All done! |
101 return 0; | 159 return 0; |
102 } | 160 } |