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