简体   繁体   English

pyAudio如何测量?

[英]What do I measure with pyAudio?

I am recording sound using the python pyAudio library, and plotting it with matplotlib. 我正在使用python pyAudio库录制声音,并使用matplotlib进行绘制。 Here I'm recording the volume and want to know in which value it is and how many Dezibel this are. 在这里,我正在记录音量,并想知道该音量是多少,这是多少个Dezibel。

Currently I think that it's recorded in PCM , but I'm not sure. 目前,我认为它已记录在PCM中 ,但我不确定。

In this part I'm setting up a stream as described in the docs : 在这一部分中,我将按照docs中的说明设置流:

# constants
CHUNK = 1024 * 2             # samples per frame
FORMAT = pyaudio.paInt16     # audio format (bytes per sample?)
CHANNELS = 1                 # single channel for microphone
RATE = 44100                 # samples per second

# pyaudio class instance
mic = pyaudio.PyAudio()

# stream object to get data from microphone
stream = mic.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

This is the part where I'm measuring the audio-signal: 这是我测量音频信号的部分:

def measure():
    # binary data
    data = stream.read(CHUNK)  

    # convert data to integers, make np array, then offset it by 127
    data_int = struct.unpack(str(2 * CHUNK) + 'B', data)

    # create np array and offset by 128
    data_np = np.array(data_int, dtype='b')[::2]
    data_np = [i+127 for i in data_np]

This is the whole code: 这是整个代码:

import pyaudio      #for capturing the audio-signal
import struct       #for converting the binary-data from the signal to integer
import matplotlib.pyplot as plt     #for displaying the audio-signal

import numpy as np
import time

#functions
def plot_setup():
    # create matplotlib figure and axes
    fig=plt.figure()
    ax=fig.add_subplot(111)

    # variable for plotting
    x = np.arange(0, 2 * CHUNK, 2)

    # create a line object with random data
    line, = ax.plot(x, [128 for i in range(2048)], '-')

    # basic formatting for the axes
    ax.set_title('AUDIO WAVEFORM')
    ax.set_xlabel('samples')
    ax.set_ylabel('volume')
    ax.set_ylim(0, 255)
    ax.set_xlim(0, 2 * CHUNK)
    plt.xticks([0, CHUNK, 2 * CHUNK])
    plt.yticks([0, 128, 255])
    # show the plot
    plt.show(block=False)
    return fig, line

def measure():
    # binary data
    data = stream.read(CHUNK)  

    # convert data to integers, make np array, then offset it by 127
    data_int = struct.unpack(str(2 * CHUNK) + 'B', data)

    # create np array and offset by 128
    data_np = np.array(data_int, dtype='b')[::2]
    data_np = [i+127 for i in data_np]

    line.set_ydata(data_np)
    try:
        fig.canvas.draw()
        fig.canvas.flush_events()
    except:
        return 0

# constants
CHUNK = 1024 * 2             # samples per frame
FORMAT = pyaudio.paInt16     # audio format (bytes per sample?)
CHANNELS = 1                 # single channel for microphone
RATE = 44100                 # samples per second

# pyaudio class instance
mic = pyaudio.PyAudio()

# stream object to get data from microphone
stream = mic.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

if __name__=="__main__":
    fig, line=plot_setup()
    while True:
        m=measure()
        if m==0:
            break

This is the current output(plot): 这是当前输出(图): 情节

On the y-axis is a value of 255 and I want to know the unit of it and convert it to Dezibel. y轴上的值为255,我想知道它的单位并将其转换为Dezibel。

The Y-axis unit is the absolute volume level. Y轴单位是绝对音量水平。 If you want to convert to decibel dB , the equation is 如果要转换为分贝dB,则等式为

Volume(dB) = 20*log10(v1/v0) where v0 is base reference level. Volume(dB) = 20*log10(v1/v0) ,其中v0是基准参考电平。

Below urls have lot of useful concepts related to sound, amplification, etc. 以下网址具有许多与声音,放大等有关的有用概念。

http://www.sengpielaudio.com/calculator-soundvalues.htm Above has table which relates dB vs level of sound. http://www.sengpielaudio.com/calculator-soundvalues.htm上面有一个表格,该表格将dB与声音水平相关。

http://www.sengpielaudio.com/calculator-FactorRatioLevelDecibel.htm http://www.sengpielaudio.com/calculator-FactorRatioLevelDecibel.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM