victor@4: /*
victor@4:  * main.cpp
victor@4:  *
victor@4:  *  Created on: Oct 24, 2014
victor@4:  *      Author: parallels
victor@4:  */
victor@4: 
victor@4: #include <iostream>
victor@4: #include <cstdlib>
victor@4: #include <libgen.h>
victor@4: #include <signal.h>
andrewm@5: #include <getopt.h>
victor@4: #include "../../include/RTAudio.h"
victor@4: 
victor@4: using namespace std;
victor@4: 
victor@4: // Handle Ctrl-C by requesting that the audio rendering stop
victor@4: void interrupt_handler(int var)
victor@4: {
victor@4: 	gShouldStop = true;
victor@4: }
victor@4: 
victor@4: // Print usage information
victor@4: void usage(const char * processName)
victor@4: {
andrewm@5: 	cerr << "Usage: " << processName << " [options]" << endl;
andrewm@5: 
andrewm@5: 	BeagleRT_usage();
andrewm@5: 
andrewm@5: 	cerr << "   --fftsize [-s] size:     Set the fSize of the FFT, in samples\n";
andrewm@5: 	cerr << "   --help [-h]:             Print this menu\n";
victor@4: }
victor@4: 
victor@4: int main(int argc, char *argv[])
victor@4: {
andrewm@5: 	RTAudioSettings settings;	// Standard audio settings
victor@4: 	int fftSize = 64;		    // Size of the FFT, in samples
andrewm@5: 
andrewm@5: 	struct option customOptions[] =
andrewm@5: 	{
andrewm@5: 		{"help", 0, NULL, 'h'},
andrewm@5: 		{"fftsize", 1, NULL, 's'},
andrewm@5: 		{NULL, 0, NULL, 0}
andrewm@5: 	};
andrewm@5: 
andrewm@5: 	// Set default settings
andrewm@5: 	BeagleRT_defaultSettings(&settings);
andrewm@5: 
andrewm@5: 	settings.useMatrix = 0;	// No matrix usage by default
victor@4: 
victor@4: 	// Parse command-line arguments
victor@4: 	while (1) {
victor@4: 		int c;
andrewm@5: 		if ((c = BeagleRT_getopt_long(argc, argv, "hs:", customOptions, &settings)) < 0)
victor@4: 				break;
victor@4: 		switch (c) {
victor@4: 		case 'h':
victor@4: 				usage(basename(argv[0]));
victor@4: 				exit(0);
victor@4: 		case 's':
victor@4: 				fftSize = atof(optarg);
victor@4: 				break;
victor@4: 		case '?':
victor@4: 		default:
victor@4: 				usage(basename(argv[0]));
victor@4: 				exit(1);
victor@4: 		}
victor@4: 	}
victor@4: 
victor@4: 	// Initialise the PRU audio device
andrewm@5: 	if(BeagleRT_initAudio(&settings, &fftSize) != 0) {
victor@4: 		cout << "Error: unable to initialise audio" << endl;
victor@4: 		return -1;
victor@4: 	}
victor@4: 
victor@4: 	// Start the audio device running
andrewm@5: 	if(BeagleRT_startAudio()) {
victor@4: 		cout << "Error: unable to start real-time audio" << endl;
victor@4: 		return -1;
victor@4: 	}
victor@4: 
andrewm@15: 	// Set up interrupt handler to catch Control-C and SIGTERM
victor@4: 	signal(SIGINT, interrupt_handler);
andrewm@15: 	signal(SIGTERM, interrupt_handler);
victor@4: 
victor@4: 	// Run until told to stop
victor@4: 	while(!gShouldStop) {
victor@4: 		usleep(100000);
victor@4: 	}
victor@4: 
victor@4: 	// Stop the audio device
andrewm@5: 	BeagleRT_stopAudio();
victor@4: 
victor@4: 	// Clean up any resources allocated for audio
andrewm@5: 	BeagleRT_cleanupAudio();
victor@4: 
victor@4: 	// All done!
victor@4: 	return 0;
victor@4: }
andrewm@5: