{"slug": "how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example", "title": "How an AI Voice Detector Works: Features, Classifiers and a Toy Python Example", "summary": "Tania Alwan published an explainer on how AI voice detectors, known in research as spoofing countermeasures, work: they frame the task as binary classification scored by equal error rate (EER) and the tandem detection cost function (t-DCF), evaluated mainly on ASVspoof challenge data. The piece covers hand-crafted spectral features, the two main detector families, and the limits of commercial accuracy claims, then walks through a toy Python detector that extracts eight features from a short-time Fourier transform and trains a logistic regression on self-generated harmonic-tone clips. Alwan is explicit that the toy detects only the artifacts she planted and says nothing about real cloned voices.", "body_md": "*By Tania Alwan*\n\nCloned and synthetic voices are now easy to make, so \"is this voice real?\" has become a practical question for podcasters, journalists, call centres and musicians. An AI voice detector tries to answer it automatically. This post explains how these detectors work under the hood, what the research community uses to test them, where they fail, and then builds a deliberately tiny detector in Python so you can see the moving parts.\n\nSpoiler: the toy works on toy data. Real detection is much harder, and I will be explicit about why.\n\nIn research, this problem is usually called **spoofing detection** or **anti-spoofing**, and the detector is a **countermeasure (CM)**. The task is binary classification:\n\nThe detector outputs a score, and a threshold turns the score into a decision. Because false alarms and misses both matter, the field reports the **equal error rate (EER)**: the operating point where the false-acceptance rate equals the false-rejection rate. Lower is better.\n\nMost published AI voice detector work is evaluated on data from the [ASVspoof challenge series](https://www.asvspoof.org/), which started in 2015 and was built to protect automatic speaker verification (ASV) systems.\n\nAlongside EER, ASVspoof also uses the **tandem detection cost function (t-DCF)**, which measures how a countermeasure affects a speaker verification system it protects.\n\nSynthetic speech can sound convincing to people while still leaving statistical traces in the signal. Classic pipelines start with hand-crafted spectral features:\n\nWhy high frequencies? Many speech generation pipelines work at limited sample rates or use vocoders whose artefacts show up in particular bands. But this is a property of specific systems, not a law of nature, and it changes with every new generator.\n\nDetectors broadly fall into two families:\n\nCommercial \"AI voice detector\" products generally do not publish their architectures or training data, so treat their accuracy claims with care unless they come with an independent evaluation.\n\nThis is the most important section.\n\nBecause detection after the fact is fragile, researchers also work on **watermarking** synthetic audio at generation time, for example [AudioSeal (San Roman et al.)](https://arxiv.org/abs/2401.17264). That only helps when the generator cooperates, so the two approaches complement each other.\n\nThe script below generates its own fake \"voices\" (harmonic tones plus breath-like noise) so it runs without downloading any dataset. \"Synthetic\" clips get a steadier pitch and a band-limited noise floor, a crude stand-in for vocoder artefacts. It extracts eight summary features (mean and standard deviation of four descriptors) from a short-time Fourier transform and trains a logistic regression.\n\nThis is a toy. It detects the artefacts *I* planted. It says nothing about real cloned voices.\n\n```\npip install numpy scikit-learn\npython\nimport numpy as np\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_curve\n\nSR = 16000\nrng = np.random.default_rng(0)\n\ndef fake_voice(synthetic, dur=1.0, cutoff=4000):\n    \"\"\"Toy 'voice': a harmonic tone plus breath noise.\n    'Bona fide' clips get pitch jitter and full-band noise;\n    'synthetic' clips get a steadier pitch and a band-limited noise floor\n    (a crude stand-in for vocoder artefacts). Not real speech!\"\"\"\n    t = np.arange(int(SR * dur)) / SR\n    f0 = rng.uniform(100, 220)\n    jitter = 0.002 if synthetic else 0.02\n    f0_track = f0 * (1 + jitter * np.cumsum(rng.standard_normal(t.size)) / np.sqrt(t.size))\n    phase = 2 * np.pi * np.cumsum(f0_track) / SR\n    y = sum(np.sin(k * phase) / k for k in range(1, 30))\n    noise = rng.standard_normal(t.size) * 0.05\n    if synthetic:  # remove most energy above `cutoff` Hz from the noise floor\n        spec = np.fft.rfft(noise)\n        spec[int(cutoff * t.size / SR):] *= 0.1\n        noise = np.fft.irfft(spec, n=t.size)\n    return (y + noise) / np.max(np.abs(y + noise))\n\ndef features(y, n_fft=512, hop=256):\n    frames = np.lib.stride_tricks.sliding_window_view(y, n_fft)[::hop]\n    mag = np.abs(np.fft.rfft(frames * np.hanning(n_fft), axis=1)) + 1e-10\n    freqs = np.fft.rfftfreq(n_fft, 1 / SR)\n    log_spec = np.log(mag)\n    centroid = (mag * freqs).sum(1) / mag.sum(1)\n    flatness = np.exp(log_spec.mean(1)) / mag.mean(1)\n    hf_ratio = mag[:, freqs > 4000].sum(1) / mag.sum(1)\n    flux = np.r_[0, np.sqrt((np.diff(log_spec, axis=0) ** 2).mean(1))]\n    feats = [centroid / 1000, flatness, hf_ratio, flux]\n    return np.array([f(x) for x in feats for f in (np.mean, np.std)])\n\nlabels = np.array([0] * 200 + [1] * 200)  # 0 = bona fide, 1 = synthetic\nX = np.array([features(fake_voice(bool(l))) for l in labels])\nX_tr, X_te, y_tr, y_te = train_test_split(X, labels, test_size=0.3, random_state=0, stratify=labels)\n\nclf = LogisticRegression(max_iter=1000).fit(X_tr, y_tr)\nscores = clf.predict_proba(X_te)[:, 1]\n\nfpr, tpr, _ = roc_curve(y_te, scores)\ni = np.nanargmin(np.abs(fpr - (1 - tpr)))\nprint(f\"accuracy: {clf.score(X_te, y_te):.2f}\")\nprint(f\"EER: {(fpr[i] + 1 - tpr[i]) / 2:.3f}\")\n\n# A \"new generator\": same idea, different artefact (cutoff at 7 kHz)\nX_new = np.array([features(fake_voice(bool(l), cutoff=7000)) for l in labels])\nprint(f\"accuracy on unseen 'generator': {clf.score(X_new, labels):.2f}\")\n```\n\nWith these seeds, my run printed:\n\n```\naccuracy: 0.95\nEER: 0.083\naccuracy on unseen 'generator': 0.72\n```\n\nYour numbers may differ slightly with other library versions. The last line is the interesting one: the same classifier, tested on \"synthetic\" clips whose artefact sits at 7 kHz instead of 4 kHz, drops sharply. That is the generalisation problem from the research above, in miniature.\n\nThings worth trying:\n\n`clf.coef_`) to see which cues the model leans on.\nIf you build or use voice-cloning tools, detection is only half the story. Consent matters too: platforms such as [Voicestars](https://www.voicestars.co/create-ai-voice) let people create AI versions of their own voice, and clear consent rules are part of making synthetic voices trustworthy.\n\nDisclosure: this article was written in partnership with Voicestars.", "url": "https://wpnews.pro/news/how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example", "canonical_source": "https://dev.to/taniaalwan/how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example-e0d", "published_at": "2026-11-23 09:05:40+00:00", "updated_at": "2026-11-23 09:12:16.510578+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "ai-safety", "ai-research"], "entities": ["Tania Alwan", "ASVspoof", "AudioSeal", "San Roman"], "also_reported_by": [], "takeaway": "Voice anti-spoofing is scored by equal error rate and t-DCF on ASVspoof data, and a toy detector built from eight STFT summary features plus logistic regression only catches artifacts the author planted, not real cloned voices.", "recommendation": {"advice": "Evaluate voice spoof detectors with EER and t-DCF on ASVspoof-style data, and treat commercial accuracy claims skeptically unless backed by independent evaluation.", "playbook": "evals-testing", "playbook_url": "https://wpnews.pro/playbooks/evals-testing"}, "alternates": {"html": "https://wpnews.pro/news/how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example", "markdown": "https://wpnews.pro/news/how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example.md", "text": "https://wpnews.pro/news/how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example.txt", "jsonld": "https://wpnews.pro/news/how-an-ai-voice-detector-works-features-classifiers-and-a-toy-python-example.jsonld"}}