2013년 3월 19일 화요일

[python sound] mp3 play

python에서 mp3 재생하기

import mp3play

filename = r'C:\Users\Public\Music\Sample Music\Kalimba.mp3'
clip = mp3play.load(filename)

clip.play()

import time
time.sleep(min(30, clip.seconds()))
clip.stop()


참고 :

https://pypi.python.org/pypi/setuptools#files
https://pypi.python.org/pypi/mp3play/

2013년 3월 18일 월요일

[python sound] chirp, sweep wave 만들기

chirp, sweep하는 정형파 만들기

Sine wave의 주파수가 일정하게 변하면서 모든 주파수를 들을 수 있다.
개인적으로 고주파를 들을 때 귀가 아픈게 짜증나지만 -.-

아래 예제는
10Hz~20kHz 주파수를 일정한 크기로 만들었지만,
사람의 청각 특성 때문에 귀로 들을 때는 일정한 크기로 들리지 않는다.

여기서 만든 wav 파일로 앞으로 만들 필터를 검증하는데 이용하려고 한다.

import math
import wave
import struct
import scipy
import numpy as np
import matplotlib.pyplot as plt

freq = 1000.0
data_size = 20*44100
fname = "ChirpWave.wav"
frate = 44100.0
amp = 64000.0     # multiplier for amplitude

t = np.arange(0, data_size/frate, 1/frate)
y= scipy.signal.chirp(t, f0=10, f1=20000, t1=5, method='linear')

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 y:
    # write the audio frames to file
    wav_file.writeframes(struct.pack('h', int(s*amp/2)))

wav_file.close()



참고 :
위키백과 : http://en.wikipedia.org/wiki/Chirp

http://www.scipy.org/Cookbook/FrequencySweptDemo
http://docs.scipy.org/doc/scipy/scipy-ref.pdf

2013년 3월 15일 금요일

[python sound] 간단한 FIR 필터 만들기

간단한 FIR 필터 만들기

간단한 FIR 필터를 만듦. 예제로 moving average 필터를 사용함.
예전 예제에서 만든 wav file(".\WaveTest.wav")을 이용함

 import wave
import struct

import numpy as np
import matplotlib.pyplot as plt

N = 20 #FIR filter length

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)
x = []
for i in range(nframes):
    waveData = wav_file.readframes(1)
    x.append(struct.unpack("
wav_file.close()

b = [1.0/N]*N #FIR filter coefficient, moving averaging filter

#Filtering mechanism
#y[n] = b[0]*x[n] + b[1]*x[n-1] + b[2]*x[n-2] + ... + b[n-1]*x[1] + b[n]*x[0]

'''
y = [0.0]*nframes
for index in range(nframes):
    for k in range(N):
        if index-k >= 0:
            y[index] = y[index] + b[k]*x[index-k]
'''
y = np.convolve(b, x, "full")
            
plt.plot(time, x, time, y)
plt.xlabel("times[s]")
plt.ylabel("data")
plt.xlim(0, 0.005)
plt.show()


필터링 된 이미지


참고 :
위키백과 : http://en.wikipedia.org/wiki/Finite_impulse_response
scipy가 지원하는 fir filter : http://mpastell.com/2010/01/18/fir-with-scipy/

2013년 3월 14일 목요일

[python sound] wav 파일 만들기

임의적으로 sine 파형을 만들어 wave 파일로 저장하는 python code

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

이상!!

2013년 2월 25일 월요일

[그리니워즈] 영어mp3 찾기


"http://www.gstatic.com/dictionary/static/sounds/de/0/"+eng+".mp3"