4

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.

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Noah Krasser
  • 1,081
  • 1
  • 14
  • 22

1 Answers1

10

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)
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • I have a question.When we invert the phase,shoudn't this help us in reducing the noise in the audio instead of muting the entire audio? Something similar to this link. https://en.wikipedia.org/wiki/Active_noise_control – Dhruv Marwha Dec 27 '19 at 05:38
  • Above example demonstrates total inversion of phase in a controlled manner (pre-recorded audio) with no ambient noise. In real life (realtime noise cancellation) this is not the case. There will be lag between original audio and inverted audio ,hence will not cause 100% noise cancellation instead will result in reduction of noise. Hope this helps. – Anil_M Dec 27 '19 at 18:11
  • I actually tried with an audio file but it muted my audio(Speech).The audio that I am using is also pre-recorded.It has no ambient noise present but some small disturbances are present. – Dhruv Marwha Dec 28 '19 at 09:42