本文純屬個人見解,是對前面學習的總結,如有描述不正確的地方還請高手指正~
1、import wave 用於讀寫wav文件
它提供了一個方便的WAV格式接口。
但是不支持壓縮/解壓縮,支持單聲道/立體聲。
讀取格式:
open(file[, mode])
如果file是一個字符串,那么就打開文件,不然就把它當做一個類文件對象。
mode是可以缺省的,如果輸入的參數是一個類文件對象,那么file.mode將會作為mode的值。
mode可選參數如下:
'r', 'rb'
Read only mode.
'w', 'wb'
Write only mode.
注意不能同時實現讀/寫操縱
2、wav文件讀操縱
3、numpy:shape轉變數組形狀
當某數軸的參數為-1時,根據元素個數,主動盤算此軸的最大長度,入將c數組改成2行
4、實例代碼
#!usr/bin/env python #coding=utf-8 from Tkinter import * import wave import matplotlib.pyplot as plt import numpy as np 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) #for the data is stereo,and format is LRLRLR... #shape the array to n*2(-1 means fit the y coordinate) wave_data.shape = -1, 2 #transpose the data wave_data = wave_data.T #calculate the time bar time = np.arange(0, nframes) * (1.0/framerate) return wave_data, time def main(): wave_data, time = read_wave_data("C:\Users\CJP\Desktop\miss_you.wav") #draw the wave plt.subplot(211) plt.plot(time, wave_data[0]) plt.subplot(212) plt.plot(time, wave_data[1], c = "g") plt.show() if __name__ == "__main__": main()
5、效果
文章結束給大家分享下程序員的一些笑話語錄: 問路
有一個駕駛熱氣球的人發現他迷路了。他降低了飛行的高度,並認出了地面 上的一個人。他繼續下降高度並對着那個人大叫,“打擾一下,你能告訴我我 在哪嗎?”
下面那個人說:“是的。你在熱氣球里啊,盤旋在 30 英尺的空中”。
熱氣球上的人說:“你一定是在 IT 部門做技術工作”。
“沒錯”,地面上的人說到,“你是怎么知道的?”
“呵呵”,熱氣球上的人說,“你告訴我的每件事在技術上都是對的,但對都沒 有用”。
地面上的人說,“你一定是管理層的人”。
“沒錯”,熱氣球上的人說,“可是你是怎么知道的?”
“呵呵”,地面上的那人說到,“你不知道你在哪里,你也不知道你要去哪,你 總希望我能幫你。你現在和我們剛見面時還在原來那個地方,但現在卻是我 錯了”。