-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio.py
More file actions
57 lines (42 loc) · 1.39 KB
/
audio.py
File metadata and controls
57 lines (42 loc) · 1.39 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
#!/usr/bin/python3
"""Audio handling
"""
from __future__ import print_function
from pydub import AudioSegment
import librosa.util
def mp3_to_wav(mp3_filename, wav_filename):
y,sr = librosa.load(mp3_filename)
librosa.output.write_wav(wav_filename, y, sr)
def mixing_librosa(filename1, filename2, outputfile, crop2=True):
import librosa
import librosa.output
y1, sr1 = librosa.load(filename1)
y2, sr2 = librosa.load(filename2, sr=sr1)
if crop2: y2 = y2[:y1.shape[0]]
yout = (y1 + y2) / 2.0
librosa.output.write_wav(outputfile, yout, sr1)
def mixing(wavfile1, wavfile2, outputfile, crop2=True):
"""Mix two wav files.
Args:
wavfile1(str): file path of the wave file
wavfile2(str): file path of the wave file
crop2(boolean): crop the second audio file according to the first
Returns:
str: output file
"""
sound1 = AudioSegment.from_mp3(wavfile1)
sound2 = AudioSegment.from_mp3(wavfile2)
sec1 = sound1.duration_seconds
sec2 = sound2.duration_seconds
if crop2:
sound2 = sound2[:sec1*1000]
combined = sound1.overlay(sound2)
combined.export(outputfile, format='mp3')
def main():
wavfile1 = 'twiliorecording.wav'
wavfile2 = 'data/heavymetal.mp3'
outputfile = 'out.wav'
#mixing(wavfile1, wavfile1, outputfile)
mixing_librosa(wavfile1, wavfile2, outputfile)
if __name__ == "__main__":
main()