diff projects/oscillator_bank/render.cpp @ 108:3068421c0737 ultra-staging

Merged default into ultra-staging
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 18 Aug 2015 00:35:15 +0100
parents 3c3a1357657d
children ac8eb07afcf5
line wrap: on
line diff
--- a/projects/oscillator_bank/render.cpp	Mon Jun 08 01:07:48 2015 +0100
+++ b/projects/oscillator_bank/render.cpp	Tue Aug 18 00:35:15 2015 +0100
@@ -6,8 +6,8 @@
  */
 
 
-#include "../../include/RTAudio.h"
-#include "../../include/Utilities.h"
+#include <BeagleRT.h>
+#include <Utilities.h>
 #include <rtdk.h>
 #include <cstdlib>
 #include <cmath>
@@ -34,7 +34,7 @@
 
 // These settings are carried over from main.cpp
 // Setting global variables is an alternative approach
-// to passing a structure to userData in initialise_render()
+// to passing a structure to userData in setup()
 
 extern int gNumOscillators;
 extern int gWavetableLength;
@@ -50,7 +50,7 @@
 							  float *lookupTable);
 }
 
-// initialise_render() is called once before the audio rendering starts.
+// setup() is called once before the audio rendering starts.
 // Use it to perform any initialisation and allocation which is dependent
 // on the period size or sample rate.
 //
@@ -58,15 +58,11 @@
 // in from the call to initAudio().
 //
 // Return true on success; returning false halts the program.
-bool initialise_render(int numMatrixChannels, int numAudioChannels,
-					   int numMatrixFramesPerPeriod,
-					   int numAudioFramesPerPeriod,
-					   float matrixSampleRate, float audioSampleRate,
-					   void *userData)
+bool setup(BeagleRTContext *context, void *userData)
 {
 	srandom(time(NULL));
 
-	if(numAudioChannels != 2) {
+	if(context->audioChannels != 2) {
 		rt_printf("Error: this example needs stereo audio enabled\n");
 		return false;
 	}
@@ -109,7 +105,7 @@
 	for(int n = 0; n < gNumOscillators; n++) {
 		gPhases[n] = 0.0;
 
-		if(numMatrixFramesPerPeriod == 0) {
+		if(context->analogFrames == 0) {
 			// Random frequencies when used without matrix
 			gFrequencies[n] = kMinimumFrequency + (kMaximumFrequency - kMinimumFrequency) * ((float)random() / (float)RAND_MAX);
 		}
@@ -120,16 +116,34 @@
 		}
 
 		// For efficiency, frequency is expressed in change in wavetable position per sample, not Hz or radians
-		gFrequencies[n] *= (float)gWavetableLength / audioSampleRate;
+		gFrequencies[n] *= (float)gWavetableLength / context->audioSampleRate;
 		gAmplitudes[n] = ((float)random() / (float)RAND_MAX) / (float)gNumOscillators;
 		gDFrequencies[n] = gDAmplitudes[n] = 0.0;
 	}
 
+	increment = 0;
+	freq = 440.0;
+
+	for(int n = 0; n < gNumOscillators; n++) {
+		// Update the frequencies to a regular spread, plus a small amount of randomness
+		// to avoid weird phase effects
+		float randScale = 0.99 + .02 * (float)random() / (float)RAND_MAX;
+		float newFreq = freq * randScale;
+
+		// For efficiency, frequency is expressed in change in wavetable position per sample, not Hz or radians
+		gFrequencies[n] = newFreq * (float)gWavetableLength / context->audioSampleRate;
+
+		freq += increment;
+	}
+
 	// Initialise auxiliary tasks
-	if((gFrequencyUpdateTask = createAuxiliaryTaskLoop(&recalculate_frequencies, 90, "beaglert-update-frequencies")) == 0)
+	if((gFrequencyUpdateTask = BeagleRT_createAuxiliaryTask(&recalculate_frequencies, 85, "beaglert-update-frequencies")) == 0)
 		return false;
 
-	gAudioSampleRate = audioSampleRate;
+	//for(int n = 0; n < gNumOscillators; n++)
+	//	rt_printf("%f\n", gFrequencies[n]);
+
+	gAudioSampleRate = context->audioSampleRate;
 	gSampleCount = 0;
 
 	return true;
@@ -140,23 +154,22 @@
 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
 // will be 0.
 
-void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
-			uint16_t *matrixIn, uint16_t *matrixOut)
+void render(BeagleRTContext *context, void *userData)
 {
 	// Initialise buffer to 0
-	memset(audioOut, 0, 2 * numAudioFrames * sizeof(float));
+	memset(context->audioOut, 0, 2 * context->audioFrames * sizeof(float));
 
 	// Render audio frames
-	oscillator_bank_neon(numAudioFrames, audioOut,
+	oscillator_bank_neon(context->audioFrames, context->audioOut,
 			gNumOscillators, gWavetableLength,
 			gPhases, gFrequencies, gAmplitudes,
 			gDFrequencies, gDAmplitudes,
 			gWavetable);
 
-	if(numMatrixFrames != 0 && (gSampleCount += numAudioFrames) >= 128) {
+	if(context->analogFrames != 0 && (gSampleCount += context->audioFrames) >= 128) {
 		gSampleCount = 0;
-		gNewMinFrequency = map(matrixIn[0], 0, MATRIX_MAX, 20.0f, 8000.0f);
-		gNewMaxFrequency = map(matrixIn[1], 0, MATRIX_MAX, 20.0f, 8000.0f);
+		gNewMinFrequency = map(context->analogIn[0], 0, 1.0, 1000.0f, 8000.0f);
+		gNewMaxFrequency = map(context->analogIn[1], 0, 1.0, 1000.0f, 8000.0f);
 
 		// Make sure max >= min
 		if(gNewMaxFrequency < gNewMinFrequency) {
@@ -166,7 +179,7 @@
 		}
 
 		// Request that the lower-priority task run at next opportunity
-		scheduleAuxiliaryTask(gFrequencyUpdateTask);
+		//BeagleRT_scheduleAuxiliaryTask(gFrequencyUpdateTask);
 	}
 }
 
@@ -194,10 +207,10 @@
 }
 
 
-// cleanup_render() is called once at the end, after the audio has stopped.
-// Release any resources that were allocated in initialise_render().
+// cleanup() is called once at the end, after the audio has stopped.
+// Release any resources that were allocated in setup().
 
-void cleanup_render()
+void cleanup(BeagleRTContext *context, void *userData)
 {
 	free(gWavetable);
 	free(gPhases);