diff projects/samples/render.cpp @ 1:24fc8026ae8e

_new sample playback example
author Victor Zappi <victor.zappi@qmul.ac.uk>
date Thu, 06 Nov 2014 14:23:26 +0000
parents
children 06f93bef7dd2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/projects/samples/render.cpp	Thu Nov 06 14:23:26 2014 +0000
@@ -0,0 +1,133 @@
+/*
+ * render.cpp
+ *
+ *  Created on: Oct 24, 2014
+ *      Author: Andrew McPherson and Victor Zappi
+ */
+
+
+#include "../../include/render.h"
+#include "../../include/RTAudio.h"	// to schedule lower prio parallel process
+#include <rtdk.h>
+#include <cmath>
+#include <stdio.h>
+#include "SampleData.h"
+
+SampleData gSampleData;	// User defined structure to get complex data from main
+int gReadPtr;			// Position of last read sample from file
+int gNumChannels;
+
+// Task for handling the update of the frequencies using the matrix
+AuxiliaryTask gTriggerSamplesTask;
+
+bool initialise_trigger();
+void trigger_samples();
+
+// initialise_render() 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.
+//
+// userData holds an opaque pointer to a data structure that was passed
+// in from the call to initAudio().
+//
+// Return true on success; returning false halts the program.
+
+bool initialise_render(int numChannels, int numMatrixFramesPerPeriod,
+					   int numAudioFramesPerPeriod, float matrixSampleRate,
+					   float audioSampleRate, void *userData)
+{
+
+	// Retrieve a parameter passed in from the initAudio() call
+	gSampleData = *(SampleData *)userData;
+
+	gReadPtr = -1;
+	gNumChannels = numChannels;
+
+	// Initialise auxiliary tasks
+	if(!initialise_trigger())
+		return false;
+
+	return true;
+}
+
+// render() is called regularly at the highest priority by the audio engine.
+// Input and output are given from the audio hardware and the other
+// 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)
+{
+	for(int n = 0; n < numAudioFrames; n++) {
+		float out = 0;
+
+		// If triggered...
+		if(gReadPtr != -1)
+			out += gSampleData.samples[gReadPtr++];	// ...read each sample...
+
+		if(gReadPtr >= gSampleData.sampleLen)
+			gReadPtr = -1;
+
+		for(int channel = 0; channel < gNumChannels; channel++)
+			audioOut[n * gNumChannels + channel] = out;	// ...and put it in both left and right channel
+	}
+
+	// Request that the lower-priority task run at next opportunity
+	scheduleAuxiliaryTask(gTriggerSamplesTask);
+}
+
+// Initialise the auxiliary task
+// and print info
+
+bool initialise_trigger()
+{
+	if((gTriggerSamplesTask = createAuxiliaryTaskLoop(&trigger_samples, 50, "beaglert-trigger-samples")) == 0)
+		return false;
+
+	rt_printf("Press 'a' to trigger sample, 's' to stop\n");
+	rt_printf("Press 'q' to quit\n");
+
+	return true;
+}
+
+// This is a lower-priority call to periodically read keyboard input
+// and trigger samples. By placing it at a lower priority,
+// it has minimal effect on the audio performance but it will take longer to
+// complete if the system is under heavy audio load.
+
+void trigger_samples()
+{
+	// This is not a real-time task!
+	// Cos getchar is a system call, not handled by Xenomai.
+	// This task will be automatically down graded.
+
+	char keyStroke = '.';
+
+	keyStroke =	getchar();
+	while(getchar()!='\n'); // to read the first stroke
+
+	switch (keyStroke)
+	{
+		case 'a':
+			gReadPtr = 0;
+			break;
+		case 's':
+			gReadPtr = -1;
+			break;
+		case 'q':
+			gShouldStop = true;
+			break;
+		default:
+			break;
+	}
+}
+
+
+
+// cleanup_render() is called once at the end, after the audio has stopped.
+// Release any resources that were allocated in initialise_render().
+
+void cleanup_render()
+{
+	delete[] gSampleData.samples;
+}