changeset 544:cdabbaf3a252 prerelease

Updated Audio examples for audioOutChannels etc.
author Robert Jack <robert.h.jack@gmail.com>
date Fri, 24 Jun 2016 13:32:07 +0100
parents 8f8809c77dda
children a11814e864a8
files examples/03-Analog/analog-input/main.cpp examples/03-Analog/analog-output/main.cpp examples/04-Audio/FFT-audio-in/render.cpp examples/04-Audio/FFT-phase-vocoder/render.cpp examples/04-Audio/bucket-brigade-chorus/render.cpp examples/04-Audio/filter-FIR/main.cpp examples/04-Audio/filter-FIR/render.cpp examples/04-Audio/filter-IIR/main.cpp examples/04-Audio/filter-IIR/render.cpp examples/04-Audio/measure-noisefloor/render.cpp examples/04-Audio/oscillator-bank/render.cpp examples/04-Audio/samples/main.cpp examples/04-Audio/samples/render.cpp
diffstat 13 files changed, 73 insertions(+), 276 deletions(-) [+]
line wrap: on
line diff
--- a/examples/03-Analog/analog-input/main.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,133 +0,0 @@
-/*
- ____  _____ _        _    
-| __ )| ____| |      / \   
-|  _ \|  _| | |     / _ \  
-| |_) | |___| |___ / ___ \ 
-|____/|_____|_____/_/   \_\
-
-The platform for ultra-low latency audio and sensor processing
-
-http://bela.io
-
-A project of the Augmented Instruments Laboratory within the
-Centre for Digital Music at Queen Mary University of London.
-http://www.eecs.qmul.ac.uk/~andrewm
-
-(c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
-  Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
-  Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
-
-The Bela software is distributed under the GNU Lesser General Public License
-(LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
-*/
-
-#include <iostream>
-#include <cstdlib>
-#include <libgen.h>
-#include <signal.h>
-#include <getopt.h>
-#include <Bela.h>
-
-using namespace std;
-
-int gSensorInputFrequency = 0;
-int gSensorInputAmplitude = 1;
-
-// Handle Ctrl-C by requesting that the audio rendering stop
-void interrupt_handler(int var)
-{
-	gShouldStop = true;
-}
-
-// Print usage information
-void usage(const char * processName)
-{
-	cerr << "Usage: " << processName << " [options]" << endl;
-
-	Bela_usage();
-
-	cerr << "   --frequency [-f] input:  Choose the analog input controlling frequency (0-7; default 0)\n";
-	cerr << "   --amplitude [-a] input:  Choose the analog input controlling amplitude (0-7; default 1)\n";
-	cerr << "   --help [-h]:             Print this menu\n";
-}
-
-int main(int argc, char *argv[])
-{
-	BelaInitSettings settings;	// Standard audio settings
-
-	struct option customOptions[] =
-	{
-		{"help", 0, NULL, 'h'},
-		{"frequency", 1, NULL, 'f'},
-		{"amplitude", 1, NULL, 'a'},
-		{NULL, 0, NULL, 0}
-	};
-
-	// Set default settings
-	Bela_defaultSettings(&settings);
-
-	// Parse command-line arguments
-	while (1) {
-		int c;
-		if ((c = Bela_getopt_long(argc, argv, "hf:a:", customOptions, &settings)) < 0)
-				break;
-		switch (c) {
-		case 'h':
-				usage(basename(argv[0]));
-				exit(0);
-		case 'f':
-				gSensorInputFrequency = atoi(optarg);
-				if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) {
-					usage(basename(argv[0]));
-					exit(0);
-				}
-				break;
-		case 'a':
-				gSensorInputAmplitude = atoi(optarg);
-				if(gSensorInputAmplitude < 0 || gSensorInputAmplitude > 7) {
-					usage(basename(argv[0]));
-					exit(0);
-				}
-				break;
-		case '?':
-		default:
-				usage(basename(argv[0]));
-				exit(1);
-		}
-	}
-
-	// Initialise the PRU audio device
-	if(Bela_initAudio(&settings, 0) != 0) {
-		cout << "Error: unable to initialise audio" << endl;
-		return -1;
-	}
-
-	if(settings.verbose) {
-		cout << "--> Frequency on input " << gSensorInputFrequency << endl;
-		cout << "--> Amplitude on input " << gSensorInputAmplitude << endl;
-	}
-
-	// Start the audio device running
-	if(Bela_startAudio()) {
-		cout << "Error: unable to start real-time audio" << endl;
-		return -1;
-	}
-
-	// Set up interrupt handler to catch Control-C and SIGTERM
-	signal(SIGINT, interrupt_handler);
-	signal(SIGTERM, interrupt_handler);
-
-	// Run until told to stop
-	while(!gShouldStop) {
-		usleep(100000);
-	}
-
-	// Stop the audio device
-	Bela_stopAudio();
-
-	// Clean up any resources allocated for audio
-	Bela_cleanupAudio();
-
-	// All done!
-	return 0;
-}
--- a/examples/03-Analog/analog-output/main.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-/*
- ____  _____ _        _    
-| __ )| ____| |      / \   
-|  _ \|  _| | |     / _ \  
-| |_) | |___| |___ / ___ \ 
-|____/|_____|_____/_/   \_\
-
-The platform for ultra-low latency audio and sensor processing
-
-http://bela.io
-
-A project of the Augmented Instruments Laboratory within the
-Centre for Digital Music at Queen Mary University of London.
-http://www.eecs.qmul.ac.uk/~andrewm
-
-(c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
-	Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
-	Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
-
-The Bela software is distributed under the GNU Lesser General Public License
-(LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
-*/
-
-
-#include <iostream>
-#include <cstdlib>
-#include <libgen.h>
-#include <signal.h>
-#include <getopt.h>
-#include <Bela.h>
-
-using namespace std;
-
-// Handle Ctrl-C by requesting that the audio rendering stop
-void interrupt_handler(int var)
-{
-	gShouldStop = true;
-}
-
-// Print usage information
-void usage(const char * processName)
-{
-	cerr << "Usage: " << processName << " [options]" << endl;
-
-	Bela_usage();
-
-	cerr << "   --frequency [-f] frequency: Set the frequency of the LED fade (default: 1.0)\n";
-	cerr << "   --help [-h]:                Print this menu\n";
-}
-
-int main(int argc, char *argv[])
-{
-	BelaInitSettings settings;	// Standard audio settings
-	float frequency = 1.0;	// Frequency of LED fades
-
-	struct option customOptions[] =
-	{
-		{"help", 0, NULL, 'h'},
-		{"frequency", 1, NULL, 'f'},
-		{NULL, 0, NULL, 0}
-	};
-
-	// Set default settings
-	Bela_defaultSettings(&settings);
-
-	// In this example, audio isn't used so might as well leave speaker muted
-	settings.beginMuted = 1;
-
-	// Parse command-line arguments
-	while (1) {
-		int c;
-		if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
-				break;
-		switch (c) {
-		case 'h':
-				usage(basename(argv[0]));
-				exit(0);
-		case 'f':
-				frequency = atof(optarg);
-				if(frequency < 0)
-					frequency = 0;
-				if(frequency > 11025.0)
-					frequency = 11025.0;
-				break;
-		case '?':
-		default:
-				usage(basename(argv[0]));
-				exit(1);
-		}
-	}
-
-	// Initialise the PRU audio device
-	if(Bela_initAudio(&settings, &frequency) != 0) {
-		cout << "Error: unable to initialise audio" << endl;
-		return -1;
-	}
-
-	// Start the audio device running
-	if(Bela_startAudio()) {
-		cout << "Error: unable to start real-time audio" << endl;
-		return -1;
-	}
-
-	// Set up interrupt handler to catch Control-C and SIGTERM
-	signal(SIGINT, interrupt_handler);
-	signal(SIGTERM, interrupt_handler);
-
-	// Run until told to stop
-	while(!gShouldStop) {
-		usleep(100000);
-	}
-
-	// Stop the audio device
-	Bela_stopAudio();
-
-	// Clean up any resources allocated for audio
-	Bela_cleanupAudio();
-
-	// All done!
-	return 0;
-}
--- a/examples/04-Audio/FFT-audio-in/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/FFT-audio-in/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -40,6 +40,14 @@
 
 bool setup(BelaContext *context, void *userData)
 {
+    
+    // Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+	
 	// Retrieve a parameter passed in from the initAudio() call
 	gFFTSize = *(int *)userData;
 
@@ -56,8 +64,8 @@
 void render(BelaContext *context, void *userData)
 {
 	for(unsigned int n = 0; n < context->audioFrames; n++) {
-		timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioChannels] +
-															context->audioIn[n*context->audioChannels+1]) * 0.5);
+		timeDomainIn[gReadPointer].r = (ne10_float32_t) ((context->audioIn[n*context->audioInChannels] +
+															context->audioIn[n*context->audioOutChannels+1]) * 0.5);
 		timeDomainIn[gReadPointer].i = 0;
 
 		if(++gReadPointer >= gFFTSize)
@@ -74,8 +82,8 @@
 			gWritePointer = 0;
 		}
 
-		for(unsigned int channel = 0; channel < context->audioChannels; channel++)
-			context->audioOut[n * context->audioChannels + channel] = (float) timeDomainOut[gWritePointer].r;
+		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
+			context->audioOut[n * context->audioOutChannels + channel] = (float) timeDomainOut[gWritePointer].r;
 		gWritePointer++;
 	}
 }
--- a/examples/04-Audio/FFT-phase-vocoder/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/FFT-phase-vocoder/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -121,6 +121,13 @@
 // Return true on success; returning false halts the program.
 bool setup(BelaContext* context, void* userData)
 {
+    // Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+    
 	midi.readFrom(0);
 	midi.setParserCallback(midiCallback);
 	// Retrieve a parameter passed in from the initAudio() call
@@ -138,7 +145,7 @@
 	memset(gOutputBuffer, 0, BUFFER_SIZE * sizeof(float));
 
 	// Allocate buffer to mirror and modify the input
-	gInputAudio = (float *)malloc(context->audioFrames * context->audioChannels * sizeof(float));
+	gInputAudio = (float *)malloc(context->audioFrames * context->audioOutChannels * sizeof(float));
 	if(gInputAudio == 0)
 		return false;
 
@@ -232,7 +239,7 @@
 {
 	float* audioOut = context->audioOut;
 	int numAudioFrames = context->audioFrames;
-	int numAudioChannels = context->audioChannels;
+	int numAudioChannels = context->audioOutChannels;
 	// ------ this code internal to the demo; leave as is ----------------
 
 	// Prep the "input" to be the sound file played in a loop
--- a/examples/04-Audio/bucket-brigade-chorus/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/bucket-brigade-chorus/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -64,6 +64,13 @@
 
 bool setup(BelaContext *context, void *userData)
 {
+    // Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+    
 	gInverseSampleRate = 1.0/context->audioSampleRate;
 	
 	gPhase1 = 0.0;
--- a/examples/04-Audio/filter-FIR/main.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/filter-FIR/main.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -145,7 +145,7 @@
 	}
 
 	if(fileName.empty()){
-		fileName = "filter/longsample.wav";
+		fileName = "longsample.wav";
 	}
 
 	if(settings.verbose) {
--- a/examples/04-Audio/filter-FIR/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/filter-FIR/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -51,6 +51,13 @@
 bool setup(BelaContext *context, void *userData)
 {
 
+    // Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+	
 	// Retrieve a parameter passed in from the initAudio() call
 	gSampleData = *(SampleData *)userData;
 
@@ -83,8 +90,8 @@
 	ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize);
 
 	for(unsigned int n = 0; n < context->audioFrames; n++) {
-		for(unsigned int channel = 0; channel < context->audioChannels; channel++)
-				context->audioOut[n * context->audioChannels + channel] = gFIRfilterOut[n];	// ...and put it in both left and right channel
+		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
+				context->audioOut[n * context->audioOutChannels + channel] = gFIRfilterOut[n];	// ...and put it in both left and right channel
 	}
 
 
--- a/examples/04-Audio/filter-IIR/main.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/filter-IIR/main.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -151,7 +151,7 @@
 	}
 
 	if(fileName.empty()){
-		fileName = "filter/longsample.wav";
+		fileName = "longsample.wav";
 	}
 
 	if(settings.verbose) {
--- a/examples/04-Audio/filter-IIR/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/filter-IIR/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -63,6 +63,13 @@
 bool setup(BelaContext *context, void *userData)
 {
 
+	// Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+
 	// Retrieve a parameter passed in from the initAudio() call
 	gSampleData = *(SampleData *)userData;
 
@@ -97,8 +104,8 @@
 		gLastY[1] = gLastY[0];
 		gLastY[0] = out;
 
-		for(unsigned int channel = 0; channel < context->audioChannels; channel++)
-			context->audioOut[n * context->audioChannels + channel] = out;	// ...and put it in both left and right channel
+		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
+			context->audioOut[n * context->audioOutChannels + channel] = out;	// ...and put it in both left and right channel
 
 	}
 
--- a/examples/04-Audio/measure-noisefloor/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/measure-noisefloor/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -49,6 +49,14 @@
 
 bool setup(BelaContext *context, void *userData)
 {	
+
+	// Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+
 	// Clear the filter data structures
 	for(int i = 0; i < 10; i++) {
 		gReadBufferPointers[i] = gWriteBufferPointers[i] = 0;
@@ -78,10 +86,10 @@
 	
 	for(unsigned int n = 0; n < context->audioFrames; n++) {
 		// Store audio inputs in buffer
-		for(unsigned int ch = 0; ch < context->audioChannels; ch++) {
+		for(unsigned int ch = 0; ch < context->audioOutChannels; ch++) {
 			if(gWriteBufferPointers[ch] < gBufferSize) {
 				gWriteBuffers[ch][gWriteBufferPointers[ch]] = 
-					context->audioIn[n * context->audioChannels + ch];
+					context->audioIn[n * context->audioOutChannels + ch];
 				gWriteBufferPointers[ch]++;
 				if(gWriteBufferPointers[ch] >= gBufferSize)
 					bufferIsFull = true;
@@ -89,13 +97,13 @@
 		}
 	}
 	
-	if(context->analogChannels != 0) {
+	if(context->analogOutChannels != 0) {
 		for(unsigned int n = 0; n < context->analogFrames; n++) {
 			// Store analog inputs in buffer, starting at channel 2
-			for(unsigned int ch = 0; ch < context->analogChannels; ch++) {
+			for(unsigned int ch = 0; ch < context->analogOutChannels; ch++) {
 				if(gWriteBufferPointers[ch + 2] < gBufferSize) {
 					gWriteBuffers[ch + 2][gWriteBufferPointers[ch + 2]] = 
-						context->analogIn[n * context->analogChannels + ch];
+						context->analogIn[n * context->analogOutChannels + ch];
 					gWriteBufferPointers[ch + 2]++;
 					if(gWriteBufferPointers[ch + 2] >= gBufferSize)
 						bufferIsFull = true;
@@ -103,7 +111,7 @@
 				
 				// Set all analog outputs to halfway point so they can be more
 				// easily measured for noise
-				context->analogOut[n * context->analogChannels + ch] = 0.5;
+				context->analogOut[n * context->analogOutChannels + ch] = 0.5;
 			}
 		}	
 	}
--- a/examples/04-Audio/oscillator-bank/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/oscillator-bank/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -77,7 +77,7 @@
 {
 	srandom(time(NULL));
 
-	if(context->audioChannels != 2) {
+	if(context->audioOutChannels != 2) {
 		rt_printf("Error: this example needs stereo audio enabled\n");
 		return false;
 	}
--- a/examples/04-Audio/samples/main.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/samples/main.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -145,7 +145,7 @@
 	}
 
 	if(fileName.empty()){
-		fileName = "samples/sample.wav";
+		fileName = "sample.wav";
 	}
 
 	if(settings.verbose) {
--- a/examples/04-Audio/samples/render.cpp	Fri Jun 24 13:19:52 2016 +0100
+++ b/examples/04-Audio/samples/render.cpp	Fri Jun 24 13:32:07 2016 +0100
@@ -38,6 +38,13 @@
 bool setup(BelaContext *context, void *userData)
 {
 
+	// Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
+		return false;
+	}
+
 	// Retrieve a parameter passed in from the initAudio() call
 	gSampleData = *(SampleData *)userData;
 
@@ -62,8 +69,8 @@
 		if(gReadPtr >= gSampleData.sampleLen)
 			gReadPtr = -1;
 
-		for(unsigned int channel = 0; channel < context->audioChannels; channel++)
-			context->audioOut[n * context->audioChannels + channel] = out;	// ...and put it in both left and right channel
+		for(unsigned int channel = 0; channel < context->audioOutChannels; channel++)
+			context->audioOut[n * context->audioOutChannels + channel] = out;	// ...and put it in both left and right channel
 	}
 
 	// Request that the lower-priority task run at next opportunity