sox_tensorflow
TensorFlow implementation of SoX-style spectrogram generation that uses TensorFlow operations for GPU acceleration.
LINKS
- sox: https://github.com/chirlu/sox
- pysox: https://github.com/marl/pysox
- audio samples:
- pnw-cnet-model: https://dse-soundhub.s3.us-west-2.amazonaws.com/public/models/pnw/PNW-Cnet_v4_TF.h5
Our analysis shows
- sox_tensorflow spectrograms are 99.81% exact-pixel-match on average relative to sox.
- Every segment falls within ±2 pixel values
- The small residual error is concentrated in the darkest pixels (0–10% brightness decile: ~99.3% accuracy) and vanishes almost entirely in brighter regions where the signals live
- 100% agreement with top-5 ranks agreement when passed through PNW-Cnet v4 model
- The model-output classes with the largest mean absolute difference are BUVI and PSFL (around 0.0004)
For more details see the scripts and notebooks found in the comparison folder.
QUICK START
import soundfile as sf
import tensorflow as tf
from sox_tensorflow import spectrogram, spectrogram_from_flac
# From a numpy array
samples, sr = sf.read('audio.flac', dtype='float64', always_2d=True)
samples = samples[:, 0] # mono
pixels = spectrogram(
audio_array=tf.constant(samples, dtype=tf.float64),
shape=(257, 1000),
sample_rate=sr,
dest='spectrogram.png'
)
# From a FLAC file directly
path = spectrogram_from_flac(
flac_path='audio.flac',
shape=(257, 1000),
duration=12.0,
segment=0,
dest='spectrogram.png'
)
API
spectrogram(audio_array, shape, dest, ...)
Generates a SoX-matching spectrogram from an audio array or TensorFlow tensor.
| Argument | Type | Description |
|---|---|---|
audio_array |
tf.Tensor or np.ndarray |
Audio samples, float32/float64 in [-1, 1] |
shape |
(int, int) |
Output shape as (height, width). Height determines frequency resolution: DFT size = 2 × (height − 1) |
dest |
str or Path, optional |
Output PNG path. If None, returns a tf.Tensor |
segment |
int, optional |
Segment index (0-based) to extract from the audio |
segment_duration |
float, optional |
Duration of each segment in seconds |
segment_overlap |
float, optional |
Overlap between segments in seconds |
sample_rate |
int |
Sample rate of the input audio in Hz |
output_sample_rate |
int |
Sample rate for spectrogram generation (default: 8000) |
db_range |
int |
Dynamic range in dB (default: 90) |
Returns a tf.Tensor of pixel values (uint8, shape (height, width)) if dest is None, otherwise the path to the saved PNG.
spectrogram_from_flac(flac_path, shape, dest, ...)
Convenience wrapper that loads a FLAC file and generates a spectrogram in one call. Accepts the same shape/segment/dest arguments as spectrogram(), plus:
| Argument | Type | Description |
|---|---|---|
flac_path |
str |
Path to FLAC file |
start_time |
float, optional |
Start time in seconds |
duration |
float |
Duration in seconds (default: 12) |
channel |
int |
Channel to extract (default: 0) |
load_audio(flac_path, start_time, segment, duration, channel)
Reads a FLAC file into a tf.Tensor. Returns (tensor, sample_rate).
NOTES
PNW-Cnet compatibility
When loading H5 models saved with older TensorFlow/Keras versions, set:
export TF_USE_LEGACY_KERAS=1
This forces TensorFlow 2.16+ to use the legacy Keras implementation, which maintains compatibility with older H5 model files.
SoX accuracy
The implementation replicates SoX's spectrogram algorithm exactly:
- Hann window with SoX-specific normalization
- FFT with SoX edge handling (partial windows at start/end of signal)
- dB conversion and pixel rendering matching SoX's palette
Resampling uses soxr (the SoX Resampler library) at HQ quality, achieving 99.8%+ pixel match with the SoX binary.
STYLE-GUIDE
Following PEP8. See setup.cfg for exceptions. Keeping honest with pycodestyle .
Metadata
Release files for sox-tensorflow 0.1.3
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 | |
|---|---|---|---|---|
| sox_tensorflow-0.1.3.tar.gz | 24.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sox_tensorflow-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 51.0 kB
Release files / sox_tensorflow-0.1.3.tar.gz
| Download URL | sox_tensorflow-0.1.3.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc0c8dd9d8221f43f203f63fd29b55ff1aa0523d6eb8fe42fa8d32a38e8a7ace
|
|
BLAKE2b-256 checksum How to use checksums |
948a6b61ea42fecb4ce51ee91e58ce901e09a6f945d9f60142d2d795dac4d428
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logRelease files / sox_tensorflow-0.1.3-py3-none-any.whl
| Download URL | sox_tensorflow-0.1.3-py3-none-any.whl |
|---|---|
| Size | 26.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3623d6f5ea41656d2acc3eeeee76c7abbf6b04a096a19fceb1eca2aa77432676
|
|
BLAKE2b-256 checksum How to use checksums |
912e4aa05029390753456602557e87eec15cb3382f291c4941df8343ef77b329
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logSecurity for sox-tensorflow 0.1.3
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for sox_tensorflow-0.1.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logA 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.