Using the Arduino for electronic music #2

How much of what we want can the Arduino do?

12493708_10207025983942846_6998465687837053403_o

This article is a continuation of Using the Arduino for electronic music #1, and picks up where that post left off.

By now we have discussed:

It’s now time to dig into the abilities of the Arduino, particularly it’s ability to handle analog to digital conversion, and digital to analog conversion.  Having done that, we can compare expected performance to the specifications listed in: The digital representation of modular synthesizer signals, and we can determine what an Arduino (and by extension the Euroduino eurorack synth module) is good for in electronic music.

What features are exposed by the Arduino, and the Euroduino?  Are there additional features in the native hardware of the  ATmega328P chip that are not supported in Arduino software or hardware?  Take a look at the following sections.

  • Analog input
  • Analog output
  • Digital input
  • Digital output

Analog input

Arduino

6 channels of 10 bit ADC

ArdGen_UNO

Your program can access the Arduino’s ADC converters using the Arduino AnalogRead command. The documentation for this command tells us that it takes about 100 microseconds (0.0001 s) to read one port.  this means that no more than 10,000 conversions can be made per second.  If all 6 ports are in use, then the sample rate for all channels can’t exceed 1,666.

The analog inputs on the Arduino connect to actual analog to digital converters (ADC) which convert analog signals to 10 bit binary numbers. 10 bits represents 1024 steps, a range of 0 to 1023.

Euroduino

4 channels of 10 bit ADCDSC07926

The Euroduino has the same capabilities as the Arduino Pro Mini that it is based on, but only 4 of the 6 ADC channels are exposed on the front panel by the 2 knobs and the 2 control voltage inputs.  If all 4 of these are in use, that means the Euroduino can sample all 4 at 10 bits 2,500 times a second.

To put this in perspective, a digital audio signal from a CD player samples a 16 bit value at 44K, or 44000 times a second.

It is clear that the Euroduino is not going to be processing audio, at least what we usually think of as audio – and that’s OK.  Knobs don’t need that kind of speed (how fast can you turn a knob?), and a control voltage signal (which is what these ports are designed to read) requires much less resolution.

ATmega328P

6 channels of 10 bit ADC

ATMEGA328P-AU TQFP32 AtmelThe microprocessor in its native state (without the overhead of the Arduino library) still has 6 ADCs. These produce 10 bit sample as often as 15,000 times a second according to the data sheet.   If all channels are being run, that would mean you can expect to get  2500 samples a second across all inputs.

The Arduino has an external clock, which overclocks the chip at 16 mhz.  When used without external clock support, the device runs at half that speed, 8 mhz.

Analog input: Will it perform?

It makes sense that the best performance will be seen from the bare chip, which runs without the overhead of the simplified Arduino environment.  Still, without going outside of the stock configuration, there is not much difference between the two.

For quality audio? No.

It is clear that the Arduino and Euroduino are not going to be processing audio, at least at the sort of rates we usually associate with audio – and that’s OK.  The Euroduino does not claim to process audio (all the analog ports are marked for control voltage), and the knobs do not need a very fast sample rate at all to perform well.

For quality LFO signals? Mixed.

Control voltage signals require much less resolution, although that resolution varies according to the signal type (The digital representation of modular synthesizer signals).  All types will perform well at the stock sample rates, both within the Arduino environment, and outside of it.  Where they vary is bit depth.  A LFO signal is still an audio level signal, and as such needs between 12 and 16 bits of depth.  Since the Arduino (etc.) only provide 10 bits ADC at best, the quality is still going to be sub-par.

For envelope and volume modulation CV?  Excellent.

For the DC, 0 to 5 volt signals that are used for volume and filter envelopes, 10 bits should be more than enough, and available sample rates sample rate is well below the available limit.

For 1 volt per octave control signals? Almost good enough.

Although the stock sample rate is probably in the range of just fine to great, the bit depth is barely adequate.  Ideally the bit depth would be at least 12 bits in order to represent tuning and smooth glissandos in higher registers, and we only have 10 at most.  1024 steps will not produce a smooth transition between values when mapped to high frequencies there will be some stair stepping (zipper noise) – just the sort of noticeable, annoying artifact that has always given digital electronic music a bad name.  It’s a pity, because that is the sort of thing I would really like to be able to process.

Conclusion

Decidedly a mixed bag. Although there are some digital modules out there that perform as well (or poorly) as the Arduino does, do we really want to be like them?  It is probably best if you limit your ideas to implementations that process:

  • Monopolar control voltages like envelopes and monopolar LFOs.
  • Pitch control voltages that use specific values (perhaps a sequencer that generates specific intervals with no vibrato, portemento or smooth slides between notes.)  There is no reason that you can’t support interesting tunings, as long as you accept that you won’t be exactly on target.  Analog VCOs are notorious for their inability to accuratly deliver precise tunings, so that may not be such a bad thing.

Work arounds

Open Music Labs has a great add on board for the Arduino the Audio Codec Shield which adds high quality CD level ADCs and DACs to your Arduino.

Advanced users might implement 12c or SPI ADCs that would support higher quality outputs if they are designing their own circuit and not using the Euroduino.

audiocodec_blue-1

Open Music Labs (which makes a great, high quality Arduino shield that supports high quality ADC and DAC) has a great article on using the ATmega line for audio, with some really interesting and borderline scary experiments pushing the chip to its limits  ATmega ADC.  Some of these give adequate performance with a sample rate as high as 32,000 samples per second. Still with 10 bit (or a little less) bit depth.

 

 

This works with stock Arduinos, as well as higher performance boards like the Leaflabs Maple (sadly discontinued).

This shield supports a sampling rate of up to 88K at a bit depth of 24  bits.  Since the Arduino can’t handle this range, the fact that it runs as slowly as 2K is much more interesting for our purposes.  There are two channels in and out of the board, and the board is DC coupled, which means it can output slowly changing DC control voltages without issue.  This board, an Arduino Uno, and your own signal conditioning circuitry would make a really good synth module.  It will not (of course) work with the Euroduino.

Open Music Labs has a great article on pushing chips in the ATmega family (like the arduino) to their limits for audio:  ATmega ADC.

Analog Output

Arduino

6 channels of 8 bit PWM

ArdGen_UNOOutput is where the rubber meets the road.  Sadly, this is the Arduino’s Achilles heel.  Where the ATmega 328 has actual ADCs, even if they are limited to 10 bits, the chip uses re-purposed digital outputs for PWM “analog” output.  This produces at best, a dirty 8 bit output when using the Arduino library.  It turns out that two of the PWM outs Your program writes to the Arduino’s PWM outputs using the Arduino analogWrite command.

The Arduino site has a quick introduction to PWM if you need to catch up.

The Arduino library also has an analogWriteResolution command, but it is intended to support the Arduino Due and the Zero.  These boards support up to 12 bit PWM, and 1 to 2 channel DACs.  Sadly, this does not affect the chips we are working with.

Sample rate:

The default sample rate of arduino PWM output is 488 times a second, which falls into the “barely adequate” range. There are hacks to improve this – See the ATmega 328 section for options outside of the Arduino library.

16 bits?

Timer 1 supports 16 bit PWM, which has more than enough accuracy – but it is not supported by the Arduino libraries.  See the ATmega 328 section for other details.

Euroduino

2 channels of 8 bit PWM

DSC07927The Euroduino has all the flaws from the Arduino section listed above, and in addition does not expose the PWM channels that can be associated with 16 bit PWM (as far as I can see, anyway).  Too bad about that.  Might just be worth modifying the device.

Here’s my reasoning to make that claim – love to hear that I was wrong:

Looking at the schematics for the Euroduino, the Arduino Pro Mini, and the datasheet for the ATmega328, this is what I see.

Front Panel CVOUT <-pin 4 SV1 <-SV3 pin <- conditioning circuit <- PWM1/1.2C <- PWM1/2.2A <- D5 <- ATmega328 pin 9 <-PD5(T1) <- PD5 (PCINT21/OC0B/T1) Timer/counter0, Output compare match B.

Front Panel CVOUT <- pin 6 SV1 <- Sv3 pin  <- conditioning circuit <- PWM2/1.2C <- PWM2/2.2B <-D6
<- ATmega328 pin 10 <-PD6(AIN0) <- PD6 (PCINT22/OC0A/IN0)Timer/counter0, Output compare match A.

ATmega328P

4 channels of 8 bit PWM on timers 0 and 2

2 channels of 16 bit PWM on timer 1

ATMEGA328P-AU TQFP32 AtmelFor Analog output, the ATmega328 chip has many features that are not supported in the Arduino library.

Sample rate:

The default sample rate for arduino PWM output is 488 times a second, which falls into the “barely adequate” range.  This rate can be increased using various hacks, but the Arduino library does not support it. There are consequences such as breaking the millis() and delay() functions.  There are many web pages that talk about these differences and consequences from many different points of view.

The article Secrets of Arduino PWM goes into greater depth describing ways that the Arduino can provide better sampling rates.

Bit depth

Timer 1 supports 16 bit PWM, which provides more than enough accuracy – but it is not supported by the Arduino libraries.  It can be used via some 16 bit hardware registers, and it will not break other functions unless you also change the clock rate.  It’s a worth checking out if you feel up to it.  It’s not all that much harder to use than analogWrite .

In any case, it is not stock Arduino, so it won’t be gone into here.

If you are limited to timers 0 or 2, which are 8 bit, (like with the Euroduino), there are still ways to improve the bit depth of your output signal.  More than a little hacky, but if it works…

To get more bit depth, you could up the sample rate x4, and do dithering to add a rough 4 bits to the output value.  The smoothing caps would probably still work since the output rate would not have changed.  There is an interrupt associated with filling the PWM counter which would allow dithering to be added fairly accurately.  The rate can be changed by changing the prescalar value.  By default that value, which is used to divide the clock frequency, is 64.  If there was an option for 16 that would be ideal, but the next step down from 64 is 8.  If that can be made to work, it would actually provide much better performance, and more than a little improvement on noise.

The Arduino has an external clock, which overclocks the chip at 16 mhz.  When used without external clock support, the device runs at half that speed, 8 mhz, which should impact some of the assumptions here.

Analog output: Will it perform?

It makes sense that the best performance will be seen from the bare chip, which runs without the overhead of the simplified Arduino environment.  Still, without going outside of the stock configuration, there is not much difference between the two.

For quality audio? Absolutely no way.

It is clear that the Arduino and Euroduino are not going to be producing audio, at least at the sort of rates we usually associate with it – and that’s OK.  The Euroduino does not claim to generate audio (the analog out ports are marked for control voltage, just as the CV in ports are.).

For quality LFO signals? No

Control voltage signals require much less resolution than audio, although that resolution varies according to the signal type (The digital representation of modular synthesizer signals).  All types will perform well at the stock sample rates, both within the Arduino environment, and outside of it.  Where they vary is bit depth.  A LFO signal is still an audio level signal, and as such needs between 12 and 16 bits of depth.  Since the stock Arduino provides 8 bit output at best, the quality is going to be sub-par.

For envelope and volume modulation CV?  Not really.

For the monopolar, 0 to 5 volt signals that are used for volume and filter envelopes, 8 bits is just not enough.

For 1 volt per octave control signals? No.

Although the stock sample rate is probably in the range of ok, the bit depth is completely inadiquate.  Ideally the bit depth would be at least 12 bits in order to represent tuning and smooth glissandos in higher registers, and we only have 8 at most.  256 steps will not produce accurate tuning in most any octave, and will not provide a smooth transition between values.  There will be significant stair stepping (zipper noise) – just the sort of noticeable, annoying artifact that has always given digital electronic music a bad name.  It’s a pity, because that is the sort of thing I would really like to be able to produce.

Conclusion

8 bits of PWM, at 488 samples per second is not good enough.  Having said that, however the chip itself has features that can help, although using them leaves the comfortable Arduino environment behind, and that is the reason the board was selected in the first place.

Work arounds

Open Music Labs has a great add on board for the Arduino the Audio Codec Shield which adds high quality CD level ADCs and DACs to your Arduino.  There are Arduino libraries available to support this board.

audiocodec_blue-1

This works with stock Arduinos such as the Uno, as well as higher performance boards like the Leaflabs Maple (sadly discontinued).

This shield supports a sampling rate of up to 88K at a bit depth of 24  bits.  Since the Arduino can’t process samples this fast, the fact that it runs as slowly as 2K is much more interesting for our purposes.  There are two channels in and out of the board, and the board is DC coupled, which means it can output slowly changing DC control voltages without issue.  This board, an Arduino Uno, and your own signal conditioning circuitry would make a really good synth module.  It will not  not fit the Euroduino since that module is based on a much smaller device.

It is possible to dither the output to the PWM – see some of the comments above.

Digital input and output

Bit depth is 1, sample rate is well over 1K

In MIDI sequencers a temporal resolution of over 2oo times a second is considered good.  That criteria probably applies here as well.  The Arduino, Euroduino, and the raw chip are all capable of much higher rates than this.

Will it perform for the following signals?  Oh, yeah.

  • Trigger signals
  • Gate signals
  • Clock signals

 

Using the Arduino for electronic music #1

Introducing the Arduino and related devices

DSC07927

Disclaimer: I’m not an engineer, and some of this material approaches the speculative.  Hope it is as useful to you as it is to me, and please feel free to comment on any errors or omissions you find.

Over the last few months I’ve been studying the teensy32_front_small
features of different microprocessor families to determine which are most suited to electronic music and synthesis.  The big winner so far is the $19.00 Teensy 3.2: But that’s not what this post is about.

The Teensy 3.2 (It is, in fact pretty teensy)

This post is going to be about the Arduino, and that is for a couple of good reasons.  The Arduino is built from the ground up as an educational environment for learning about microcontrollers, and it has become, by orders of magnitude the most popular microcontroller platform for hobbyists and amateur engineers. This popularity means that it is often the first thing that comes to mind when creating a new project.

ArdGen_UNOThere is a lot to like about the Arduino:

  • The boards are inexpensive
  • Very well documented, both for hardware and for software
  • Supported by a huge community of other users
  • Supported by a huge number of libraries to do just about anything (within reason)
  • The tools needed to write and program the device are simple and free

And there are some weaknesses to the platform:

  • The device runs slowly (typically 16 MHz)
  • The device is 8 bit, which means that it is limited in the numerical processing it can do
  • The device could have more memory to store programs and data
  • The tools are best for simple, small programs and can fail when doing something complex
  • It is difficult to get high quality analog signals out of the device

In fact, the device is so popular that there is even a EuroRack synthesizer module that is built just so that you can add the device to your synthesizer rack.

The EuroDuino

DSC07926

This is Circuit Abbey’s Euroduino module.  The main board comes as an easy to assemble kit, and is supported by an extra module that will let you program the device while it is bolted into your rack.  It features:

  • Two generic knobs that can be used to change program settings
  • Two 5 volt digital inputs and outputs
  • Two 5 volt analog inputs and outputs
  • Two general  purpose switches

The processor inside the module is the Arduino Pro Mini, made by SparkFun.

This board has all the functionality of the normal Arduino Uno in a very small package which fits in the narrow Euroduino module pictured above.

The Euroduino exposes a subset of the functions that the device is capable of:

  • The 2 knobs and the two analog inputs connect to 4 of the 6 analog input pins
  • The 2 switches, the two digital ins and the two digital outs connect to 6 of the 14 digital I/O pins
  • The 2 analog outs connect to 2 more of the digital I/O pins which are set to Pulse Width Modulation (PWM) analog output.

To evaluate the practicality of the Arduino ecosystem (both hardware and software), we need to do some background research into the various signals used in Eurorack, and how much resolution they require, both in bit depth and in sample rate.  Some limitations are obvious, and some require further research, particularly when using control voltage to represent loudness and 1 volt per octave pitch.

This is discussed in these two pages:

Once you understand those things, you can evaluate how close the Arduino comes to meeting the needs of modular synthesis, which is discussed in the second part of this post:

Using the Arduino for modular synthesis #2

DSC07930.JPG

Just a reminder what it’s all about