Skip to content

Extract numerical features from audio

Jyotika Singh edited this page May 20, 2022 · 2 revisions

Extracting features from audios

This feature lets the user extract aggregated data features calculated per audio file. See feature options for more information on choices of features available.

Python examples

Code example for performing gfcc and mfcc feature extraction can be found below.

from pyAudioProcessing.extract_features import get_features

# Feature extraction of a single file

features = get_features(
  file="<path to audio>",
  feature_names=["gfcc", "mfcc"]
)

# Feature extraction of a multiple files

features = get_features(
  file_names={
    "music": [<path to audio>, <path to audio>, ..],
    "speech": [<path to audio>, <path to audio>, ..]
  },
  feature_names=["gfcc", "mfcc"]
)

# or if you have a dir with  sub-folders and audios
# features = get_features(folder_path="data_samples/testing", feature_names=["gfcc", "mfcc"])

# features is a dictionary that will hold data of the following format
"""
{
  music: {file1_path: {"features": <list>, "feature_names": <list>}, ...},
  speech: {file1_path: {"features": <list>, "feature_names": <list>}, ...},
  ...
}
"""

To save features in a json file,

from pyAudioProcessing import utils
utils.write_to_json("audio_features.json", features)

Command-line examples

If you cloned the project via git, the following command line example of for gfcc and mfcc feature extractions can be used as well. The features argument should be a comma separated string, example gfcc,mfcc.
To use your own audio files for feature extraction, pass in the directory path containing .wav files as the -f argument. Please refer to the format of directory data_samples/testing or the section on Training and Testing Data structuring.

python pyAudioProcessing/extract_features.py -f "data_samples/testing"  -feats "gfcc,mfcc"

Features extracted get saved in audio_features.json.