andrewm@71: //scope.cpp giuliomoro@109: #ifndef SCOPE_H_ giuliomoro@109: #define SCOPE_H_ giuliomoro@109: giuliomoro@129: #ifdef USE_JUCE giuliomoro@131: #include giuliomoro@129: #else andrewm@71: #include andrewm@71: #include andrewm@71: #include andrewm@71: #include giuliomoro@111: #include giuliomoro@131: #include giuliomoro@131: extern bool gShouldStop; giuliomoro@129: #endif /* USE_JUCE */ andrewm@71: giuliomoro@107: #define NETWORK_AUDIO_BUFFER_SIZE 302 giuliomoro@131: #define UDP_BUFFER_HEADER_CHANNEL_INDEX 0 giuliomoro@131: #define UDP_BUFFER_HEADER_TIMESTAMP_INDEX 1 giuliomoro@131: #define UDP_BUFFER_HEADER_LENGTH 2 giuliomoro@109: giuliomoro@109: struct NetworkBuffer{ giuliomoro@109: int channelNumber; giuliomoro@131: int numBuffers; giuliomoro@131: int writeBuffer; giuliomoro@131: int readBuffer; giuliomoro@131: int writePointer; giuliomoro@131: float** buffers; giuliomoro@131: bool* doneOnTime; giuliomoro@131: bool* readyToBeSent; giuliomoro@131: bool enabled; giuliomoro@131: int sampleCount; giuliomoro@131: static const int bufferLength=NETWORK_AUDIO_BUFFER_SIZE; giuliomoro@131: static const int headerLength=UDP_BUFFER_HEADER_LENGTH; giuliomoro@131: static const int headerChannelIndex=UDP_BUFFER_HEADER_CHANNEL_INDEX; giuliomoro@131: static const int headerTimestampIndex=UDP_BUFFER_HEADER_TIMESTAMP_INDEX; andrewm@71: }; andrewm@71: giuliomoro@129: #ifdef USE_JUCE giuliomoro@129: class NetworkSend: public Thread { giuliomoro@129: #else giuliomoro@111: class NetworkSend { giuliomoro@129: #endif /* USE_JUCE */ giuliomoro@129: float sampleRate; giuliomoro@129: #ifdef USE_JUCE giuliomoro@129: DatagramSocket udpClient; giuliomoro@131: int sleepTimeMs; giuliomoro@131: String remoteHostname; giuliomoro@131: int remotePortNumber; giuliomoro@129: #else giuliomoro@111: UdpClient udpClient; giuliomoro@131: bool isThreadRunning(); giuliomoro@131: static int sleepTimeMs; giuliomoro@131: static bool threadShouldExit(); giuliomoro@131: static bool threadIsExiting; giuliomoro@131: static bool threadRunning; giuliomoro@111: static bool staticConstructed; giuliomoro@129: static void staticConstructor(); giuliomoro@129: static AuxiliaryTask sendDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope) giuliomoro@129: static std::vector objAddrs; giuliomoro@129: #endif /* USE_JUCE */ giuliomoro@131: void dealloc(); giuliomoro@131: public: giuliomoro@129: NetworkBuffer channel; giuliomoro@131: #ifdef USE_JUCE giuliomoro@131: NetworkSend(const String &threadName); giuliomoro@131: #else giuliomoro@129: NetworkSend(); giuliomoro@131: #endif giuliomoro@129: ~NetworkSend(); giuliomoro@131: void setup(float aSampleRate, int blockSize, int aChannelNumber, int aPort, const char *aServer); giuliomoro@131: void cleanup(); giuliomoro@129: void sendData(); giuliomoro@129: void log(float value); giuliomoro@129: void setPort(int aPort); giuliomoro@129: void setServer(const char* aServer); giuliomoro@129: void setChannelNumber(int aChannelNumber); giuliomoro@129: int getChannelNumber(); giuliomoro@132: int getTimestamp(); giuliomoro@129: #ifdef USE_JUCE giuliomoro@129: void run(); giuliomoro@129: #else giuliomoro@129: static int getNumInstances(); giuliomoro@129: static void sendAllData(); giuliomoro@131: static void startThread(); giuliomoro@131: static void stopThread(); giuliomoro@129: static void run(); giuliomoro@129: #endif /* USE_JUCE */ andrewm@71: }; andrewm@71: giuliomoro@129: #ifdef USE_JUCE giuliomoro@129: #else giuliomoro@111: /** giuliomoro@111: * An array of NetworkSend objects with some default parameters giuliomoro@111: * giuliomoro@111: * All sending on the same port (defaults to 9999) giuliomoro@111: * All sending to the same server (defaults to 127.0.0.1) giuliomoro@111: */ giuliomoro@109: class Scope { giuliomoro@111: std::vector channels; giuliomoro@109: void deallocate(); giuliomoro@109: public: giuliomoro@109: Scope(int aNumChannels); giuliomoro@109: ~Scope(); giuliomoro@109: void log(int channel, float value); giuliomoro@111: void setup(); giuliomoro@111: void setup(float sampleRate, int aPort, const char* aServer); giuliomoro@109: void sendData(); giuliomoro@119: void setPort(int port); giuliomoro@119: void setPort(int channel, int port); giuliomoro@111: int getNumChannels(); giuliomoro@109: }; giuliomoro@129: #endif /* USE_JUCE */ giuliomoro@129: giuliomoro@109: #endif /* SCOPE_H */