robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: //#include robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: // setup() is called once before the audio rendering starts. robert@464: // Use it to perform any initialisation and allocation which is dependent robert@464: // on the period size or sample rate. robert@464: // robert@464: // userData holds an opaque pointer to a data structure that was passed robert@464: // in from the call to initAudio(). robert@464: // robert@464: // Return true on success; returning false halts the program. robert@464: robert@464: NetworkSend networkSend; robert@464: ReceiveAudioThread receive; robert@464: float gFrequency; robert@464: float gInverseSampleRate; robert@464: float gPhase; robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: // Retrieve a parameter passed in from the initAudio() call robert@464: gFrequency = *(float *)userData; robert@464: robert@464: networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); robert@464: receive.init(10000, context->audioFrames, 0); robert@464: receive.startThread(); robert@464: gInverseSampleRate = 1.0 / context->audioSampleRate; robert@464: gPhase = 0; robert@464: return true; robert@464: } robert@464: robert@464: // render() is called regularly at the highest priority by the audio engine. robert@464: // Input and output are given from the audio hardware and the other robert@464: // ADCs and DACs (if available). If only audio is available, numMatrixFrames robert@464: // will be 0. robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: float out = 0.7f * sinf(gPhase); robert@464: gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; robert@464: if(gPhase > 2.0 * M_PI) robert@464: gPhase -= 2.0 * M_PI; robert@464: robert@464: networkSend.log(out); robert@464: float in; robert@464: int ret = receive.getSamplesSrc(&in, 1, 1); robert@464: for(unsigned int channel = 0; channel < context->audioChannels; channel++){ robert@464: audioWrite(context, n, channel, in); robert@464: } robert@464: } robert@464: } robert@464: robert@464: // cleanup() is called once at the end, after the audio has stopped. robert@464: // Release any resources that were allocated in setup(). robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: } robert@464: robert@464: /* ------------ Project Explantation ------------ */ robert@464: robert@464: /** robert@500: \example basic-network/render.cpp robert@464: robert@464: Networking robert@464: ---------- robert@464: robert@464: This sketch allows you to send audio and sensor data over UDP to a robert@464: DAW on the host. The host needs to run Udpioplugin which you can find robert@464: [here](https://code.soundsoftware.ac.uk/projects/udpioplugin). robert@464: robert@464: Note that this sketch and the accompanying plugin are still in testing. robert@464: */