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