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