pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
computePitch.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2"""
3computePitch
4
5computes the fundamental frequency of the (monophonic) audio
6supported pitch trackers are:
7 'SpectralAcf',
8 'SpectralHps',
9 'TimeAcf',
10 'TimeAmdf',
11 'TimeAuditory',
12 'TimeZeroCrossings',
13 Args:
14
15 x: array with floating point audio data
16 f_s: sample rate
17 afWindow: FFT window of length iBlockLength (default: hann)
18 iBlockLength: internal block length (default: 4096 samples)
19 iHopLength: internal hop length (default: 2048 samples)
20
21 Returns:
22 f frequency
23 t time stamp for the frequency value
24"""
25
26import matplotlib.pyplot as plt
27
28import pyACA
29from pyACA.computeSpectrogram import computeSpectrogram
30from pyACA.ToolPreprocAudio import ToolPreprocAudio
31from pyACA.ToolComputeHann import ToolComputeHann
32from pyACA.ToolReadAudio import ToolReadAudio
33
34
35
53def computePitch(cPitchTrackName, x, f_s, afWindow=None, iBlockLength=4096, iHopLength=2048):
54
55 # mypackage = __import__(".Pitch" + cPitchTrackName, package="pyACA")
56 hPitchFunc = getattr(pyACA, "Pitch" + cPitchTrackName)
57
58 # pre-processing
59 x = ToolPreprocAudio(x)
60
61 if isSpectral(cPitchTrackName):
62 # compute window function for FFT
63 if afWindow is None:
64 afWindow = ToolComputeHann(iBlockLength)
65
66 assert(afWindow.shape[0] == iBlockLength), "parameter error: invalid window dimension"
67
68 # in the real world, we would do this block by block...
69 [X, f, t] = computeSpectrogram(x, f_s, None, iBlockLength, iHopLength)
70
71 # compute instantaneous pitch chroma
72 f_0 = hPitchFunc(X, f_s)
73
74 if isTemporal(cPitchTrackName):
75 [f_0, t] = hPitchFunc(x, iBlockLength, iHopLength, f_s)
76
77 return f_0, t
78
79
80
82def isSpectral(cName):
83 bResult = False
84 if "Spectral" in cName:
85 bResult = True
86
87 return bResult
88
89
90def isTemporal(cName):
91 bResult = False
92 if "Time" in cName:
93 bResult = True
94
95 return bResult
96
97
98
100def computePitchCl(cPath, cPitchTrackName, bPlotOutput=False):
101
102 # read audio file
103 [f_s, afAudioData] = ToolReadAudio(cPath)
104 # afAudioData = np.sin(2*np.pi * np.arange(f_s*1)*440./f_s)
105
106 # compute feature
107 [v, t] = computePitch(cPitchTrackName, afAudioData, f_s)
108
109 # plot feature output
110 if bPlotOutput:
111 plt.plot(t, v)
112
113 return v, t
114
115
116if __name__ == "__main__":
117 import argparse
118
119 # add command line args and parse them
120 parser = argparse.ArgumentParser(description='Compute key of wav file')
121 parser.add_argument('--infile', metavar='path', required=False,
122 help='path to input audio file')
123 parser.add_argument('--featurename', metavar='string', required=False,
124 help='feature name in the format SpectralPitchChroma')
125 parser.add_argument('--plotoutput', metavar='bool', required=False,
126 help='option to plot the output')
127
128 # retrieve command line args
129 args = parser.parse_args()
130 cPath = args.infile
131 cPitchTrackName = args.featurename
132 bPlotOutput = args.plotoutput
133
134 # only for debugging
135 if __debug__:
136 if not cPath:
137 cPath = "c:/temp/test.wav"
138 if not cPitchTrackName:
139 cPitchTrackName = "TimeAmdf"
140 if not bPlotOutput:
141 bPlotOutput = True
142
143 # call the function
144 computePitchCl(cPath, cPitchTrackName, bPlotOutput)
isSpectral(cName)
helper functions
computePitchCl(cPath, cPitchTrackName, bPlotOutput=False)
main