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