0

Pydub documentation

I am trying to mix 2 mp3 files with microphone input using pydub and pyaudio. Here is what i have done:

from pydub import AudioSegment
from pydub.playback import play

sound1 = AudioSegment.from_file(r"ΑΓΙΑ ΣΚΕΠΗ.mp3")
sound1_channels = sound1.split_to_mono()
sound1 = sound1_channels[0].overlay(sound1_channels[1])
sound1 = sound1 - 48 # make sound1 quiter 48dB

sound2 = AudioSegment.from_file(r"ΑΓΙΑ ΚΥΡΙΑΚΗ.mp3")
sound2_channels = sound2.split_to_mono()
sound2 = sound2_channels[0].overlay(sound2_channels[1])
sound2 = sound2 - 48 # make sound2 quiter 48dB

import pyaudio

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024

p = pyaudio.PyAudio()
player = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True,frames_per_buffer=CHUNK)
mic_stream = p.open(format=FORMAT, channels=CHANNELS,rate=RATE, input=True,frames_per_buffer=CHUNK)

chunk_time_in_seconds = int(RATE/CHUNK)
chunk_number = 0

while(True):
    mic_data = mic_stream.read(CHUNK)
    mic_sound = AudioSegment(mic_data, sample_width=2, channels=1, frame_rate=RATE)
    
    sound1_part = sound1[chunk_number*chunk_time_in_seconds:(chunk_number+1)*chunk_time_in_seconds]
    sound2_part = sound2[chunk_number*chunk_time_in_seconds:(chunk_number+1)*chunk_time_in_seconds]
    
    
    
    #player.write(mic_sound.raw_data) works well
    mix_sound = sound1_part.overlay(sound2_part).overlay(mic_sound)
    player.write(mix_sound.raw_data) # low microphone quality
    
    chunk_number = chunk_number+1

The above code seems to work, but it output microphone input with low quality.

Can you please help me about?

Chris P
  • 2,059
  • 4
  • 34
  • 68
  • 1
    Does it work if you mix three mp3? This could help to determine if the issue is specific to the mic input or is it possibly the mixing method... – voyager42 Feb 24 '21 at 16:17

1 Answers1

0

I don't know if this aproach is good for real time audio processing but the following code works well!

from pydub import AudioSegment
from pydub.playback import play

sound1 = AudioSegment.from_file(r"ΑΓΙΑ ΣΚΕΠΗ.mp3")
sound1_channels = sound1.split_to_mono()
sound1 = sound1_channels[0].overlay(sound1_channels[1])
sound1 = sound1 - 30 # make sound1 quiter 30dB

sound2 = AudioSegment.from_file(r"ΑΓΙΑ ΚΥΡΙΑΚΗ.mp3")
sound2_channels = sound2.split_to_mono()
sound2 = sound2_channels[0].overlay(sound2_channels[1])
sound2 = sound2 - 30 # make sound2 quiter 30dB

import pyaudio

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024

p = pyaudio.PyAudio()
player = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True,frames_per_buffer=CHUNK)
mic_stream = p.open(format=FORMAT, channels=CHANNELS,rate=RATE, input=True,frames_per_buffer=CHUNK)

#chunk_time_in_seconds = int(RATE/CHUNK)
chunk_number = 0

while(True):
    mic_data = mic_stream.read(CHUNK)
    mic_sound = AudioSegment(mic_data, sample_width=2, channels=1, frame_rate=RATE)
    mic_sound_duration = len(mic_sound)
    
    sound1_part = sound1[chunk_number*mic_sound_duration:(chunk_number+1)*mic_sound_duration]
    sound2_part = sound2[chunk_number*mic_sound_duration:(chunk_number+1)*mic_sound_duration]
    
    
    
    #player.write(mic_sound.raw_data) works well
    mix_sound = sound1_part.overlay(sound2_part).overlay(mic_sound)
    player.write(mix_sound.raw_data) # low microphone quality
    
    chunk_number = chunk_number+1
Chris P
  • 2,059
  • 4
  • 34
  • 68