Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 474
Noah Krasser Avatar asked Jan 06 '23 08:01

Noah Krasser


1 Answers

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)
like image 136
Anil_M Avatar answered Jan 31 '23 02:01

Anil_M