지난 오디오 데이터의 이해에 이어서 이번에도 데이터를 이해해보는 과정이다.
문제는 raw data의 shape과 data를 출력해보고 출력한 data에 대한 파형정보를 그래프로 표현해보는 것.
지난 포스팅에서 waveform을 그려봤기때문에 쉽게 해결할 수 있을 것 같다.
librosa 대신 wave 라이브러리를 사용해봤다.
코드는 이곳을 참고하였다.
전 포스팅에서 사용했던 코드에다 아래코드를 추가한다.
import wave
wav = wave.open(path+'/'+folder_list[0]+'/'+fname[0], mode='rb')
signal = wav.readframes(-1) # 최대 n 프레임의 오디오를 bytes 객체로 읽고 반환합니다.
signal = np.fromstring(signal, 'Int16')
bed 폴더 안에 있는 00176480_nohash_0.wav 파일을 로드했다.
readframes(-1) : 파일의 처음부터 끝까지 bytes 객체로 읽고 반환한다.
bytes는 shape 출력이 안되고 값을 우리가 원하는 대로 표현해주지 않기 때문에
np.fromstring()으로 정수형 배열로 변환해준다.
그 후에 다음과 같이 음원의 시간을 계산해주고 pyplot으로 그려주면 된다.
fs = wav.getframerate()
Time=np.linspace(0, len(signal)/fs, num=len(signal))
print(signal)
print(signal.shape)
plt.plot(Time,signal)
np.linspace는 0부터 샘플링개수/샘플링빈도사이의 값을 샘플링 개수 만큼 배열을 생성한다.
https://docs.python.org/3/library/wave.html
wave — Read and write WAV files — Python 3.8.0 documentation
wave — Read and write WAV files Source code: Lib/wave.py The wave module provides a convenient interface to the WAV sound format. It does not support compression/decompression, but it does support mono/stereo. The wave module defines the following function
docs.python.org
'DAFIT > 903 - 음성단어 분류 알고리즘' 카테고리의 다른 글
<DAFIT> 03 음성 단어 분류기 알고리즘 구현 04 - 오디오 데이터의 이해과정 문제 (0) | 2019.11.08 |
---|---|
<DAFIT> 03 음성 단어 분류기 알고리즘 구현 02 - 오디오 데이터의 이해 과정 문제 (0) | 2019.11.05 |
<DAFIT> 03 음성 단어 분류기 알고리즘 구현 - 01 데이터 셋 준비 과정 (0) | 2019.10.29 |