# HG changeset patch # User Giulio Moro # Date 1466024342 -3600 # Node ID 429260bd99b2247073c8ec0ed42210d036bd3f78 # Parent 41436dcd2cb09560b91e716d218ee493717d5bd0 Removed unused core and examples files (NetworkSend, ReceiveAudioThread, IirFilter) diff -r 41436dcd2cb0 -r 429260bd99b2 .cproject --- a/.cproject Wed Jun 15 20:58:48 2016 +0100 +++ b/.cproject Wed Jun 15 21:59:02 2016 +0100 @@ -14,7 +14,7 @@ - + @@ -135,7 +135,7 @@ - + @@ -159,7 +159,7 @@ - + @@ -264,7 +264,7 @@ - + diff -r 41436dcd2cb0 -r 429260bd99b2 core/IirFilter.cpp --- a/core/IirFilter.cpp Wed Jun 15 20:58:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/* - * IirFilter.cpp - * - * Created on: 17 Sep 2015 - * Author: giulio - */ -#include "IirFilter.h" - -IirFilterStage::IirFilterStage(){ - for(int n = 0; n < IIR_FILTER_STAGE_COEFFICIENTS; n++){ - coefficients[n] = 0; - } - for(int n = 0; n < IIR_FILTER_STAGE_STATES; n++){ - states[n]=0; - } -} -void IirFilterStage::setCoefficients(double* newCoefficients){ - for(int n = 0; n < IIR_FILTER_STAGE_COEFFICIENTS; n++){ - coefficients[n] = newCoefficients[n]; - } -} -void IirFilterStage::setStates(double* newStates){ - for(int n = 0; n < IIR_FILTER_STAGE_STATES; n++){ - states[n] = newStates[n]; - } -} - -/* struct IirFilterStage* stages; - int numberOfStages; -*/ -IirFilter::IirFilter(){ - stages=(IirFilterStage**)0; - setNumberOfStages(0); -} -IirFilter::IirFilter(int newNumberOfStages){ - setNumberOfStages(newNumberOfStages); -} -IirFilter::IirFilter(int newNumberOfStages, double* newCoefficients){ - setNumberOfStages(newNumberOfStages); - setCoefficients(newCoefficients); -} -void IirFilter::dealloc(){ - if( stages == 0 ) - return; - for(int n = 0; n < numberOfStages; n++){ - delete stages[n]; - } - delete stages; - stages = 0; - numberOfStages = 0; -} -void IirFilter::setCoefficients(double* newCoefficients){ - for(int n = 0; n < numberOfStages; n++){ - stages[n]->setCoefficients(newCoefficients); - } -}; -void IirFilter::setStates(double* newStates){ - for(int n = 0; n < numberOfStages; n++){ - stages[n]->setStates(newStates); - } -}; -void IirFilter::setNumberOfStages(int newNumberOfStages){ - dealloc(); - numberOfStages=newNumberOfStages; - stages = new IirFilterStage*[numberOfStages]; - for( int n = 0; n < numberOfStages; n++){ - stages[n] = new IirFilterStage; - } -} diff -r 41436dcd2cb0 -r 429260bd99b2 core/NetworkSend.cpp --- a/core/NetworkSend.cpp Wed Jun 15 20:58:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,257 +0,0 @@ -#include "NetworkSend.h" - -#ifdef USE_JUCE -#else -//initialize the static members of NetworkSend -bool NetworkSend::staticConstructed=false; -int NetworkSend::sleepTimeMs; -bool NetworkSend::threadIsExiting; -bool NetworkSend::threadRunning; -std::vector NetworkSend::objAddrs(0); -AuxiliaryTask NetworkSend::sendDataTask=NULL; - -void sendData(){ - NetworkSend::run(); -} -void NetworkSend::staticConstructor(){ - if(staticConstructed==true) - return; - staticConstructed=true; - threadIsExiting=false; - threadRunning=false; - sendDataTask = Bela_createAuxiliaryTask(::sendData, 95, "sendDataTask"); //TODO: allow variable priority -} -void NetworkSend::sendAllData(){ - for(unsigned int n=0; nsendData(); - } -} -int NetworkSend::getNumInstances(){ - return objAddrs.size(); -} -void NetworkSend::startThread(){ - Bela_scheduleAuxiliaryTask(sendDataTask); -} -void NetworkSend::stopThread(){ - threadIsExiting=true; -} -bool NetworkSend::threadShouldExit(){ - return(gShouldStop || threadIsExiting); -} -bool NetworkSend::isThreadRunning(){ - return threadRunning; -} -#endif /* USE_JUCE */ - -#ifdef USE_JUCE -NetworkSend::NetworkSend(const String &threadName): - Thread(threadName) -#else -NetworkSend::NetworkSend() -#endif /* USE_JUCE */ -{ - channel.buffers=NULL; - channel.doneOnTime=NULL; - channel.readyToBeSent=NULL; - channel.enabled=false; - sleepTimeMs=2; //This should actually be initialized in the staticConstructor for non-Juce code, but doing it here makes it more portable - channel.sampleCount=0; -} - -NetworkSend::~NetworkSend(){ -#ifdef USE_JUCE - stopThread(1000); -#else - stopThread(); - for(unsigned int n=0; n4*channel.bufferLength ? blockSize*4 : 4*channel.bufferLength; - channel.numBuffers= (1+numSamples/channel.bufferLength) * 3; //the +1 takes the ceil() of the division - channel.buffers=(float**)malloc(channel.numBuffers*sizeof(float*)); - printf("NumBuffers: %d\n", channel.numBuffers); - if(channel.buffers==NULL) - return; - for(int n=0; n( t3 - t1 ).count(); - // auto duration2 = std::chrono::duration_cast( t2 - t1 ).count(); - // if(duration2>0) - // std::cout << "Duration is: " << duration2 <<". Whole loop is: " << duration1 << "\n"; - usleep(1000); - } -#else - threadRunning=true; - while(threadShouldExit()==false){ - sendAllData(); - usleep(sleepTimeMs*1000); - } - threadRunning=false; -#endif -} diff -r 41436dcd2cb0 -r 429260bd99b2 core/ReceiveAudioThread.cpp --- a/core/ReceiveAudioThread.cpp Wed Jun 15 20:58:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,313 +0,0 @@ -#include - -#ifdef USE_JUCE -#else -//initialise static members -bool ReceiveAudioThread::staticConstructed=false; -AuxiliaryTask ReceiveAudioThread::receiveDataTask=NULL; -std::vector ReceiveAudioThread::objAddrs(0); -bool ReceiveAudioThread::threadRunning; -bool ReceiveAudioThread::threadIsExiting; -int ReceiveAudioThread::sleepTime; - -void receiveData(){ - ReceiveAudioThread::run(); -} -void ReceiveAudioThread::staticConstructor(){ - if(staticConstructed==true) - return; - staticConstructed=true; - threadIsExiting=false; - receiveDataTask=Bela_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities -} -#endif /* USE_JUCE */ - -void ReceiveAudioThread::dealloc(){ - free(buffer); - buffer=NULL; - free(stackBuffer); - stackBuffer=NULL; -} -void ReceiveAudioThread::wrapWritePointer(){ - //this is not quite a simple wrapping as you would do in a circular buffer, - //as there is no guarantee the buffer will be full at all times, given that there must alwas be enough space at the end of it - //to hold a full payload - // lastValidPointer indicates the last pointer in the buffer containing valid data - // - if(writePointer+payloadLength+headerLength>bufferLength){ //if we are going to exceed the length of the buffer with the next reading - // lastValidPointer=writePointer+headerLength; //remember where the last valid data are - // for(int n=headerLength;n>5, the - // destructor (always or sometimes) never actually gets called, despite run() returns ...see issue #1381 -#ifdef USE_JUCE -#else - lastTime=rt_timer_read(); -// rt_printf("lastTimeread= %llu\n", lastTime); -#endif /* USE_JUCE */ - pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0 - //read header+payload - int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting. - //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header - if(numBytes<0){ - printf("error numBytes1\n"); - return -3; //TODO: something went wrong, you have to discard the rest of the packet! - } - if(numBytes==0){//TODO: this should not happen unless you actually receive a packet of size zero (is it at all possible?) -// printf("received 0 bytes\n"); - return 0; - } - if(numBytes != bytesToRead){ //this is equivalent to (numBytesbufferLength) - return NULL; - readPointer+=length; - if(readPointer>lastValidPointer){ - readPointer=headerLength; - } - return buffer+(int)readPointer; -}; -int ReceiveAudioThread::getSamplesSrc(float *destination, int length, - float samplingRateRatio, int numChannelsInDestination, - int channelToWriteTo) -{ - if (!(samplingRateRatio>0 && samplingRateRatio<=2)) - return -2; - if(isListening()==false) - return -1; - static int numCalls=0; - if(writePointer<0 /*|| (numCalls&16383)==0*/){ //if writePointer has not been initalized yet ... -#ifdef USE_JUCE -#else //debug - readPointer = headerLength; -#endif /* USE_JUCE */ - // this cumbersome line means: start writing at a position which is as close as possible - // to the center of the buffer, but still is aligned to (payloadLength*x)+headerLength - // thus allowing buffering to allow clock drift to go either way - writePointer = headerLength + ((bufferLength-headerLength)/payloadLength/2)*payloadLength; - // This will help keeping them in sync. - //TODO: handle what happens when the remote stream is interrupted and then restarted - printf("write pointer inited at: %d\n", writePointer); - } - numCalls++; - if(length>lastValidPointer) { - //not enough samples available, we fill the buffer with what is available, but the destination buffer will not be filled completely - //at this very moment the other thread might be writing at most one payload into the buffer. - //To avoid a race condition, we need to let alone the buffer where we are currently writing - //as writing the payload also temporarily overwrites the previous headerLength samples, we need to account for them as well - //TODO: This assumes that the writePointer and readPointer do not drift. When doing clock synchronization we will find out that it is not true! - length=lastValidPointer-payloadLength-headerLength; - if(length<0) //no samples available at all! - return 0; - } - for(int n=0; n=lastValidPointer){ - readPointer=readPointer-lastValidPointer+headerLength; - } - } - return length; -} -int ReceiveAudioThread::getSamplesSrc(float *destination, int length, float samplingRateRatio){ - return getSamplesSrc(destination, length, samplingRateRatio, 1,0); - // TODO: rewriting this so that it does not call the override method we can save a multiply and add - // for each sample. -} -bool ReceiveAudioThread::isBufferReady(){ - return bufferReady; -} -#ifdef USE_JUCE -#else -void ReceiveAudioThread::startThread(){ - Bela_scheduleAuxiliaryTask(receiveDataTask); -} -void ReceiveAudioThread::stopThread(){ - threadIsExiting=true; -} -bool ReceiveAudioThread::threadShouldExit(){ - return(gShouldStop || threadIsExiting ); -} -RTIME ReceiveAudioThread::getLastTime(){ - return lastTime; -} -#endif /* USE_JUCE */ -int ReceiveAudioThread::getTimestamp(){ - return timestamp; -} -void ReceiveAudioThread::run(){ - // fd2=fopen("buffer.m","w"); //DEBUG - // fprintf(fd2, "buf=["); //DEBUG - threadRunning=true; - int maxCount=10; - int count=0; - // Clean the socket from anything that is currently in it. -#ifdef USE_JUCE - // this is borrowed from Bela's UdpServer class. - int n; - do { - float waste; - if(socket.waitUntilReady(true, 0)==0) - break; - n=socket.read((void*)&waste, sizeof(float), false); - count++; - if(n<0){ - printf("error\n"); - break; - } - printf("n: %d\n",n); - } while (n>0 && (maxCount<=0 || countsocket.empty(maxCount); - } -#endif /* USE_JUCE */ - printf("socket emptied with %d reads\n", count); - - while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting -#ifdef USE_JUCE - readUdpToBuffer(); // read into the oldBuffer - sleep(sleepTime); -#else - for(unsigned int n=0; nreadUdpToBuffer(); - } - usleep(sleepTime); //TODO: use rt_task_sleep instead -#endif /* USE_JUCE */ - } - threadRunning=false; - printf("Thread is not running \n"); - // fprintf(fd,"];readPointer,writePointer,lastValidPointer,destination]=deal(var(:,1), var(:,2), var(:,3), var(:,4));"); //DEBUG - // fclose(fd);//DEBUG - // fprintf(fd2,"];");//DEBUG - // fclose(fd2); //DEBUG -} diff -r 41436dcd2cb0 -r 429260bd99b2 examples/basic_network/render.cpp --- a/examples/basic_network/render.cpp Wed Jun 15 20:58:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -/* - ____ _____ _ _ -| __ )| ____| | / \ -| _ \| _| | | / _ \ -| |_) | |___| |___ / ___ \ -|____/|_____|_____/_/ \_\.io - -*/ - -/* - * render.cpp - * - * Created on: Oct 24, 2014 - * Author: parallels - */ - -/** -\example 5_basic_network - -Networking ----------- - -This sketch allows you to send audio and sensor data over UDP to a -DAW on the host. The host needs to run Udpioplugin which you can f -ind [here](https://code.soundsoftware.ac.uk/projects/udpioplugin). - -Note that this sketch and the accompanying plugin are still in testing. -*/ - -#include -//#include -#include -#include -#include - -// setup() is called once before the audio rendering starts. -// Use it to perform any initialisation and allocation which is dependent -// on the period size or sample rate. -// -// userData holds an opaque pointer to a data structure that was passed -// in from the call to initAudio(). -// -// Return true on success; returning false halts the program. - -NetworkSend networkSend; -ReceiveAudioThread receive; -float gFrequency; -float gInverseSampleRate; -float gPhase; -bool setup(BelaContext *context, void *userData) -{ - // Retrieve a parameter passed in from the initAudio() call - gFrequency = *(float *)userData; - - networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); - receive.init(10000, context->audioFrames, 0); - receive.startThread(); - gInverseSampleRate = 1.0 / context->audioSampleRate; - gPhase = 0; - return true; -} - -// render() is called regularly at the highest priority by the audio engine. -// Input and output are given from the audio hardware and the other -// ADCs and DACs (if available). If only audio is available, numMatrixFrames -// will be 0. - -void render(BelaContext *context, void *userData) -{ - for(unsigned int n = 0; n < context->audioFrames; n++) { - float out = 0.7f * sinf(gPhase); - gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; - if(gPhase > 2.0 * M_PI) - gPhase -= 2.0 * M_PI; - - networkSend.log(out); - float in; - int ret = receive.getSamplesSrc(&in, 1, 1); - for(unsigned int channel = 0; channel < context->audioChannels; channel++){ - audioWrite(context, n, channel, in); - } - } -} - -// cleanup() is called once at the end, after the audio has stopped. -// Release any resources that were allocated in setup(). - -void cleanup(BelaContext *context, void *userData) -{ -} diff -r 41436dcd2cb0 -r 429260bd99b2 include/NetworkSend.h --- a/include/NetworkSend.h Wed Jun 15 20:58:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -//scope.cpp -#ifndef SCOPE_H_ -#define SCOPE_H_ - -#ifdef USE_JUCE -#include -#else -#include -#include -#include -#include -#include -#include -extern int gShouldStop; -#endif /* USE_JUCE */ - -#define NETWORK_AUDIO_BUFFER_SIZE 302 -#define UDP_BUFFER_HEADER_CHANNEL_INDEX 0 -#define UDP_BUFFER_HEADER_TIMESTAMP_INDEX 1 -#define UDP_BUFFER_HEADER_LENGTH 2 - -struct NetworkBuffer{ - int channelNumber; - int numBuffers; - int writeBuffer; - int readBuffer; - int writePointer; - float** buffers; - bool* doneOnTime; - bool* readyToBeSent; - bool enabled; - int sampleCount; - static const int bufferLength=NETWORK_AUDIO_BUFFER_SIZE; - static const int headerLength=UDP_BUFFER_HEADER_LENGTH; - static const int headerChannelIndex=UDP_BUFFER_HEADER_CHANNEL_INDEX; - static const int headerTimestampIndex=UDP_BUFFER_HEADER_TIMESTAMP_INDEX; -}; - -#ifdef USE_JUCE -class NetworkSend: public Thread { -#else -class NetworkSend { -#endif /* USE_JUCE */ - float sampleRate; -#ifdef USE_JUCE - DatagramSocket udpClient; - int sleepTimeMs; - String remoteHostname; - int remotePortNumber; -#else - UdpClient udpClient; - bool isThreadRunning(); - static int sleepTimeMs; - static bool threadShouldExit(); - static bool threadIsExiting; - static bool threadRunning; - static bool staticConstructed; - static void staticConstructor(); - static AuxiliaryTask sendDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope) - static std::vector objAddrs; -#endif /* USE_JUCE */ - void dealloc(); -public: - NetworkBuffer channel; -#ifdef USE_JUCE - NetworkSend(const String &threadName); -#else - NetworkSend(); -#endif - ~NetworkSend(); - void setup(float aSampleRate, int blockSize, int aChannelNumber, int aPort, const char *aServer); - void cleanup(); - void sendData(); - void log(float value); - void setPort(int aPort); - void setServer(const char* aServer); - void setChannelNumber(int aChannelNumber); - int getChannelNumber(); - int getTimestamp(); -#ifdef USE_JUCE - void run(); -#else - static int getNumInstances(); - static void sendAllData(); - static void startThread(); - static void stopThread(); - static void run(); -#endif /* USE_JUCE */ -}; - -#ifdef USE_JUCE -#else -/** - * An array of NetworkSend objects with some default parameters - * - * All sending on the same port (defaults to 9999) - * All sending to the same server (defaults to 127.0.0.1) -*/ -class Scope { - std::vector channels; - void deallocate(); -public: - Scope(int aNumChannels); - ~Scope(); - void log(int channel, float value); - void setup(); - void setup(float sampleRate, int aPort, const char* aServer); - void sendData(); - void setPort(int port); - void setPort(int channel, int port); - int getNumChannels(); -}; -#endif /* USE_JUCE */ - -#endif /* SCOPE_H */ diff -r 41436dcd2cb0 -r 429260bd99b2 include/ReceiveAudioThread.h --- a/include/ReceiveAudioThread.h Wed Jun 15 20:58:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,105 +0,0 @@ -#ifndef RECEIVEAUDIOTHREAD_H_INCLUDED -#define RECEIVEAUDIOTHREAD_H_INCLUDED - -#ifdef USE_JUCE -#include -#else -#include -#include -#include -#include -#include -#include -#include - -#endif /*USE_JUCE*/ - -#ifdef USE_JUCE -class ReceiveAudioThread : public Thread { -#else -class ReceiveAudioThread{ -#endif /* USE_JUCE */ -private: - // FILE *fd; //DEBUG - // FILE *fd2; //DEBUG -#ifdef USE_JUCE - DatagramSocket socket; -#else - UdpServer socket; -#endif /* USE_JUCE */ - bool listening; - bool bufferReady; -#ifdef USE_JUCE - bool threadRunning; //do we really need this ? -#else - static bool threadRunning; - static bool threadIsExiting; -#endif - float *buffer; - float *stackBuffer; - int bufferLength; - float readPointer; - int writePointer; - int lastValidPointer; -#ifdef USE_JUCE - int sleepTime; -#else - static int sleepTime; -#endif - int waitForSocketTime; - int payloadLength; //size of the payload of each datagram - int headerLength; //size of the header of each datagram - int bytesToRead; - int threadPriority; - int channel; - int timestamp; - void dealloc(); - void wrapWritePointer(); - void pushPayload(int startIndex); - void popPayload(int startIndex); - int readUdpToBuffer(); -#ifdef USE_JUCE -#else - RTIME lastTime; // Used for clock synchronization - static bool threadShouldExit(); - static bool staticConstructed; - static void staticConstructor(); - static AuxiliaryTask receiveDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope) - static std::vector objAddrs; -#endif -public: -#ifdef USE_JUCE - ReceiveAudioThread(const String &threadName); -#else - ReceiveAudioThread(); -#endif - ~ReceiveAudioThread(); - void init(int port, int aSamplesPerBlock, int channel); - void bindToPort(int aPort); - bool isListening(); - float* getCurrentBuffer(int length); - /** - * Copies the samples to a non-interleaved buffer. - */ - int getSamplesSrc(float *destination, int length, float samplingRateRatio); - /** - * Copies the samples to an interleaved buffer. - */ - int getSamplesSrc(float *destination, int length, - float samplingRateRatio, int numChannelsInDestination, - int channelToWriteTo); - bool isBufferReady(); - int getTimestamp(); -#ifdef USE_JUCE // if we are in Juce, then we run a separate thread for each receiver - // (as each of them are typically receiving on a mono or stereo track) - void run(); -#else - RTIME getLastTime(); - void static run(); //while in Bela we have a single thread that receives for all the instances. - //TODO: make run() private in Bela - static void startThread(); - static void stopThread(); - static int getNumInstances(); -#endif // USE_JUCE -}; -#endif // RECEIVEAUDIOTHREAD_H_INCLUDED