This is a modified version of P2FA for Python3 compatibility. Everything else remains the same as the original P2FA. Forced alignment helps to align linguistic units (e.g., phoneme or words) with the corresponding sound file. All you need is to have a sound file with a transcription file. The output will be .TextGrid file with time-aligned phone, word and optionally state-level tiers.
This was tested on macOS Ventura and Arch Linux.
First, you need to download HTK source code (http://htk.eng.cam.ac.uk/). This HTK installation guide is retrieved from Link. (2021-04-13) Installation is based on macOS Sierra. (2023-05-06) Installation is based on macOS Ventura 13.3.1 (a) - Apple M1 Max
I couldn't run HTK-3.4.1 on Arch Linux. I switched to 3.4.0 and everything works fine. Installation of HTK is the same as the one described below.
Unzip HTK-3.4.1.tar.gz file. I unzipped htk under the current repository so as to keep it in the same directory.
$ tar -xvf HTK-3.4.1.tar.gz
After extracting the tar file, switch to htk directory.
$ cd htk
Compile HTK in the htk directory.
$ export CPPFLAGS=-UPHNALG
$ ./configure --disable-hlmtools --disable-hslab
$ make clean # necessary if you're not starting from scratch
$ make all
$ sudo make install # use "sudo" to make htk functions available for all users
(Tested as of 2023-05-06)
# Because I assume 64-bit platform, you may need to install 32-bit headers and libraries first (See: https://stackoverflow.com/a/54082790/7170059)
$ sudo apt-get install gcc-multilib
$ ./configure --disable-hlmtools --disable-hslab
$ make clean # necessary if you're not starting from scratch
$ make all
$ sudo make install # use "sudo" to make htk functions available for all users
# Quick test if HVite works to confirm that htk functions are installed correctly
$ HVite
(Tested as of 2023-05-06) You may need to follow these steps before compiling HTK:
# Add CPPFLAGS, LIBRARY_PATH
$ export CPPFLAGS=-I/opt/X11/include
$ export LIBRARY_PATH=/opt/X11/lib
# If the above doesn't work, do
$ ln -s /opt/X11/include/X11 /usr/local/include/X11
# Replace line 21 (#include <malloc.h>) of HTKLib/strarr.c as below
# include <malloc/malloc.h>
# Replace line 1650 (labid != splabid) of HTKLib/HRec.c as below
# labpr != splabid
# This step will prevent "ERROR [+8522] LatFromPaths: Align have dur<=0"
# See: https://speechtechie.wordpress.com/2009/06/12/using-htk-3-4-1-on-mac-os-10-5/
# Compile with options if necessary
$ ./configure
$ make all
$ sudo make install # use "sudo" to make htk functions available for all users
# Quick test if HVite works to confirm that htk functions are installed correctly
$ HVite
-
If the "make" command generates errors such as:
HGraf.c:73:10: fatal error: 'X11/Xlib.h' file not found
- you may need to install XQuartz. Download XQuartz from this site. You will have
/opt/X11
folder generated with necessary files. You need to manually compile HTKLib with the following command:
$ cd HTKLib # Compile $ gcc -ansi -g -O2 -DNO_AUDIO -D'ARCH="darwin"' -I/usr/include/malloc -Wall -Wno-switch -g -O2 -I. -DPHNALG -c -o HGraf.o HGraf.c -I /opt/X11/include $ cd .. # Set the path $ export LIBRARY_PATH=/opt/X11/lib # Run it again $ make all $ make install (See: http://unixnme.blogspot.com/2018/01/build-htk-on-macos.html)
- you may need to install XQuartz. Download XQuartz from this site. You will have
-
If you encounter
strarr.c:21:10: fatal error: 'malloc.h' file not found
, then comment out#include <malloc.h>
and add#include <stdlib.h>
instead as follows. (See: JoFrhwld/FAVE#48 (comment)) -
If you see errors like
HTrain.c implicitly declaring library function 'finite'
, replace allfinite
functions inHTKLib/HTrain.c
withisfinite
. (See: https://trac.macports.org/ticket/61614) -
Architecture errors like
esignal.c:1184:25: error: use of undeclared identifier 'ARCH'
requires fixing it with the right architecture specifier. OpenHTKLib/esignal.c
and replace lines includingARCH
as in(strcmp(architecture, ARCH) == 0)
with"darwin"
as in(strcmp(architecture, "darwin") == 0)
(See: https://wstyler.ucsd.edu/posts/p2fa_mac.html)
$ sudo apt-get install sox
# or in Arch
$ sudo pacman -S sox
# or using brew
$ brew install sox
$ python align.py examples/ploppy.wav examples/ploppy.txt examples/ploppy.TextGrid
You can invoke the aligner from your code:
from p2fa import align
phoneme_alignments, word_alignments = align.align('WAV_FILE_PATH', 'TRANSCRIPTION_FILE_PATH')
# or
phoneme_alignments, word_alignments, state_alignments = align.align('WAV_FILE_PATH', 'TRANSCRIPTION_FILE_PATH', state_align=True)
With state-alignments
- Updated installation guide
- Refactor
align.py
- http://www.ling.upenn.edu/phonetics/p2fa/
- Jiahong Yuan and Mark Liberman. 2008. Speaker identification on the SCOTUS corpus. Proceedings of Acoustics '08.
- https://github.com/prosodylab/Prosodylab-Aligner (P2FA seems better than Prosodylab-Aligner based on my qualitative evaluation)
- English HMM-state level aligner: Link
- Korean Forced Aligner: Link from EMCSLabs.
- Installing p2fa on Mac Link