annotate projects/basic_network/render.cpp @ 82:d6e5f7ca4958

merge
author andrewm
date Fri, 17 Jul 2015 22:17:25 +0100
parents 3c3a1357657d
children 7cc79d46ecb9
rev   line source
giuliomoro@24 1 /*
giuliomoro@24 2 * render.cpp
giuliomoro@24 3 *
giuliomoro@24 4 * Created on: Oct 24, 2014
giuliomoro@24 5 * Author: parallels
giuliomoro@24 6 */
giuliomoro@24 7
andrewm@56 8 #include <BeagleRT.h>
giuliomoro@24 9 #include <cmath>
andrewm@56 10 #include <client.h>
andrewm@56 11
giuliomoro@24 12 float gFrequency;
giuliomoro@24 13 float gPhase;
giuliomoro@24 14 float gInverseSampleRate;
giuliomoro@24 15 int gCount=0;
giuliomoro@24 16 networkData networkObject;
giuliomoro@24 17 AuxiliaryTask transmitReceiveDataTask;
giuliomoro@24 18
giuliomoro@24 19 void transmitReceiveData(){
giuliomoro@25 20 printf("transmitReceiveData auxiliary task has started\n");
giuliomoro@24 21 while(!gShouldStop){
giuliomoro@25 22 sendMessage(networkObject);
giuliomoro@25 23 receiveMessage(networkObject);
giuliomoro@25 24 usleep(1000);
giuliomoro@24 25 }
giuliomoro@24 26 closeSockets();
giuliomoro@24 27 }
giuliomoro@24 28
andrewm@56 29 // setup() is called once before the audio rendering starts.
giuliomoro@24 30 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@24 31 // on the period size or sample rate.
giuliomoro@24 32 //
giuliomoro@24 33 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@24 34 // in from the call to initAudio().
giuliomoro@24 35 //
giuliomoro@24 36 // Return true on success; returning false halts the program.
andrewm@56 37 bool setup(BeagleRTContext *context, void *userData)
giuliomoro@24 38 {
giuliomoro@24 39 // Retrieve a parameter passed in from the initAudio() call
giuliomoro@24 40 gFrequency = *(float *)userData;
giuliomoro@24 41
andrewm@56 42 gInverseSampleRate = 1.0 / context->audioSampleRate;
giuliomoro@24 43 gPhase = 0.0;
giuliomoro@24 44
giuliomoro@24 45 networkObject.counter=&gCount;
giuliomoro@24 46 networkObject.variables[0]=&gFrequency;
giuliomoro@24 47 networkObject.variables[1]=&gPhase;
giuliomoro@24 48 networkObject.numVariables=2;
andrewm@56 49
giuliomoro@24 50 setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
andrewm@56 51 transmitReceiveDataTask= BeagleRT_createAuxiliaryTask(*transmitReceiveData, 80, "transmit-receive-data");
giuliomoro@24 52 //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
giuliomoro@24 53 return true;
giuliomoro@24 54 }
giuliomoro@24 55
giuliomoro@24 56 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@24 57 // Input and output are given from the audio hardware and the other
giuliomoro@24 58 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@24 59 // will be 0.
giuliomoro@24 60
andrewm@56 61 void render(BeagleRTContext *context, void *userData)
giuliomoro@24 62 {
andrewm@56 63 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@24 64 float out = 0.7f * sinf(gPhase);
giuliomoro@24 65 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
giuliomoro@24 66 if(gPhase > 2.0 * M_PI)
giuliomoro@24 67 gPhase -= 2.0 * M_PI;
giuliomoro@24 68
andrewm@56 69 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
andrewm@56 70 context->audioOut[n * context->audioChannels + channel] = out;
andrewm@56 71
andrewm@56 72 if(gCount == 0){
andrewm@56 73 BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask);
giuliomoro@24 74 }
giuliomoro@24 75 gCount++;
giuliomoro@24 76 }
giuliomoro@24 77 }
giuliomoro@24 78
andrewm@56 79 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 80 // Release any resources that were allocated in setup().
giuliomoro@24 81
andrewm@56 82 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@24 83 {
giuliomoro@24 84 }