annotate include/NetworkSend.h @ 143:dd24379336f1 ClockSync

Merged
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 14 Sep 2015 17:35:18 +0100
parents e24c531220ee
children
rev   line source
andrewm@71 1 //scope.cpp
giuliomoro@109 2 #ifndef SCOPE_H_
giuliomoro@109 3 #define SCOPE_H_
giuliomoro@109 4
giuliomoro@129 5 #ifdef USE_JUCE
giuliomoro@131 6 #include <JuceHeader.h>
giuliomoro@129 7 #else
andrewm@71 8 #include <BeagleRT.h>
andrewm@71 9 #include <rtdk.h>
andrewm@71 10 #include <cmath>
andrewm@71 11 #include <UdpClient.h>
giuliomoro@111 12 #include <vector>
giuliomoro@131 13 #include <string>
giuliomoro@131 14 extern bool gShouldStop;
giuliomoro@129 15 #endif /* USE_JUCE */
andrewm@71 16
giuliomoro@107 17 #define NETWORK_AUDIO_BUFFER_SIZE 302
giuliomoro@131 18 #define UDP_BUFFER_HEADER_CHANNEL_INDEX 0
giuliomoro@131 19 #define UDP_BUFFER_HEADER_TIMESTAMP_INDEX 1
giuliomoro@131 20 #define UDP_BUFFER_HEADER_LENGTH 2
giuliomoro@109 21
giuliomoro@109 22 struct NetworkBuffer{
giuliomoro@109 23 int channelNumber;
giuliomoro@131 24 int numBuffers;
giuliomoro@131 25 int writeBuffer;
giuliomoro@131 26 int readBuffer;
giuliomoro@131 27 int writePointer;
giuliomoro@131 28 float** buffers;
giuliomoro@131 29 bool* doneOnTime;
giuliomoro@131 30 bool* readyToBeSent;
giuliomoro@131 31 bool enabled;
giuliomoro@131 32 int sampleCount;
giuliomoro@131 33 static const int bufferLength=NETWORK_AUDIO_BUFFER_SIZE;
giuliomoro@131 34 static const int headerLength=UDP_BUFFER_HEADER_LENGTH;
giuliomoro@131 35 static const int headerChannelIndex=UDP_BUFFER_HEADER_CHANNEL_INDEX;
giuliomoro@131 36 static const int headerTimestampIndex=UDP_BUFFER_HEADER_TIMESTAMP_INDEX;
andrewm@71 37 };
andrewm@71 38
giuliomoro@129 39 #ifdef USE_JUCE
giuliomoro@129 40 class NetworkSend: public Thread {
giuliomoro@129 41 #else
giuliomoro@111 42 class NetworkSend {
giuliomoro@129 43 #endif /* USE_JUCE */
giuliomoro@129 44 float sampleRate;
giuliomoro@129 45 #ifdef USE_JUCE
giuliomoro@129 46 DatagramSocket udpClient;
giuliomoro@131 47 int sleepTimeMs;
giuliomoro@131 48 String remoteHostname;
giuliomoro@131 49 int remotePortNumber;
giuliomoro@129 50 #else
giuliomoro@111 51 UdpClient udpClient;
giuliomoro@131 52 bool isThreadRunning();
giuliomoro@131 53 static int sleepTimeMs;
giuliomoro@131 54 static bool threadShouldExit();
giuliomoro@131 55 static bool threadIsExiting;
giuliomoro@131 56 static bool threadRunning;
giuliomoro@111 57 static bool staticConstructed;
giuliomoro@129 58 static void staticConstructor();
giuliomoro@129 59 static AuxiliaryTask sendDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope)
giuliomoro@129 60 static std::vector<NetworkSend *> objAddrs;
giuliomoro@129 61 #endif /* USE_JUCE */
giuliomoro@131 62 void dealloc();
giuliomoro@131 63 public:
giuliomoro@129 64 NetworkBuffer channel;
giuliomoro@131 65 #ifdef USE_JUCE
giuliomoro@131 66 NetworkSend(const String &threadName);
giuliomoro@131 67 #else
giuliomoro@129 68 NetworkSend();
giuliomoro@131 69 #endif
giuliomoro@129 70 ~NetworkSend();
giuliomoro@131 71 void setup(float aSampleRate, int blockSize, int aChannelNumber, int aPort, const char *aServer);
giuliomoro@131 72 void cleanup();
giuliomoro@129 73 void sendData();
giuliomoro@129 74 void log(float value);
giuliomoro@129 75 void setPort(int aPort);
giuliomoro@129 76 void setServer(const char* aServer);
giuliomoro@129 77 void setChannelNumber(int aChannelNumber);
giuliomoro@129 78 int getChannelNumber();
giuliomoro@132 79 int getTimestamp();
giuliomoro@129 80 #ifdef USE_JUCE
giuliomoro@129 81 void run();
giuliomoro@129 82 #else
giuliomoro@129 83 static int getNumInstances();
giuliomoro@129 84 static void sendAllData();
giuliomoro@131 85 static void startThread();
giuliomoro@131 86 static void stopThread();
giuliomoro@129 87 static void run();
giuliomoro@129 88 #endif /* USE_JUCE */
andrewm@71 89 };
andrewm@71 90
giuliomoro@129 91 #ifdef USE_JUCE
giuliomoro@129 92 #else
giuliomoro@111 93 /**
giuliomoro@111 94 * An array of NetworkSend objects with some default parameters
giuliomoro@111 95 *
giuliomoro@111 96 * All sending on the same port (defaults to 9999)
giuliomoro@111 97 * All sending to the same server (defaults to 127.0.0.1)
giuliomoro@111 98 */
giuliomoro@109 99 class Scope {
giuliomoro@111 100 std::vector<NetworkSend> channels;
giuliomoro@109 101 void deallocate();
giuliomoro@109 102 public:
giuliomoro@109 103 Scope(int aNumChannels);
giuliomoro@109 104 ~Scope();
giuliomoro@109 105 void log(int channel, float value);
giuliomoro@111 106 void setup();
giuliomoro@111 107 void setup(float sampleRate, int aPort, const char* aServer);
giuliomoro@109 108 void sendData();
giuliomoro@119 109 void setPort(int port);
giuliomoro@119 110 void setPort(int channel, int port);
giuliomoro@111 111 int getNumChannels();
giuliomoro@109 112 };
giuliomoro@129 113 #endif /* USE_JUCE */
giuliomoro@129 114
giuliomoro@109 115 #endif /* SCOPE_H */