annotate examples/tank_wars/main.cpp @ 349:6e454ebd9cc4 prerelease

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