Python–產生Wav格式檔案

來源:互聯網
上載者:User

1、下載與安裝scipy

scipy下載連結: http://www.scipy.org/Download#head-0dfc04e10313d2e70988c6cb3bef7a9e09860c8f

同時可以下載說明文檔連結http://docs.scipy.org/doc/

2、wav檔案寫操作

3、signal.chirp函數使用說明

4、點擊按鈕實現訊號產生,點擊按鈕實現訊號顯示

5、Python代碼

 

#!usr/bin/env python#code=utf-8from Tkinter import *import waveimport numpy as npimport scipy.signal as signalimport matplotlib.pyplot as pltimport syssys.setrecursionlimit(1000000)#define the params of wavechannels = 1sampwidth = 2framerate = 9600file_name = 'sweep.wav'frequency_begin = 1frequency_end = 100#define the time of wavetime = 1def Generate_Wav():#generate the time bart = np.arange(0,time,1.0/framerate)#generate the chirp signal from 300 to 3300Hzwave_data = signal.chirp(t, frequency_begin, time, frequency_end, method = 'linear')*1000#cast to the type of shortwave_data = wave_data.astype(np.short)#open a wav documentf = wave.open(file_name,"wb")#set wav paramsf.setnchannels(channels)f.setsampwidth(sampwidth)f.setframerate(framerate)#turn the data to stringf.writeframes(wave_data.tostring())f.close()def my_button(root,label_text,button_text,button_func):      '''''function of creat label and button'''      #label details      label = Label(root)      label['text'] = label_text      label.pack()      #label details      button = Button(root)      button['text'] = button_text      button['command'] = button_func      button.pack()def read_wave_data(file_path):      #open a wave file, and return a Wave_read object      f = wave.open(file_path,"rb")      #read the wave's format infomation,and return a tuple      params = f.getparams()      #get the info      nchannels, sampwidth, framerate, nframes = params[:4]      #Reads and returns nframes of audio, as a string of bytes.       str_data = f.readframes(nframes)      #close the stream      f.close()      #turn the wave's data to array      wave_data = np.fromstring(str_data, dtype = np.short)       time = np.arange(0, nframes) * (1.0/framerate)      return wave_data, timedef Plot_Wav():wave_data, time = read_wave_data(file_name)plt.plot(time, wave_data)plt.grid(True)plt.show()def main():root = Tk()my_button(root, 'Generate a sweep wav', 'Generate', Generate_Wav)my_button(root, 'Plot the wav', 'Plot', Plot_Wav)root.mainloop()if __name__ == "__main__":main()

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.