Python simple visualization of music - extraction spectrum method

3e9cb90cd703152805a8397e7b2245b1.png

Have you ever wondered how the spectral effects of some music software are made, and why they are so beautiful? Have you ever wanted to try to extract the music spectrum yourself and visualize it? Today, let’s combine the last music editing operation:

Python editing music is so simple

Let’s crudely visualize the spectrum of the song below!

1. Prepare

Before starting, you need to make sure that Python and pip have been successfully installed on your computer. If not, please visit this article: Super detailed Python installation guide  to install.

Open Cmd (Start-Run-CMD) in Windows environment, open Terminal (command+space to enter Terminal) in Apple system environment, and prepare to start typing commands to install dependencies.

Of course, I recommend you to use the VSCode editor, copy the code of this article, and install the dependent modules in the terminal below the editor. What a comfortable thing: The best partner for Python programming—VSCode detailed guide .

Enter the following command in the terminal to install the dependent modules we need:

pip install pydub
pip install librosa

If you see Successfully installed xxx, the installation is successful.

2. Spectrum display

Using librosa and matplot, we can display the entire spectrum in 10 lines of code:

52bdf536bb3d66ca3d91ad31e3da98c0.png

import matplotlib.pyplot as plt
import librosa.display
# 音乐文件载入
audio_path = 'Fenn.mp3'
music, sr = librosa.load(audio_path)
# 宽高比为14:5的图
plt.figure(figsize=(14,5))
librosa.display.waveplot(music, sr=sr)
# 显示图
plt.show()

However, such a spectrum is the whole piece of music, which looks very ugly. Next, we use pydub to cut the spectrum to get better results. Let's break down into 0 to 1 second segments to see the spectrum:

7ec7ee24343b5e697ab53d626ae11556.png

import matplotlib.pyplot as plt
import librosa.display
import numpy as np
from pydub import AudioSegment
# 1秒=1000亳秒
SECOND = 1000
#音乐文件
AUDIO_PATH = 'Fenn.mp3'

def split_music(begin, end, filepath):
    # 导儿音乐
    song = AudioSegment.from_mp3(filepath)
    # 取begin秒至ijend秒间的片段
    song = song[begin*SECOND: end*SECOND]
    # 存储为临时文件做备份
    temp_path = 'backup/'+filepath
    song.export(temp_path)
    return temp_path
music, sr = librosa.load(split_music(0, 1, AUDIO_PATH))
#宽高比为14:5的图
plt.figure(figsize=(14, 5))
librosa.display.waveplot(music, sr=sr)
plt‍.show()

This is detailed, but it is still too complicated. In fact, when we display the spectrum, we may only need positive values:

4fb8eff1b6cd95aec987704409db8404.png

Then we can further zoom in, say the spectrum between 0.9 seconds and 1 second:

bdac2170261090dfd8907a9b9c86f8a1.png

# 公众号:Python 实用宝典
n0 = 9000
n1 = 10000
music = np.array([mic for mic in music if mic > 0])
plt.figure(figsize=(14, 5))
pit.plot(music[n0:n1])
plt.grid()
#显示图
plt.show()

This looks much better, but if you want to achieve the effect of QQ music, you still need to make a lot of modifications.

For example, use exquisite image elements to fill the replacement, and then how to deal with the zero value? How to make the spectrum more stable? In addition, we are a static image, and we need to dynamically continue the band according to the event.

The code for production is definitely more complex than our simple code, and it shouldn't be brute force to remove negative values ​​​​to draw images. Interested readers can do their own research.

If you like today's Python tutorial, please continue to pay attention to Python Practical Collection. If it is helpful to you, please like/watch below. If you have any 66e820eefbe9770da7f9fb48ca274b57.pngquestions, you can leave a message below, and we will patiently answer them!

Click below to read the original text to get all the codes and links!

Python Practical Dictionary (pythondict.com)

not just a tome

Welcome to pay attention to the public account: Python Practical Collection

b9423559ca36c049243a351757bd94a6.png

Guess you like

Origin blog.csdn.net/u010751000/article/details/125270358