The Mixer: One Sum, Three Outputs
Every sound source on the Next adds its number to one sum, 28 million times a second. Nothing is ever subtracted, so silence is not zero. And that one sum has to come out of a 3.5 mm jack, a small speaker inside the case and an HDMI cable, all at the same time. How does a single number become three different outputs, and how much room is there in it before the sound breaks?
I think of this as the most important room in the whole sound system, because every program’s sound passes through it. A game’s music, its effects, its sampled speech and the beeper’s clicks all meet here, and whether they come out clean depends on how they fit together. A mix that is too loud does not merely get louder: it can break up. Know how the sum is built, and you know how loud each part of your mix may be, and why the speaker can click while the headphones stay quiet.
Read The Big Picture: Four Sources, One Mixer first. It introduces the four kinds of source and the mixer, and here we take the mixer apart in full. The background chapter How a Computer Makes Sound explains the terms used below: a sample, a DAC, a filter, sound from a single bit and mixing. And if you read only one chapter after the overview, I would make it this one.
The road ahead
We start with the sum itself: five inputs, each lined up at its own place in 13 bits, placed so that the adder can never overflow. Then comes a small surprise, why a silent Next does not add up to zero but to 1,536, and why I count that as a good thing. The key idea sits in the section after it, Headroom: how much of the range real music takes, and what the audio jack does with a sum it cannot hold, which is not what you would expect. From there we follow the sum out of the machine three ways: through the jack’s one-bit converter, through the little speaker on the board, and through HDMI, the only one of the three that samples. We end by putting the whole sum back together.
Levels are in the units of the Next’s mixer: the sum of one side runs from 0 to 8,191, and every level here is a number on that scale (EAR is 512, one AY channel at full volume 255). Each side is mixed on its own; the numbers are for one side. The figures of the sum and its outputs are worked out from the hardware’s rules.
Five inputs, one sum
The mixer has two sides, and on each it adds five inputs. The beeper gives two of them, one per bit
of port $FE; each of the other three kinds of source gives one. The sum is taken again at every tick of
the 28 MHz master clock, so a change in any input reaches the sum within a 28-millionth of a second.
I like to picture each side’s sum as the collection plate: it goes round, every source drops in what it has, and nobody ever takes anything out. The picture fits better than you might think, as the next section shows: even in a silent church there are coins on the plate.
The sum is a 13-bit number, 0 to 8,191. Each input is a binary number too, and the mixer lines it up at a fixed place in those 13 bits before adding. Where an input sits decides how much it can add:
- EAR, port
$FEbit 4, is one bit placed at bit 9 of the sum: 512 while the port bit is 1, 0 while it is 0. - MIC is one bit at bit 7: 128 or 0. It is port
$FEbit 3, combined with the tape input and the Pi’s EAR bit (the overview explains the combination). EAR and MIC count 4 to 1, as the 48K’s resistors weighted them. - The AY chips arrive as one 12-bit number per side, the channels of all three chips on that side already added together: 0 to 2,295. One channel at full volume is 255. The TurboSound chapter’s Where the channels go explains how the channels are spread over the sides; with all three chips in mono, each chip gives both sides up to 765, three times 255.
- The DACs arrive as one 9-bit number per side, the side’s two 8-bit channels added (A + B on the left, C + D on the right), 0 to 510. Placed two bits up, it counts four times: 0 to 2,040.
- The Pi arrives as a 10-bit sample per side, 0 to 1,023, added as it is.
What is the most they can add, then? Every input at its largest gives 512 + 128 + 2,295 + 2,040 + 1,023 = 5,998. The sum’s 13 bits reach 8,191, so the sum itself can never overflow: the adder needs no check, and nothing is cut off. I find this one of the neatest decisions in the machine. Overflow is not handled; it simply cannot happen.
The weights are no accident either. They keep EAR and MIC at the 48K’s 4 to 1, and they make one DAC channel’s full swing, 255 steps of 4, a little louder than all three channels of one AY chip together (1,020 against 765). That leaves room for drums and speech over AY music, which is what the Spectrum’s DAC sound cards were for. If you ask me, that is exactly the right balance: the drummer should be heard over the band.
Silence is 1,536
Every input is a count upwards from zero. None of them can be negative, and the mixer never subtracts. A sound is a movement up and down around some level, and here that level is whatever the sources add while they are quiet.
That level is not zero, because two of the sources rest in the middle of their range:
- A DAC channel rests at
$80, 128, halfway up its 0–255 range. A sample is a movement around that middle, so it has room to swing both ways. The two channels of a side add 256, which counts four times: 1,024. - The Pi rests at 512, the middle of its 0–1,023, for the same reason.
- The AY chips and the beeper rest at 0: an AY channel at volume 0, and EAR and MIC low.
So a silent Next adds 1,536 on each side, and stays there however long nothing plays. Those are the coins that lie on the collection plate before the first source has dropped in a single note.
Why go to the trouble? Because of the moment a source is switched on. If the DACs rested at 0 and jumped
to their middle when your program enabled them, the sum would leap by 1,024 in an instant, and the
speaker would answer with a loud click. The Next avoids that by holding them at $80 even while
they are switched off. Bit 3 of the Peripheral 3 Setting register, NextReg $08, enables the DACs, and
while it is 0 every channel is held at $80, so switching them on or off changes nothing you can hear.
The Pi does the same: switched off or muted, it adds its resting 512, not 0. I wish every sound card of
the era had been this polite. The beeper cannot play this trick. The first time a program sets port
$FE bit 4, the sum rises by 512, which is exactly the click a 48K made too.
And what happens to the constant 1,536? It is not lost on its way out, but you never hear it. After the converters, outside the FPGA, the jack’s circuit lets only changes through, as the background chapter describes for a capacitor; what reaches your headphones is the sound moving around zero.
Headroom
How much of the range do real mixes use? Here are six of them, as the Next’s hardware adds them: on the sum’s 0 to 8,191 scale, with a line at 4,095, which matters for the jack. They are worked out from the mixer’s rules, not captured.
Let’s work them out, for the left side:
- Silence: the DACs at rest, 1,024, and the Pi at rest, 512: 1,536.
- A 128K tune on one chip, all three channels at full volume: on the left, channels A and B arrive (the TurboSound chapter’s Where the channels go), 2 × 255 = 510: 2,046.
- TurboSound, nine channels at full volume: three chips’ 510 each, 1,530: 3,066.
- The same, with the beeper on top, EAR and MIC both high, 640: 3,706.
- Three chips in mono at full volume, each giving 765 to both sides, 2,295, with EAR on top: 4,343.
- Every source at its largest, the DACs and the Pi at full scale instead of resting: 5,998.
The sum handles all six: it has room up to 8,191. The audio jack does not. Its converters take only the lowest 12 bits of the sum, 0 to 4,095, and leave bit 12 out. A sum below 4,096 arrives whole. A sum of 4,096 or more loses its top bit, which takes 4,096 off it: 4,343 arrives as 247. Uhm, yes, you read that right. The sound does not stop at a ceiling, as the background chapter’s clipping would; it wraps around to the bottom of the range, like an old car’s odometer rolling over from all nines to all zeros:
I will not pretend I like this wrap: a flattened top would be far kinder to the ear. But before you start to worry: in practice the jack has 2,559 units of room above silence, and it plays everything a 128K tune or TurboSound in stereo can produce, with the beeper on top. What goes past is the extreme: three chips in mono at full volume with the beeper, or a DAC or Pi sample near full scale on top of loud music. So here is my advice: keep your total below 4,096, and you have nothing to worry about. HDMI, which carries all 13 bits (see HDMI audio below), never wraps at all.
The audio jack: one pin, 4,096 levels
The jack’s sound leaves the FPGA as one pin per side that can only be on or off. Have you ever wondered how a single on-off pin can sing in 4,096 shades? That is the job of a one-bit converter, the delta-sigma kind the background chapter describes under sound from a single bit.
It works on the 28 MHz clock, one decision per tick:
- The converter keeps a running total. At every tick it adds the input, the 12-bit value from the sum.
- If the total is in the top half of its own range, the pin is on for this tick, and 4,096 is taken off the total. Otherwise the pin is off.
- Over many ticks, the pin is on for exactly the share of ticks that the input is of 4,096. An input of 1,024 switches it on one tick in four; 1,536, three ticks in eight.
The pin’s pulses are far too fast to hear: the pattern repeats every few ticks, millions of times a second. A resistor and a capacitor on the board after the pin, a low-pass filter, average them into a smooth voltage: the larger the input, the higher the voltage. Then the circuit that removes the constant level (see Silence is 1,536) leaves the sound alone for your headphones or amplifier.
Two things follow from this design, and I like both of them. The jack has no sample rate: its converter follows the sum as it changes, at 28 MHz, so nothing is ever sampled and nothing is aliased on the way out. And its resolution is the whole 12 bits: a change of a single unit in the sum moves the share of on-ticks by one in 4,096. For a part that costs one pin, a resistor and a capacitor, I call that a bargain.
The internal speaker
The Next has a small speaker of its own on the board, driven by one more one-bit converter of the same kind. It has no left and right, so it gets a mono mix: the left and right values the jack takes, both 12 bits, added together. Some boards bias the result to suit the transistor that drives the speaker, but the idea is the same on all of them. Nobody will master an album on it, but I am glad it is there: a Spectrum that beeps from its own case still feels like a Spectrum.
Two registers control it:
- The Peripheral 3 Setting register, NextReg
$08, bit 4 switches the speaker on (1, the power-on value) or off. Off, the converter keeps running but its pin is held low. - The Peripheral 2 Setting register, NextReg
$06, bit 6 makes the speaker play the beeper only. With it set and the speaker on, EAR and MIC leave the mix of the jack and HDMI altogether, and the speaker gets a word of its own with nothing in it but them.
Here is what each output takes from the sum, bit by bit:
In the beeper-only word, EAR is bit 10 and MIC bit 8: 1,024 and 256 of the speaker’s 8,191, the same 4-to-1 ratio, and as loud as EAR and MIC are in the speaker’s normal mix, where each side brings its 512 and 128. What is that mode for? A program that wants its key clicks and alerts to stay in the machine while its music goes to the headphones. Let me warn you about one thing, though: the beeper’s sound is moved, not copied, so the headphones no longer hear it.
HDMI audio
HDMI carries the picture as numbers, and its sound as numbers too: 48,000 samples a second for each side, travelling between the lines of the picture. The Next builds each sample as a 16-bit word: a 0, the whole 13-bit sum, and two 0 bits at the bottom (the figure in the previous section). The word is therefore the sum times 4, and it never wraps: 5,998 arrives as 23,992.
The Peripheral 4 Setting register, NextReg $09, bit 2 silences HDMI audio, for a television whose
own speakers should stay quiet while the sound goes through the jack.
Unlike the jack, HDMI samples the sum. 48,000 times a second it takes the sum’s value at that moment, without averaging what happened since the last one. That is why I call HDMI the photographer: it takes a snapshot of the collection plate, then looks away until the next one, and whatever happens between two clicks of its shutter it learns about only from the next picture. A change that falls between two samples is seen only at the next one, up to a 48,000th of a second (21 µs) late:
Should you care? For most sound, I would say no: 48,000 samples a second is enough for anything a human hears, as the background chapter explains under Fast enough. For the sharp edges of the beeper it is a small cost: an edge that moves by up to one sample adds a faint roughness, a little aliasing, to a high beeper tone that the jack plays clean. The photographer is quick, but not quick enough for every blink. The last chapter shows the same choice in an emulator, which must sample too.
The constant level travels with the numbers: a silent Next sends 1,536 × 4 = 6,144 in every word.
What to take home
Here is the collection plate one last time, with everything that goes on it. On each side, the Next’s mixer adds five inputs at 28 MHz, each lined up at its own place in a 13-bit sum: EAR at bit 9 (512), MIC at bit 7 (128), the AY chips (0 to 2,295), the DACs (0 to 510, counted four times, so 0 to 2,040) and the Pi (0 to 1,023). Every input at its largest gives 5,998, inside the sum’s 8,191, so the sum never overflows.
Every input counts up from zero, and the DACs and the Pi rest in the middle of their range, $80 and 512,
even while they are switched off: a silent Next sums to 1,536, and switching those sources never
clicks. The circuit after the converters removes that constant level before you hear anything.
The audio jack takes only the sum’s lowest 12 bits, up to 4,095, which leaves 2,559 above silence: enough for nine AY channels at full volume and the beeper. Past 4,095 the jack’s value wraps around, a hard crackle rather than a flattened top. Its one-bit converter switches a pin on for the input’s share of 4,096 of the 28 MHz ticks, a resistor and capacitor smooth that into a voltage, and so the jack has no sample rate at all.
The internal speaker plays the left and right values added, through one more such converter; bit 4
of the Peripheral 3 Setting register, NextReg $08, switches it on, and bit 6 of the Peripheral 2
Setting register, NextReg $06, gives it the beeper alone, which then leaves the jack and HDMI.
HDMI, the photographer, sends the whole sum as 16-bit words, 48,000 a second, taking the sum’s value
at each sample’s moment; bit 2 of the Peripheral 4 Setting register, NextReg $09, silences it.
If I had to keep one rule from all of this, it would be the jack’s limit. Keep your mix under 4,096, and the Next will play every note of it.