Smooth_1H2A9576

It’s 3 a.m., and you can’t sleep. You toss and turn as the neighbor’s cat serenades a mouse. The drip in your bathroom faucet has become a steady drumbeat louder than a bazooka. Punch-drunk from exhaustion, you arrive at work only to have your ears assaulted by a deafening crescendo of office chatter.

Howling cats, leaky faucets, and workmates with voices stuck on 11 are inescapable facts of life. But you just might cope, thanks to the Smooth Moods Machine, a compact and self-contained music generator built around an Arduino. Programming in the Arduino produces nine types of relaxing sound and music effects, including wind chimes, surf, and a floating cascade of musical chords that seem to carry you into the clouds.

I’ll show you how to build your own Smooth Moods Machine and easily change the music program by pressing a button on a TV remote control. You can listen with headphones, or connect a pair of amplified speakers to fill the room with calming sound.

Inside the Smooth Moods Machine

The basic Smooth Moods Machine is an Arduino Uno, MIDI Musical Instrument Shield from SparkFun, infrared remote control module, and a couple of wires. Construction takes about 15 minutes, and most of that is in soldering a set of 4 headers to the MIDI shield. An enhanced version uses a homemade interface board that allows you to connect the MIDI shield to most any external sound amplifier.

Let’s begin with a short description of what Smooth Moods is, and how it works. At its heart is a tiny MIDI synthesizer, in the form of a surface-mount IC. This chip is conveniently soldered to a shield, making it easy to connect the synthesizer to the Arduino. The Arduino serves as a MIDI controller, the part that literally controls the synthesizer, telling it what to do

You may already be familiar with the concept of MIDI, but here are the essentials: MIDI stands for Musical Instrument Digital Interface, a standard method of producing electronic music. Rather than recording actual sounds, MIDI songs are stored as data, akin to the paper rolls of a player piano. When reproducing music, a MIDI synthesizer mimics different instruments — a grand piano here, an electric guitar there.

Your PC or Mac has a MIDI synthesizer built into its operating system. Click on a .mid file, and the song plays through your computer’s sound circuits.

Smooth Moods works about the same way, except it uses just an Arduino Uno and the synthesizer shield. And instead of playing back a song contained in a file, Smooth Moods generates its own MIDI data on the fly. Instruments and notes are combined at random to give an ethereal quality to the music. Make that music* — with an asterisk. Smooth Moods doesn’t play tunes, though it does produce random sequences of chimes, chords, and other sounds that can be considered musical.

A Panoply of Sounds for Any Mood

The Smooth Moods Machine creates over a half-dozen music-like sound effects. You select the music style using a universal infrared remote control. See the table “Smooth Moods Music Modes” for a full list of the pre-programmed music and sounds.

  • The 4 chime programs produce random tubular bell effects by combining several chime instruments playing together, and often at different volume levels. The timbre — how the instrument sounds — varies for each chime program. On Hidden Grotto, for example, the tone is muted, giving a subterranean feel.
  • Three of the music programs, 5 through 7, use the MIDI surf effect to produce pink noise. The programs differ in how the volume of the noise fades in and out. For Pink Android, for example, the noise level never changes. For Ebb Tide the volume fades in and out completely, giving a surfy beach sound.
  • Floating With Clouds is the centerpiece of the Smooth Moods Machine. It generates a random selection of 3 major chords — C, F, and G (also referred to as I, IV, and V chords) — that swell up and down, and sometimes dissolve into one another. The chords are varied over 3 octaves. These 3 chords, and combinations of them, are common in music, giving a sense of melody even when the chords are strung together at random.
  • Dark and Foggy Night plays a random pick of 3-note chords at a very low register, without regard to musical key. The program uses the MIDI calliope instrument, which has a breathy noise background, giving the sound an eerie waterfront foghorn quality.

To change music programs just press a button on the remote control. Press button 0 (zero) to mute and unmute the sound.

All programs are automatically turned off after a 30-minute period. To play again just press a button on the remote. (You can modify the Smooth Moods sketch if you want to extend or shorten the auto-shutoff delay.)

Smooth Moods Music Modes

Sound-table

A Quick Peek at MIDI

You don’t have to understand the technical aspects of MIDI to make and use the Smooth Moods Machine, so feel free to skip this part if you’d like. Otherwise, here it is in a nutshell:

MIDI data is composed of messages, which are nothing more than a sequence of 2 or 3 bytes sent over an asynchronous serial interface. Messages specify such things as the instrument to play, the note (or notes), and their volume.

As with most MIDI synthesizers, the SparkFun MIDI Musical Instrument Shield supports the General MIDI 1 (GM1) set of melodic instruments. In documentation on MIDI, the instruments are numbered 1 to 128; when programming MIDI it’s more common to refer to instruments starting at 0. Instrument 0 in the GM1 set is an acoustic grand piano; instrument 1 is a bright acoustic piano, 2 is an electric grand piano, and so on. A second set, or bank, provides various non-melodic percussion instruments.

MIDI supports up to 16 instrument channels; you can program a different instrument on each channel. See the diagram for the basic concept. You can vary the volume for each channel, plus the volume of each separate note you play through a channel. The multi-channel feature allows you to “layer” sounds, one on top of the other. The technique is used in Smooth Moods, for example, to create unique chime effects, where multiple tubular bells strike at different volume levels. This helps the chimes sound more realistic.

FigureA

MIDI makes it fairly easy to play notes. Each note is simply a number, and corresponds to the white and black keys on a piano. To play a middle C, the synthesizer is sent a message that specifies the note has a pitch of 60. To play a concert A (the A above middle C), the message specifies a pitch of 69. Because notes are just whole integers, it’s possible to easily create all types of chords just by using simple math.

FigureB

For instance, a C major triad starting at middle C is the notes 60, 64, and 67. Keep in mind that in MIDI, the black keys are counted, too. So instead of thirds and fifths to specify the notes of a major chord, it’s root, root+4, and root+7.

For more details on how MIDI is programmed in Smooth Moods, check out the MIDI entry on Wikipedia, which contains extensive background on MIDI and the electronics used to create it.

Project Steps

Assemble the MIDI shield.

The MIDI shield comes already built, but lacks stackable header pins for connecting into the Arduino. Assembly is easy.

Begin by soldering two 8×1 and two 6×1 stackable headers pins to the SparkFun MIDI Musical Instrument Shield. These headers are included with the retail package version of the shield, but not with the development board version. Be sure to order the set of headers if your MIDI shield doesn’t come with them.

Carefully inspect your soldering work and look for solder bridges between pins. For an extra measure of safety, use a multimeter to test for shorts between adjacent pins. Dial the meter to read continuity — if your meter supports it, use the audible tone setting so you get audio feedback if there are any shorts between pins.

Connect the shield and Arduino.

Align the pins of the MIDI shield over the Arduino Uno, and carefully press the two together.

WARNING: When plugging the MIDI shield into your Arduino be absolutely, positively sure that the pins go into the right sockets — especially the power pins. It’s surprisingly easy to misalign shield pins. The Arduino is designed in such a way that shifting the power pins one position to the left or right will cause a short circuit, possibly damaging the Arduino or the shield.

Plug in the infrared module.

Gently bend the leads of the infrared receiver module at the middle to make a right angle. The ends of the leads should point in the opposite direction of the “dome” (lens) side of the module.

Plug the module into pins 8, 9, and 10 of the MIDI shield. The lens of the module should be over the shield, and should point up.

NOTE: For the time being, do not connect the runtime jumper (see Step 5).

Program the Arduino.

Download the SmoothMoods sketch here: SmoothMoodsSketch.zip. The zip file contains a folder for the SmoothMoods.ino sketch itself, plus 3 add-on libraries. You need to move the 3 libraries into your Arduino libraries folder. On a Windows machine, this is located under (My) Documents/Arduino/libraries. If you don’t have a libraries folder already you’ll need to create one.

Start the Arduino 1.0 or later IDE software; the sketch will not work in pre-release versions of the software. If the IDE was already running when you moved the library files, you’ll need to exit the software and restart.

Connect a USB cable between the Arduino and your computer, open the SmoothMoods sketch into the Arduino IDE, and upload it to your Arduino board.

Test it out.

Connect a short (3″) jumper wire between digital pins 1 and 3 on the MIDI shield. This is the runtime jumper, which loops the Arduino’s hardware serial port into the MIDI In connection of the shield.

Plug a pair of stereo headphones into the MIDI shield. Can’t hear any sound? Some things to check:

  • Be sure you’ve added the runtime jumper wire between pins 1 and 3 along the top of the shield.
  • Ensure the headphones are pushed all the way into the jack on the MIDI shield. If your headphones have an integrated volume control, dial it to maximum.
  • Reset the Arduino. The MIDI shield provides a reset button, located to the left of the power pins.
  • Re-upload the sketch, and try again.

If you still don’t hear any sound, the SmoothMoods sketch supports a debugging mode that lets you view troubleshooting debug messages provided at different points in the sketch. See “Modifying and Debugging the SmoothMoods Sketch” in Step 11 for additional details.

Set up your remote.

The Smooth Moods Machine has 9 music programs, which you change using a standard universal TV remote control. The remote must support Sony TV codes. Most do; refer to the programming guide sheet that comes with your remote for the proper Sony TV code to use.

You may need to try several of the codes to find the one that matches the programming in the SmoothMoods sketch. You’ll know it’s working when you press a number button — 1 through 9 — and hear a 2-note strum. This strum signals you’ve successfully changed programs. See the “Smooth Moods Music Modes” table for a list of the programs and the sounds they produce.

When first programmed, the sketch defaults to program 8, Floating with Clouds. The sketch stores the current mode in the Arduino’s EEPROM memory (by using EEPROM the data is not lost when the Arduino is de-powered). The next time you apply juice to your Arduino the last-used mode is recalled again.

Connect an external amplifier (optional).

The MIDI Musical Instrument Shield provides a convenient 1/8″ jack for hooking up stereo headphones. It also provides a 3-pin tap, labeled Speaker, for attaching to an external amplifier and speaker. Good enough. But while you can directly connect some types of battery-operated amps to the shield (particularly those that don’t share a ground connection with the Arduino), you may want to instead use the interface circuit shown in Step 8.

Why bother? The main reason is that the negative (–) pin on the Speaker tap isn’t actually a ground connection, but rather a buffered ground that produces a constant 1.23V reference voltage. This line is intended as the common for stereo headphones.

The resistors and capacitors on the interface board act as a buffer between the MIDI shield and external amplifier. It blocks DC voltage from reaching the amp, and also filters out objectionable digital noise that may be present in the sound output.

Build the amp interface board (optional).

You can construct the interface board on a small (170 pin) solderless breadboard, or solder it together on PCB board. Cut the PCB so that there are at least 12 columns. You only need the top or bottom half of the board. Cut so that you leave a hole in one corner for mounting, plus a long strip at the bottom for a common ground connection.

Insert the resistors and capacitors as shown in the illustration, and in turn solder them to the underside of the board. Clip off the excess leads when you’re done. Be sure to observe the polarity marking on the 10μF electrolytic capacitor. The other components are not polarized, and they can be inserted in any orientation.

Following the wiring diagram, solder 3 short (4″ or so) wires from the interface board to a 1/8″ stereo jack.

IMPORTANT: The wiring connections on your jack may differ from those shown here. Refer to the diagram that comes with the jack you use. It doesn’t much matter if you swap the L(eft) and R(ight) connections.

Clip off about 5″ from a 12″ length of 3-wire servo extension. Be sure to leave the 3-pin female connector on the end. Attach the bare ends of the 3 wires to the interface board.

Double-check your wiring before continuing. Be sure there are no short circuits, loose components, or bad soldering joints.

Attach the amp interface board (optional).

Unplug the headphones from the MIDI shield. Solder a set of 3 right-angle male header pins to the MIDI shield. The top of the pins should point away from the shield.

NOTE: You will not make a connection to the Speaker – pin. I soldered a pin to this point anyway, because it’s easier to attach 3 pins than just 2. If you’d like you can clip off the top of the pin after soldering.

Plug the 3-pin header from the interface board to the Speaker L and R pins, as shown in the illustration. Connect a short 3″ jumper between one of the Arduino’s Gnd (ground) pins to the ground wire from the amplifier interface board.

Finally, attach a battery- or AC-powered amplified speaker to the 1/8″ jack wired to the board. Power up the Arduino, and turn the amplifier on. Adjust the volume control on the amplifier.

Put it all in an enclosure (optional).

A completed Smooth Moods Machine is shown in Figure H, with the parts mounted to a base made of 1/8″ plastic. I drilled holes to match those in the Arduino and shield (only 2 can be used; upper-left and lower-right), plus another hole to mount the amplifier interface board. I added small rubber bumpers underneath the base to protect the furniture.

You can get as simple or fancy as you’d like with an enclosure for your Smooth Moods. For mine, I put a digitally printed cover over the guts, drilling holes for 2 standoffs mounted over the shield, and another hole for attaching the 1/8″ stereo jack. I also drilled a series of holes right over pins 8, 9, and 10, for plugging in the IR remote module.

The cover approach leaves access to the Arduino USB port and power plug, plus the headphone jack on the MIDI shield. You can add panels to the other 3 sides if you’d like.

Plug it in.

The Smooth Moods Machine can be powered through the Arduino’s USB cable during programming and initial testing, but for regular use you’ll want to juice it with a DC wall adapter as specified on the Materials list.

For my Smooth Moods, I plugged the DC wall adapter and amplified speakers into a 3-receptacle power strip.

Modifying and Debugging the SmoothMoods Sketch
The SmoothMoods sketch contains numerous settings you can change, including the instruments used for the sounds, the range of octaves for each setting, and the auto-shutoff time delay. These settings are contained in a constants.h file that accompanies the sketch.

In case of trouble, the SmoothMoods sketch incorporates a debugging mode where you can verify that the program is active and working properly. Refer to the readme file that accompanies the SmoothMoods sketch for additional details.

With your Smooth Moods Machine built and programmed, you can now kick back and let your neighbor’s cat bother someone else!

Setting the Runtime Jumper
The runtime jumper, connected between pins 1 and 3, sets the communications mode between the Arduino and the MIDI shield. Remove the jumper when uploading sketches to your Arduino. Replace the jumper for normal sound playback.

When experimenting with and debugging the SmoothMoods sketch, remove the jumper for both uploading and sound playback. You will also need to change the DEBUG line in the sketch when working in Debug mode. See the readme file that accompanies the SmoothMoods sketch for additional information.

What’s Old Is New Again.

Great ideas never die — they just get rewritten. Inspiration for the Smooth Moods Machine comes from a magazine project called the Chord Egg, published in the mid-1970s by noted electronic music guru John Simonton. For a time, John’s company, PAiA Electronics, manufactured Chord Egg kits. (Though John passed on a few years ago, PAiA is still in business today, and continues to sell some of the products they’re best known for, including the fantastic Theremax all-analog theremin. Be sure to check them out!)

Inside the Chord Egg was a fairly involved all-analog circuit that produced random C, F, and G major chords, piping them through a top octave synthesizer integrated circuit to produce the sounds. The chords swept back and forth at random, while the volume faded in and out. Additional cross-channel phasing affects were added to give the chords an otherworldly sound. John coined the phrase Encephalo-Gratification Generator (EGG for short), and suggested the use of stereo headphones for full effect.

Top octave synthesizers are hard to find these days, but no matter — they’re not needed for this type of project, thanks to microcontrollers and readymade digital synthesizer boards. Smooth Moods mimics aspects of John’s Chord Egg, but in an all-digital way. It also adds other music effects, simply because it can — the MIDI instrument repertoire provides a large orchestra for experimenting.