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