Audio from the Raspberry Pi: Samples on Four Wires
Open a Spectrum Next, and next to the FPGA you find a whole second computer: a Raspberry Pi Zero, with an ARM processor, hundreds of megabytes of memory and a Linux of its own. Compared with the Z80, it is a lorry parked next to a bicycle. A program on it can decode an MP3, speak from text, or pretend to be any sound chip you like. But a computer that clever is no use to the Next’s sound unless its sound can get in. How do you pour the output of a Linux machine into a 1980s home computer, and even trick that home computer into loading a tape from it?
The answer matters as soon as a program wants more than the Next can make by itself. Music recorded in full, a long piece of speech, a whole tape played as sound for the Next to load: these are jobs for a machine with memory to spare and a processor to decode. The Pi does the heavy lifting, and the Next only has to listen. I call the Pi the lodger: it lives in the Next’s case, pays its rent in samples, and keeps a whole life of its own that the Next never needs to know about.
Read The Big Picture: Four Sources, One Mixer
first: it introduces the Pi as one of the four kinds of source. The chapter on
the beeper, EAR and MIC explains port $FE
bit 6 and the MIC line, which the Pi’s tape trick plugs into, and the previous chapter,
The 8-Bit DACs: Four Channels, Six Sound Cards,
shows NextRegs $2C to $2E from the writing side. Samples and digital sound are explained in the
background chapter How a Computer Makes Sound.
The road ahead
First, the link itself: four wires, a clock the Pi drives, and words that take turns, left and right. Then the one register that controls it all, and what it does to the two sides: stereo, mono from either word, or a side muted. We look at how a sample from the Pi becomes the Next’s 10-bit word, and at the exact moment each word arrives. From there the words go three ways: into the mixer, to three NextRegs a program can read, and, the trick I would ask you to remember, through a threshold into the tape input, so that the Next can load a program the Pi plays as sound. That threshold has a surprise in it, by the way. We end with the way back, the Next’s own sound sent to the Pi.
The numbers are for the Next’s 28 MHz master clock. The Pi’s samples are signed 16-bit numbers,
−32,768 to 32,767, and the Next’s are 10-bit words, $000 to $3FF. The captures come from an emulated
Next at 48,000 samples a second, from programs checked to do what the text says, with a stream of real
samples fed into the Next’s receiving half of the link in the Pi’s place, so what you see is the Next’s half
working as it does on the hardware.
Four wires and a clock from the Pi
The Pi and the Next talk sound over I2S, a link made for exactly one job: carrying a stream of stereo samples, one bit at a time. It needs only a few wires:
$A2 bit 4 decides which pin is which.- The bit clock ticks once for every bit sent.
- The word select says whose word the bits belong to: low for the left word, high for the right one.
- Two data pins carry the bits themselves, one in each direction.
On the Next, the Pi is always the master: it drives the bit clock and the word select, and the Next only listens to them. So the Pi, not the Next, decides the sample rate. Whatever rate the Pi’s software chooses, 44,100 or 48,000 a second or anything else, the Next takes the words as they come. Well, that is one fewer thing for you to set up, and I am all for that. The lodger keeps its own hours, and the Next is polite enough not to ask.
Here is one sample period on the wires:
Each word starts one clock after the word select changes, its most significant bit first. And notice where the words are latched, that is, where the Next takes a finished word and starts using it: the left word at the word select’s rise, in the middle of the period, and the right word at its fall, at the end. We will come back to that half a period of difference in a moment. It looks like a footnote now; trust me, it is not.
One register: NextReg $A2
Everything the Next does with the link is set in one register, the PI I2S Audio Control register, NextReg
$A2. I like that: one byte to read, one byte to get right.
$A2. Bits 5 and 1 are fixed: write bit 5 as 0 and bit 1 as 1.- Bits 7 and 6 enable the left and the right word. Both clear means the link is off: the Next ignores the Pi, and the receiver is held at rest. That is how it starts after a reset.
- Bit 4 chooses which of the two data pins brings the Pi’s samples in and which takes the Next’s mix out.
The register list calls the two settings “hats” (
0) and “pi” (1). - Bits 3 and 2 mute the left and the right side.
- Bit 0 sends the Pi’s sound to EAR, the tape input, instead of to the mixer. More on that below.
Bits 7 and 6 do one more thing. With only one of them set, the side whose bit is clear does not go silent; it
takes the other side’s word. So 10 plays the Pi’s left word on both sides, and 01 plays the right word on
both. Here is a stream with a 1,000 Hz sine on its left word and a 1,500 Hz sine on its right, heard four ways:
$A2, each side as the mixer gets it. A muted side holds the rest word $200.A muted side, a link that is off and a stream routed to EAR all give the mixer the same word: $200, the
middle of the range, which is silence. I find it reassuring that every way of saying “not now” ends at the
same word.
From a sample to a word
The Pi’s software sends ordinary signed samples, typically 16 bits each, sometimes 24 or 32. The Next keeps only what it needs:
The receiver keeps the top 13 bits of each word as it arrives, whatever the word’s length. That is why a
16-, a 24- and a 32-bit stream all work: the extra bits fall off the bottom. The Next then uses the top 10
of those and inverts the sign bit. Inverting the sign bit is the cheapest trick in the book for turning a signed
number into one that counts from 0, the form every other source of the mixer uses: −32,768 becomes $000,
0 becomes $200 and 32,767 becomes $3FF. If you like formulas, the word is
((sample >> 6) & $3FF) xor $200 for a 16-bit sample.
Ten bits sounds stingy next to the Pi’s sixteen, doesn’t it? But it matches the rest of the Next: a DAC channel has 8 bits, and the mixer’s whole range for a side is 13. The Pi gets more steps than any one DAC. I find that a fair deal: six bits stay at the door, and what comes in is still plenty.
When a word arrives
The left and the right word of one sample do not arrive together. Remember the latches: the left word at the middle of a sample period, the right word at its end. Here is a slow stream, 1,000 samples a second, whose samples alternate between two levels, the same on both sides:
At a real sample rate the gap is tiny, about 10 µs at 48,000 samples a second, and no ear can hear it. But a program that reads both words and compares them can see it, and so, as you will see, can the tape input. Two more details: a link switched on starts at the rest word and takes the Pi’s words from the next latch, and a link switched off goes back to rest at once. If the Pi stops clocking, nothing is latched any more, and the receiver holds its last two words. I would remember that last rule; a stuck level with no sound coming in is the lodger gone quiet, not the Next going wrong.
Into the mixer
Each side’s word goes into the mixer as it is: 0 to 1,023 units, one unit a step.
One unit a step is a quarter of a DAC step, which the mixer counts four times. So a Pi stream at full scale swings 1,023 units, about the same as one DAC channel at full scale, and twice the beeper’s 512. At rest it adds 512 units, the other part of the 1,536 a silent Next sits at, next to the DACs’ 1,024. The mixer chapter’s Headroom shows how much room is left for the rest.
Reading the Pi’s samples
A program can also read the words, through the same three NextRegs that write the DACs: the DAC B Mirror
(left) register, NextReg $2C, the DAC A+D Mirror (mono) register, NextReg $2D, and the DAC C Mirror
(right) register, NextReg $2E. NextReg $2C reads the left word and NextReg $2E the right one, but a
NextReg has only eight bits, and a word has ten. So the read comes in two parts:
$2C returns the top eight bits and keeps the bottom two for NextReg $2D.- A read of NextReg
$2Creturns bits 9–2 of the left word, as the selection gives it: muted or off, that is the rest word. At the same moment it copies bits 1–0 into a latch. - A read of NextReg
$2Dreturns those two latched bits in its bits 7 and 6.
NextReg $2E works the same way for the right word, through the same latch. I think of that latch as the
cloakroom: you hand in the low two bits when you read the top eight, and collect the same two bits with
NextReg $2D, however long you take. Why the latch? Because the word can change between your two reads, and
the latch makes sure the low bits belong to the same word as the high bits you just read. Most programs need
only the top eight bits anyway: $2C alone gives you a perfectly good 8-bit sample, and that is what I would
start with. With nothing connected, $2C and $2E read $80 and $2D reads $00, the rest word in two
parts.
Loading a tape from the Pi
Here is my favourite trick of the whole link. Set bit 0 of the PI I2S Audio Control register, NextReg
$A2, and the Pi’s words stop going to the mixer. Instead they drive a single bit, the EAR bit, which
goes where a tape recorder’s signal goes: into port $FE bit 6, the bit a tape loader reads. A program on
the Pi plays a tape as sound, and the Next loads it, none the wiser. And the best part: it even sounds like a
tape loading, because the EAR bit also goes into the MIC line, which the beeper chapter showed you reaches the
speaker. The lodger has learned to impersonate a cassette recorder, stripes and all.
How does a 10-bit word become one bit? Not with a plain threshold at the middle. A tape signal is never clean: it has noise on it, and at the moment a slow wave crosses a threshold, the noise would flip the bit back and forth several times, and every flip looks like an edge to a loader. So the Next uses hysteresis, two thresholds instead of one:
- The bit rises when the top two bits of the left word and of the right word, ORed together, read
11. - It falls when they read
00: both words’ top two bits are00, so both are below$100, a quarter of the range. - Anything in between, and the bit holds what it had.
Now the surprise I promised. You might expect the bit to rise only at three quarters of the range, at $300,
where a word’s own top bits read 11. But the rule ORs the bits of two words, and remember, the left word is
latched half a period before the right. On a rising wave, at the moment it crosses the middle, the left word has
just stepped to $200 or a little above, top bits 10, while the right word is still just below the middle,
top bits 01. And 10 OR 01 is 11. So the bit rises right at the upward zero crossing, and falls only
when the wave has dropped below a quarter. Here it is on a slow, noisy 250 Hz wave:
$A2 = $C1. The captured bit rises at the upward zero crossing and falls below −16,384: two clean edges a cycle. A plain threshold at 0, worked out for the same wave, chatters at every crossing.Two clean edges a cycle, while a plain threshold chatters. For a loader, that is the difference between loading
and “R Tape loading error”. And for a real tape signal, which swings hard from one side to the other, it does not
matter where exactly between the middle and a quarter the bit flips: the edges come at the same rate, and that
is all a loader counts. A Z80 loop that reads port $FE bit 6 while the Pi streams a 1,000 Hz square wave
counts 2,000 edges a second, as it would from a tape. I love this one: a rule that looks lopsided on paper, and
works perfectly for the only job it has.
Port $FE bit 6 is the EAR bit XORed with the real tape input, ORed with the others the beeper chapter lists, so a
quiet tape input leaves it as it is. The EAR bit is 0 whenever the link is off or the routing bit is clear.
The way back: the Next’s sound to the Pi
The link carries sound the other way too. On the other data pin, the Next sends the Pi its whole mixed sound, both sides, the same 13-bit sums the mixer adds, with the top bit inverted to make them signed again, at the top of each word. The Pi’s software can record it, analyse it, or send it on, to a network or a Bluetooth speaker, say. The Next needs no setting for this beyond enabling the link: whenever the Pi clocks a word in, the Next clocks its own word out. A fair exchange, if you ask me: the lodger pays in samples, and gets the whole house’s sound in return.
What to take home
One link, four wires, and a lodger who keeps the beat. The Raspberry Pi inside the Next sends its sound over
I2S: a bit clock and a word select, both driven by the Pi, and a data pin each way. The Pi chooses the rate;
the Next keeps the top 13 bits of each word, uses the top 10 with the sign bit inverted, and so turns −32,768, 0
and 32,767 into $000, $200 and $3FF. The left word is latched in the middle of each sample period, the
right word at its end.
NextReg $A2 controls it all: bits 7 and 6 enable the left and right word, and a side whose bit is clear takes
the other side’s word; bits 3 and 2 mute; bit 4 swaps the data pins; bit 0 sends the sound to EAR. In the mixer
the Pi adds 0 to 1,023 units a side, 512 at rest. NextRegs $2C and $2E read the top eight bits of each word
and keep the bottom two for NextReg $2D, the cloakroom where they wait for you.
Routed to EAR, the words drive port $FE bit 6 and the MIC line through hysteresis: up when the two words’
top two bits OR to 11, which a rising wave reaches already at its zero crossing, down when both are below a
quarter. That is what lets the Next load a tape the Pi plays, without a single loader noticing. And on the other
pin, the Next’s own mix goes back to the Pi, so the conversation runs both ways.