-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoc_audio.py
More file actions
126 lines (110 loc) · 4.09 KB
/
Copy pathpoc_audio.py
File metadata and controls
126 lines (110 loc) · 4.09 KB
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
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wavfile
import os
import sys
import time
import shutil
import requests
import argparse
import logging
from pathlib import Path
from typing import Optional
# Paramètres globaux
SAMPLE_RATE = 16000
CHANNELS = 1
SEGMENT_DURATION = 60 # secondes (1 minute pour le POC)
CLOUD_SIM_DIR = "cloud_sim"
CHECK_URL = "https://www.google.com" # Pour simuler la vérif 4G
# Mode Sim
parser = argparse.ArgumentParser()
parser.add_argument('--simulate', action='store_true', help="Utiliser le mode simulation")
args = parser.parse_args()
USE_SIMULATION = args.simulate
os.makedirs(CLOUD_SIM_DIR, exist_ok=True)
class AudioRecorder:
"""Gestionnaire d'enregistrement audio"""
def __init__(self):
self.logger = logging.getLogger(__name__)
self._setup_logging()
def _setup_logging(self):
"""Configure les logs"""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('audio_recorder.log'),
logging.StreamHandler(sys.stdout)
]
)
def record_segment(self, segment_num: int) -> Optional[Path]:
"""Enregistre un segment audio avec gestion d'erreurs"""
try:
filename = Path(CLOUD_SIM_DIR) / f"segment_{segment_num}.wav"
if USE_SIMULATION:
return self._record_simulation(filename)
return self._record_real(filename)
except Exception as e:
self.logger.error(f"Erreur d'enregistrement: {e}")
return None
def _record_real(self, filename: Path) -> Path:
"""Enregistre l'audio depuis le microphone."""
try:
self.logger.info("Enregistrement réel...")
audio = sd.rec(int(SEGMENT_DURATION * SAMPLE_RATE),
samplerate=SAMPLE_RATE,
channels=CHANNELS, dtype='int16')
sd.wait()
wavfile.write(filename, SAMPLE_RATE, audio)
self.logger.info(f"Fichier enregistré : {filename}")
return filename
except Exception as e:
self.logger.error(f"Erreur lors de l'enregistrement: {str(e)}")
raise
def _record_simulation(self, filename: Path) -> Path:
"""Simule un enregistrement audio en générant une onde sinusoïdale."""
try:
self.logger.info("Simulation d'enregistrement...")
t = np.linspace(0, SEGMENT_DURATION,
int(SAMPLE_RATE * SEGMENT_DURATION), False)
audio = 0.5 * np.sin(2 * np.pi * 440 * t)
audio = (audio * 32767).astype(np.int16)
wavfile.write(filename, SAMPLE_RATE, audio)
self.logger.info(f"Fichier simulé : {filename}")
return filename
except Exception as e:
self.logger.error(f"Erreur lors de la simulation: {str(e)}")
raise
def is_connected():
try:
requests.get(CHECK_URL, timeout=3)
return True
except:
return False
def replay_audio(filename):
# Read the WAV file
fs, data = wavfile.read(filename)
logging.info(f"Lecture du fichier {filename} à {fs} Hz")
sd.play(data, fs)
sd.wait()
logging.info("Lecture terminée.")
def main():
try:
recorder = AudioRecorder()
segment_num = 1
while segment_num <= 3:
if not is_connected():
logging.warning("Connexion 4G perdue, tentative de reconnexion...")
time.sleep(5)
continue
if segment_file := recorder.record_segment(segment_num):
segment_num += 1
logging.info(f"Segment {segment_num} enregistré avec succès")
else:
logging.error(f"Échec de l'enregistrement du segment {segment_num}")
time.sleep(2)
except Exception as e:
logging.error(f"Erreur critique: {e}")
sys.exit(1)
if __name__ == "__main__":
main()