andrewm@55: /*
andrewm@55:  * render.cpp
andrewm@55:  *
andrewm@55:  *  Created on: Oct 24, 2014
andrewm@55:  *      Author: parallels
andrewm@55:  */
andrewm@55: 
andrewm@55: 
andrewm@56: #include <BeagleRT.h>
andrewm@55: #include <cmath>
andrewm@55: 
andrewm@55: #define ANALOG_LOW	(2048.0 / 65536.0)
andrewm@55: #define ANALOG_HIGH (50000.0 / 65536.0)
andrewm@55: 
andrewm@55: const int gDACPinOrder[] = {6, 4, 2, 0, 1, 3, 5, 7};
andrewm@55: 
andrewm@55: uint64_t gLastErrorFrame = 0;
andrewm@55: uint32_t gEnvelopeSampleCount = 0;
andrewm@55: float gEnvelopeValue = 0.5;
andrewm@55: float gEnvelopeDecayRate = 0.9995;
andrewm@55: 
andrewm@56: // setup() is called once before the audio rendering starts.
andrewm@55: // Use it to perform any initialisation and allocation which is dependent
andrewm@55: // on the period size or sample rate.
andrewm@55: //
andrewm@55: // userData holds an opaque pointer to a data structure that was passed
andrewm@55: // in from the call to initAudio().
andrewm@55: //
andrewm@55: // Return true on success; returning false halts the program.
andrewm@55: 
andrewm@56: bool setup(BeagleRTContext *context, void *userData)
andrewm@55: {
andrewm@55: 	return true;
andrewm@55: }
andrewm@55: 
andrewm@55: // render() is called regularly at the highest priority by the audio engine.
andrewm@55: // Input and output are given from the audio hardware and the other
andrewm@55: // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@55: // will be 0.
andrewm@55: 
andrewm@55: void render(BeagleRTContext *context, void *userData)
andrewm@55: {
andrewm@55: 	static float phase = 0.0;
andrewm@55: 	static int sampleCounter = 0;
andrewm@55: 	static int invertChannel = 0;
andrewm@55: 	float frequency = 0;
andrewm@55: 
andrewm@55: 	// Play a sine wave on the audio output
andrewm@55: 	for(unsigned int n = 0; n < context->audioFrames; n++) {
andrewm@55: 		context->audioOut[2*n] = context->audioOut[2*n + 1] = gEnvelopeValue * sinf(phase);
andrewm@55: 
andrewm@55: 		// If one second has gone by with no error, play one sound, else
andrewm@55: 		// play another
andrewm@55: 		if(context->audioSampleCount + n - gLastErrorFrame > 44100) {
andrewm@55: 			gEnvelopeValue *= gEnvelopeDecayRate;
andrewm@55: 			gEnvelopeSampleCount++;
andrewm@55: 			if(gEnvelopeSampleCount > 22050) {
andrewm@55: 				gEnvelopeValue = 0.5;
andrewm@55: 				gEnvelopeSampleCount = 0;
andrewm@55: 			}
andrewm@55: 			frequency = 880.0;
andrewm@55: 		}
andrewm@55: 		else {
andrewm@55: 			gEnvelopeValue = 0.5;
andrewm@55: 			frequency = 220.0;
andrewm@55: 		}
andrewm@55: 
andrewm@55: 		phase += 2.0 * M_PI * frequency / 44100.0;
andrewm@55: 		if(phase >= 2.0 * M_PI)
andrewm@55: 			phase -= 2.0 * M_PI;
andrewm@55: 	}
andrewm@55: 
andrewm@55: 	for(unsigned int n = 0; n < context->analogFrames; n++) {
andrewm@55: 		// Change outputs every 512 samples
andrewm@55: 		if(sampleCounter < 512) {
andrewm@55: 			for(int k = 0; k < 8; k++) {
andrewm@55: 				if(k == invertChannel)
andrewm@55: 					context->analogOut[n*8 + gDACPinOrder[k]] = ANALOG_HIGH;
andrewm@55: 				else
andrewm@55: 					context->analogOut[n*8 + gDACPinOrder[k]] = 0;
andrewm@55: 			}
andrewm@55: 		}
andrewm@55: 		else {
andrewm@55: 			for(int k = 0; k < 8; k++) {
andrewm@55: 				if(k == invertChannel)
andrewm@55: 					context->analogOut[n*8 + gDACPinOrder[k]] = 0;
andrewm@55: 				else
andrewm@55: 					context->analogOut[n*8 + gDACPinOrder[k]] = ANALOG_HIGH;
andrewm@55: 			}
andrewm@55: 		}
andrewm@55: 
andrewm@55: 		// Read after 256 samples: input should be low
andrewm@55: 		if(sampleCounter == 256) {
andrewm@55: 			for(int k = 0; k < 8; k++) {
andrewm@55: 				if(k == invertChannel) {
andrewm@55: 					if(context->analogIn[n*8 + k] < ANALOG_HIGH) {
andrewm@55: 						rt_printf("FAIL [output %d, input %d] -- output HIGH input %f (inverted)\n", gDACPinOrder[k], k, context->analogIn[n*8 + k]);
andrewm@55: 						gLastErrorFrame = context->audioSampleCount + n;
andrewm@55: 					}
andrewm@55: 				}
andrewm@55: 				else {
andrewm@55: 					if(context->analogIn[n*8 + k] > ANALOG_LOW) {
andrewm@55: 						rt_printf("FAIL [output %d, input %d] -- output LOW --> input %f\n", gDACPinOrder[k], k, context->analogIn[n*8 + k]);
andrewm@55: 						gLastErrorFrame = context->audioSampleCount + n;
andrewm@55: 					}
andrewm@55: 				}
andrewm@55: 			}
andrewm@55: 		}
andrewm@55: 		else if(sampleCounter == 768) {
andrewm@55: 			for(int k = 0; k < 8; k++) {
andrewm@55: 				if(k == invertChannel) {
andrewm@55: 					if(context->analogIn[n*8 + k] > ANALOG_LOW) {
andrewm@55: 						rt_printf("FAIL [output %d, input %d] -- output LOW input %f (inverted)\n", gDACPinOrder[k], k, context->analogIn[n*8 + k]);
andrewm@55: 						gLastErrorFrame = context->audioSampleCount + n;
andrewm@55: 					}
andrewm@55: 				}
andrewm@55: 				else {
andrewm@55: 					if(context->analogIn[n*8 + k] < ANALOG_HIGH) {
andrewm@55: 						rt_printf("FAIL [output %d, input %d] -- output HIGH input %f\n", gDACPinOrder[k], k, context->analogIn[n*8 + k]);
andrewm@55: 						gLastErrorFrame = context->audioSampleCount + n;
andrewm@55: 					}
andrewm@55: 				}
andrewm@55: 			}
andrewm@55: 		}
andrewm@55: 
andrewm@55: 		if(++sampleCounter >= 1024) {
andrewm@55: 			sampleCounter = 0;
andrewm@55: 			invertChannel++;
andrewm@55: 			if(invertChannel >= 8)
andrewm@55: 				invertChannel = 0;
andrewm@55: 		}
andrewm@55: 	}
andrewm@55: }
andrewm@55: 
andrewm@56: // cleanup() is called once at the end, after the audio has stopped.
andrewm@56: // Release any resources that were allocated in setup().
andrewm@55: 
andrewm@56: void cleanup(BeagleRTContext *context, void *userData)
andrewm@55: {
andrewm@55: 
andrewm@55: }