Pitch

class audioflux.Pitch(pitch_type=None, samplate=32000, low_fre=27.5, high_fre=2093.004522404789, radix2_exp=12, slide_length=1024, auto_length=2048)

Pitch - YIN, STFT, etc algorithm

Parameters
pitch_type: PitchType

Pitch type

samplate: int

Sampling rate of the incoming audio.

low_fre: float

Lowest frequency.

high_fre: float

Highest frequency.

radix2_exp: int

fft_length=2**radix2_exp

slide_length: int

Window sliding length.

auto_length: int

Auto length

Examples

Get a 220Hz’s audio file

>>> import audioflux as af
>>> audio_arr, sr = af.read(af.utils.sample_path('220'))
# >>> audio_arr = audio_arr[:8192]

Create Pitch object and get frequency

>>> from audioflux.type import PitchType
>>> obj = af.Pitch(pitch_type=PitchType.YIN)
>>> fre_arr, value_arr1, value_arr2 = obj.pitch(audio_arr)

Display plot

>>> import matplotlib.pyplot as plt
>>> from audioflux.display import fill_wave, fill_plot
>>> import numpy as np
>>> audio_len = audio_arr.shape[-1]
>>> fig, axes = plt.subplots(nrows=2)
>>> fill_wave(audio_arr, samplate=sr, axes=axes[0])
>>>
>>> ax = fill_plot(np.arange(len(fre_arr)), fre_arr, label='fre', axes=axes[1])
>>> ax.set_ylabel('frequency(Hz)')
../_images/pitch-1.png

Methods

cal_time_length(data_length)

Calculate the length of a frame from audio data.

pitch(data_arr)

Compute pitch

set_thresh(thresh)

Set thresh

set_thresh(thresh)

Set thresh

Parameters
thresh: float
cal_time_length(data_length)

Calculate the length of a frame from audio data.

  • fft_length = 2 ** radix2_exp

  • (data_length - fft_length) / slide_length + 1

Parameters
data_length: int

The length of the data to be calculated.

Returns
out: int
pitch(data_arr)

Compute pitch

Parameters
data_arr: np.ndarray [shape=(…, n)]

Input audio array

Returns
fre_arr: np.ndarray [shape=(…, time)]
value1_arr: np.ndarray [shape=(…, time)]
value2_arr: np.ndarray [shape=(…, time)]