robert@501: /*
robert@501:  ____  _____ _        _    
robert@501: | __ )| ____| |      / \   
robert@501: |  _ \|  _| | |     / _ \  
robert@501: | |_) | |___| |___ / ___ \ 
robert@501: |____/|_____|_____/_/   \_\
robert@501: 
robert@501: The platform for ultra-low latency audio and sensor processing
robert@501: 
robert@501: http://bela.io
robert@501: 
robert@501: A project of the Augmented Instruments Laboratory within the
robert@501: Centre for Digital Music at Queen Mary University of London.
robert@501: http://www.eecs.qmul.ac.uk/~andrewm
robert@501: 
robert@501: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@501:   Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@501:   Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@501: 
robert@501: The Bela software is distributed under the GNU Lesser General Public License
robert@501: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@501: */
robert@501: 
robert@501: 
robert@501: #include <unistd.h>
robert@501: #include <iostream>
robert@501: #include <cstdlib>
robert@501: #include <libgen.h>
robert@501: #include <signal.h>
robert@501: #include <getopt.h>
robert@501: #include <Bela.h>
robert@501: 
robert@501: using namespace std;
robert@501: 
robert@501: // Handle Ctrl-C by requesting that the audio rendering stop
robert@501: void interrupt_handler(int var)
robert@501: {
robert@501: 	gShouldStop = true;
robert@501: }
robert@501: 
robert@501: // Print usage information
robert@501: void usage(const char * processName)
robert@501: {
robert@501: 	cerr << "Usage: " << processName << " [options]" << endl;
robert@501: 
robert@501: 	Bela_usage();
robert@501: 
robert@501: 	cerr << "   --frequency [-f] frequency: Set the frequency of the oscillator\n";
robert@501: 	cerr << "   --help [-h]:                Print this menu\n";
robert@501: }
robert@501: 
robert@501: int main(int argc, char *argv[])
robert@501: {
robert@501: 	BelaInitSettings settings;	// Standard audio settings
robert@501: 	float frequency = 440.0;	// Frequency of oscillator
robert@501: 
robert@501: 	struct option customOptions[] =
robert@501: 	{
robert@501: 		{"help", 0, NULL, 'h'},
robert@501: 		{"frequency", 1, NULL, 'f'},
robert@501: 		{NULL, 0, NULL, 0}
robert@501: 	};
robert@501: 
robert@501: 	// Set default settings
robert@501: 	Bela_defaultSettings(&settings);
robert@501: 
robert@501: 	// Parse command-line arguments
robert@501: 	while (1) {
robert@501: 		int c;
robert@501: 		if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
robert@501: 				break;
robert@501: 		switch (c) {
robert@501: 		case 'h':
robert@501: 				usage(basename(argv[0]));
robert@501: 				exit(0);
robert@501: 		case 'f':
robert@501: 				frequency = atof(optarg);
robert@501: 				break;
robert@501: 		case '?':
robert@501: 		default:
robert@501: 				usage(basename(argv[0]));
robert@501: 				exit(1);
robert@501: 		}
robert@501: 	}
robert@501: 
robert@501: 	// Initialise the PRU audio device
robert@501: 	if(Bela_initAudio(&settings, &frequency) != 0) {
robert@501: 		cout << "Error: unable to initialise audio" << endl;
robert@501: 		return -1;
robert@501: 	}
robert@501: 
robert@501: 	// Start the audio device running
robert@501: 	if(Bela_startAudio()) {
robert@501: 		cout << "Error: unable to start real-time audio" << endl;
robert@501: 		return -1;
robert@501: 	}
robert@501: 
robert@501: 	// Set up interrupt handler to catch Control-C and SIGTERM
robert@501: 	signal(SIGINT, interrupt_handler);
robert@501: 	signal(SIGTERM, interrupt_handler);
robert@501: 
robert@501: 	// Run until told to stop
robert@501: 	while(!gShouldStop) {
robert@501: 		usleep(100000);
robert@501: 	}
robert@501: 
robert@501: 	// Stop the audio device
robert@501: 	Bela_stopAudio();
robert@501: 
robert@501: 	// Clean up any resources allocated for audio
robert@501: 	Bela_cleanupAudio();
robert@501: 
robert@501: 	// All done!
robert@501: 	return 0;
robert@501: }