Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PRONTO-Mangeoire2025
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
La purge estivale des projets de gitlab-df sera réalisée jeudi 10 juillet vers 10h.
Show more breadcrumbs
MIRANDA GONZALES Marcelo
PRONTO-Mangeoire2025
Commits
8f270712
Commit
8f270712
authored
1 month ago
by
MIRANDA GONZALES Marcelo
Browse files
Options
Downloads
Patches
Plain Diff
Delete seuilACTfiltre.py
parent
5176703b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
seuilACTfiltre.py
+0
-109
0 additions, 109 deletions
seuilACTfiltre.py
with
0 additions
and
109 deletions
seuilACTfiltre.py
deleted
100644 → 0
+
0
−
109
View file @
5176703b
import
sounddevice
as
sd
import
numpy
as
np
import
soundfile
as
sf
from
collections
import
deque
from
datetime
import
datetime
from
scipy.signal
import
butter
,
lfilter
# --- Paramètres audio ---
RATE
=
44100
CHANNELS
=
2
THRESHOLD
=
-
50.0
# Ajuste ce seuil selon tes tests
duration_before
=
5
# secondes avant événement
duration_after
=
5
# secondes après l'événement
# --- Buffers ---
buffer
=
deque
(
maxlen
=
int
(
RATE
*
duration_before
))
intensity_buffer
=
deque
(
maxlen
=
int
(
RATE
*
5
))
# Buffer intensité pour 5 secondes
# --- Filtrage passe-haut Butterworth ---
def
butter_highpass
(
cutoff
,
fs
,
order
=
4
):
nyq
=
0.5
*
fs
normal_cutoff
=
cutoff
/
nyq
return
butter
(
order
,
normal_cutoff
,
btype
=
'
high
'
,
analog
=
False
)
def
highpass_filter
(
data
,
cutoff
=
1500.0
,
fs
=
44100
,
order
=
4
):
b
,
a
=
butter_highpass
(
cutoff
,
fs
,
order
=
order
)
return
lfilter
(
b
,
a
,
data
)
# --- Fonction pour sauvegarder un fichier FLAC ---
def
save_wav
(
filename
,
data
,
samplerate
):
filename
=
filename
.
replace
(
'
.wav
'
,
'
.flac
'
)
sf
.
write
(
filename
,
data
,
samplerate
,
format
=
'
FLAC
'
)
print
(
f
"
Audio enregistré sous
{
filename
}
"
)
# --- Fonction score ---
def
score
(
x
,
threshold
,
axis
=
0
):
x
=
np
.
asarray
(
x
)
x
=
x
>=
threshold
count
=
sum
(
x
,
axis
)
s
=
sum
(
x
,
axis
)
/
x
.
shape
[
axis
]
return
s
,
count
# --- Activité acoustique ---
def
acoustic_activity
(
xdB
,
dB_threshold
,
axis
=
1
):
ACTfract
,
ACTcount
=
score
(
xdB
,
dB_threshold
,
axis
=
axis
)
ACTfract
=
ACTfract
.
tolist
()
ACTcount
=
ACTcount
.
tolist
()
ACTmean
=
dB2linear
(
xdB
[
xdB
>
dB_threshold
],
mode
=
'
power
'
)
return
ACTfract
,
ACTcount
,
ACTmean
# --- Conversion dB vers linéaire ---
def
dB2linear
(
x
,
mode
=
'
amplitude
'
,
db_gain
=
0
):
if
mode
==
'
amplitude
'
:
y
=
10
**
((
x
-
db_gain
)
/
20
)
elif
mode
==
'
power
'
:
y
=
10
**
((
x
-
db_gain
)
/
10
)
return
y
# --- Callback pour audio continu ---
def
audio_callback
(
indata
,
frames
,
time
,
status
):
global
buffer
,
intensity_buffer
buffer
.
extend
(
indata
[:,
0
])
intensity_buffer
.
extend
(
indata
[:,
0
])
# --- Lancement du stream audio ---
with
sd
.
InputStream
(
samplerate
=
RATE
,
channels
=
1
,
callback
=
audio_callback
,
blocksize
=
1024
):
print
(
"
Début de l
'
écoute en continu. Appuyez sur Ctrl+C pour arrêter.
"
)
try
:
while
True
:
sd
.
sleep
(
1000
)
# actualisation chaque seconde
# --- Analyse d'intensité ---
current_window
=
highpass_filter
(
np
.
array
(
intensity_buffer
),
cutoff
=
1500.0
,
fs
=
RATE
)
# --- Calcul de l'activité acoustique ---
xdB
=
10
*
np
.
log10
(
np
.
abs
(
current_window
)
**
2
+
1e-11
)
_
,
ACT
,
_
=
acoustic_activity
(
xdB
,
dB_threshold
=
THRESHOLD
+
12
,
axis
=-
1
)
ACT
=
np
.
sum
(
np
.
asarray
(
ACT
))
/
RATE
print
(
f
"
Activité acoustique:
{
ACT
:
.
2
f
}
"
)
intensity_buffer
.
clear
()
if
ACT
>
0.6
:
# Seuil ajustable
print
(
"
Son détecté, enregistrement en cours...
"
)
# --- Pré-enregistrement ---
previous_audio
=
np
.
array
(
buffer
)
# --- Enregistrement post-détection ---
next_audio
=
sd
.
rec
(
int
(
RATE
*
duration_after
),
samplerate
=
RATE
,
channels
=
1
)
sd
.
wait
()
combined_audio
=
np
.
concatenate
((
previous_audio
,
next_audio
[:,
0
]))
# --- Filtrage de l'audio complet ---
filtered_audio
=
highpass_filter
(
combined_audio
,
cutoff
=
1500.0
,
fs
=
RATE
)
# --- Sauvegarde ---
filename
=
datetime
.
now
().
strftime
(
"
record_%Y%m%d_%H%M%S.wav
"
)
save_wav
(
filename
,
filtered_audio
,
RATE
)
buffer
.
clear
()
intensity_buffer
.
clear
()
print
(
"
Écoute relancée...
"
)
except
KeyboardInterrupt
:
print
(
"
Arrêt de l
'
écoute.
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment