SE-09/HapticsProject
2
1import os2from pydub import AudioSegment3 4def 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 one16 audio_file = os.path.splitext(audio_file)[0] + ".mp3"17 return audio_file18 19 # Check and convert audio files if needed20 sound1_path = check_and_convert(sound1_path)21 sound2_path = check_and_convert(sound2_path)22 23 # Load audio files24 sound1 = AudioSegment.from_file(sound1_path)25 sound2 = AudioSegment.from_file(sound2_path)26 27 # Mix sound2 with sound1, starting at 1000ms into sound128 output = sound1.overlay(sound2, position)29 30 # Save the result (assuming mp3 format)31 output.export(r"mixedAudio\mixed_haptic_audioFile.mp3", format="mp3")32 return "The audio has been mixed."