簡體   English   中英

pyAudio如何測量?

[英]What do I measure with pyAudio?

我正在使用python pyAudio庫錄制聲音,並使用matplotlib進行繪制。 在這里,我正在記錄音量,並想知道該音量是多少,這是多少個Dezibel。

目前,我認為它已記錄在PCM中 ,但我不確定。

在這一部分中,我將按照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
)

這是我測量音頻信號的部分:

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]

這是整個代碼:

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

這是當前輸出(圖): 情節

y軸上的值為255,我想知道它的單位並將其轉換為Dezibel。

Y軸單位是絕對音量水平。 如果要轉換為分貝dB,則等式為

Volume(dB) = 20*log10(v1/v0) ,其中v0是基准參考電平。

以下網址具有許多與聲音,放大等有關的有用概念。

http://www.sengpielaudio.com/calculator-soundvalues.htm上面有一個表格,該表格將dB與聲音水平相關。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM