annotate examples/basic_network/render.cpp @ 372:db2fe4e1b88e prerelease

Doxygen content added to each example render.cpp. References to AnalogReadFrame etc. removed from doxygen content.
author Robert Jack <robert.h.jack@gmail.com>
date Thu, 09 Jun 2016 18:16:05 +0100
parents 1feb9c23ac57
children 9dc5a0ccad25
rev   line source
robert@372 1 /*
robert@372 2 ____ _____ _ _
robert@372 3 | __ )| ____| | / \
robert@372 4 | _ \| _| | | / _ \
robert@372 5 | |_) | |___| |___ / ___ \
robert@372 6 |____/|_____|_____/_/ \_\.io
robert@372 7
robert@372 8 */
robert@372 9
giuliomoro@24 10 /*
giuliomoro@24 11 * render.cpp
giuliomoro@24 12 *
giuliomoro@24 13 * Created on: Oct 24, 2014
giuliomoro@24 14 * Author: parallels
giuliomoro@24 15 */
giuliomoro@24 16
robert@372 17 /**
robert@372 18 \example 5_basic_network
robert@372 19
robert@372 20 Networking
robert@372 21 ----------
robert@372 22
robert@372 23 This sketch allows you to send audio and sensor data over UDP to a
robert@372 24 DAW on the host. The host needs to run Udpioplugin which you can f
robert@372 25 ind [here](https://code.soundsoftware.ac.uk/projects/udpioplugin).
robert@372 26
robert@372 27 Note that this sketch and the accompanying plugin are still in testing.
robert@372 28 */
robert@372 29
giuliomoro@301 30 #include <Bela.h>
giuliomoro@92 31 //#include <rtdk.h>
giuliomoro@24 32 #include <cmath>
giuliomoro@221 33 #include <NetworkSend.h>
giuliomoro@222 34 #include <ReceiveAudioThread.h>
giuliomoro@92 35 #include <Utilities.h>
giuliomoro@92 36
andrewm@56 37 // setup() is called once before the audio rendering starts.
giuliomoro@24 38 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@24 39 // on the period size or sample rate.
giuliomoro@24 40 //
giuliomoro@24 41 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@24 42 // in from the call to initAudio().
giuliomoro@24 43 //
giuliomoro@24 44 // Return true on success; returning false halts the program.
giuliomoro@221 45
giuliomoro@221 46 NetworkSend networkSend;
giuliomoro@222 47 ReceiveAudioThread receive;
giuliomoro@221 48 float gFrequency;
giuliomoro@221 49 float gInverseSampleRate;
giuliomoro@221 50 float gPhase;
giuliomoro@301 51 bool setup(BelaContext *context, void *userData)
giuliomoro@24 52 {
giuliomoro@24 53 // Retrieve a parameter passed in from the initAudio() call
giuliomoro@24 54 gFrequency = *(float *)userData;
giuliomoro@24 55
giuliomoro@222 56 networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1");
giuliomoro@222 57 receive.init(10000, context->audioFrames, 0);
giuliomoro@222 58 receive.startThread();
andrewm@56 59 gInverseSampleRate = 1.0 / context->audioSampleRate;
giuliomoro@222 60 gPhase = 0;
giuliomoro@24 61 return true;
giuliomoro@24 62 }
giuliomoro@24 63
giuliomoro@24 64 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@24 65 // Input and output are given from the audio hardware and the other
giuliomoro@24 66 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@24 67 // will be 0.
giuliomoro@24 68
giuliomoro@301 69 void render(BelaContext *context, void *userData)
giuliomoro@221 70 {
andrewm@56 71 for(unsigned int n = 0; n < context->audioFrames; n++) {
giuliomoro@24 72 float out = 0.7f * sinf(gPhase);
giuliomoro@24 73 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
giuliomoro@24 74 if(gPhase > 2.0 * M_PI)
giuliomoro@24 75 gPhase -= 2.0 * M_PI;
giuliomoro@24 76
giuliomoro@222 77 networkSend.log(out);
giuliomoro@222 78 float in;
giuliomoro@222 79 int ret = receive.getSamplesSrc(&in, 1, 1);
giuliomoro@221 80 for(unsigned int channel = 0; channel < context->audioChannels; channel++){
andrewm@308 81 audioWrite(context, n, channel, in);
giuliomoro@24 82 }
giuliomoro@92 83 }
giuliomoro@24 84 }
giuliomoro@24 85
andrewm@56 86 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 87 // Release any resources that were allocated in setup().
giuliomoro@24 88
giuliomoro@301 89 void cleanup(BelaContext *context, void *userData)
giuliomoro@24 90 {
giuliomoro@24 91 }