-
Notifications
You must be signed in to change notification settings - Fork 8
/
audio.py
68 lines (50 loc) · 1.37 KB
/
audio.py
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
from pydub import AudioSegment
from pydub.utils import make_chunks
import speech_recognition
import numpy
import os
"""
Takes an audio file on disk, and turns it into multiple files
Unused
"""
def split_file(audioFile: str):
song = AudioSegment.from_mp3(audioFile)
splitSong = make_chunks(song, 100)
for i, splice in enumerate(splitSong):
name = "{1}{2}.wav".format(audioFile, audioFile, i)
splice.export(name, format="wav")
return
print(1)
"""
Takes an audio file name and returns a list of pitches for timestamps throughout the audio file
"""
def get_pitches(audioFile: str):
pitches = []
stream = os.popen('aubio pitch {}'.format(audioFile))
outputs = stream.readlines()
for line in outputs:
pitches.append(float(line.rstrip().split("\t")[1]))
return pitches
print(2)
"""
Takes two lists of integers and returns a score value
A: Actual pitches
B: User pitches
"""
def compare_pitches(a: list, b: list):
sum = 0
for i in range(len(a)):
# Prevents dividing by zero
if(a[i] == 0):
continue
if(i >= len(b)):
break
# Percentage difference between the i'th element in a and b
actual_pitch = a[i]
user_pitch = b[i]
if(abs(actual_pitch - user_pitch) <= 50):
sum += 1
print(3)
sum /= len(a)
sum *= 100
return sum