fcts.py 3.67 KB
Newer Older
Candre Nathan's avatar
Candre Nathan committed
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 10:04:06 2021

@author: lucasdaniele
"""

from scipy.io import wavfile
import matplotlib.pyplot as plt
import numpy as np
12
import frequency_estimator as f0
LucasDANIELE's avatar
LucasDANIELE committed
13
import os
Candre Nathan's avatar
Candre Nathan committed
14
15
16
17
18
19
20
21
22
23
24


def listen(nom,sampleRate, data):
    wavfile.write(nom, sampleRate, data.real)
    
def aff(titre,x,y):
    if(len(x)==len(y)):
        plt.plot(x, y.real, "-")
    else:
        plt.plot(y.real, "-")
    plt.title(titre)
25
26
    plt.show()

LucasDANIELE's avatar
LucasDANIELE committed
27
def decouperExtraits(data, seuil, dureeSilence, sampleRate, sampleRateChoosen):
Candre Nathan's avatar
Candre Nathan committed
28
29
    DATA=[]
    onAir=False
LucasDANIELE's avatar
LucasDANIELE committed
30
31
32
    step=int(np.round(sampleRateChoosen*dureeSilence))
    echelle = sampleRate/sampleRateChoosen
    dureeExtrait = len(data)/sampleRate
Candre Nathan's avatar
Candre Nathan committed
33
    k=0
LucasDANIELE's avatar
LucasDANIELE committed
34
35
36
    d=0
    f=0
    for i in range(0,int(np.round(dureeExtrait)*sampleRateChoosen-step)-1000):
LucasDANIELE's avatar
LucasDANIELE committed
37
38
        if (i%40000==0):
            afficherAvancement(i,int(np.round(dureeExtrait)*sampleRateChoosen-step)-1000)
LucasDANIELE's avatar
LucasDANIELE committed
39
        if ((abs(data[int(np.round(i*echelle))])<seuil) & onAir):
Candre Nathan's avatar
Candre Nathan committed
40
41
42
43
44
            if(k<step):
                k+=1
            else:
                k=0
                onAir=False
LucasDANIELE's avatar
LucasDANIELE committed
45
46
47
48
                f=int(np.round(i*echelle))
                DATA.append([d,f])
                d=0
                f=0
Candre Nathan's avatar
Candre Nathan committed
49
        else:
LucasDANIELE's avatar
LucasDANIELE committed
50
            if(abs(data[int(np.round(i*echelle))])>=seuil):
Candre Nathan's avatar
Candre Nathan committed
51
52
                if(not(onAir)):
                    onAir=True
LucasDANIELE's avatar
LucasDANIELE committed
53
                    d=int(np.round(i*echelle))
Candre Nathan's avatar
Candre Nathan committed
54
                k=0
LucasDANIELE's avatar
LucasDANIELE committed
55
56
    if(d>0):
        DATA.append([d,len(data)-1])
Candre Nathan's avatar
Candre Nathan committed
57
    return DATA    
LucasDANIELE's avatar
LucasDANIELE committed
58

LucasDANIELE's avatar
LucasDANIELE committed
59
60
61
62
63
64
def afficherAvancement(t,T):
    os.system('clear')
    barre = "|"
    for i in range(int(np.round(np.round(t/T,2)*50))):
        barre += "#"
    for i in range(50-int(np.round(np.round(t/T,2)*50))):
LucasDANIELE's avatar
LucasDANIELE committed
65
        barre += "_"
LucasDANIELE's avatar
LucasDANIELE committed
66
67
68
    barre +="| "
    barre += str(np.round(t/T,2)) + " %"
    print(barre)
Candre Nathan's avatar
Candre Nathan committed
69
        
LucasDANIELE's avatar
LucasDANIELE committed
70
def garderUniquementCris(sampleRate,tMin, tMax, fMin, fMax, f0Func, dataCut, data):
Candre Nathan's avatar
Candre Nathan committed
71
72
73
    nSamplesMin = tMin*sampleRate 
    nSamplesMax = tMax*sampleRate
    res=[]
74
    freqs=[]
Candre Nathan's avatar
Candre Nathan committed
75
    for i in range(len(dataCut)):
LucasDANIELE's avatar
LucasDANIELE committed
76
77
78
79
        d=dataCut[i][0]
        f=dataCut[i][1]
        if((f-d>nSamplesMin) & (f-d<nSamplesMax)):
            freq = np.round(f0Func(np.asarray(data[d:f]),sampleRate))
Candre Nathan's avatar
Candre Nathan committed
80
            if((freq>fMin) & (freq<fMax)):
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
                quantiteDeSonA5Pourcents = np.sum(abs(data[d:d+int(np.round(0.05*(f-d)))]))
                quantiteDeSon = np.sum(abs(data[d:f]))
                if (quantiteDeSonA5Pourcents / quantiteDeSon > 0.065):
                    res.append(dataCut[i])
                    freqs.append(freq)
                    
                    listen("output/cri"+str(i)+".wav", sampleRate, np.asarray(data[d:f]))
                    print("*************************")
                    print("* enregistrement: " + str(i))
                    print("*************************")
                    print("*   f0 -> " + str(freq))
                    print("*   duration -> " + str((f-d)/sampleRate))  
                    print("*   quantité de son à 5% -> " + str(np.sum(abs(data[d:d+int(np.round(0.05*(f-d)))]))/np.sum(abs(data[d:f]))))    
                    print("*************************")
                    print("")
                    print("_________________________")
                    print("")
98
    
99
    return res, freqs
Candre Nathan's avatar
Candre Nathan committed
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