diff projects/filter_FIR/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 021ac8a1a4f9
children 323a4eb9b7c0
line wrap: on
line diff
--- a/projects/filter_FIR/main.cpp	Thu Nov 06 19:02:48 2014 +0000
+++ b/projects/filter_FIR/main.cpp	Sat Nov 08 16:16:55 2014 +0100
@@ -10,14 +10,13 @@
 #include <libgen.h>
 #include <signal.h>
 #include <string>
+#include <getopt.h>
 #include <sndfile.h>				// to load audio files
 #include "../../include/RTAudio.h"
 #include "SampleData.h"
 
 using namespace std;
 
-int gPeriodSize = 8;			// Period size in sensor frames
-
 // Load samples from file
 int initFile(string file, SampleData *smp)//float *& smp)
 {
@@ -81,44 +80,43 @@
 // Print usage information
 void usage(const char * processName)
 {
-	cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << 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 << "   -m:           Enable the matrix (ADC and DAC) as well as audio\n";
-	cerr << "   -f filename:  Name of the file to load (default is \"sample.wav\")\n";
+	cerr << "Usage: " << processName << " [options]" << endl;
+
+	BeagleRT_usage();
+
+	cerr << "   --file [-f] filename:    Name of the file to load (default is \"longsample.wav\")\n";
+	cerr << "   --help [-h]:             Print this menu\n";
 }
 
 int main(int argc, char *argv[])
 {
-	int verbose = 0;			// Verbose printing level
-	int useMatrix = 0;			// Whether to use the matrix or just audio
+	RTAudioSettings settings;	// Standard audio settings
 	string fileName;			// Name of the sample to load
 
 	SampleData sampleData;		// User define structure to pass data retrieved from file to render function
 	sampleData.samples = 0;
 	sampleData.sampleLen = -1;
 
+
+	struct option customOptions[] =
+	{
+		{"help", 0, NULL, 'h'},
+		{"file", 1, NULL, 'f'},
+		{NULL, 0, NULL, 0}
+	};
+
+	// Set default settings
+	BeagleRT_defaultSettings(&settings);
+
 	// Parse command-line arguments
 	while (1) {
 		int c;
-		if ((c = getopt(argc, argv, "hp:vms:")) < 0)
+		if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
 				break;
 		switch (c) {
 		case 'h':
 				usage(basename(argv[0]));
 				exit(0);
-		case 'p':
-				gPeriodSize = atoi(optarg);
-				if(gPeriodSize < 1)
-					gPeriodSize = 1;
-				break;
-		case 'v':
-				verbose = 1;
-				break;
-		case 'm':
-				useMatrix = 1;
-				break;
 		case 'f':
 				fileName = string((char *)optarg);
 				break;
@@ -130,18 +128,10 @@
 	}
 
 	if(fileName.empty()){
-		fileName = "filter/longsample.wav";
+		fileName = "longsample.wav";
 	}
 
-	// Set verbose logging information (optional by using value > 0; default is 0)
-	setVerboseLevel(verbose);
-
-	if(verbose) {
-		cout << "Starting with period size " << gPeriodSize << endl;
-		if(useMatrix)
-			cout << "Matrix enabled\n";
-		else
-			cout << "Matrix disabled\n";
+	if(settings.verbose) {
 		cout << "Loading file " << fileName << endl;
 	}
 
@@ -152,17 +142,18 @@
 		return -1;
 	}
 
-	if(verbose)
+	if(settings.verbose)
 		cout << "File contains " << sampleData.sampleLen << " samples" << endl;
 
+
 	// Initialise the PRU audio device
-	if(initAudio(gPeriodSize, useMatrix, &sampleData) != 0) {
+	if(BeagleRT_initAudio(&settings, &sampleData) != 0) {
 		cout << "Error: unable to initialise audio" << endl;
 		return -1;
 	}
 
 	// Start the audio device running
-	if(startAudio()) {
+	if(BeagleRT_startAudio()) {
 		cout << "Error: unable to start real-time audio" << endl;
 		return -1;
 	}
@@ -176,15 +167,12 @@
 	}
 
 	// 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;
 }
+