Kaldi-ark loading and writing module

2.18.1 · active · verified Mon Apr 13

kaldiio is a pure Python module for reading and writing Kaldi ark and scp files. It provides utilities for handling various Kaldi object types, including binary/text matrices, vectors, and compressed matrices, by interacting with NumPy arrays. The library also facilitates I/O operations through Unix pipes and supports some extended ark formats, including those used for NumPy, Pickle, WAV, and FLAC files. It is actively maintained with frequent minor and patch releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to write a dictionary of NumPy arrays to a Kaldi .ark file and generate a corresponding .scp file using `save_ark`. It then shows two common methods for reading data: `ReadHelper` for sequential iteration (supporting Kaldi-style specifiers and pipes) and `load_scp` for random access to features by utterance ID.

import numpy as np
from kaldiio import load_scp, ReadHelper, WriteHelper, save_ark

# Example: Create dummy data for writing
utt_ids = ['utt1', 'utt2', 'utt3']
features = {
    'utt1': np.random.rand(100, 40).astype(np.float32),
    'utt2': np.random.rand(120, 40).astype(np.float32),
    'utt3': np.random.rand(90, 40).astype(np.float32)
}

# Write features to an ARK file and generate an SCP file
output_ark_path = 'output.ark'
output_scp_path = 'output.scp'
save_ark(output_ark_path, features, scp=output_scp_path)
print(f"Written to {output_ark_path} and {output_scp_path}")

# Read SCP file using ReadHelper (sequential access, supports pipes)
print("\nReading via ReadHelper (scp:)...")
with ReadHelper(f'scp:{output_scp_path}') as reader:
    for key, matrix in reader:
        print(f"Key: {key}, Matrix shape: {matrix.shape}")

# Read SCP file using load_scp (random access, returns dict-like object)
print("\nReading via load_scp (random access)...")
loaded_features = load_scp(output_scp_path)
print(f"Loaded 'utt2' shape: {loaded_features['utt2'].shape}")

# Cleanup (optional)
import os
os.remove(output_ark_path)
os.remove(output_scp_path)

view raw JSON →