diff projects/oscillator_bank/main.cpp @ 5:09f03ac40fcc

API improvements and cleanups. Now all common audio command-line options can be parsed automatically.
author andrewm
date Sat, 08 Nov 2014 16:16:55 +0100
parents 8a575ba3ab52
children 901d205d1a3c
line wrap: on
line diff
--- a/projects/oscillator_bank/main.cpp	Thu Nov 06 19:02:48 2014 +0000
+++ b/projects/oscillator_bank/main.cpp	Sat Nov 08 16:16:55 2014 +0100
@@ -9,6 +9,7 @@
 #include <cstdlib>
 #include <libgen.h>
 #include <signal.h>
+#include <getopt.h>
 #include "../../include/RTAudio.h"
 
 using namespace std;
@@ -16,7 +17,6 @@
 int gNumOscillators = 32;
 int gWavetableLength = 1024;
 
-
 // Handle Ctrl-C by requesting that the audio rendering stop
 void interrupt_handler(int var)
 {
@@ -26,38 +26,39 @@
 // Print usage information
 void usage(const char * processName)
 {
-	cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl;
-	cerr << "   -h:           Print this menu\n";
-	cerr << "   -v:           Enable verbose messages\n";
-	cerr << "   -p period:    Set the period (hardware buffer) size in sensor frames\n";
-    cerr << "   -n oscs:      Set the number of oscillators to use (default: 32)\n";
-    cerr << "   -w length:    Set the wavetable length in samples (default: 1024)\n";
-	cerr << "   -m:           Enable the matrix (ADC and DAC) for controlling parameters\n";
+	cerr << "Usage: " << processName << " [options]" << endl;
+
+	BeagleRT_usage();
+
+	cerr << "   --num-oscillators [-n] oscs: Set the number of oscillators to use (default: 32)\n";
+	cerr << "   --wavetable [-w] length:     Set the wavetable length in samples (default: 1024)\n";
+	cerr << "   --help [-h]:                 Print this menu\n";
 }
 
 int main(int argc, char *argv[])
 {
-	int periodSize = 8;			// Period size in sensor frames
-	int verbose = 0;			// Verbose printing level
-	int useMatrix = 0;
+	RTAudioSettings settings;	// Standard audio settings
+
+	struct option customOptions[] =
+	{
+		{"help", 0, NULL, 'h'},
+		{"num-oscillators", 1, NULL, 'n'},
+		{"wavetable", 1, NULL, 'w'},
+		{NULL, 0, NULL, 0}
+	};
+
+	// Set default settings
+	BeagleRT_defaultSettings(&settings);
 
 	// Parse command-line arguments
 	while (1) {
 		int c;
-		if ((c = getopt(argc, argv, "hp:vn:w:m")) < 0)
+		if ((c = BeagleRT_getopt_long(argc, argv, "hn:w:", customOptions, &settings)) < 0)
 				break;
 		switch (c) {
 		case 'h':
 				usage(basename(argv[0]));
 				exit(0);
-		case 'p':
-				periodSize = atoi(optarg);
-				if(periodSize < 1)
-					periodSize = 1;
-				break;
-		case 'v':
-				verbose = 1;
-				break;
 		case 'n':
 				gNumOscillators = atoi(optarg);
 				if(gNumOscillators <= 0) {
@@ -72,9 +73,6 @@
 				if(gWavetableLength > 16384)
 					gWavetableLength = 16384;
 				break;
-		case 'm':
-				useMatrix = 1;
-				break;
 		case '?':
 		default:
 				usage(basename(argv[0]));
@@ -82,26 +80,18 @@
 		}
 	}
 
-
-	// Set verbose logging information (optional by using value > 0; default is 0)
-	setVerboseLevel(verbose);
-
-	if(verbose) {
-		cout << "Starting with period size " << periodSize << endl;
-		cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
-		cout << "--> Matrix ";
-		if(useMatrix) cout << "enabled\n";
-		else cout << "disabled\n";
-	}
-
 	// Initialise the PRU audio device
-	if(initAudio(periodSize, useMatrix, 0) != 0) {
+	if(BeagleRT_initAudio(&settings, 0) != 0) {
 		cout << "Error: unable to initialise audio" << endl;
 		return -1;
 	}
 
+	if(settings.verbose) {
+		cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
+	}
+
 	// Start the audio device running
-	if(startAudio()) {
+	if(BeagleRT_startAudio()) {
 		cout << "Error: unable to start real-time audio" << endl;
 		return -1;
 	}
@@ -115,14 +105,10 @@
 	}
 
 	// Stop the audio device
-	stopAudio();
-
-	if(verbose) {
-		cout << "Cleaning up..." << endl;
-	}
+	BeagleRT_stopAudio();
 
 	// Clean up any resources allocated for audio
-	cleanupAudio();
+	BeagleRT_cleanupAudio();
 
 	// All done!
 	return 0;