annotate projects/basic_network/render.cpp @ 222:6a23c07d0fbb mergingClockSync

Working with UdpIoPlugin
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 14 Feb 2016 01:09:23 +0000
parents dbff109f64c2
children ac8eb07afcf5
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@92 9 //#include <rtdk.h>
giuliomoro@24 10 #include <cmath>
giuliomoro@221 11 #include <NetworkSend.h>
giuliomoro@222 12 #include <ReceiveAudioThread.h>
giuliomoro@92 13 #include <Utilities.h>
giuliomoro@92 14
andrewm@56 15 // setup() is called once before the audio rendering starts.
giuliomoro@24 16 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@24 17 // on the period size or sample rate.
giuliomoro@24 18 //
giuliomoro@24 19 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@24 20 // in from the call to initAudio().
giuliomoro@24 21 //
giuliomoro@24 22 // Return true on success; returning false halts the program.
giuliomoro@221 23
giuliomoro@221 24 NetworkSend networkSend;
giuliomoro@222 25 ReceiveAudioThread receive;
giuliomoro@221 26 float gFrequency;
giuliomoro@221 27 float gInverseSampleRate;
giuliomoro@221 28 float gPhase;
andrewm@56 29 bool setup(BeagleRTContext *context, void *userData)
giuliomoro@24 30 {
giuliomoro@24 31 // Retrieve a parameter passed in from the initAudio() call
giuliomoro@24 32 gFrequency = *(float *)userData;
giuliomoro@24 33
giuliomoro@222 34 networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1");
giuliomoro@222 35 receive.init(10000, context->audioFrames, 0);
giuliomoro@222 36 receive.startThread();
andrewm@56 37 gInverseSampleRate = 1.0 / context->audioSampleRate;
giuliomoro@222 38 gPhase = 0;
giuliomoro@24 39 return true;
giuliomoro@24 40 }
giuliomoro@24 41
giuliomoro@24 42 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@24 43 // Input and output are given from the audio hardware and the other
giuliomoro@24 44 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@24 45 // will be 0.
giuliomoro@24 46
andrewm@56 47 void render(BeagleRTContext *context, void *userData)
giuliomoro@221 48 {
andrewm@56 49 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@24 50 float out = 0.7f * sinf(gPhase);
giuliomoro@24 51 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
giuliomoro@24 52 if(gPhase > 2.0 * M_PI)
giuliomoro@24 53 gPhase -= 2.0 * M_PI;
giuliomoro@24 54
giuliomoro@222 55 networkSend.log(out);
giuliomoro@222 56 float in;
giuliomoro@222 57 int ret = receive.getSamplesSrc(&in, 1, 1);
giuliomoro@221 58 for(unsigned int channel = 0; channel < context->audioChannels; channel++){
giuliomoro@222 59 audioWriteFrame(context, n, channel, in);
giuliomoro@24 60 }
giuliomoro@92 61 }
giuliomoro@24 62 }
giuliomoro@24 63
andrewm@56 64 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 65 // Release any resources that were allocated in setup().
giuliomoro@24 66
andrewm@56 67 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@24 68 {
giuliomoro@24 69 }