Audio features for native learning
Connect explicit signal recipes and CPU Float32 features to native Tensor, NN and ML workflows.
// Explicit analysis recipe and interoperability fixtures; not trained recognition.
import aner.audio
import aner.signal
import aner.tensor
import aner.nn
import aner.ml
import aner.plot
let clip = audio.tone(seconds: 0.128, rate: 8000, frequency: 500.0)
let plan = signal.stft_plan(rate: 8000, n_fft: 128, hop: 64, window: "hann", scaling: "power")
let powers = signal.spectrogram(clip, plan)
let mel_plan = signal.mel_plan(rate: 8000, n_fft: 128, bands: 16,
scale: "htk", normalization: "none", floor: 1e-10, log_base: "natural")
let logs = signal.spectrogram_slot()
let cepstra = signal.spectrogram_slot()
signal.log_mel_into(mel_plan, powers, logs)
signal.mfcc_into(logs, cepstra, coefficients: 8)
let images = signal.tensor(logs, layout: "cnn")
let sequence = signal.tensor(cepstra, layout: "sequence")
print(tensor.dim(images, 0))
print(tensor.dim(images, 1))
print(tensor.dim(images, 2))
print(tensor.dim(images, 3))
print(tensor.dim(sequence, 2))
// Random weights exercise native [B,C,F,T] compatibility; scores have no quality claim.
let model = nn.cnn(channels: 1, height: 16, width: 16, seed: 42)
.conv2d(outputs: 2, padding: 1).relu().head(classes: 2)
.on(device: "cpu", precision: "Float32").prepare()
print(tensor.cols(model.logits(images)))
// A deliberately trivial self-query fixture verifies the classical ML boundary.
// Evaluation needs separate recordings/speakers and a fixed training recipe.
let features = signal.features(clip).astype("Float64")
let labels: Tensor = [[0.0]]
let neighbors = ml.fit_knn(features, labels, k: 1)
print(tensor.item(neighbors.predict(features)))
signal.spectrogram_plot(logs).title("Explicit HTK log-Mel recipe").save("signal-log-mel.svg")
signal.spectrogram_plot(cepstra).save("signal-mfcc.svg")
Make it your experiment.
Use Aner 0.1.0 or newer. Save this program as examples/media/signal_learning.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/media/signal_learning.aner
aner run examples/media/signal_learning.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Requires Aner 0.1.0 or newer. Native PCM/WAV, synthesis, FFT and speech activity are available independently of optional FFmpeg and model backends. Decoding, resampling or local transcription requires the corresponding capability and separately supplied inputs or weights.
aner check examples/media/signal_learning.aner
aner run examples/media/signal_learning.anerBundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution