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