When a voice-analyzed amateur listened to anti-phase music, it was confusing and quiet to visit.

I was interested in programming voice analysis, but I have never written it, so I tried it.

I'm an amateur, but I know the following, so I wanted to ask what would happen if the music was out of phase.

--Sound is a wave --If the phases are inverted and combined, they cancel each other out.

At first I thought I would write it in Go, but I gave up because I didn't have a nice package. ٩ (๑òωó๑) ۶ I'll write it someday

I used python because there was a library called pydub that contained everything I wanted to do. As expected Python. The analysis system is strong

I actually wrote it

Bang bang bang bang bang bang Bang bang bang bang Bang (∩` ・ ω ・) Bang Bang _ / _Mitsu /  ̄ ̄ ̄ /    \/___/ ̄ ̄

Preparation

#It seems to be used in pydub
brew install ffmpeg

pip install pydub

#Used to plot the waveform
pip install matplotlib

** Data used ** Chopin: Waltz No. 6 in D flat major Op.64-1 《Puppy Waltz》 http://classical-sound.seesaa.net/article/211557687.html Anything was fine if it was a short song

First thing I did

For the time being, I listened to the anti-phase music that was the original purpose.

from pydub import AudioSegment
from pydub.playback import play

#Load an audio file
myAudioFile = "test.mp3"
sound1 = AudioSegment.from_mp3(myAudioFile)

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

# play audio data
play(sound2)

(゚ д ゚) Poker (listen now ...)

\ (^ O ^) / ... I don't really understand the difference! !! \ (^ O ^) / ... Maybe it wasn't written well! ??

I was not sure, so I tried plotting the waveform

from pydub import AudioSegment
from pydub.playback import play
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

#Load an audio file
myAudioFile = "test.mp3"
sound1 = AudioSegment.from_mp3(myAudioFile)

plot audio data
data = np.array(sound1.get_array_of_samples())
x = data[::sound1.channels]
plt.plot(x[::100000])
plt.grid()
plt.show()

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

plot audio data
data = np.array(sound2.get_array_of_samples())
xr = data[::sound2.channels]
plt.plot(xr[::100000])
plt.grid()
plt.show()

Entered data Figure_1.png

Opposite phase Figure_2.png

Certainly the phase is reversed.

Is it really out of phase?

Still, I was worried, so I tried to synthesize music data. If it is really out of phase, it should be silent if you combine the input music data with the data that is out of phase! !!

from pydub import AudioSegment
from pydub.playback import play

#Load an audio file
myAudioFile = "test.mp3"
sound1 = AudioSegment.from_mp3(myAudioFile)

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

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

# play audio data
play(combined)

Oh, silence! !! Σ (゚ □ ゚)

Plot result after synthesis

Figure_3.png

Final form

from pydub import AudioSegment
from pydub.playback import play
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

#Load an audio file
myAudioFile = "test.mp3"
sound1 = AudioSegment.from_mp3(myAudioFile)

plot audio data
data = np.array(sound1.get_array_of_samples())
x = data[::sound1.channels]
plt.plot(x[::100000])
plt.grid()
plt.show()

# play
play(sound1)

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

plot audio data
data = np.array(sound2.get_array_of_samples())
xr = data[::sound2.channels]
plt.plot(xr[::100000])
plt.grid()
plt.show()

# play audio data
play(sound2)

# Export merged audio file
sound2.export("invert_phase.mp3", format="mp3")

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

data = np.array(combined.get_array_of_samples())
xr = data[::combined.channels]
plt.plot(xr[::100000])
plt.grid()
plt.show()

# play audio data
play(combined)

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

Conclusion

It feels strange, but I couldn't tell the difference just by listening to the anti-phase music. I will study a little more and start again. I think that you can reproduce it by tracing the article, so if you are interested, please try it

Recommended Posts

When a voice-analyzed amateur listened to anti-phase music, it was confusing and quiet to visit.
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
It was dangerous to specify a relative path when generating a symbolic link
I set up TensowFlow and was addicted to it, so make a note
I was surprised to receive a nice review when I wrote Python to CheckIO and its explanation