annotate projects/basic_network/render.cpp @ 41:4255ecbb9bec ultra-staging

Timers to measure performances, ultra experimental
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 May 2015 16:41:07 +0100
parents 98aed580452a
children d3f869b98147
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
giuliomoro@24 8 #include "../../include/RTAudioSettings.h"
giuliomoro@24 9 #include "../../include/render.h"
giuliomoro@24 10 #include <cmath>
giuliomoro@24 11 #include "../../include/client.h"
giuliomoro@24 12 #include "../../include/RTAudio.h" // to schedule lower prio parallel process
giuliomoro@24 13 #include <rtdk.h>
giuliomoro@41 14 #include <native/timer.h>
giuliomoro@41 15 #include "../../include/PRU.h"
giuliomoro@24 16 float gFrequency;
giuliomoro@24 17 float gPhase;
giuliomoro@24 18 float gInverseSampleRate;
giuliomoro@24 19 int gCount=0;
giuliomoro@24 20 networkData networkObject;
giuliomoro@41 21 networkAudio netAudio;
giuliomoro@41 22 extern PRU *gPRU;
giuliomoro@41 23 AuxiliaryTask printIntervalTask;
giuliomoro@41 24 AuxiliaryTask transmitReceiveAudioTask;
giuliomoro@41 25 void transmitReceiveData(){ //transmit and receive asynchronous messages
giuliomoro@41 26 // printf("transmitReceiveData auxiliary task has started\n");
giuliomoro@41 27 // while(!gShouldStop){
giuliomoro@41 28 // sendMessage(&networkObject);
giuliomoro@41 29 // receiveMessage(networkObject);
giuliomoro@41 30 // usleep(1000);
giuliomoro@41 31 // }
giuliomoro@41 32 // closeSockets();
giuliomoro@41 33 }
giuliomoro@24 34
giuliomoro@41 35 void transmitReceiveAudio(){ //transmit and receive audio buffers
giuliomoro@41 36 sendAudio(&netAudio);
giuliomoro@24 37 }
giuliomoro@24 38
giuliomoro@41 39
giuliomoro@24 40 // initialise_render() is called once before the audio rendering starts.
giuliomoro@24 41 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@24 42 // on the period size or sample rate.
giuliomoro@24 43 //
giuliomoro@24 44 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@24 45 // in from the call to initAudio().
giuliomoro@24 46 //
giuliomoro@24 47 // Return true on success; returning false halts the program.
giuliomoro@24 48 bool initialise_render(int numMatrixChannels, int numDigitalChannels, int numAudioChannels,
giuliomoro@24 49 int numMatrixFramesPerPeriod,
giuliomoro@24 50 int numAudioFramesPerPeriod,
giuliomoro@24 51 float matrixSampleRate, float audioSampleRate,
giuliomoro@24 52 void *userData, RTAudioSettings *settings)
giuliomoro@24 53 {
giuliomoro@24 54 // Retrieve a parameter passed in from the initAudio() call
giuliomoro@24 55 gFrequency = *(float *)userData;
giuliomoro@24 56
giuliomoro@24 57 gInverseSampleRate = 1.0 / audioSampleRate;
giuliomoro@24 58 gPhase = 0.0;
giuliomoro@24 59
giuliomoro@41 60 // networkObject.counter=&gCount;
giuliomoro@41 61 // networkObject.variables[0]=&gFrequency;
giuliomoro@41 62 // networkObject.variables[1]=&gPhase;
giuliomoro@41 63 // networkObject.numVariables=2;
giuliomoro@41 64 // netAudio.doneOnTime=1;
giuliomoro@41 65 // netAudio.index=0;
giuliomoro@41 66 // netAudio.currentBuffer=0;
giuliomoro@41 67 // setupSockets(settings->receivePort, settings->transmitPort, settings->serverName);
giuliomoro@41 68 //// transmitReceiveDataTask=createAuxiliaryTaskLoop(*transmitReceiveData, 10, "transmit-receive-data");
giuliomoro@41 69 //// scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work
giuliomoro@41 70 // transmitReceiveAudioTask=createAuxiliaryTaskLoop(*transmitReceiveAudio, 98, "transmit-receive-audio");
giuliomoro@24 71 return true;
giuliomoro@24 72 }
giuliomoro@24 73
giuliomoro@24 74 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@24 75 // Input and output are given from the audio hardware and the other
giuliomoro@24 76 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@24 77 // will be 0.
giuliomoro@24 78
giuliomoro@24 79 void render(int numAnalogFrames, int numAudioFrames, int numDigitalFrames, float *audioIn, float *audioOut,
giuliomoro@24 80 float *analogIn, float *analogOut, uint32_t *digital)
giuliomoro@24 81 {
giuliomoro@24 82 for(int n = 0; n < numAudioFrames; n++) {
giuliomoro@24 83 float out = 0.7f * sinf(gPhase);
giuliomoro@41 84 float fake=0.1;
giuliomoro@41 85 for(int a=0; a<24; a++){
giuliomoro@41 86 fake = 0.7f * sinf(fake+out);
giuliomoro@41 87 }
giuliomoro@41 88 fake/=1000000000000000;
giuliomoro@24 89 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
giuliomoro@24 90 if(gPhase > 2.0 * M_PI)
giuliomoro@24 91 gPhase -= 2.0 * M_PI;
giuliomoro@24 92
giuliomoro@41 93 // for(int channel = 0; channel < gNumAudioChannels; channel++)
giuliomoro@41 94 // audioOut[n * gNumAudioChannels + channel] = audioIn[n * gNumAudioChannels + 0]+audioIn[n * gNumAudioChannels + 1];
giuliomoro@41 95 audioOut[n * gNumAudioChannels] = fake*0.0000000001;
giuliomoro@41 96 audioOut[n * gNumAudioChannels+1]=out;
giuliomoro@41 97 if(0==gCount){
giuliomoro@41 98 // scheduleAuxiliaryTask(transmitReceiveDataTask);
giuliomoro@24 99 }
giuliomoro@41 100 // if(netAudio.index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ...
giuliomoro@41 101 // netAudio.index=0; //reset the counter
giuliomoro@41 102 // if(netAudio.doneOnTime==0)
giuliomoro@41 103 // rt_printf("Network buffer underrun :-{\n");
giuliomoro@41 104 // netAudio.timestamp=gCount;
giuliomoro@41 105 // netAudio.currentBuffer=!netAudio.currentBuffer; //switch buffer
giuliomoro@41 106 // netAudio.doneOnTime=0;
giuliomoro@41 107 // scheduleAuxiliaryTask(transmitReceiveAudioTask); //send the buffer
giuliomoro@41 108 // }
giuliomoro@41 109 // netAudio.buffers[netAudio.currentBuffer][netAudio.index++]=audioOut[n*gNumAudioChannels + 0];//copy channel 0 to the buffer
giuliomoro@24 110 gCount++;
giuliomoro@24 111 }
giuliomoro@24 112 }
giuliomoro@24 113
giuliomoro@24 114 // cleanup_render() is called once at the end, after the audio has stopped.
giuliomoro@24 115 // Release any resources that were allocated in initialise_render().
giuliomoro@24 116
giuliomoro@24 117 void cleanup_render()
giuliomoro@24 118 {
giuliomoro@41 119 // closeSockets();
giuliomoro@24 120 }