comparison examples/05-Communication/basic-network/render.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children b935f890e512
comparison
equal deleted inserted replaced
463:c47709e8b5c9 464:8fcfbfb32aa0
1 /*
2 ____ _____ _ _
3 | __ )| ____| | / \
4 | _ \| _| | | / _ \
5 | |_) | |___| |___ / ___ \
6 |____/|_____|_____/_/ \_\
7
8 The platform for ultra-low latency audio and sensor processing
9
10 http://bela.io
11
12 A project of the Augmented Instruments Laboratory within the
13 Centre for Digital Music at Queen Mary University of London.
14 http://www.eecs.qmul.ac.uk/~andrewm
15
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
19
20 The Bela software is distributed under the GNU Lesser General Public License
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */
23
24
25 #include <Bela.h>
26 //#include <rtdk.h>
27 #include <cmath>
28 #include <NetworkSend.h>
29 #include <ReceiveAudioThread.h>
30 #include <Utilities.h>
31
32 // setup() is called once before the audio rendering starts.
33 // Use it to perform any initialisation and allocation which is dependent
34 // on the period size or sample rate.
35 //
36 // userData holds an opaque pointer to a data structure that was passed
37 // in from the call to initAudio().
38 //
39 // Return true on success; returning false halts the program.
40
41 NetworkSend networkSend;
42 ReceiveAudioThread receive;
43 float gFrequency;
44 float gInverseSampleRate;
45 float gPhase;
46 bool setup(BelaContext *context, void *userData)
47 {
48 // Retrieve a parameter passed in from the initAudio() call
49 gFrequency = *(float *)userData;
50
51 networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1");
52 receive.init(10000, context->audioFrames, 0);
53 receive.startThread();
54 gInverseSampleRate = 1.0 / context->audioSampleRate;
55 gPhase = 0;
56 return true;
57 }
58
59 // render() is called regularly at the highest priority by the audio engine.
60 // Input and output are given from the audio hardware and the other
61 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
62 // will be 0.
63
64 void render(BelaContext *context, void *userData)
65 {
66 for(unsigned int n = 0; n < context->audioFrames; n++) {
67 float out = 0.7f * sinf(gPhase);
68 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
69 if(gPhase > 2.0 * M_PI)
70 gPhase -= 2.0 * M_PI;
71
72 networkSend.log(out);
73 float in;
74 int ret = receive.getSamplesSrc(&in, 1, 1);
75 for(unsigned int channel = 0; channel < context->audioChannels; channel++){
76 audioWrite(context, n, channel, in);
77 }
78 }
79 }
80
81 // cleanup() is called once at the end, after the audio has stopped.
82 // Release any resources that were allocated in setup().
83
84 void cleanup(BelaContext *context, void *userData)
85 {
86 }
87
88 /* ------------ Project Explantation ------------ */
89
90 /**
91 \example 05-basic-network
92
93 Networking
94 ----------
95
96 This sketch allows you to send audio and sensor data over UDP to a
97 DAW on the host. The host needs to run Udpioplugin which you can find
98 [here](https://code.soundsoftware.ac.uk/projects/udpioplugin).
99
100 Note that this sketch and the accompanying plugin are still in testing.
101 */