pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
computeFeature.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2
3import matplotlib.pyplot as plt
4
5import pyACA
6from pyACA.computeSpectrogram import computeSpectrogram
7from pyACA.ToolPreprocAudio import ToolPreprocAudio
8from pyACA.ToolComputeHann import ToolComputeHann
9from pyACA.ToolReadAudio import ToolReadAudio
10
11
12
43def computeFeature(cFeatureName, x, f_s, afWindow=None, iBlockLength=4096, iHopLength=2048):
44
45 # mypackage = __import__(".Feature" + cFeatureName, package="pyACA")
46 hFeatureFunc = getattr(pyACA, "Feature" + cFeatureName)
47
48 # pre-processing
49 x = ToolPreprocAudio(x)
50
51 if isSpectral(cFeatureName):
52 # compute window function for FFT
53 if afWindow is None:
54 afWindow = ToolComputeHann(iBlockLength)
55
56 assert(afWindow.shape[0] == iBlockLength), "parameter error: invalid window dimension"
57
58 # in the real world, we would do this block by block...
59 [X, f, t] = computeSpectrogram(x, f_s, None, iBlockLength, iHopLength)
60
61 # compute instantaneous feature
62 v = hFeatureFunc(X, f_s)
63
64 if isTemporal(cFeatureName):
65 [v, t] = hFeatureFunc(x, iBlockLength, iHopLength, f_s)
66
67 return v, t
68
69
70
72def isSpectral(cName):
73 bResult = False
74 if "Spectral" in cName:
75 bResult = True
76
77 return bResult
78
79
80def isTemporal(cName):
81 bResult = False
82 if "Time" in cName:
83 bResult = True
84
85 return bResult
86
87
88
90def computeFeatureCl(cPath, cFeatureName, bPlotOutput=False):
91
92 # read audio file
93 [f_s, afAudioData] = ToolReadAudio(cPath)
94
95 # for debugging
96 iBlockLength = 4096
97 iHopLength = 2048
98
99 # compute feature
100 [v, t] = computeFeature(cFeatureName, afAudioData, f_s, None, iBlockLength, iHopLength)
101
102 # plot feature output
103 if bPlotOutput:
104 plt.plot(t, v)
105
106 return v, t
107
108
109if __name__ == "__main__":
110 import argparse
111
112 # add command line args and parse them
113 parser = argparse.ArgumentParser(description='Compute feature from wav file')
114 parser.add_argument('--infile', metavar='path', required=False,
115 help='path to input audio file')
116 parser.add_argument('--featurename', metavar='string', required=False,
117 help='feature name in the format SpectralPitchChroma')
118 parser.add_argument('--plotoutput', metavar='bool', required=False,
119 help='option to plot the output')
120
121 # retrieve command line args
122 args = parser.parse_args()
123 cPath = args.infile
124 cFeatureName = args.featurename
125 bPlotOutput = args.plotoutput
126
127 # only for debugging
128 if __debug__:
129 if not cPath:
130 cPath = "../ACA-Plots/audio/sax_example.wav"
131 if not cFeatureName:
132 cFeatureName = "SpectralKurtosis"
133 if not bPlotOutput:
134 bPlotOutput = False
135
136 # call the function
137 computeFeatureCl(cPath, cFeatureName, bPlotOutput)
isSpectral(cName)
helper functions
computeFeatureCl(cPath, cFeatureName, bPlotOutput=False)
main