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