robert@493: /* robert@493: * main.cpp robert@493: * robert@493: * Created on: Oct 24, 2014 robert@493: * Author: parallels robert@493: */ robert@493: robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: robert@493: extern int gScreenFramesPerSecond; robert@493: robert@493: float *gMusicBuffer = 0; robert@493: int gMusicBufferLength = 0; robert@493: float *gSoundBoomBuffer = 0; robert@493: int gSoundBoomBufferLength = 0; robert@493: float *gSoundHitBuffer = 0; robert@493: int gSoundHitBufferLength = 0; robert@493: robert@493: robert@493: using namespace std; robert@493: robert@493: // Load a sound sample from file robert@493: int loadSoundFile(const string& path, float **buffer, int *bufferLength) robert@493: { robert@493: SNDFILE *sndfile ; robert@493: SF_INFO sfinfo ; robert@493: robert@493: if (!(sndfile = sf_open (path.c_str(), SFM_READ, &sfinfo))) { robert@493: cout << "Couldn't open file " << path << endl; robert@493: return 1; robert@493: } robert@493: robert@493: int numChan = sfinfo.channels; robert@493: if(numChan != 1) robert@493: { robert@493: cout << "Error: " << path << " is not a mono file" << endl; robert@493: return 1; robert@493: } robert@493: robert@493: *bufferLength = sfinfo.frames * numChan; robert@493: *buffer = new float[*bufferLength]; robert@493: if(*buffer == 0){ robert@493: cout << "Could not allocate buffer" << endl; robert@493: return 1; robert@493: } robert@493: robert@493: int subformat = sfinfo.format & SF_FORMAT_SUBMASK; robert@493: int readcount = sf_read_float(sndfile, *buffer, *bufferLength); robert@493: robert@493: // Pad with zeros in case we couldn't read whole file robert@493: for(int k = readcount; k < *bufferLength; k++) robert@493: (*buffer)[k] = 0; robert@493: robert@493: sf_close(sndfile); robert@493: return 0; robert@493: } robert@493: robert@493: // Handle Ctrl-C by requesting that the audio rendering stop robert@493: void interrupt_handler(int var) robert@493: { robert@493: gShouldStop = true; robert@493: } robert@493: robert@493: // Print usage information robert@493: void usage(const char * processName) robert@493: { robert@493: cerr << "Usage: " << processName << " [options]" << endl; robert@493: robert@493: Bela_usage(); robert@493: robert@493: cerr << " --fps [-f] value: Set target frames per second\n"; robert@493: cerr << " --help [-h]: Print this menu\n"; robert@493: } robert@493: robert@493: int main(int argc, char *argv[]) robert@493: { robert@493: BelaInitSettings settings; // Standard audio settings robert@493: string musicFileName = "music.wav"; robert@493: string soundBoomFileName = "boom.wav"; robert@493: string soundHitFileName = "hit.wav"; robert@493: robert@493: struct option customOptions[] = robert@493: { robert@493: {"help", 0, NULL, 'h'}, robert@493: {"fps", 1, NULL, 'f'}, robert@493: {NULL, 0, NULL, 0} robert@493: }; robert@493: robert@493: // Set default settings robert@493: Bela_defaultSettings(&settings); robert@493: robert@493: // Parse command-line arguments robert@493: while (1) { robert@493: int c; robert@493: if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) robert@493: break; robert@493: switch (c) { robert@493: case 'f': robert@493: gScreenFramesPerSecond = atoi(optarg); robert@493: if(gScreenFramesPerSecond < 1) robert@493: gScreenFramesPerSecond = 1; robert@493: if(gScreenFramesPerSecond > 100) robert@493: gScreenFramesPerSecond = 100; robert@493: break; robert@493: case 'h': robert@493: usage(basename(argv[0])); robert@493: exit(0); robert@493: case '?': robert@493: default: robert@493: usage(basename(argv[0])); robert@493: exit(1); robert@493: } robert@493: } robert@493: robert@493: // Load the sound files robert@493: if(loadSoundFile(musicFileName, &gMusicBuffer, &gMusicBufferLength) != 0) { robert@493: cout << "Warning: unable to load sound file " << musicFileName << endl; robert@493: } robert@493: if(loadSoundFile(soundBoomFileName, &gSoundBoomBuffer, &gSoundBoomBufferLength) != 0) { robert@493: cout << "Warning: unable to load sound file " << soundBoomFileName << endl; robert@493: } robert@493: if(loadSoundFile(soundHitFileName, &gSoundHitBuffer, &gSoundHitBufferLength) != 0) { robert@493: cout << "Warning: unable to load sound file " << soundHitFileName << endl; robert@493: } robert@493: robert@493: // Initialise the PRU audio device robert@493: if(Bela_initAudio(&settings, 0) != 0) { robert@493: cout << "Error: unable to initialise audio" << endl; robert@493: return -1; robert@493: } robert@493: robert@493: // Start the audio device running robert@493: if(Bela_startAudio()) { robert@493: cout << "Error: unable to start real-time audio" << endl; robert@493: return -1; robert@493: } robert@493: robert@493: // Set up interrupt handler to catch Control-C and SIGTERM robert@493: signal(SIGINT, interrupt_handler); robert@493: signal(SIGTERM, interrupt_handler); robert@493: robert@493: // Run until told to stop robert@493: while(!gShouldStop) { robert@493: usleep(100000); robert@493: } robert@493: robert@493: // Stop the audio device robert@493: Bela_stopAudio(); robert@493: robert@493: // Clean up any resources allocated for audio robert@493: Bela_cleanupAudio(); robert@493: robert@493: // Release sound files robert@493: if(gMusicBuffer != 0) robert@493: free(gMusicBuffer); robert@493: if(gSoundBoomBuffer != 0) robert@493: free(gSoundBoomBuffer); robert@493: if(gSoundHitBuffer != 0) robert@493: free(gSoundHitBuffer); robert@493: robert@493: // All done! robert@493: return 0; robert@493: }