annotate projects/samples/render.cpp @ 52:a6d223473ea2 newapi

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