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