giuliomoro@117
|
1 #ifndef RECEIVEAUDIOTHREAD_H_INCLUDED
|
giuliomoro@117
|
2 #define RECEIVEAUDIOTHREAD_H_INCLUDED
|
giuliomoro@128
|
3
|
giuliomoro@128
|
4 #ifdef USE_JUCE
|
giuliomoro@128
|
5 #include <JuceHeader.h>
|
giuliomoro@125
|
6 #else
|
giuliomoro@117
|
7 #include <vector>
|
giuliomoro@117
|
8 #include <iostream>
|
giuliomoro@117
|
9 #include <UdpServer.h>
|
giuliomoro@117
|
10 #include <BeagleRT.h>
|
giuliomoro@117
|
11 #include <native/task.h>
|
giuliomoro@131
|
12 #include <math.h>
|
giuliomoro@128
|
13 #endif /*USE_JUCE*/
|
giuliomoro@117
|
14
|
giuliomoro@128
|
15 #ifdef USE_JUCE
|
giuliomoro@125
|
16 class ReceiveAudioThread : public Thread {
|
giuliomoro@125
|
17 #else
|
giuliomoro@117
|
18 class ReceiveAudioThread{
|
giuliomoro@128
|
19 #endif /* USE_JUCE */
|
giuliomoro@117
|
20 private:
|
giuliomoro@117
|
21 // FILE *fd; //DEBUG
|
giuliomoro@117
|
22 // FILE *fd2; //DEBUG
|
giuliomoro@128
|
23 #ifdef USE_JUCE
|
giuliomoro@125
|
24 DatagramSocket socket;
|
giuliomoro@125
|
25 #else
|
giuliomoro@117
|
26 UdpServer socket;
|
giuliomoro@128
|
27 #endif /* USE_JUCE */
|
giuliomoro@117
|
28 bool listening;
|
giuliomoro@117
|
29 bool bufferReady;
|
giuliomoro@128
|
30 #ifdef USE_JUCE
|
giuliomoro@125
|
31 bool threadRunning; //do we really need this ?
|
giuliomoro@119
|
32 #else
|
giuliomoro@119
|
33 static bool threadRunning;
|
giuliomoro@119
|
34 static bool threadIsExiting;
|
giuliomoro@119
|
35 #endif
|
giuliomoro@117
|
36 float *buffer;
|
giuliomoro@117
|
37 float *stackBuffer;
|
giuliomoro@117
|
38 int bufferLength;
|
giuliomoro@117
|
39 float readPointer;
|
giuliomoro@117
|
40 int writePointer;
|
giuliomoro@117
|
41 int lastValidPointer;
|
giuliomoro@128
|
42 #ifdef USE_JUCE
|
giuliomoro@117
|
43 int sleepTime;
|
giuliomoro@119
|
44 #else
|
giuliomoro@119
|
45 static int sleepTime;
|
giuliomoro@119
|
46 #endif
|
giuliomoro@117
|
47 int waitForSocketTime;
|
giuliomoro@117
|
48 int payloadLength; //size of the payload of each datagram
|
giuliomoro@117
|
49 int headerLength; //size of the header of each datagram
|
giuliomoro@117
|
50 int bytesToRead;
|
giuliomoro@117
|
51 int threadPriority;
|
giuliomoro@119
|
52 int channel;
|
giuliomoro@117
|
53 void dealloc();
|
giuliomoro@117
|
54 void wrapWritePointer();
|
giuliomoro@117
|
55 void pushPayload(int startIndex);
|
giuliomoro@117
|
56 void popPayload(int startIndex);
|
giuliomoro@117
|
57 int readUdpToBuffer();
|
giuliomoro@128
|
58 #ifdef USE_JUCE
|
giuliomoro@119
|
59 #else
|
giuliomoro@119
|
60 static bool threadShouldExit();
|
giuliomoro@119
|
61 static bool staticConstructed;
|
giuliomoro@119
|
62 static void staticConstructor();
|
giuliomoro@117
|
63 static AuxiliaryTask receiveDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope)
|
giuliomoro@119
|
64 static std::vector<ReceiveAudioThread *> objAddrs;
|
giuliomoro@119
|
65 #endif
|
giuliomoro@117
|
66 public:
|
giuliomoro@128
|
67 #ifdef USE_JUCE
|
giuliomoro@125
|
68 ReceiveAudioThread(const String &threadName);
|
giuliomoro@125
|
69 #else
|
giuliomoro@117
|
70 ReceiveAudioThread();
|
giuliomoro@125
|
71 #endif
|
giuliomoro@117
|
72 ~ReceiveAudioThread();
|
giuliomoro@119
|
73 void init(int port, int aSamplesPerBlock, int channel);
|
giuliomoro@117
|
74 void bindToPort(int aPort);
|
giuliomoro@117
|
75 bool isListening();
|
giuliomoro@117
|
76 float* getCurrentBuffer(int length);
|
giuliomoro@120
|
77 /**
|
giuliomoro@120
|
78 * Copies the samples to a non-interleaved buffer.
|
giuliomoro@120
|
79 */
|
giuliomoro@117
|
80 int getSamplesSrc(float *destination, int length, float samplingRateRatio);
|
giuliomoro@120
|
81 /**
|
giuliomoro@120
|
82 * Copies the samples to an interleaved buffer.
|
giuliomoro@120
|
83 */
|
giuliomoro@120
|
84 int getSamplesSrc(float *destination, int length,
|
giuliomoro@120
|
85 float samplingRateRatio, int numChannelsInDestination,
|
giuliomoro@120
|
86 int channelToWriteTo);
|
giuliomoro@117
|
87 bool isBufferReady();
|
giuliomoro@128
|
88 #ifdef USE_JUCE // if we are in Juce, then we run a separate thread for each receiver
|
giuliomoro@119
|
89 // (as each of them are typically receiving on a mono or stereo track)
|
giuliomoro@117
|
90 void run();
|
giuliomoro@119
|
91 #else
|
giuliomoro@119
|
92 void static run(); //while in BeagleRT we have a single thread that receives for all the instances.
|
giuliomoro@119
|
93 //TODO: make run() private in BeagleRT
|
giuliomoro@119
|
94 static void startThread();
|
giuliomoro@119
|
95 static void stopThread();
|
giuliomoro@119
|
96 static int getNumInstances();
|
giuliomoro@128
|
97 #endif // USE_JUCE
|
giuliomoro@117
|
98 };
|
giuliomoro@117
|
99 #endif // RECEIVEAUDIOTHREAD_H_INCLUDED
|