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