一个输出Sine Sweep Wave文件的Python程序 {{{ #!python # this porgram output sine sweep wave file # HYRY Studio 2007/01/17 from math import * from time import clock import array import wave import psyco psyco.profile() def sinesweep(f0, f1, sweeptime, samplingrate, peak): k = exp(log(float(f1)/f0)/sweeptime) data = array.array("h", "\x00"*sweeptime*samplingrate*2) dt = 1.0/samplingrate t = 0.0 p = 2*pi*f0/log(k) for i in xrange(sweeptime*samplingrate): data[i] = int(peak*sin( p*(pow(k,t)-1) )) t += dt return data def sindata(f0, time, samplingrate, peak): data = [int(peak*sin(2*pi*f0*x/samplingrate)) for x in xrange(int(time*samplingrate))] return array.array("h", data) if __name__ =="__main__": SAMPLING_RATE = 44100 SWEEP_TIME = 50 F0 = 20 F1 = 20000 PEAK = 0x2000 start = clock() data = sinesweep(F0, F1, SWEEP_TIME, SAMPLING_RATE, PEAK) print clock() - start f = wave.open("sweep.wav","wb") f.setnchannels(1) # mono wave f.setsampwidth(2) # 16bit f.setframerate(SAMPLING_RATE) #sampling rate f.setnframes((6+SWEEP_TIME)*SAMPLING_RATE) f.setcomptype("NONE","NONE") f.writeframes("\x00" * 2 * 2 * SAMPLING_RATE) f.writeframes(sindata(1000, 0.1, SAMPLING_RATE, 0x6000).tostring()) f.writeframes("\x00" * int(2 * 1.9 * SAMPLING_RATE)) f.writeframes(data.tostring()) f.writeframes("\x00" * 2 * 2 * SAMPLING_RATE) f.close() }}} = 反馈 =