giuliomoro@117: #ifndef RECEIVEAUDIOTHREAD_H_INCLUDED giuliomoro@117: #define RECEIVEAUDIOTHREAD_H_INCLUDED giuliomoro@117: #include giuliomoro@117: #include giuliomoro@117: #include giuliomoro@117: #include giuliomoro@117: #include giuliomoro@117: giuliomoro@117: class ReceiveAudioThread{ giuliomoro@117: private: giuliomoro@117: // FILE *fd; //DEBUG giuliomoro@117: // FILE *fd2; //DEBUG giuliomoro@117: UdpServer socket; giuliomoro@117: //JUCE DatagramSocket socket; giuliomoro@117: bool listening; giuliomoro@117: bool bufferReady; giuliomoro@119: #ifdef JUCE giuliomoro@117: bool threadRunning; giuliomoro@119: #else giuliomoro@119: static bool threadRunning; giuliomoro@119: static bool threadIsExiting; giuliomoro@119: #endif giuliomoro@117: float *buffer; giuliomoro@117: float *stackBuffer; giuliomoro@117: int bufferLength; giuliomoro@117: float readPointer; giuliomoro@117: int writePointer; giuliomoro@117: int lastValidPointer; giuliomoro@119: #ifdef JUCE giuliomoro@117: int sleepTime; giuliomoro@119: #else giuliomoro@119: static int sleepTime; giuliomoro@119: #endif giuliomoro@117: int waitForSocketTime; giuliomoro@117: int payloadLength; //size of the payload of each datagram giuliomoro@117: int headerLength; //size of the header of each datagram giuliomoro@117: int bytesToRead; giuliomoro@117: int threadPriority; giuliomoro@119: int channel; giuliomoro@117: void dealloc(); giuliomoro@117: void wrapWritePointer(); giuliomoro@117: void pushPayload(int startIndex); giuliomoro@117: void popPayload(int startIndex); giuliomoro@117: int readUdpToBuffer(); giuliomoro@119: #ifdef JUCE giuliomoro@119: #else giuliomoro@119: static bool threadShouldExit(); giuliomoro@119: static bool staticConstructed; giuliomoro@119: static void staticConstructor(); giuliomoro@117: static AuxiliaryTask receiveDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope) giuliomoro@119: static std::vector objAddrs; giuliomoro@119: #endif giuliomoro@117: public: giuliomoro@117: ReceiveAudioThread(); giuliomoro@117: ~ReceiveAudioThread(); giuliomoro@119: void init(int port, int aSamplesPerBlock, int channel); giuliomoro@117: void setup(); giuliomoro@117: void bindToPort(int aPort); giuliomoro@117: bool isListening(); giuliomoro@117: float* getCurrentBuffer(int length); giuliomoro@120: /** giuliomoro@120: * Copies the samples to a non-interleaved buffer. giuliomoro@120: */ giuliomoro@117: int getSamplesSrc(float *destination, int length, float samplingRateRatio); giuliomoro@120: /** giuliomoro@120: * Copies the samples to an interleaved buffer. giuliomoro@120: */ giuliomoro@120: int getSamplesSrc(float *destination, int length, giuliomoro@120: float samplingRateRatio, int numChannelsInDestination, giuliomoro@120: int channelToWriteTo); giuliomoro@117: bool isBufferReady(); giuliomoro@119: #ifdef JUCE // if we are in Juce, then we run a separate thread for each receiver giuliomoro@119: // (as each of them are typically receiving on a mono or stereo track) giuliomoro@117: void run(); giuliomoro@119: #else giuliomoro@119: void static run(); //while in BeagleRT we have a single thread that receives for all the instances. giuliomoro@119: //TODO: make run() private in BeagleRT giuliomoro@119: static void startThread(); giuliomoro@119: static void stopThread(); giuliomoro@119: static int getNumInstances(); giuliomoro@119: #endif // JUCE giuliomoro@117: }; giuliomoro@117: #endif // RECEIVEAUDIOTHREAD_H_INCLUDED