0

我正在使用 Windows 上的 pyaudio 进行录制。我正在使用 matplotlib 绘制录制的声音。

记录长度为 60 秒。

缓冲区大小为 1024

我得到的是,在记录时,对于前几次迭代,我得到了垃圾值。之后,它开始录制实际声音。

我还发现,即使 MIC 静音,它也会给出垃圾值 wjen 我绘制它。

这些垃圾值正在影响我的计算结果。

知道吗,为什么要记录这种垃圾值/声音?有什么快速的解决方案??

代码:

class record(Thread):

def __init__(self):
    #Thread.__init__(self)
    super(record, self).__init__()
    self.lock=threading.Lock()

    self.project=projectutils.getActiveProject()
    self.outfile=self.project['name']+'.wav'
    self.tw=tool.getToolWindow()
    self.duration = 60 #record for 1 second. Pretty long duration don't you think

    self.buffer=1024
    self.pin = pyaudio.PyAudio()
    self.channels=2
    ccare.rate=self.rate=8820
    self.format=pyaudio.paInt16
    self.inStream = self.pin.open(format=self.format, channels=self.channels, rate=self.rate,input=True, frames_per_buffer=self.buffer)
    self.flag=1
    self.out = []
    self.upper_lim = self.rate / self.buffer * self.duration 

def run(self):      
    ccare.val=[]

    x=[]
    if not self.inStream:
       return
    self.lock.acquire()
    data = self.inStream.read(self.buffer)
    self.lock.release()

    x=list(struct.unpack("%dh"%(len(data)/2),data))

    self.lock.acquire()
    ccare.val=ccare.val+list(x)
    self.lock.release()
    self.out.append(data)
    for i in xrange(1, self.upper_lim):

        x=[]
        if not self.inStream:
           break
        data = self.inStream.read(self.buffer)
        x=list(struct.unpack("%dh"%(len(data)/2),data))
        self.lock.acquire()
        ccare.val=ccare.val+list(x)
        self.lock.release()
        self.out.append(data)

    if self.inStream:
       self.inStream.stop_stream()
       self.inStream.close()
       self.pin.terminate()
    self.save_file()

简单代码:

import pyaudio
import wave
import struct
val = []

def record(out_file):
    duration = 60 #record for 1 second. Pretty long duration don't you think
    buffer=1024

    pin = pyaudio.PyAudio()

    channels=2
    rate=8820
    format=pyaudio.paInt16
    inStream = pin.open(format=format, channels=channels, rate=rate,input=True, frames_per_buffer=buffer)

    out = []
    upper_lim = rate / buffer * duration 
    val=[]

    x=[]
    if not inStream:
       return
    data = inStream.read(buffer)

    x=list(struct.unpack("%dh"%(len(data)/2),data))

    val=val+list(x)
    out.append(data)

    for i in xrange(1, upper_lim):
        x=[]
        if not inStream:
           break
        data = inStream.read(buffer)
        x=list(struct.unpack("%dh"%(len(data)/2),data))

        val=val+list(x)
        out.append(data)

    if inStream:
       inStream.stop_stream()
       inStream.close()
       pin.terminate()

'val' 变量中存储的值将使用 matplotlib 绘制在不同的线程中。

4

0 回答 0