annotate projects/tank_wars/main.cpp @ 151:e9c9404e3d1f ClockSync

Pff partially working. No PID. When setting the audio clock on the bbb to 44098 the master and slave clock keep diverging instead of converging ...
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 22 Sep 2015 04:10:07 +0100
parents 3c3a1357657d
children 8d80eda512cd
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>
andrewm@56 14 #include <BeagleRT.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@22 22
andrewm@10 23 using namespace std;
andrewm@10 24
andrewm@22 25 // Load a sound sample from file
andrewm@22 26 int loadSoundFile(const string& path, float **buffer, int *bufferLength)
andrewm@22 27 {
andrewm@22 28 SNDFILE *sndfile ;
andrewm@22 29 SF_INFO sfinfo ;
andrewm@22 30
andrewm@22 31 if (!(sndfile = sf_open (path.c_str(), SFM_READ, &sfinfo))) {
andrewm@22 32 cout << "Couldn't open file " << path << endl;
andrewm@22 33 return 1;
andrewm@22 34 }
andrewm@22 35
andrewm@22 36 int numChan = sfinfo.channels;
andrewm@22 37 if(numChan != 1)
andrewm@22 38 {
andrewm@22 39 cout << "Error: " << path << " is not a mono file" << endl;
andrewm@22 40 return 1;
andrewm@22 41 }
andrewm@22 42
andrewm@22 43 *bufferLength = sfinfo.frames * numChan;
andrewm@22 44 *buffer = new float[*bufferLength];
andrewm@22 45 if(*buffer == 0){
andrewm@22 46 cout << "Could not allocate buffer" << endl;
andrewm@22 47 return 1;
andrewm@22 48 }
andrewm@22 49
andrewm@22 50 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
andrewm@22 51 int readcount = sf_read_float(sndfile, *buffer, *bufferLength);
andrewm@22 52
andrewm@22 53 // Pad with zeros in case we couldn't read whole file
andrewm@22 54 for(int k = readcount; k < *bufferLength; k++)
andrewm@22 55 (*buffer)[k] = 0;
andrewm@22 56
andrewm@22 57 sf_close(sndfile);
andrewm@22 58 return 0;
andrewm@22 59 }
andrewm@22 60
andrewm@10 61 // Handle Ctrl-C by requesting that the audio rendering stop
andrewm@10 62 void interrupt_handler(int var)
andrewm@10 63 {
andrewm@10 64 gShouldStop = true;
andrewm@10 65 }
andrewm@10 66
andrewm@10 67 // Print usage information
andrewm@10 68 void usage(const char * processName)
andrewm@10 69 {
andrewm@10 70 cerr << "Usage: " << processName << " [options]" << endl;
andrewm@10 71
andrewm@10 72 BeagleRT_usage();
andrewm@10 73
andrewm@10 74 cerr << " --fps [-f] value: Set target frames per second\n";
andrewm@10 75 cerr << " --help [-h]: Print this menu\n";
andrewm@10 76 }
andrewm@10 77
andrewm@10 78 int main(int argc, char *argv[])
andrewm@10 79 {
andrewm@56 80 BeagleRTInitSettings settings; // Standard audio settings
andrewm@22 81 string musicFileName = "music.wav";
andrewm@22 82 string soundBoomFileName = "boom.wav";
andrewm@10 83
andrewm@10 84 struct option customOptions[] =
andrewm@10 85 {
andrewm@10 86 {"help", 0, NULL, 'h'},
andrewm@10 87 {"fps", 1, NULL, 'f'},
andrewm@10 88 {NULL, 0, NULL, 0}
andrewm@10 89 };
andrewm@10 90
andrewm@10 91 // Set default settings
andrewm@10 92 BeagleRT_defaultSettings(&settings);
andrewm@10 93
andrewm@10 94 // Parse command-line arguments
andrewm@10 95 while (1) {
andrewm@10 96 int c;
andrewm@10 97 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
andrewm@10 98 break;
andrewm@10 99 switch (c) {
andrewm@10 100 case 'f':
andrewm@10 101 gScreenFramesPerSecond = atoi(optarg);
andrewm@10 102 if(gScreenFramesPerSecond < 1)
andrewm@10 103 gScreenFramesPerSecond = 1;
andrewm@10 104 if(gScreenFramesPerSecond > 100)
andrewm@10 105 gScreenFramesPerSecond = 100;
andrewm@10 106 break;
andrewm@10 107 case 'h':
andrewm@10 108 usage(basename(argv[0]));
andrewm@10 109 exit(0);
andrewm@10 110 case '?':
andrewm@10 111 default:
andrewm@10 112 usage(basename(argv[0]));
andrewm@10 113 exit(1);
andrewm@10 114 }
andrewm@10 115 }
andrewm@10 116
andrewm@22 117 // Load the sound files
andrewm@22 118 if(loadSoundFile(musicFileName, &gMusicBuffer, &gMusicBufferLength) != 0) {
andrewm@22 119 cout << "Warning: unable to load sound file " << musicFileName << endl;
andrewm@22 120 }
andrewm@22 121 if(loadSoundFile(soundBoomFileName, &gSoundBoomBuffer, &gSoundBoomBufferLength) != 0) {
andrewm@22 122 cout << "Warning: unable to load sound file " << soundBoomFileName << endl;
andrewm@22 123 }
andrewm@22 124
andrewm@10 125 // Initialise the PRU audio device
andrewm@10 126 if(BeagleRT_initAudio(&settings, 0) != 0) {
andrewm@10 127 cout << "Error: unable to initialise audio" << endl;
andrewm@10 128 return -1;
andrewm@10 129 }
andrewm@10 130
andrewm@10 131 // Start the audio device running
andrewm@10 132 if(BeagleRT_startAudio()) {
andrewm@10 133 cout << "Error: unable to start real-time audio" << endl;
andrewm@10 134 return -1;
andrewm@10 135 }
andrewm@10 136
andrewm@15 137 // Set up interrupt handler to catch Control-C and SIGTERM
andrewm@10 138 signal(SIGINT, interrupt_handler);
andrewm@15 139 signal(SIGTERM, interrupt_handler);
andrewm@10 140
andrewm@10 141 // Run until told to stop
andrewm@10 142 while(!gShouldStop) {
andrewm@10 143 usleep(100000);
andrewm@10 144 }
andrewm@10 145
andrewm@10 146 // Stop the audio device
andrewm@10 147 BeagleRT_stopAudio();
andrewm@10 148
andrewm@10 149 // Clean up any resources allocated for audio
andrewm@10 150 BeagleRT_cleanupAudio();
andrewm@10 151
andrewm@22 152 // Release sound files
andrewm@22 153 if(gMusicBuffer != 0)
andrewm@22 154 free(gMusicBuffer);
andrewm@22 155 if(gSoundBoomBuffer != 0)
andrewm@22 156 free(gSoundBoomBuffer);
andrewm@22 157
andrewm@10 158 // All done!
andrewm@10 159 return 0;
andrewm@10 160 }