-
Notifications
You must be signed in to change notification settings - Fork 0
/
noiser.sh
489 lines (412 loc) · 12.8 KB
/
noiser.sh
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/bin/bash
# pour positionner le début en haut d'écran
clear
# ____ _
# | _ \ __ _ _ __ __ _ _ __ ___ ___| |_ ___ _ __ ___
# | |_) / _` | '__/ _` | '_ ` _ \ / _ \ __/ _ \ '__/ __|
# | __/ (_| | | | (_| | | | | | | __/ || __/ | \__ \
# |_| \__,_|_| \__,_|_| |_| |_|\___|\__\___|_| |___/
#
# memorize parameters on command line for save
cliParameters="$@"
# script directory
my_dir=`dirname "$0"`
# full directory
script_path=$(dirname $(readlink -f "$0"))
# script name
me=`basename "$0"`
# full name
fullScriptName="$script_path/$me"
# _ __ _ _
# __| |___ / _|__ _ _ _| | |_
# / _` / -_) _/ _` | || | | _|
# \__,_\___|_| \__,_|\_,_|_|\__|
#
yes="yes"
source "$my_dir/default.sh"
# ___ _ _ _
# / __| |___| |__ __ _| | __ ____ _ _ _ ___
# | (_ | / _ \ '_ \/ _` | | \ V / _` | '_(_-<
# \___|_\___/_.__/\__,_|_| \_/\__,_|_| /__/
#
bruit=$record/noise.$ext
# Statistiques du signal
maximumAmplitude=''
minimumAmplitude=''
midlineAmplitude=''
rmsLevDb=''
rmsPkDb=''
rmsTrDb=''
crestFactor=''
pkcount=''
# Statistiques du signal précédent
memoMaximumAmplitude=''
memoMinimumAmplitude=''
memoMidlineAmplitude=''
memoRmsLevDb=''
memoRmsPkDb=''
memoRmsTrDb=''
memoCrestFactor=''
memoPkcount=''
memoTimestamp=''
# séparateur de stat sox
IFS=$'\n'
nbFilesEnregistres=0
nbFilesSauvegardes=0
lastRecordSave=""
# on suppose que les applications utiles sont présentes
soxOk="yes"
amixerOk="yes"
gnuplotOk="yes"
ffmpegOk="yes"
flacOk="yes"
# pour les échantillons au dessous du seuil
declare -A myIndexedArrayThreshold
nbSampleSaved=0
# Load average
loadAverage1m=0
memoLoadAverage1m=0
# logo
logo="$script_path/logo.txt"
# _ _ _
# (_)_ _ __| |_ _ __| |___
# | | ' \/ _| | || / _` / -_)
# |_|_||_\__|_|\_,_\__,_\___|
source "$my_dir/functions.sh"
# ___ _ _
# |_ _|_ _ (_) |_
# | || ' \| | _|
# |___|_||_|_|\__|
#
# vérification de la présence des applications
checkApplicationsExists
showApplicationLogo
# _ _ _
# | | ___ __ _ __| | _ __ _ _ ___ ___ ___| |_ ___
# | |__/ _ \/ _` / _` | | '_ \ '_/ -_|_-</ -_) _(_-<
# |____\___/\__,_\__,_| | .__/_| \___/__/\___|\__/__/
# |_|
#
listOptions="a:b:cd:e:f:ghi:jk:l:m:n:op:qrst:uv:z"
while getopts "$listOptions" option
do
case "${option}" in
i)
preset="${OPTARG}"
;;
*)
;;
esac
done
source "$my_dir/presets.sh"
# ___ ____ _____ ___ ___ _ _ ____
# / _ \| _ \_ _|_ _/ _ \| \ | / ___|
#| | | | |_) || | | | | | | \| \___ \
#| |_| | __/ | | | | |_| | |\ |___) |
# \___/|_| |_| |___\___/|_| \_|____/
#
# bash getopts use an environment variable OPTIND to keep track the last option argument processed.
# The fact that OPTIND was not automatically reset each time you called getopts in the same shell session,
# only when the shell was invoked. So from second time you called getopts with the same arguments
# in the same session, OPTIND wasn't changed, getopts thought it had done the job and do nothing.
#
# You can reset OPTIND manually to make it work
# URL : https://unix.stackexchange.com/questions/233728/bash-function-with-getopts-only-works-the-first-time-its-run
OPTIND=1
# recup options
while getopts "$listOptions" option
do
case "${option}" in
a)
attente="${OPTARG}"
;;
b)
bit="${OPTARG}"
;;
c)
globalSound="yes"
;;
d)
duree="${OPTARG}"
;;
e)
ext="${OPTARG}"
;;
f)
record="${OPTARG}"
;;
g)
listen="no"
;;
h)
showHelp
exit 0
;;
j)
filteredSound="yes"
;;
k)
recordDateTimeLimit="${OPTARG}"
;;
l)
seuil="${OPTARG}"
;;
m)
microlevel="${OPTARG}"
;;
n)
seuilNoise="${OPTARG}"
;;
o)
compressToFlac="no"
;;
p)
case "${option}" in
"hig")
rate=44100
;;
"med")
rate=22050
;;
*)
rate=11025
;;
esac
;;
q)
takePicture="yes"
;;
r)
listenNoiseReduction="yes"
;;
s)
showTrace="yes"
;;
t)
sauvegardeBase="${OPTARG}"
;;
u)
saveSample="yes"
;;
v)
nbSample="${OPTARG}"
;;
z)
calibrate="yes"
;;
*)
;;
esac
done
# __ __ _ ____
# | \/ | __ _(_)_ __ | _ \ _ __ ___ __ _ _ __ __ _ _ __ ___
# | |\/| |/ _` | | '_ \ | |_) | '__/ _ \ / _` | '__/ _` | '_ ` _ \
# | | | | (_| | | | | | | __/| | | (_) | (_| | | | (_| | | | | | |
# |_| |_|\__,_|_|_| |_| |_| |_| \___/ \__, |_| \__,_|_| |_| |_|
# |___/
if [ "$soxOk" == "no" ]; then
printf "> L'application ${RED}sox${NC} est nécessaire !\n"
printf "> apt-get install sox\n"
exit
fi
if [ "$amixerOk" == "no" ]; then
printf "> L'application ${RED}amixer${NC} est nécessaire pour régler le niveau du microphone !\n"
printf "> apt-get install alsa-utils\n"
exit
fi
# if we are on virtual terminal
if [[ "$isTTY" == "yes" ]]; then
# Le terminal virtuel uniquement est éteint après une minute d'inativité
# Appuyer sur touche pour afficher l'écran
setterm --blank 1
fi
doYouWantToContinue=""
let minTimeDiffToStop=3*$duree # 3 fois car 2 fois est insuffisant quand on affiche la trace du signal et la FFT
dureeForFFT=`echo 4096/$rate | bc -l`
while [ "$doYouWantToContinue" == "" ]; do
# Set a trap for SIGINT and SIGTERM signals
setTrapCtrlC
# Création d'un sous-dossier dans sauvegarde et vérification de son existence
mkdir -p "$sauvegardeBase"
if [ -d $sauvegardeBase ]; then
ladate=`date +"%Y-%m-%d_%H-%M-%S"`
mkdir "$sauvegardeBase/$ladate"
if [ "$filteredSound" == "yes" ]; then
mkdir "$sauvegardeBase/$ladate/filtered"
fi
sauvegardeNow="$sauvegardeBase/$ladate"
fi
if [ -d $sauvegardeNow ]; then
printsep
printf "> Creation de ${GREEN}$sauvegardeNow${NC} reussie !\n"
saveParameters
saveEnTeteStat
if [ "$calibrate" == "yes" ]; then
saveEnTeteStatCalibrate
fi
# Echantillons sous le seuil
initArrayThreshold
else
printf "Le dossier ${RED}$sauvegardeNow${NC} n'existe pas !\n"
exit 1
fi
showEnTete
# suppression des fichiers
removefileinto "$record"
#removefileinto "$sauvegardeNow" # suppression des fichiers
# niveau du micro
reglerNiveauMicro $microlevel
waitSomeTime
# si appui sur Ctrl-C pendant attente
if [ "$stop" == "$yes" ]; then
exit
fi
whiteRecord
# ___ _
# | _ \___ __ ___ _ _ __| |
# | / -_) _/ _ \ '_/ _` |
# |_|_\___\__\___/_| \__,_|
#
printsep
echo "> Enregistrement ..."
if [ "$listenNoiseReduction" == "yes" ]; then
printf ">>> ${RED}Pas de bruit SVP${NC} , enregistrement du profil de bruit (seuil < ${RED}%s${NC}) ...\n" "$seuilNoise"
fi
cd "$record"
nbRecord=0
while true; do
timestampBeforeRecord=`date +"%s"`
case "$stop" in
"yes")
break
;;
*)
# enregistrement du bruit
recordMicrophone $ext "$bruit" $duree
readLoadAverage1m
# Fin d'enregistrement si l'écart entre 2 enregistrements est supérieur à 2 fois la durée d'un échantillon
isSupended "$timestampBeforeRecord" "$minTimeDiffToStop"
if [ "$stop" == "yes" ]; then
break
fi
getSoundStats "$bruit"
isToSave=$(echo "$maximumAmplitude >= $seuil || $minimumAmplitude <= -$seuil" | bc -l)
showWithGnuplot "$bruit"
# vérification de l'espace disque disponible
spaceused=$(getSpaceDir "$sauvegardeNow")
hasEnoughSpace=$(echo "$spaceused < 99" | bc -l)
# S'il reste assez d'espace disque
if (( $hasEnoughSpace )); then
# Si le signal est supérieur au seuil
if (( $isToSave )); then
if [ "$precStatSaved" == "no" ]; then
saveMemoStat
fi
saveRecordedTrack
precStatSaved="yes"
printPoint="no"
else
# marqueur de fichier enregistré mais non sauvegardé
printf "."
printPoint="yes"
# sauvegarde des stats pour évaluer le bruit
saveOneStatCalibrate
# on sauvegarde les stats actuelles
if [ "$precStatSaved" == "yes" ]; then
saveOneStat
fi
# on sauvegarde un échantillon sous le seuil pour vérifier son contenu
saveOneSampleUnderThreshold
precStatSaved="no"
# Si le profil de bruit n'est pas enregistré et qu'on veut entendre la réduction de bruit
if [[ "$listenNoiseReduction" == "yes" && "$profilSave" == "no" ]]; then
isNoiseToSave=$(echo "$maximumAmplitude <= $seuilNoise && $minimumAmplitude >= -$seuilNoise" | bc -l)
if (( $isNoiseToSave )); then
saveNoiseProfile
profilSave="yes"
fi
fi
fi
else
stop=$yes
listen="no"
printf "> ${RED}Plus assez d'espace de sauvegarde${NC} !\n"
fi
;;
esac
# fin d'enregistrement si on dépasse la date limite définie
if [ "$recordDateTimeLimit" != "infinity" ]; then
if [ $(date +"%s" --date="$recordDateTimeLimit") -lt $(date +"%s") ]; then
printf "\n"
break
fi
fi
isSupended "$timestampBeforeRecord" "$minTimeDiffToStop"
done
# ___ _ _ _
# | __(_) | |_ ___ _ _
# | _|| | | _/ -_) '_|
# |_| |_|_|\__\___|_|
#
createFilteredSound
# _ _ _
# | | (_)__| |_ ___ _ _
# | |__| (_-< _/ -_) ' \
# |____|_/__/\__\___|_||_|
#
printStatEnregistrement
# on écoute si on a arrété manuellement l'application
if [ "$listen" == "yes" ]; then
# et si des fichiers à écouter existent
if [ -n "$(ls -A $sauvegardeNow)" ]; then
listenFinalResult
listenWithNoiseReduce
fi
fi
# ___ _ _ _ ___ _
# / __| |___| |__ __ _| | / __| ___ _ _ _ _ __| |
# | (_ | / _ \ '_ \/ _` | | \__ \/ _ \ || | ' \/ _` |
# \___|_\___/_.__/\__,_|_| |___/\___/\_,_|_||_\__,_|
#
createGlobalSound
# ___ _ _ _ _ _
# / __| |_ __ _| |_(_)__| |_(_)__ ___
# \__ \ _/ _` | _| (_-< _| / _(_-<
# |___/\__\__,_|\__|_/__/\__|_\__/__/
#
plotStatWithGnuplot "$sauvegardeNow" "0.99"
plotRegisteredNoiseBoxplotWithGnuplot "$sauvegardeNow" "$seuil" "0.99"
plotNoiseBoxplotWithGnuplot "$sauvegardeNow"
# ___ _ _
# / __|___ _ _ __| |_ _ __(_)___ _ _
# | (__/ _ \ ' \/ _| | || (_-< / _ \ ' \
# \___\___/_||_\__|_|\_,_/__/_\___/_||_|
#
addConclusion
# ___ _
# / __|___ _ __ _ __ _ _ ___ _____(_)___ _ _
# | (__/ _ \ ' \| '_ \ '_/ -_|_-<_-< / _ \ ' \
# \___\___/_|_|_| .__/_| \___/__/__/_\___/_||_|
# |_|
#
compressAndRemoveSounds
# ___ _ _
# / _ \ _ _(_) |_
# | (_) | || | | _|
# \__\_\\_,_|_|\__|
#
# quit
printsep
printf "Appuyer sur la touche ${RED}Entrée${NC} pour relancer immédiatement"
if [ "$showTrace" == "yes" ]; then
printf " et effacer la trace Gnuplot"
fi
printf " ou '${GREEN}q${NC}' pour quitter : "
read doYouWantToContinue
attente=0
killGnuplotWindow
if [ "$doYouWantToContinue" == "" ]; then
clear
showApplicationLogo
fi
done