less than 1 minute read

官方文档 https://people.csail.mit.edu/hubert/pyaudio/docs/

一、 安装

使用 pyaudio + numpy + pylab 录音并显示:打开电脑的麦克风,然后接受音频输入,再以图像的形式展示出来;

可轻松录制与播放音频;


TOP

附录

A 示例

1. 录音并显示

# -*- coding: utf-8 -*-
import pyaudio
import numpy as np
import pylab
import time

RATE = 44100
CHUNK = int(RATE/20) # RATE/number of uplast_modified_ats per second

def sound_plot(stream):
    t1 = time.time() # time starting
    data = np.fromstring(stream.read(CHUNK),dtype = np.int16)
    pylab.plot(data)
    pylab.title(i)
    pylab.grid()
    pylab.axis([0,len(data),-2**8,2**8])
    pylab.savefig("sound.png",dpi=50)
    pylab.show(block = False)
    time.sleep(0.5)
    pylab.close('all')
    print("took %.2f ms." % (time.time() - t1)*1000)

if __name__ == '__main__':
    p = pyaudio.PyAudio()
    stream = p.open(format = pyaudio.paInt16,channels = 1,rate = RATE,
                    input = True,frames_per_buffer = CHUNK)
    for i in range(int(20*RATE/CHUNK)):
        # for 10 seconds
        sound_plot(stream)
    stream.stop_stream()
    stream.close()
    p.terminate()

参考资料

  1. lyrichu. 介绍几个python的音频处理库[EB/OL]. https://www.cnblogs.com/lyrichu/p/6848732.html. -/2019-01-11

Comments