Python: Extracting pitch using Aubio

二次信任 提交于 2020-04-11 18:45:23

问题


I want to use the aubio module to extract pitch using python 3.5. However, the documentation is difficult to comprehend.

In example I have a numpy array:

import numpy
import math

sample_rate=44100
x=numpy.zeros(44100)
for i in range(44100):
    x[i]=math.sin(i/225)

How to use aubio to extract an array containing the pitch of the array?


回答1:


Here is an example (works python2.x and python3).

Note the changed sinewave generation.

#! /usr/bin/env python

import numpy as np
import aubio

sample_rate=44100
x=np.zeros(44100)
for i in range(44100):
    x[i]=np.sin(2. * np.pi * i * 225. / sample_rate)

# create pitch object
p = aubio.pitch("yin", samplerate = sample_rate)
# other examples:
# = aubio.pitch("yinfft", 4096, 512, 44100)
# = aubio.pitch("yin", 2048, 512, 44100)
# = aubio.pitch("mcomb", 4096, 512, 44100)
# = aubio.pitch("schmitt", samplerate = 44100, hop_size = 512, buf_size = 2048)

# pad end of input vector with zeros
pad_length = p.hop_size - x.shape[0] % p.hop_size
x_padded = np.pad(x, (0, pad_length), 'constant', constant_values=0)
# to reshape it in blocks of hop_size
x_padded = x_padded.reshape(-1, p.hop_size)

# input array should be of type aubio.float_type (defaults to float32)
x_padded = x_padded.astype(aubio.float_type)

for frame, i in zip(x_padded, range(len(x_padded))):
    time_str = "%.3f" % (i * p.hop_size/float(sample_rate))
    pitch_candidate = p(frame)[0]
    print (time_str, "%.3f" % pitch_candidate)


来源:https://stackoverflow.com/questions/38858242/python-extracting-pitch-using-aubio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!