본문 바로가기
음악 표절 연구

[Audio PreProcessing] 오디오(음악 mp3, wav) 데이터 필요한 부분 스플라이싱 및 시각화, 들어보기, 저장 __ librosa.load / iPython.display.Audio / plt.plot

by Le Hayyim 2023. 8. 9.
반응형

1. 오디오 음악 데이터를 librosa를 통해 불러온다.

librosa를 import하고,

first, import librosa library

import librosa

librosa.load 함수를 이용하여, mp3및 wav 파일을 파이썬으로 불러온다. 

using a librosa.load function, import Import mp3 and wav files into Python.

audio_SD = "/content/audios/shutdown_MV.mp3"
SD_y, SD_sr = librosa.load(audio_SD, sr=None, offset=0, duration=16.0)

audio_LA = "/content/audios/LaCampanella.mp3"
LA_y, LA_sr = librosa.load(audio_LA, sr=None, offset=0, duration=12.0)

print(SD_sr, LA_sr)

그러면 오디오 series 데이터와 sample rate(sr)로 나뉜다. 

다른 레퍼런스에서는 보편적으로 y, sr로 표현하니 참고. 

It is then divided into audio series data and sample rate(sr). 
Note that in other references it is commonly expressed as y, sr.

duration을 조절하여 오디오파일을 처음에서부터 몇초까지 자를지 정할 수 있다. 

You can adjust the duration to decide how many seconds to cut the audio file from the beginning.

 

 

 

2. 잘린 오디오파일 들어보기 

아래와 같이 Ipython을 이용하여 오디오 파일로 들어볼 수 있다. 

You can listen to it as an audio file using Ipython as follows.

import IPython.display as ipd

위와 같이 Ipython을 import한다.

그리고, LA_y및 SD_y에 저장된 오디오 정보를 ipd.Audio 함수에 전달한다. 

Import Ipython as above.
And the audio information stored in LA_y and SD_y is ipd.Pass to Audio function.

audio = (SD_y, LA_y)
ipd.Audio(audio[0], rate=audio_sr[0])

 아래 코드에서는, audio라는 튜플을 하나 생성하였다.

In the code below, we generated a tuple called audio.

실행하면 위와 같이 오디오 파일을 재생할 수 있는 UI가 생긴다. 

When executed, there is a UI that can play audio files as above.

 

 

 

3. 오디오파일 시각화

audio_name = ["shutdown_MV.mp3", "LaCampanella.mp3"]
audio_color = ['pink', 'orange']

나는 그래프의 제목과 색깔을 미리 지정해두었다.

리스트에 저장하고 아래에서 변수만 불러와 계속 사용했다. 

I specified the title and color of the graph in advance.
I saved it to the list and only the variables from below were retrieved and used continuously.

# 같이 그리기
plt.figure(figsize=(18,3))
for i in range(len(audio)):
    plt.plot(audio[i], label = audio_name[i], color=audio_color[i])   
plt.legend()
plt.show()

# 따로 그리기
for i in range(len(audio)):
  plt.figure(figsize=(18,2))
  plt.plot(audio[i], color=audio_color[i])
  plt.title(audio_name[i], fontsize=18)
  plt.show()

그래프를 시각화하면 위와 같이 예쁜 그래프를 얻을 수 있다. 

By visualizing the graph, you can get a graph as above.

 

 

4. 오디오파일 저장하기 

import soundfile as sf

soundfile이라는 라이브러리를 불러온다. 

Loads a library called soundfile.

sf.write('SD_processed.mp3', SD_y, SD_sr)

다음과 같은 코드를 작성하면, SD_processed.mp3 파일을 형성해준다. 

함께 들어가는 매개변수는 추출한 y와 sr(sample rate) 값이다. 

When the following code is written, the SD_processed.mp3 file is formed. 
The parameters together are the extracted y and sr(sample rate) values.

 

 

 

 

 

Reference

https://whiteduck.tistory.com/160

Chat GPT

반응형