python中加入声音_在python中同时播放多个声音

本文介绍了如何使用pydub模块在Python程序中实现同时播放多个声音,通过AudioSegment的overlay功能叠加音频文件,解决了等待音频播放完成的问题,适合于交互式程序需求。
摘要由CSDN通过智能技术生成

I have been looking into a way to play sounds from a list of samples, and I found some modules that can do this.

I am using AudioLazy module to play the sound using the following script:

from audiolazy import AudioIO

sound = Somelist

with AudioIO(True) as player:

player.play(sound, rate=44100)

The problem with this code is that it stop the whole application till the sound stop playing and I can't play multiple sound at the same time.

My program is interactive so what I want is to be able to play multiple sound at the same time,So for instance I can run this script which will play a 5 second sound then at the second 2 I can play a 5 second sound again.

And I don't want the whole program to stop till the sound finish playing.

解决方案

Here is a simpler solution using pydub.

Using overlay function of AudioSegment module, you can very easily superimpose multiple audio on to each other.

Here is a working code to combine three audio files. Using same concept you can combine multiple audio onto each other.

More on overlay function here

pydub supports multiple audio formats as well.

from pydub import AudioSegment

from pydub.playback import play

audio1 = AudioSegment.from_file("chunk1.wav") #your first audio file

audio2 = AudioSegment.from_file("chunk2.wav") #your second audio file

audio3 = AudioSegment.from_file("chunk3.wav") #your third audio file

mixed = audio1.overlay(audio2) #combine , superimpose audio files

mixed1 = mixed.overlay(audio3) #Further combine , superimpose audio files

#If you need to save mixed file

mixed1.export("mixed.wav", format='wav') #export mixed audio file

play(mixed1) #play mixed audio file

Here are updates as per our discussions.

First we create 44KHz signal and save to sound.wav

Next Read wave file and save signal to text file

Then create three variations of input signal to test overlay.

Original signal has dtype int16

Then we create three audio segments

then mix/overlay as above.

wav signal data is stored in test.txt

Working Modified Code

import numpy as np

from scipy.io.wavfile import read

from pydub import AudioSegment

from pydub.playback import play

import wave, struct, math

#Create 44KHz signal and save to 'sound.wav'

sampleRate = 44100.0 # hertz

duration = 1.0 # seconds

frequency = 440.0 # hertz

wavef = wave.open('sound.wav','w')

wavef.setnchannels(1) # mono

wavef.setsampwidth(2)

wavef.setframerate(sampleRate)

for i in range(int(duration * sampleRate)):

value = int(32767.0*math.cos(frequency*math.pi*float(i)/float(sampleRate)))

data = struct.pack('

wavef.writeframesraw( data )

wavef.writeframes('')

wavef.close()

#Read wave file and save signal to text file

rate, signal = read("sound.wav")

np.savetxt('test.txt', signal, delimiter=',') # X is an array

#load wav data from text file

wavedata1 = np.loadtxt("test.txt", comments="#", delimiter=",", unpack=False, dtype=np.int16)

#Create variation of signal

wavedata2 = np.loadtxt("test.txt", comments="#", delimiter=",", unpack=False, dtype=np.int32)

#Create variation of signal

wavedata3 = np.loadtxt("test.txt", comments="#", delimiter=",", unpack=False, dtype=np.float16)

#create first audio segment

audio_segment1 = AudioSegment(

wavedata1.tobytes(),

frame_rate=rate,

sample_width=2,

channels=1

)

#create second audio segment

audio_segment2 = AudioSegment(

wavedata2.tobytes(),

frame_rate=rate,

sample_width=2,

channels=1

)

#create third audio segment

audio_segment3 = AudioSegment(

wavedata3.tobytes(),

frame_rate=rate,

sample_width=2,

channels=1

)

# Play audio (requires ffplay, or pyaudio):

play(audio_segment1)

play(audio_segment2)

play(audio_segment3)

#Mix three audio segments

mixed1 = audio_segment1.overlay(audio_segment2) #combine , superimpose audio files

mixed2 = mixed1.overlay(audio_segment3) #Further combine , superimpose audio files

#If you need to save mixed file

mixed2.export("mixed.wav", format='wav') #export mixed audio file

play(mixed2) #play mixed audio file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值