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