python - 在python中绘制音高跟踪线
问题描述
我编写了这段代码来从麦克风捕获人声,并使用 pyaudio 和 aubio 找到它的基频:
import sys
import aubio
from aubio import pitch
import queue
import music21
import pyaudio
import numpy as np
# Open stream.
# PyAudio object.
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, input=True,
input_device_index=0, frames_per_buffer=512)
q = queue.Queue()
current_pitch = music21.pitch.Pitch()
filename = 'piano.wav'
samplerate = 44100
win_s = 512
hop_s = 512
tolerance = 0.8
pitch_o = pitch("default",win_s,hop_s,samplerate)
#pitch_o.set_unit("")
pitch_o.set_tolerance(tolerance)
# total number of frames read
total_frames = 0
def get_current_note():
pitches = []
confidences = []
current_pitch = music21.pitch.Pitch()
while True:
data = stream.read(hop_s, exception_on_overflow=False)
samples = np.fromstring(data,dtype=aubio.float_type)
pitch = (pitch_o(samples)[0])
#pitch = int(round(pitch))
confidence = pitch_o.get_confidence()
#if confidence < 0.8: pitch = 0.
pitches += [pitch]
confidences += [confidence]
current='Nan'
if pitch>0:
current_pitch.frequency = float(pitch)
current=current_pitch.nameWithOctave
print(pitch,'----',current,'----',current_pitch.microtone.cents)
q.put({'Note': current, 'Cents': current_pitch.microtone.cents,'hz':pitch})
if __name__ == '__main__':
get_current_note()
现在我需要在 pygame 或其他任何东西(而不是 matplotlib 之类的静态图!)这样的视觉环境中及时动态地绘制频率线(及时动画)。类似于我附在这个问题上的图片。
解决方案
推荐阅读
- android - 从另一个活动返回时,如何在 mainActivity 的 edittext 中恢复先前输入的文本?
- react-native - 批量状态更新螺丝应用逻辑
- ios - AudioKit AKPlayer 无法环回以 setPosition 开头
- database - MongoDB 中的数据库范围搜索
- tensorflow - A neural network runs fine on a laptop, but on laptop with GPU gives error message
- vue.js - 使用 Vue 将道具从 Child 传递到插槽
- python - 在弹性搜索中搜索月份和日期
- python - 同时运行两个不和谐机器人令牌
- python - Pandas:将列转换为唯一列表
- java - java.sql.SQLException - 未找到列