How to get Pi-Phase from sound to get an destructive interference in Python

元气小坏坏 提交于 2020-01-06 02:28:10

问题


first: I don't know where to put this topic because it's an programming and sound-question. Please comment if it's at the wrong place.

But this is my question: How can I load a sound into Python and create the "reverse-sound" of it. So when I play the original and the "pi-shifted" file, they create an destructive interference and cancel each other out so you hear almost nothing. Are there any Libraries to use?

Here's a small explanation-video.

Thank you a lot. Just want to experiment a little.


回答1:


Easiest ways to load audio in python is using external library modules. Once such module is pydub. See here for details.

Next, what you are talking about is reversing phase of input sound such that when one adds two sounds with inverse phase, they cancel each other.
Same principal is used for noise cancelling technology. See details here

Below is a sample code that demonstrates phase cancelling effect by merging two sound of opposite phases.

Demo Code

from pydub import AudioSegment
from pydub.playback import play

#Load an audio file
myAudioFile = "yourAudioFile.wav"
sound1 = AudioSegment.from_file(myAudioFile, format="wav")

#Invert phase of audio file
sound2 = sound1.invert_phase()

#Merge two audio files
combined = sound1.overlay(sound2)

#Export merged audio file
combined.export("outAudio.wav", format="wav")

#Play audio file :
#should play nothing since two files with inverse phase cancel each other
mergedAudio = AudioSegment.from_wav("outAudio.wav")
play(mergedAudio)


来源:https://stackoverflow.com/questions/36432343/how-to-get-pi-phase-from-sound-to-get-an-destructive-interference-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!