snfa
snfa (Simple Neural Forced Aligner) is a phoneme-to-audio forced aligner built for embedded usage in python programs, with its only inference dependency being numpy and python 3.7 or later.
- Tiny model size (~1 MB)
- Numpy as the only dependency
- MFA comparable alignment quality
Note: You still need PyTorch and some other libs if you want to do training.
Inference
pip install snfa
A pre-trained model weight jp.npz is included.
jp.npz is a weight file trained on Japanese Common Voice Corpus 14.0, 6/28/2023. The model weight is released into Public Domain.
import snfa
from snfa import Segment
import librosa # or soundfile, torchaudio, scipy, etc.
aligner = snfa.Aligner() # use custom model by passing its path to this function
# NOTE: the default model is uncased, it doesn't make difference between `U` and `u`
transcript = "k o N n i ch i w a".lower().split(" ") # remember to lower it here
# you can also use `scipy` or `wavfile` as long as it's
# 1. mono channel numpy array with shape (T,), dtype=np.float32
# 2. normalized to [-1,1]
# 3. sample rate matches model's `sr`
x, sr = librosa.load("sample.wav", sr=aligner.sr)
# trim the audio, this may improve alignment quality
x, _ = librosa.effects.trim(x, top_db=20)
# we also provide a utility function to trim
# it's basically ripped off from librosa so you don't have to install it
x, _ = snfa.trim_audio(x, top_db=20)
segments: list[Segment] = aligner(x, transcript)
print(segments)
# (phoneme label, start mili-sec, end mili-sec)
# [('pau', 0, 900),
# ('k', 900, 920),
# ('o', 920, 1080),
# ...]
# NOTE: The timestamps are in mili-sec, you can convert them to the indices on wavform by
wav_index = int(timestamp * aligner.sr / 1000)
Development
We use uv to manage dependencies.
The following command will install them.
uv sync
Training
Download Common Voice Dataset and extract it somewhere.
We use the split from whole validated.tsv, while filtered out the dev and test split.
Filter the dataset:
uv run filter_dataset.py -d /path/to/common/voice/
Start training:
uv run -c config.yaml -d /path/to/common/voice/
Checkpoints and tensorboard logs will be saved to logs/lightning_logs/
Be noted that parameter -d should point to where the *.tsvs are. In Japanese CV dataset, it's sub directory ja.
Exporting
To use the model in numpy, export the checkpoint with
uv run export.py -c config.yaml --ckpt /path/to/checkpoint -o output.npz
Publishing
Usually I am responsible for publishing the package to PyPI, this section serves as a reminder for myself.
- copy the exported
jp.npztosrc/snfa/models/ - uv build
- uv publish
Bundle
When bundling app with pyinstaller, add
from PyInstaller.utils.hooks import collect_data_files
data = collect_data_files('snfa')
# consume `data` in Analyzer
To bundle the model weights properly. I'd appreciate it if you offer a better way.
Todos
- Rust crate
- multi-language
Licence
snfa is released under ISC Licence, as shown here.
The file snfa/stft.py and snfa/util.py contains code adapted from librosa which obeys ISC Licence with different copyright claim. A copy of librosa's licence can be found in librosa's repo.
The file snfa/viterbi.py contains code adapted from torchaudio which obeys BSD 2-Clause "Simplified" License. A copy of torchaudio's licence can be found in torchaudio's repo.
The testing audio file is ripped from Japanese Common Voice Corpus 14.0, 6/28/2023, Public Domain.
Credit
The neural network used in snfa is basically a PyTorch implementation of CTC* structure described in Evaluating Speech—Phoneme Alignment and Its Impact on Neural Text-To-Speech Synthesis.
Metadata
Release files for snfa 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| snfa-0.3.0.tar.gz | 1.2 MB | Without provenance | Details for snfa-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| snfa-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for snfa-0.3.0-py3-none-any.whl |
Total release size: 2.3 MB
Release files / snfa-0.3.0.tar.gz
| Download URL | snfa-0.3.0.tar.gz |
|---|---|
| Size | 1.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
84a498810ea98106ad7ebbebf95bb5552a2b49923ec30c2e80aa678976ad00d2
|
|
BLAKE2b-256 checksum How to use checksums |
317f3e79aff571ae64beea1fa9412f0c028b36d441b14f354b97065079bf921e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.19
|
Release files / snfa-0.3.0-py3-none-any.whl
| Download URL | snfa-0.3.0-py3-none-any.whl |
|---|---|
| Size | 1.2 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9af4d2af5555b9bf2165f7656303dc297c171ded30d92a2df264fe3f1a5419d1
|
|
BLAKE2b-256 checksum How to use checksums |
7a0690580b3a9ec1ebb8b7dbb25d265095cb9f48a3750600176d776a50a95222
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.19
|
Security for snfa 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.