Extracting pitch track from audio files into a data frame

1 minute read

Published:

My task was to extract pitch values from a long list of audio files. Previously I'd used Praat and R for this, but looping in R was rather slow, so I went looking for another approach. I developed the following on Linux (Ubuntu).

First, I tried aubio (a CLI-only Python tool) to extract pitch from WAV files. aubio has fewer options than Praat and returned some odd values on its default settings, so I didn't pursue it — though it is easy to use, nicely simple, and Python-native. To extract pitch with aubio:

sudo apt install aubio-tools
aubiopitch -i P17_trim_short_10.000-11.150.wav

In the end I stuck with Praat, the workhorse of phonetics, which can also be driven from the command line.

Praat records every command you run, which is a great starting point for a script. There's more on Praat scripting here. My solution is below:

This script extracts a .pitch file from every .wav file in the working directory and saves them to a subfolder. Praat scripts can be called from the command line:

praat --run extract_pitch_script.praat

This extracts pitch tracks from every .wav file in the directory, using Praat's default settings, and produces one .pitch file per .wav file. Those files list all the candidates and aren't in a tidy format, so they need transforming. I could probably have done this in Praat scripting too, but I ran out of patience and switched to R, which produces the output I wanted with far less effort.

R can be called from the command line with littler; the shebang on the first line is what makes that possible. The script below turns the .pitch files into clean .csv files.

To invoke the R script, run in the command line:

r praat_pitch_analysis_CLI.R untitled_script.pitch

This creates a .csv file holding the best candidate pitch above a given confidence threshold. The pitch extraction algorithm Praat uses was developed by Boersma (1993).