-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboum.cpp
276 lines (242 loc) · 7.83 KB
/
boum.cpp
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
268
269
270
271
272
273
274
275
276
##include "boum.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "pa_asio.h"
#include <QApplication>
#include <windows.h>
#include "portaudio.h"
#include "notes_jugees.h"
#include "measure.h"
#include "sheet.h"
#include "colored.h"
#define SAMPLE_RATE (44100)//(44100)
#define FRAMES_PER_BUFFER (512)//(512)
#define NUM_SECONDS (0.08)// Le temps de traitement de l'enregistrement et de réinitialisation est de 0,08s
#define NUM_CHANNELS (2)
#define DITHER_FLAG (0)
#define WRITE_TO_FILE (0)
#if 1
#define PA_SAMPLE_TYPE paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE (128)
#define PRINTF_S_FORMAT "%d"
#endif
using namespace std;
typedef struct
{
int frameIndex; /* Index into sample array. */
int maxFrameIndex;
SAMPLE *recordedSamples;
}
paTestData;
// Callback
static int recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
(void)outputBuffer; /* Prevent unused variable warnings. */
(void)timeInfo;
(void)statusFlags;
(void)userData;
if (framesLeft < framesPerBuffer)
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if (inputBuffer == NULL)
{
for (i = 0; i<framesToCalc; i++)
{
*wptr++ = SAMPLE_SILENCE; /* left */
if (NUM_CHANNELS == 2) *wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
for (i = 0; i<framesToCalc; i++)
{
*wptr++ = *rptr++; /* left */
if (NUM_CHANNELS == 2) *wptr++ = *rptr++; /* right */
}
}
data->frameIndex += framesToCalc;
return finished;
}
Boum::Boum() //constructeur
{}
// fonction qui permet de lancer le debug qui affiche boum si on tape sur le pad
int Boum::fonctionBoum(void)
{
PaStreamParameters inputParameters,
outputParameters;
PaStream* stream;
PaError err = paNoError;
paTestData data;
int i, a = 0, b = 1; // M est le seuil de detection
float M = 0.008;
int totalFrames;
int numSamples;
int numBytes;
SAMPLE max, val;
double average;
//Etude des peripheriques
int numDevices;
const PaDeviceInfo *deviceInfo;
/*
for(i=0; i<numDevices; i++)
{
deviceInfo = Pa_GetDeviceInfo(i);
system("pause");
}
*/
//Debut de la boucle
a = 0; // parametre determinant au bout de cbn de notes arreter le programme
while (a<200)
{
val = 0.; // maximum d'amplitude des pics mesures
a = a + 1;
while (val<M)
{
// qDebug() <<"patest_record.c"<< endl;
fflush(stdout);
data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
data.frameIndex = 0;
numSamples = totalFrames * NUM_CHANNELS;
numBytes = numSamples * sizeof(SAMPLE);
data.recordedSamples = (SAMPLE *)malloc(numBytes); /* From now on, recordedSamples is initialised. */
if (data.recordedSamples == NULL)
{
//qDebug() << "Could not allocate record array." << endl;
goto done;
}
for (i = 0; i < numSamples; i++)
{
data.recordedSamples[i] = 0;
}
err = Pa_Initialize();
if (err != paNoError) goto done;
//qDebug() << Pa_GetDeviceCount() << Pa_GetDefaultInputDevice();
inputParameters.device = /*PaDeviceIndex(4)*/7 ;
// 0 correspond a l'entree micro
if (inputParameters.device == paNoDevice)
{
//qDebug() << "Error : No default input device" << endl;
goto done;
}
inputParameters.channelCount = 2; //stereo input
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
// Ouverture du Stream puis ecoute
//qDebug() << ".";
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff,
recordCallback,
&data);
if(err!=paNoError) goto done;
err = Pa_StartStream(stream);
if(err!=paNoError) goto done;
//qDebug() << "Now recording"<< endl;
fflush(stdout);
while ((err = Pa_IsStreamActive(stream)) == 1)
{
Pa_Sleep(50);
//qDebug() << "Now stream active" << endl;
//qDebug() << "index "<< data.frameIndex << endl;
fflush(stdout);
}
//if(err < 0) goto done;
err = Pa_CloseStream(stream);
if(err!=paNoError) goto done;
/*MAXIMUM PEAK AMPLITUDE*/
i = 0;
max = 0;
while (i<numSamples-1 && val<M)
{
i++;
val = data.recordedSamples[i];
// if(data.recordedSamples[i] > 0.5)
//qDebug() << data.recordedSamples[i] << endl ;
if(val<0) val=-val;
if (val>max) max = val;
}
//qDebug() << " /";
//qDebug() << "valeur du pic" << max << endl;
if (val>M)
{
//qDebug("BOUM %d %f",b,val);
//qDebug("temps au boum %d %d",b, time.elapsed());
this->resultReady();
simulationEspace();//Simulation de la touche espace
Pa_Sleep(100);
/*INPUT simulationAppuiEspace [1];
simulationAppuiEspace[0].type = 1; //Pour dire que c'est un keyboard event.
simulationAppuiEspace[0].ki.wScan = 0x41;
simulationAppuiEspace[0].ki.time = 0;
INPUT simulationRelacheEspace [1];
simulationRelacheEspace[0].type = 1;
simulationRelacheEspace[0].ki.wScan = 0x41d;
simulationRelacheEspace[0].ki.dwFlags = KEYEVENTF_KEYUP;
simulationRelacheEspace[0].ki.time = 0;
SendInput (1,simulationAppuiEspace, sizeof(simulationAppuiEspace));
SendInput (1,simulationRelacheEspace, sizeof(simulationAppuiEspace)); */
//Pa_Sleep(80);
b = b+1;
}
}
}
done:
Pa_Terminate();
if (data.recordedSamples)
free(data.recordedSamples);
if (err != paNoError)
//qDebug() << "Error" << endl;
return 0;
}
void Boum::doWork () //slot qui permet de lancer fonctionBoum
{
qDebug("foncboum");
this->fonctionBoum();
}
void Boum::lancerTimer()
{
qDebug("lancée du timeboum");
this->time.start();
}
void resultReady ()
{}