Spaces:
Runtime error
Runtime error
adding mixing into a function
#23
by Banuka - opened
mixing.py
CHANGED
|
@@ -1,33 +1,37 @@
|
|
| 1 |
import os
|
| 2 |
from pydub import AudioSegment
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
sound1_path =
|
| 20 |
-
sound2_path =
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
sound2_path = check_and_convert(sound2_path)
|
| 25 |
|
| 26 |
-
sound1
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
output
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from pydub import AudioSegment
|
| 3 |
|
| 4 |
+
def mixingAudio(sound1_path, sound2_path,position):
|
| 5 |
+
def check_and_convert(audio_file):
|
| 6 |
+
"""
|
| 7 |
+
Checks file format and converts to supported format if necessary.
|
| 8 |
+
"""
|
| 9 |
+
# Get file extension
|
| 10 |
+
extension = os.path.splitext(audio_file)[1].lower()
|
| 11 |
+
if extension not in (".mp3", ".wav", ".flac"):
|
| 12 |
+
# Convert to a supported format (e.g. mp3)
|
| 13 |
+
sound = AudioSegment.from_file(audio_file)
|
| 14 |
+
sound.export(os.path.splitext(audio_file)[0] + ".mp3", format="mp3")
|
| 15 |
+
# Update filename to the converted one
|
| 16 |
+
audio_file = os.path.splitext(audio_file)[0] + ".mp3"
|
| 17 |
+
return audio_file
|
| 18 |
|
| 19 |
+
# Check and convert audio files if needed
|
| 20 |
+
sound1_path = check_and_convert(sound1_path)
|
| 21 |
+
sound2_path = check_and_convert(sound2_path)
|
| 22 |
|
| 23 |
+
sound1 = AudioSegment.from_file(sound1_path)
|
| 24 |
+
sound2 = AudioSegment.from_file(sound2_path)
|
|
|
|
| 25 |
|
| 26 |
+
# Mix sound2 with sound1, starting at 1000ms into sound1
|
| 27 |
+
output = sound1.overlay(sound2, position)
|
| 28 |
|
| 29 |
+
# Save the result (assuming mp3 format)
|
| 30 |
+
output.export(r"mixedAudio\mixed_haptic_audioFile.mp3", format="mp3")
|
| 31 |
+
return "The audio have been mixed."
|
| 32 |
|
| 33 |
+
|
| 34 |
+
# for testing
|
| 35 |
+
# sound1_path = r"audio\mixkit-distant-explosion-1690.wav"
|
| 36 |
+
# sound2_path = r"audio\videoplayback_mastered (1).wav"
|
| 37 |
+
# mixingAudio(sound1_path, sound2_path,1000)
|