AI Beyond Language Models ยท Everyday AI

Sound as Numbers: Where Speech AI Starts

A microphone measures air pressure many times a second. Sample a simple wave, and find its pitch by counting how often it rises through zero.

sound.py

import math

# Speech AI starts with sound as numbers: air pressure, measured many times a second.
rate = 16                     # samples per second (real audio uses 16,000 or more)
times = list(range(rate))
samples = [round(math.sin(2 * math.pi * 2 * t / rate), 2) for t in times]   # a low 2 Hz hum

# How many times does the wave go up through zero? That is its pitch, in waves per second.
crossings = len([1 for a, b in zip(samples, samples[1:]) if a <= 0 < b])
print("pitch:", crossings, "waves per second")

Step by step

  1. This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
  2. The sample rate: how many measurements per second. 16 here, so the numbers stay readable.
  3. Sample times: 0 to 15.
  4. One second of a 2 Hz hum: a wave that goes up and down twice. Each number is one sample; the chart shows the wave they make.
  5. Count the places where the wave goes from zero or below to above zero: each is the start of a new wave.
  6. 2 waves per second: 2 Hz. Real speech mixes many pitches at once, so speech AI first turns the samples into a spectrogram (how strong each pitch is, moment by moment) and feeds that to a transformer, much like the tokens of a sentence.