Saturday, March 15, 2025

A lightweight ADSR for AVRs

    I've been doing more projects that involve digital control of analog circuits, and I found myself needing to generate ADSR envelopes in software, on an Arduino. I found some approaches that implement a digital filter to simulate the RC curve you get from a capacitor charging. This usually involves slow floating-point calculations that the Arduino isn't well-suited to crunching.

RC curve ADSR - AudioMulch

Look up, it's a bird, it's a table

    A classic way to avoid slow math is to precompute it, and store the results in a table. Then you just look up the result for your given input. Storing the result for every possible input can result in an excessively large table though, so maybe you want to store fewer results and estimate values between them. This is called interpolation, and there are multiple ways of doing it. We could use the continuous curves of splines to connect our points, but this would be slower than the actual calculation we're trying to avoid. Instead we'll use fast linear interpolation, that just draws straight lines.

An interpolated attack curve

    Usually you would store a series of x and y coordinates, and then calculate the line between each pair. We really only care about the lines though, so we might as well precompute those also. Thinking back to algebra 1, we just need to store the slope and y-intercept to define them.

It's not a phase, period

    Now that we have a curve, we need to progress through it over time. A naive approach might be to march through every possible value, and simply do this as fast as needed. This is likely a waste of processing time, and we can instead strategically skip over some values to progress through the table more quickly. The Prophet 600 reportedly updates its envelopes at just 200Hz. It simply takes larger or smaller "steps" each update to change the length(period) of the stages.

Ain't no half‐steppin'

    Another beginner trap is to only think in terms of whole numbers, since we're avoiding floating points. Let's say we have 10 possible output values, and our smallest step is 1. It then takes 10 steps to get through all outputs. If our next smallest step is 2, it only takes 5, and that's twice as fast. It'd be nice to have 1.5 as an option. We can do this by using fixed point numbers. We can use steps of 15, and count up to 100 to get the same effect as steps of 1.5 counting up to 10. We just need to divide by 10 at the end to get the correct output. If we use powers of 2, instead of 10, the division becomes bit-shifting, and that's very fast.

Bit shifting to divide by 2 - Wikipedia

Non-canonical sections

    We just saw that a step size of 2 is twice as fast as 1. This hints at the funny relationship between the step size and the period. It's just period = max count / step, but this results in a very uneven response. When the step size is small, a slight change results in a very different period length. When the step is large, even significant changes have very little impact on the period length.

period = max count / step size

    Algebra 1 strikes again; This curve is a conic section called a hyperbola. It comes from us having a function in the form of 1/x. We can't really help that we're dividing a constant by a variable, but we can control the variable. If we replace x with 1/x (the reciprocal), we effectively multiply by x instead of dividing by it. 10/(1/x) = 10 * x. Now we have a straight line.

period = max count / (1 / step size)

    This looks better, but it doesn't feel right when mapped to a control. This is because the period is being adjusted with the same granularity on slow attacks vs fast attacks. That ends up being too coarse at one end, and too fine at the other. We need a curve after all.

The epitome of hyperbola

    The hyperbola we started with was too severe, and didn't pass through any particular points. What if we could fix those problems? Then a hyperbola might be a suitable curve.

    1 / (x + 1) / (10 - x) will approach points 0,10 and 10,0. That's helpful. We can change the severity of the curve by multiplying x by a coefficient in the numerator. Here's 1 / (0.25x + 1) / (10 - x)

period = max count / ((0.25 * step size + 1) / (10 - step size))

    By continuing to manipulate this formula, I landed on a new one that lets you adjust the maximum period without distorting the curve. I'm using 10 bits for the step size, so the number 1024 comes from the maximum step size. c sets the curve, and m sets the maximum period


    I'm using a sampling rate of 4kHz, and a maximum count of 2^20 = 1048576. This gives us the whole calculation to find the length of a period in seconds:

period length in seconds

    c, m and the constants are all known at compile time, so this can be simplified in our program prior to execution. A c of 4976 and an m of 3 simplify to roughly:

c = 4976, m = 3

    You can graph this function and adjust it in real time at this desmos link

Starting over

    There's another snag, restarting the envelope. It's tempting to begin counting from 0, but that's not how analog envelopes typically work. They start the attack stage from whatever their output level currently is. That would be simple, but we progress through our stages linearly, while the output is the result of our lookup table. So we have to convert from an output level, back to a position in the attack stage (that yields the same output). I chose to use a second lookup table to convert outputs back to attack stage positions. 

Release

    The C++ source code is on my github here. The focus is really the ADSR library, but the project is a functional example. It uses an Arduino nano, four potentiometers plus a trigger for input, and an MPC4728 for 12-bit analog output.

    Here's an incomplete prototype that I wrote in JavaScript. The gate is controlled in real time by mouse click. The red dots indicate the progress through each stage, and the black line is the actual output.




Thursday, January 23, 2025

Timex Sinclair 1000 New Case and Keyboard

    The Timex Sinclair 1000 (ZX81) is a charmingly limited computer from 1982. It has one of the worst keyboards of all time, and it begs for you to DIY something better. Many people have have already created improved keyboards and cases, but I wanted to try my hand. I figured there are enough chunky, old computers out there though; I want to make something thin and sleek.

Video

RF modulator

    The RF modulator is the tallest part of the board, and needed to go. It can be replaced with a simple transistor buffer to get composite video. Some ULAs require a more advanced mod to get usable video. One option is this Ginger Electronic board. It has the bonus feature of allowing you to invert the screen "colors".

Channel switch

    The channel switch on the bottom of the PCB also adds to the height, and needed to be removed.

Power

Heatsink

    The heatsink isn't thick, but it is quite large. It's meant to extend under the keyboard, but this would add to the thickness of our new keyboard. Out it went. The regulator can't run without it though, so that had to be replaced. I found these generic buck converters that are drop in replacements, and don't require heatsinks.

Keyboard

Redragon K603

    The original keyboard is as thin as it is unpleasant to use. To keep the new one thin, I looked for low-profile switches and especially short keycaps. I found the Redragon K603 keyboard to be a cheap source of both. The switches are even socketed, so they can be easily removed. The switches can be found separately though, and there are many options for keycaps.

Keycap stickers (image from 4keyboard.com)

    The next issue was the key legends. They're unique to this computer. Luckily I found these stickers from 4keyboard.com. They have all the information of the original keys, and they fit on a standard keycap.

Keyboard membrane connector

    It's easy enough to design a mounting plate and board for a custom keyboard, but it has to connect to the motherboard's strange connector. It's expecting the mylar sheets of the original button matrix. I could have bypassed or replaced the connector, but that felt like cheating. Instead, I designed a flat flex cable that fits into the original connector, and a standard flat flex connector on the new keyboard PCB.

New flex cable

Case

    I don't have any experience with 3D printing, and I'm not always impressed with the results I see. I stuck with 2D and designed a laser cut case. To keep things simple, I used all right angles, and dovetail joints. I settled for using super glue to join the majority of pieces (these nozzles helped immensely). The rest are held with screws and m2 nylon standoffs. Many of these are recessed into the bottom of the case as a way of achieving non-standard heights.
    A few standoffs go through the case lid also. This requires a screw with a head larger than the standoff to secure the lid. I found this pack that includes "large head" m2 screws.


M2 case screws

Jacks

    The power, video, ear & mic jacks need to be brought to the outside of the new case. One option is to butt the motherboard against the left side, and expose the original jacks. This offsets things strangely due to the width of the new keyboard. The expansion edge connector ends up in the middle of the case, and the mounting holes off to the left. So, I placed it at the far right, and used new panel mount connectors instead.


Motherboard placement

    I took the opportunity to remove the headphone-style connector that's misused for power, and replace it with a normal 2.1mm barrel jack. I also added the conspicuously missing power switch. Lastly, I included a small toggle switch that can be used to invert the video signal.

Lettering

    The case needed some lettering to differentiate the "Ear" jack from the "Mic" jack, and to add some character. These letters can be etched in, but you get very little contrast with acrylic. Paint is pretty much required. Luckily, laser etching usually gets you a free stencil because they etch through the protective film that comes on the acrylic. This makes it very easy to paint the parts, and then remove the film. I found that model spray paint works well for this. 

Lettering on the new case

Putting it all together

    After all the gluing, painting, soldering, and desoldering, I had a finished case.


    

    The design files are on my github.




Sunday, November 24, 2024

Synsonics Pro Dual Kit Drum schematic

    I'm always looking for drum synths to document, and I recently found a "Synsonics Pro Dual Kit" from Japan. Not to be confused with any other drum by a toy manufacturer, or guitar for that matter.

    It consists of two identical drum voices, so I've drawn one of them. Sadly, there's nothing too novel here. We have a decay-only envelope, a triangle VCO, and an OTA-based VCA.

Synsonics Pro Dual schematic

    The VCO design pops up a lot when you look for simple, DIY designs. Despite this, I realized that I had never taken the time to learn how it works. As always, it has an integrator and a schmitt trigger; How different could it be?

Basic Oscillator

    Let's start by looking at a non-voltage controlled version of the circuit. Rt sets the frequency of the oscillator by limiting the current flowing between the schmitt's output and the integrator's input.


Voltage Control

    We could maybe put a voltage controlled current sink in place of Rt to achieve voltage control. The trouble is that we don't only need to sink current; Half the time we need to source current instead. 

    Here's one solution to that, courtesy of the LM13700 datasheet. The righthand OTA is again just a schmitt trigger. The lefthand OTA is used as a voltage controlled current sink/source. You can specify the direction (sink/source) via its non-inverting input. The addition of the capacitor turns it into an integrator of sorts.


    Here's a closer look at this funny integrator. It has two controls: one for rate (amount of current), and one for the direction (inversion).

OTA integrator

    Really, the "rate" control is a normal integrator input, except it can't go negative. The "direction" input is what specifies that the input is negative (or positive).


The Synsonics Version

    We can see that the Synsonics version doesn't use an OTA, but it still has an integrator that can be inverted. How do they pull it off? The integrator works very much like a summing amp, and they're making it do arithmetic.

    The transistor is acting as a switch that can connect R17 to ground (0V). R15 and R16 form a voltage divider that puts half of our "Rate" voltage at the non-inverting input of the op-amp. We can substitute those in the simulator to simplify things. 

simplified circuit

The Math

    The voltage at the non-inverting input (non-inv) is subtracted from each of the inputs. Each difference drops across its respective resistor, and the resulting currents are summed.

We can represent a single input's current like this: (input - non-inv)/resistor

    In our circuit, the non-inv input will always be half the rate voltage, so we can put that in the formula. Now we can calculate the current for the regular rate input: (rate - rate/2)/100kΩ = (rate/2)/100kΩ

If we plug in 10V, we get this: (10V/2)/100kΩ = 5V/100kΩ = 50uA

Rate current


    Here's the second input when it's connected to ground: (0V - rate/2)/50kΩ = (-rate/2)/50kΩ

We plug in our 10V again to get: (-10V/2)/50kΩ = -5V/50kΩ = -100uA

0V current


    So, the second input's current is twice the magnitude of the first, but negative. Remember these currents get summed together, giving us this: current - 2*current = -current

Inverted current

That's how the integrator is inverted, by subtracting twice the normal current. The rest of the oscillator is standard, so we'll leave off with a Falstad simulator link.


Falstad simulation


Sunday, November 3, 2024

Ibanez EM5 and DL5 schematic comparison

     After redrawing the Ibanez DL5 schematic, I moved on to the EM5 from the same line. The EM5 has become sought after, and commands hundreds of dollars. The DL5 goes for tens.

    There is an official schematic available for the EM5, but I thought it would be interesting to rearrange it to match my DL5 layout. This highlights differences and similarities. So, here they both are for a-b-ing.

Redrawn EM5 schematic


DL5 schematic

Delay Line

    The biggest visible difference is the delay chip, M50195 vs M65831. The DL5's M50195 uses external RAM and an external comparator, but it functions the same way. Both chips are built around the same adaptive delta modulation converters and 1-bit delay line. The external RAM is larger, at 64kb vs 48kb.

    The input filters got moved around a little, but they're identical 5.8kHz lowpasses. The integrator caps in the modulator/demodulator are also the same. The M65831 datasheet specifies that it needs 30Ω series resistors for some reason.


Clock

    The clock is exactly the same, and validates what we found when investigating that part of the DL5.

    For some reason, the extra gates from the inverter weren't used for the bypass logic this time. Instead, they added transistors set up as inverters. These pedals are a bit unreliable when it comes to switching on/off, so maybe this was an improvement to the circuit.


Filters

    The input stage, preemphasis, and deemphasis stages are all basically the same. The frequencies that are boosted and attenuated haven't changed.

    We already compared in the delay line's input filters, and found that they're the same. We also have output filters though, and they're different. The DL5 cuts at 5.8kHz, matching its input filter. The EM5 cuts lower at 3.9kHz. Immediately after that, there's an additional lowpass formed by R16 and C19. Together these give the EM5 a darker sound, and probably help filter the additional noise that comes from using less memory for the same length of delay.

    The EM5's output filter also has soft-clipping diodes, presumably to prevent runaway echos that get louder and louder. There's more gain in the feedback loop (lower R17 value) to compensate for this lower output. Strangely, R20 has been more than doubled, limiting the maximum volume of the repeats.


In Closing

    The two pedals don't have any major differences that explain the 10x price gap, at least not to my satisfaction. I suspect people happen to like the darker sound, and maybe the clipping of the EM5. It should be trivial to make the DL5 sound the same though. Since it's built on the same tech, I believe a humble PT2399 delay could also sound the same.


Saturday, October 26, 2024

Ibanez DL5 schematic

    

     I've been looking at various delays, digital and analog. I came across the Ibanez DL5, from their "Soundtank" line. There is a reverse-engineered schematic, but it just didn't make sense to me.


Tic Toc, Ya Don't Start


    The thing that I was most interested in was the clock source for the delay chip. It vaguely looks like an oscillator, but not really. There are two inverting stages made from IC6. You need an odd number of inversions to make an astable oscillator though. Also, what is TR6 doing? It has two fixed voltages going into it, and a current flowing into C29?

Bad clock schematic

    I wanted to validate this, so I opened up my pedal, took some pictures, and started tracing.

Traced PCB

    It turns out that TR6 was labeled incorrectly, and it's actually TR3. R28 is actually R26. R24 and R25 don't go to different voltages; they both connect to what is probably +5V.

    The key mistake was the omission of R23. This connects TR6(TR3) to the second inverter, and totally changes what's going on.

    Here it is redrawn. TR5 helps to bias the CV going into TR4. TR4 acts as an exponential current sink, pulling current out of C29. The inverters monitor the capacitor voltage at pin 1, and use TR3 to recharge the capacitor when it gets low.

Corrected clock schematic

    I made a version of it in Falstad, so you can experiment with it. Link here

Falstad simulation


    It's actually very similar to the Monotron's VCO that we looked at previously

Monotron VCO

I Can't Be Your Lover


    The number of mistakes in this one area calls into question the whole schematic. What else can we find wrong?
Incorrect schematic

    The "Ref" pin (24) of the M50195 delay chip is incorrectly labeled "OP1+", making it look like it's an input for one of the internal opamps. It's acutally a reference voltage that's used by the opamps.

    The opamp on the delay's output (IC5B) is connected backwards. What should be its output is instead its input.

    Long story short, I decided to redraw the schematic. This let me make some more conventional layout choices too.

Redrawn schematic

    I skipped a few things that didn't interest me, like the LED, some power filtering, and jacks.

    Now we can see what all the blocks of the circuit are, and how the signal flows through them.


    At the top left we have an input buffer, and a preemphasis filter that feeds into the delay line. We also have a deemphasis filter that mixes the dry and wet signal together.

    Below that is a lowpass filter that cleans up the wet delay signal. After that is a FET that bypasses the effect. Then comes the repeat control that mixes the signal back into the delay line.

Input, filtering, and mixing

    At the bottom left is the delay line itself. It has a similar lowpass filter that's built using one of the M50195's internal opamps.

Delay line and RAM


    Bottom right is the latch that controls the FET. Pressing the tact switch toggles the state of the latch, and the FET, turning the effect on or off.

Bypass Latch

Bonus

    Here are the traced and redrawn boards. It's not a faithful recreation, but good enough to make a schematic from. The 70mil-spaced 36 pin DIP was "fun" to recreate.

Saturday, September 7, 2024

Koogo Analog Delay schematic (Donner Yellow Fall)

    I've been experimenting with controlling analog effects pedals, like this chorus. I bought a cheap "Koogo Analog Delay" to try to repeat my success. Turns out that it's not analog at all. It's the same digital PT2399 chip that seems to be in every delay. It does have a leg up by adding a sa571 compander though. Turns out this is the same design as the Donner Yellow Fall pedal; It's just been rebranded.

    I decided to draw a schematic in case there was anything to learn from it. I didn't add any capacitor values since they're not marked, and would have to be desoldered and measured. I did put in a few speculative values though.

    The PT2399 lives on a daughterboard and follows the datasheet design pretty closely. So closely that I just copied and pasted the last schematic I made from a PT2399 board.

PT2399 daughterboard

    
    The main board isn't too interesting either. It uses variations on the compressor and expander from the SA571 datasheet.

    
    An extra lowpass filter has been added before the expander. This is probably to help knock down some of the aliasing noise that would throw off the envelope detector of the SA571.

    A bypass FET has been put in the feedback loop so that you'll still get "tails" when turning off the circuit. This means that the delayed signal won't be immediately cut off; The last repeat will ring out, but no additional repeats will be created.

    The input buffer adds pre-emphasis, boosting the high frequencies. The summing amp is the complement to it, doing de-emphasis duty. These cancel each other out, and have no real effect on the dry signal. The benefit is to the signal-to-noise ratio of the delay line. By de-emphasizing (low passing) the output of the delay line, the high frequency noise can be greatly reduced.

Mainboard schematic


    I think these additions were inspired by actual analog pedals, despite the delay line being digital. You can see the pre/de-emphasis, lowpass filter, compander, and feedback bypass in the Boss DM-3

Boss DM-3