1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| /**
* @file FluxControl
*
* @version 1.00
* @date 26/06/2017
* @author Martin Baty
* @copyright
*
* This program uses the PortAudio Portable Audio Library.
* For more information see: http://www.portaudio.com
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "portaudio.h"
#include "FluxControl.h"
#include "Client.h"
#include <pthread.h>
#define SAMPLE_RATE (8000)
#define PA_SAMPLE_TYPE paFloat32
//#define FRAMES_PER_BUFFER (64)
#define FRAMES_PER_BUFFER (8)
static SAMPLE * myOut;
Msg msg;
static SAMPLE audioOutputFlux[8];
static int gNumNoInputs = 0;
static int firstUse = 1;
static int trueCallback = 0;
static pthread_t fluxThread;
static pthread_cond_t conditionPlay = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutexPlay = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t conditionRec = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutexRec = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutexLock = PTHREAD_MUTEX_INITIALIZER;
int a = 0;
static void parametersInitializer(FluxControl * this);
static int fuzzCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData);
static void * startStream(void * param);
/* This is the structure of the object FluxControl */
struct FluxControl_t
{
PaStreamParameters inputParameters, outputParameters;
PaStream *stream;
Brain * brain;
};
FluxControl * FluxControlInit(Brain * brain)
{
assert(brain != NULL);
PaError err;
//Creation and memory allocation of the object FluxControl
FluxControl *this;
this = (FluxControl*)malloc(sizeof(FluxControl));
assert(this != NULL);
this->brain = brain;
trueCallback = 0;
for (int i = 0; i<8; i++)
{
audioOutputFlux[i] = 0;
}
err = Pa_Initialize();
assert(err == paNoError);
return this;
}
void FluxControlFree(FluxControl * this)
{
assert(this != NULL);
free(this);
}
void FluxControlStart(FluxControl * this)
{
assert(this != NULL);
PaError err;
parametersInitializer(this);
err = Pa_OpenStream(
&(this->stream),
&(this->inputParameters),
&(this->outputParameters),
SAMPLE_RATE,
FRAMES_PER_BUFFER,
0, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
fuzzCallback,
NULL );
assert(err == paNoError);
//printf("FIRST DEVICE = %d \n", firstDevice);
pthread_create (&fluxThread, NULL, &startStream, this);
}
static void * startStream(void * param)
{
FluxControl *this = (FluxControl*)param;
PaError err;
err = Pa_StartStream(this->stream);
char * myError = (char*)Pa_GetErrorText(err);
printf("myError : %s \n", myError);
perror("START STREAM");
assert(err == paNoError);
return NULL;
}
void FluxControlStop(FluxControl * this)
{
assert(this != NULL);
pthread_cond_broadcast(&conditionPlay);
PaError err;
err = Pa_CloseStream(this->stream);
assert(err == paNoError);
printf("Finished. gNumNoInputs = %d\n", gNumNoInputs);
Pa_Terminate();
pthread_join(fluxThread, NULL);
}
void FluxControlSetAudioOutput(SAMPLE * outputAudio)
{
//pthread_mutex_lock(&mutexLock);
// puts("YOOOOOO");
pthread_cond_broadcast(&conditionPlay);
for (int i = 0 ; i<8; i++)
{
// printf("PLAYED = %f \n", msg.inputAudio[i]);
*myOut++ = outputAudio[i];
*myOut++ = outputAudio[i];
//audioOutputFlux[i] = outputAudio[i];
//trueCallback=1;
}
//pthread_cond_broadcast(&conditionRec);
//pthread_mutex_unlock(&mutexLock);
}
static void parametersInitializer(FluxControl * this)
{
//INPUT
this->inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
perror("INPUT PARAMETER ERROR");
assert(this->inputParameters.device != paNoDevice);
this->inputParameters.channelCount = 2; /* stereo input */
this->inputParameters.sampleFormat = PA_SAMPLE_TYPE;
this->inputParameters.suggestedLatency = Pa_GetDeviceInfo((this->inputParameters).device)->defaultLowInputLatency;
this->inputParameters.hostApiSpecificStreamInfo = NULL;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//OUTPUT
this->outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
perror("OUTPUT PARAMETER ERROR");
assert(this->inputParameters.device != paNoDevice);
this->outputParameters.channelCount = 2; /* stereo output */
this->outputParameters.sampleFormat = PA_SAMPLE_TYPE;
this->outputParameters.suggestedLatency = Pa_GetDeviceInfo((this->outputParameters).device)->defaultLowOutputLatency;
this->outputParameters.hostApiSpecificStreamInfo = NULL;
}
static int fuzzCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
//pthread_mutex_lock(&mutexLock);
SAMPLE *in = (SAMPLE*)inputBuffer;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) userData;
SAMPLE *out = (SAMPLE*)outputBuffer;
myOut = out;
//pthread_mutex_lock(&mutexPlay);
//pthread_cond_wait(&conditionPlay, &mutexPlay);
//pthread_mutex_unlock(&mutexPlay);
if(inputBuffer == NULL)
{
for(int i=0; i<framesPerBuffer; i++)
{
msg.inputAudio[i] = 0; /* left - silent */
}
gNumNoInputs += 1;
}
else
{
for(int i=0; i<framesPerBuffer; i++)
{
//Remplissage du tableau msg.inputAudio[] de taille 8 (FRAMES_PER_BUFFER)
msg.inputAudio[i] = *in++;
*in++;
//*out++ = *in++;
//*out++ = *in++;
}
}
//Envoi du buffer contenant 8 frames
sendBroadcast();
return paContinue;
}
void setCondition()
{
pthread_cond_broadcast(&conditionPlay);
} |