HussainLatiff Banuka commited on
Commit
09299d3
Β·
verified Β·
1 Parent(s): b4d49dc

adding mixing into a function (#23)

Browse files

- adding mixing into a function (4fd0604a63a5c19884013917290af0e4265b6b6c)


Co-authored-by: Banuka Nanayakkara <Banuka@users.noreply.huggingface.co>

Files changed (1) hide show
  1. mixing.py +29 -25
mixing.py CHANGED
@@ -1,33 +1,37 @@
1
  import os
2
  from pydub import AudioSegment
3
 
4
- def check_and_convert(audio_file):
5
- """
6
- Checks file format and converts to supported format if necessary.
7
- """
8
- # Get file extension
9
- extension = os.path.splitext(audio_file)[1].lower()
10
- if extension not in (".mp3", ".wav", ".flac"):
11
- # Convert to a supported format (e.g. mp3)
12
- sound = AudioSegment.from_file(audio_file)
13
- sound.export(os.path.splitext(audio_file)[0] + ".mp3", format="mp3")
14
- # Update filename to the converted one
15
- audio_file = os.path.splitext(audio_file)[0] + ".mp3"
16
- return audio_file
 
17
 
18
- # Using raw strings to avoid issues with backslashes
19
- sound1_path = r"audio\mixkit-distant-explosion-1690.wav"
20
- sound2_path = r"audio\videoplayback_mastered (1).wav"
21
 
22
- # Check and convert audio files if needed
23
- sound1_path = check_and_convert(sound1_path)
24
- sound2_path = check_and_convert(sound2_path)
25
 
26
- sound1 = AudioSegment.from_file(sound1_path)
27
- sound2 = AudioSegment.from_file(sound2_path)
28
 
29
- # Mix sound2 with sound1, starting at 1000ms into sound1
30
- output = sound1.overlay(sound2, position=1000)
 
31
 
32
- # Save the result (assuming mp3 format)
33
- output.export(r"mixedAudio\mixed_haptic_audioFile.mp3", format="mp3")
 
 
 
 
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)