Realtime Fast Audio Library: sounddevice
설치방법
Python 기본 내장 library wave
import numpy as np
import wave
import matplotlib.pyplot as plt
import sounddevice as sd
class SoundFile:
def __init__(self, file_name=None):
# https://docs.python.org/3.6/library/wave.html
pass
def write(self, file_name, signal, nchannels=1, sampwidth=2, framerate=44100, duration_sec = 4, comptype="NONE", compname="noncompressed"):
file = wave.open(file_name, 'wb')
file.setparams( ( nchannels, sampwidth, framerate, framerate * duration_sec, comptype, compname) )
file.writeframes( signal )
file.close()
def read(self, file_name):
file = wave.open(file_name, 'rb')
nchannels, sampwidth, framerate, nframes, comptype, compname = file.getparams()
duration_sec = nframes / framerate
signal = file.readframes( nframes )
signal = np.frombuffer(signal)
file.close()
return signal, nchannels, sampwidth, framerate, duration_sec, nframes, comptype, compname
사용 예시 코드
sf = SoundFile()
file_name = "test.wav"
time = np.arange(0, 5000, 0.02);
amplitude = np.sin(time)
plt.plot(time, amplitude)
sd.play(amplitude, samplerate)
sf.write(file_name, amplitude)
signal, nchannels, sampwidth, framerate, duration_sec, nframes, comptype, compname = sf.read("test.wav")
sd.play(signal, samplerate)
Sound Level 측정 함수
import sounddevice as sd
from numpy import linalg as LA
import numpy as np
duration = 10 # seconds
def print_sound(indata, outdata, frames, time, status):
volume_norm = np.linalg.norm(indata)*10
print (int(volume_norm))
with sd.Stream(callback=print_sound):
sd.sleep(duration * 1000)
Wav file에서 Decibel 계산하기
Sound Pressure Level
Decibel
Decibel Level 연산 library
Sound 처리 관련 라이브러리
soundmeter
pip install soundmeter --allow-all-external --allow-unverified pyaudio
https://github.com/shichao-an/soundmeter
audioop
https://docs.python.org/2/library/audioop.html#module-audioop
PySox
https://github.com/rabitt/pysox
Library Pydub
설치방법
'Development > Python' 카테고리의 다른 글
Python Callback (0) | 2020.08.13 |
---|---|
Python Subprocess (0) | 2020.08.13 |
한글을 Sub-character level로 파싱하기(python으로 유니코드 파싱) (0) | 2020.04.16 |
CSV파일 인코딩(Encoding) (0) | 2020.03.23 |
Conda로 Python 버전 별 설치, 관리, 삭제하기 (0) | 2020.02.27 |
댓글