-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut.py
More file actions
65 lines (51 loc) · 1.37 KB
/
Copy pathcut.py
File metadata and controls
65 lines (51 loc) · 1.37 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
from scipy.io import wavfile
import matplotlib.pyplot as plt
import wave
import pyaudio
import numpy as np
TRESH = 200
DEBUG = False
def debug(param):
if DEBUG:
print(param)
# On indique le fichier à traiter
file = "record.wav"
# On ouvre le fichier
SPF = wave.open(file, "r")
# On vérifie que l'enregistrement est bien en mono
# Si pas on le passe du stéréo au mono
if SPF.getnchannels() != 1:
from pydub import AudioSegment
sound = AudioSegment.from_wav(file)
# On passe en mono
sound = sound.set_channels(1)
# On exporte le fichier mono
sound.export("nostereo.wav", format="wav")
# On remplace la variable par le nouveau fichier mono
file = "nostereo.wav"
samplerate, data = wavfile.read(file)
debug(type(data))
debug(data[0])
in_index = 0
out_index = 0
for i in range(0, len(data)):
if (data[i] > TRESH):
in_index = i
break
for o in range(0, len(data)):
if (data[-o] > TRESH):
out_index = -o
break
debug(in_index)
debug(out_index)
debug(f"longueur du signal: {len(data)}" )
debug(len(data) + out_index)
debug(f"Moyenne du signal: {sum(data)/len(data)}")
data_processed = data[int(in_index): len(data) + int(out_index)]
plt.subplot(1 ,2, 1)
plt.plot(data)
plt.subplot(1 ,2, 2)
plt.plot(data_processed)
plt.show()
scaled = np.int16(data_processed)
wavfile.write("test.wav", samplerate, scaled)