Month: March 2014

Preamps 101

Here’s some info about preamps. Both for electric guitars/bass guitars, and for microphones.

Preamps (or pre-amplifiers) are necessary if you want to do some signal processing on your live music. Microphones and guitars simply don’t product loud enough signals. We can’t work on them as-is. We need to amplify them just enough to allow analogue-to-digital conversion. Most ADCs work on signals in the range of +/- 3V or +/- 5V, while microphones and guitars produce signals in the range of millivolts.

Why preamps and not amps? Because amps are much heavier. They’re designed for driving speakers, producing much louder signals. We don’t need that. Proper amplifiers have a much higher power consumption, much bigger footprints, and require different electrical components (e.g. chips with heat sinks).

I compiled a list of preamps I like. I didn’t invent any of them, but I did build them all so I got to test them myself. I also managed to insert some minor modifications to these preamps which seem to better suit my needs. I even designed PCBs for some of them. Check out my OshPark shared projects for that.

Guitar/Bass preamps:

Tonemender by RunOffGroove. I built this for my bass and it sounds really good. The three tone knobs work well for bass/mid/treble, and the gain is fine.

Why I like it:

  • Fine gain, clean boost
  • Bass/mid/treble controls
  • Pretty clean, no distortion

My modifications:

  • Replaced the power circuit with +VCC and -VCC. Scrapped the voltage-divider resistors.
  • Replaced the suggested op-amp with OP284/OPA1642 to enable power supply of up to +/-18V.
  • Changed the resistors/capacitors in the tone stack to fit a FET op-amp and suit bass guitars as well.

Note: if you build this with all the pots and stuff, you’d better build a good ground shield, or it’s going to be noisy. Avoid using long wires and PCBs with stripes, or print a PCB with ground planes.

My suggested schematics are here.

Simpler guitar/bass preamps can be found in the following links:

Microphone Preamps:

  • TI’s INA217Works perfectly with dynamic microphones. High-end quality.
  • A simpler one. This one, with minor modifications, can be built with my PCB designed for a guitar/bass preamp (schematics are here). Just change the caps/resistor values.

DIY Sound Effects – Intro

So, you want to do some sound effects? This means sampling a live audio signal (preferably stereo), applying some filters on the sampled audio, and converting it back to an analog signal.

What do we need for this?

  1. An audio codec. This means an ADC and a DAC, working at 48KHz or more, with 16 to 24 bits per sample. Stereo codecs are preferable.
  2. A processor. Preferably a 32-bit processor (forget about 8-bit or 16-bit if you want to process 24-bit audio samples) with floating-point capabilities (for most reasonable filters).
  3. Amplifiers. Whether it’s a guitar or a microphone, we need a decent preamp to convert its miserable millivolts signal into a proper signal in the range of +/- 5V.

My first device for this task was a Chumby Hacker Board (more info here) which is based on Freescale’s iMX233. This chip is pretty good for audio because it has a built-in stereo audio codec that can work at 44.1KHz with 24 bits per sample. It also has an ARM processor that clocks at 454MHz, which gives you plenty of computational power. And who needs DSP anyway?

A similar device is the OLinuXino from Olimex (more info here) which resembles the Chumby Hacker Board but has a slightly different layout (some say better).

Both devices are perfectly fine with Linux, which means we can re-write a driver for this task and use it. The audio output of the device can go to your home receiver or audio amplifier. The only thing we’re missing is a preamp to get the signal in.

I found a good DIY microphone preamp here. For guitar/bass preamps I used modified versions of either a simple preamp or a preamp with tone controls, such as runoffgroove’s tonemender.

The power supply is a bit tricky: we need +5V for the Chumby (or later – for an Arduino or a Raspberry Pi) and we need +/- 9V or +/- 12V for the pre-amps. Conveniently enough, the power supply of an average PC has these outputs, plus a proper ground connection to prevent electromagnetic interference.

So there you go – that’s the basics. Future posts will show pictures, schematics, and code samples as well.

My Very First (Crappy) Synthesizer

About a year and a half ago, I started thinking about synths. I bought an Arduino Uno and started designing my very first crappy synthesizer.

A demo of the result can be found here.
Code and schematics can be found here.

Why crappy? Because Arduino Uno is based on a simple AVR chip which doesn’t have a proper DAC. All it has is 8-bit PWM (which could be 9-bit PWM if you ask it nicely) and a 16MHz CPU that can drive it.

Let’s do the math: if we want to produce sounds with reasonable quality we need a sample rate of at least 32KHz (because an average human ear can pick sounds in the range of 25Hz-16KHz and we need at least twice the frequency for producing the sound). If we limit ourselves to one channel (mono), one note at a time, then we need to set up a timer for 32KHz and an interrupt handler to handle it. Since our CPU clocks at 16MHz, we have less than 16,000,000 / 32,000 clock cycles to produce our sample. Since our CPU is 8-bit, this doesn’t leave much room for computations.

So what do we do to make the best out of this hardware? Here’s some rules of thumb:

  1. Avoid heavy computations. For example, if you need a sine wave, don’t compute the sine function on the fly. You can either choose a decent (and fast!) approximation like a 3rd-order polynomial or use pre-built tables.
  2. Keep the interrupt handler as short as possible. If you’re using an Arduino, don’t call analogRead of digitalRead inside an interrupt handler. Do it outside, save the result in a global volatile variable, and read it in the handler.
  3. Use -O3 instead of -Os as a compiler flag. O3 optimises for speed while Os (Arduino’s default optimisation flag) optimises for code size.
  4. Optimise your code, even at C-level. This paper contains some tips and tricks.

I pretty much used all of these tips and tricks and squeezed in as much features as I could. I tried to add some more features but couldn’t keep up with the fast interrupt rate…

Here’s what I managed to squeeze in:

  1. One oscillator (sine/square/saw-tooth/triangular waves)
  2. A first-degree low-pass filter with resonance
  3. Vibrato (with constant rate and variable amplitude)
  4. Simple distortion (created by bitwise AND with a constant pattern)
  5. Contorlled fade-in and fade-out for the amplitude/low-pass envelope