1kHz sine wav 파일 만들기
import math
import wave
import struct
freq = 1000.0
data_size = 40000*4
fname = "WaveTest.wav"
frate = 44100.0
amp = 64000.0 # multiplier for amplitude
sine_list_x = []
for x in range(data_size):
sine_list_x.append(math.sin(2*math.pi*freq*(x/frate)))
wav_file = wave.open(fname, "w")
nchannels = 1
sampwidth = 2
framerate = int(frate)
nframes = data_size
comptype = "NONE"
compname = "not compressed"
wav_file.setparams((nchannels, sampwidth, framerate, nframes,
comptype, compname))
for s in sine_list_x:
# write the audio frames to file
wav_file.writeframes(struct.pack('h', int(s*amp/2)))
wav_file.close()
import os
os.startfile(fname)
|
참조 : http://stackoverflow.com/questions/3637350/how-to-write-stereo-wav-files-in-python
Reading
위 코드에서 저장한 wav file을 읽어 그래프로 보는 python code 임.
import wave
import struct
import numpy as np
import matplotlib.pyplot as plt
fname = ".\WaveTest.wav"
wav_file = wave.open(fname, "r")
framerate = float(wav_file.getframerate())
nframes = wav_file.getnframes()
time = np.arange(0, nframes/framerate, 1/framerate)
data = []
for i in range(0,nframes):
waveData = wav_file.readframes(1)
data.append(struct.unpack("
wav_file.close()
plt.plot(time, data)
plt.xlabel("times[s]")
plt.ylabel("data")
plt.xlim(0, 0.01)
plt.show()
|
참조1 : http://stackoverflow.com/questions/2060628/how-to-read-wav-file-in-python
참조2 : http://matplotlib.org/faq/usage_faq.html
down : matplotlib, numpy
이상!!
댓글 없음:
댓글 쓰기