Python--pyaudio音效卡錄音__Python

來源:互聯網
上載者:User

1、關於pyaudio的使用,請參看前一篇博文:

Python--pyAudio播放wav格式聲音: http://blog.csdn.net/xsc_c/article/details/8944077


2、關於wav檔案的操作,參看博文:

Python--讀取wav格式檔案: http://blog.csdn.net/xsc_c/article/details/8941338


3、錄音的過程與播放使用的函數類似,可以參考使用手冊:

API文檔地址 : http://people.csail.mit.edu/hubert/pyaudio/docs/#class-pyaudio


4、python代碼

#!usr/bin/env python#coding=utf-8import numpy as npfrom pyaudio import PyAudio,paInt16from datetime import datetimeimport wavefrom Tkinter import *#define of paramsNUM_SAMPLES = 2000framerate = 8000channels = 1sampwidth = 2#record timeTIME = 10def save_wave_file(filename, data):'''save the date to the wav file'''wf = wave.open(filename, 'wb')wf.setnchannels(channels)wf.setsampwidth(sampwidth)wf.setframerate(framerate)wf.writeframes("".join(data))wf.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 record_wave():#open the input of wavepa = PyAudio()stream = pa.open(format = paInt16, channels = 1,rate = framerate, input = True,frames_per_buffer = NUM_SAMPLES)save_buffer = []count = 0while count < TIME*4:#read NUM_SAMPLES sampling datastring_audio_data = stream.read(NUM_SAMPLES)save_buffer.append(string_audio_data)count += 1print '.'filename = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+".wav"save_wave_file(filename, save_buffer)save_buffer = []print filename, "saved"def main():root = Tk()my_button(root,"Record a wave","clik to record",record_wave)root.mainloop()if __name__ == "__main__":main()

5、效果

介面:


儲存檔案:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.