-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
62 lines (46 loc) · 1.93 KB
/
helpers.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
import os
from librosa import clicks
from librosa.beat import beat_track
import soundfile as sf
from pydub import AudioSegment
from scipy.io import wavfile as wav
import numpy as np
ALLOWED_EXTENSIONS = {'wav', 'mp3'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def generate_click(file, filename, click_freq, click_duration, vol_adj_song, vol_adj_click, convert_folder):
saveFileExt = filename.rsplit('.', 1)[1].lower()
saveName = filename
if(saveFileExt != "wav"):
saveName = saveName.rsplit('.', 1)[0].lower() + ".wav"
inputAudio, _, newName = convert_file(file, filename, saveName, convert_folder)
sr = 44100
_, beats = beat_track(inputAudio, sr)
clickAudio = clicks(
frames = beats, # the beats to place clicks
sr = sr, # sample rate
length = len(inputAudio), # length of the song (necessary to align clicktrack and song)
click_freq = click_freq, # frequency of each click (in Hz)
click_duration = click_duration # duration of each click (in seconds)
)
inputAudio *= vol_adj_song
clickAudio *= vol_adj_click
sf.write(os.path.join(convert_folder, newName), inputAudio + clickAudio, sr)
return newName
def convert_file(file, filename, saveName, convert_folder):
extension = filename.rsplit('.', 1)[1].lower()
if(extension == "mp3"):
audio = AudioSegment.from_mp3(file)
elif(extension == "wav"):
audio = AudioSegment.from_wav(file)
else:
audio = AudioSegment.from_file(file, extension)
audio.export(os.path.join(convert_folder, saveName), format = "wav", bitrate = '16k')
sr, data = wav.read(os.path.join(convert_folder, saveName))
# Converts to mono to keep file small and usable with beat_track
if len(data.shape) == 2:
data = data.sum(axis=1) / 2
# Normalize amplitudes
data = data / np.max(np.abs(data))
return(data, sr, saveName)