annotate projects/samples/render.cpp @ 151:e9c9404e3d1f ClockSync

Pff partially working. No PID. When setting the audio clock on the bbb to 44098 the master and slave clock keep diverging instead of converging ...
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 22 Sep 2015 04:10:07 +0100
parents 3c3a1357657d
children ac8eb07afcf5
rev   line source
victor@1 1 /*
victor@1 2 * render.cpp
victor@1 3 *
victor@1 4 * Created on: Oct 24, 2014
victor@1 5 * Author: Andrew McPherson and Victor Zappi
victor@1 6 */
victor@1 7
victor@1 8
andrewm@56 9 #include <BeagleRT.h>
victor@1 10 #include <cmath>
victor@1 11 #include "SampleData.h"
victor@1 12
victor@1 13 SampleData gSampleData; // User defined structure to get complex data from main
victor@1 14 int gReadPtr; // Position of last read sample from file
victor@1 15
victor@1 16 // Task for handling the update of the frequencies using the matrix
victor@1 17 AuxiliaryTask gTriggerSamplesTask;
victor@1 18
victor@1 19 bool initialise_trigger();
victor@1 20 void trigger_samples();
victor@1 21
andrewm@56 22 // setup() is called once before the audio rendering starts.
victor@1 23 // Use it to perform any initialisation and allocation which is dependent
victor@1 24 // on the period size or sample rate.
victor@1 25 //
victor@1 26 // userData holds an opaque pointer to a data structure that was passed
victor@1 27 // in from the call to initAudio().
victor@1 28 //
victor@1 29 // Return true on success; returning false halts the program.
victor@1 30
andrewm@56 31 bool setup(BeagleRTContext *context, void *userData)
victor@1 32 {
victor@1 33
victor@1 34 // Retrieve a parameter passed in from the initAudio() call
victor@1 35 gSampleData = *(SampleData *)userData;
victor@1 36
victor@1 37 gReadPtr = -1;
victor@1 38
victor@1 39 // Initialise auxiliary tasks
victor@1 40 if(!initialise_trigger())
victor@1 41 return false;
victor@1 42
victor@1 43 return true;
victor@1 44 }
victor@1 45
victor@1 46 // render() is called regularly at the highest priority by the audio engine.
victor@1 47 // Input and output are given from the audio hardware and the other
victor@1 48 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
victor@1 49 // will be 0.
victor@1 50
andrewm@52 51 void render(BeagleRTContext *context, void *userData)
victor@1 52 {
andrewm@56 53 for(unsigned int n = 0; n < context->audioFrames; n++) {
victor@1 54 float out = 0;
victor@1 55
victor@1 56 // If triggered...
victor@1 57 if(gReadPtr != -1)
victor@1 58 out += gSampleData.samples[gReadPtr++]; // ...read each sample...
victor@1 59
victor@1 60 if(gReadPtr >= gSampleData.sampleLen)
victor@1 61 gReadPtr = -1;
victor@1 62
andrewm@56 63 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
andrewm@52 64 context->audioOut[n * context->audioChannels + channel] = out; // ...and put it in both left and right channel
victor@1 65 }
victor@1 66
victor@1 67 // Request that the lower-priority task run at next opportunity
andrewm@52 68 BeagleRT_scheduleAuxiliaryTask(gTriggerSamplesTask);
victor@1 69 }
victor@1 70
victor@1 71 // Initialise the auxiliary task
victor@1 72 // and print info
victor@1 73
victor@1 74 bool initialise_trigger()
victor@1 75 {
andrewm@52 76 if((gTriggerSamplesTask = BeagleRT_createAuxiliaryTask(&trigger_samples, 50, "beaglert-trigger-samples")) == 0)
victor@1 77 return false;
victor@1 78
victor@1 79 rt_printf("Press 'a' to trigger sample, 's' to stop\n");
victor@1 80 rt_printf("Press 'q' to quit\n");
victor@1 81
victor@1 82 return true;
victor@1 83 }
victor@1 84
victor@1 85 // This is a lower-priority call to periodically read keyboard input
victor@1 86 // and trigger samples. By placing it at a lower priority,
victor@1 87 // it has minimal effect on the audio performance but it will take longer to
victor@1 88 // complete if the system is under heavy audio load.
victor@1 89
victor@1 90 void trigger_samples()
victor@1 91 {
victor@1 92 // This is not a real-time task!
victor@1 93 // Cos getchar is a system call, not handled by Xenomai.
victor@1 94 // This task will be automatically down graded.
victor@1 95
victor@1 96 char keyStroke = '.';
victor@1 97
victor@1 98 keyStroke = getchar();
victor@1 99 while(getchar()!='\n'); // to read the first stroke
victor@1 100
victor@1 101 switch (keyStroke)
victor@1 102 {
victor@1 103 case 'a':
victor@1 104 gReadPtr = 0;
victor@1 105 break;
victor@1 106 case 's':
victor@1 107 gReadPtr = -1;
victor@1 108 break;
victor@1 109 case 'q':
victor@1 110 gShouldStop = true;
victor@1 111 break;
victor@1 112 default:
victor@1 113 break;
victor@1 114 }
victor@1 115 }
victor@1 116
victor@1 117
victor@1 118
andrewm@56 119 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 120 // Release any resources that were allocated in setup().
victor@1 121
andrewm@56 122 void cleanup(BeagleRTContext *context, void *userData)
victor@1 123 {
victor@1 124 delete[] gSampleData.samples;
victor@1 125 }