Successfully reading a Midi file

In my previous post I covered the Arduino-based circuit I built to strike the chimes. I’ve also, with a little effort, gotten the Sparkfun MicroSD shield to work with the Arduino Mega 2560, so I’m now in the thick of writing the software to read music files for the Robotic Glockenspiel.

Not happy with the Arduino Midi libraries I’ve seen (they seem not very file-oriented), I’ve written the bones of what will become a Midi file-reading library. This library will enable an Arduino Sketch to read events from a Midi file, one by one, so that it can play the notes with the proper timing.

The trick of reading the Midi file was the “running status”, which omits midi channel bytes when they’re repeated.

Here are some useful pages for understanding Midi file format:

My next post points to the code I wrote to read Midi files.

DIY Glockenspiel: the circuit

Earlier, I described how to build a frame for the glockenspiel. In this post, I cover the circuit that will strike each chime.

I’ve successfully tested the glockenspiel control circuit. It’s an Arduino Mega 2560, a Sparkfun Wifi Shield,  and 19 repetitions of a simple solenoid control circuit.

Glockenspiel control circuit
Glockenspiel control circuit

(by the way, the solenoids in the photo are there only for testing. In the finished Glockenspiel, there will be long wires connecting the circuit to the glockenspiel-mounted solenoids.)

I started from a drum control circuit described in Make Magazine.

Tactile Metronome Solenoid Circuit from Make Magazine
Tactile Metronome Solenoid Circuit from Make Magazine

Then I chose a few specifics: at TIP120 transistor, a 1000uF capacitor, a 1N4004 diode, and a 5V solenoid from Sparkfun. …and did 19 iterations of the circuit; one per chime in the glockenspiel.

I’ve done a quick Fritzing circuit diagram, which I plan to upload to GitHub.

My main concern was whether the solenoids could be powered only by the VIn pin on the Arduino when powered via a 9V 650mA wall power supply. Turns out the answer seems to be “Yes”: the 1000uF capacitor prevents the solenoid from drawing down the main power enough to reset the Arduino, and the power consumption of the solenoids (see below) is easily supported by the power supply.

To avoid burning out the solenoids each one is powered only a few milliseconds at a time.  This causes the solenoid to hit the chime with just enough power to ring nicely, but not too loudly. The math’s pretty straightforward: the data sheet provides the maximum average wattage that the solenoid can dissipate and the minimum resistance of the solenoid. The power supply puts out about 9V. Working out the numbers, it looks like for quickly-paced music, a 6ms-per-strike is about the maximum the solenoid should support (conservatively). Playing most music, this shouldn’t be anywhere near the maximum power the solenoid can support.

You can see a video of the test of this circuit, showing that it can powerup, power down, and run through each of the solenoids.

Next, I write the code to read the music.

My DIY Glockenspiel works!

In my previous post, I crashed and burned with a glockenspiel frame that slipped while gluing.  This post covers the version two, working frame.

Ok, it’s not robotic yet, but I now have a manually-playable, octave-and-a-fourth glockenspiel made from 1/2″ EMT conduit.

If you want to know about cutting the chimes, see my earlier post on measuring and tuning the chimes.

This is my second try at making the frame for the chimes. You can read about the Epic Fail first attempt in my earlier post. The trick was figuring out how to make a wooden frame precisely-aligned enough that the chimes fit.

Here’s my tale of this second, successful attempt, phrased as “How to build a glockenspiel frame”

Suppose your glockenspiel will have 19 chimes (like mine) spaced 1″ apart, with a 1″ space at the top and bottom, then a 1.5″ piece of wood at the top and bottom, giving a total height of 24″.

Start by screwing together a  24″ square frame from overlapping pieces, where the sides are just for lining up marks; the top and bottom pieces will remain. Add a small triangle at each corner, to keep the frame square. Add a center piece of wood whose side (not center) marks the center of the frame. Later you will use this piece to line up the center marks of all the chimes.

The starting point of a glockenspiel frame
The starting point of a glockenspiel frame

Next, mark evenly-spaced 1″ marks that – when transferred to the angled parts of the frame – will show where each pipe goes. (I later found I should have marked 1/2″ spaces as well.)

marking evenly-spaced 1" marks on the frame sides
marking evenly-spaced 1″ marks on the frame sides

Next you’ll add the angled parts of the frame:

  1. on the longest pipe and the shortest pipe, measure the distance from the pipe center to the pipe node (one of the holes)
  2. Mark the longest pipe distance on the top piece of the frame, and the shortest pipe distance on the bottom piece. It’s a little more complex than that, but you get the idea: place the pieces so that all the pegs will fit on them.
  3. Mark the centerline of two 1.5″ wide pieces of wood.
  4. Line up the centers of the two pieces of wood with the marks on the top and bottom pieces.  Mark where the two pieces of wood extend past the top and bottom parts.
  5. Cut off the extended parts of those pieces of wood, then replace them on their marks.
  6. Glue and clamp the two pieces of wood to the top and bottom parts of the frame, making sure they are still aligned with their marks.
  7. While the glue is drying, use the 1″-spaced marks on the side pieces to mark 1″-spaced marks on the two pieces that you’re gluing. Then similarly mark 1/2″ marks (ok, I should have originally marked 1/2″ marks on the side pieces). These 1/2″ marks will line up with the holes in each chime.

The result should look a  lot like the photo below:

Waiting for the glue to dry
Waiting for the glue to dry

Once the glue is dry, unscrew the outer parts of the original frame, and cut the over-long remaining pieces, leaving you with what will be the final frame, with an extra piece running down the middle.

What will be the final frame, cut free from its larger frame
What will be the final frame, cut free from its larger frame

Next comes the tricky part: drilling the holes for the pegs that will hold each chime. The pegs need to fit precisely into the holes of the chimes so that the chimes will wiggle.  Since the chime holes are 3/16″ and the pegs are 1/8″, there isn’t a lot of room for error. This is the point I messed up in the first attempt at a frame.

To make sure the pegs are lined up, here’s how to add the peg holes for one chime:

  1. place the chime so that its holes are aligned with the 1/2″ centerline on the frame, and so that the lengthwise center of the chime is aligned with the long center piece running down the middle of the frame.
  2. Hold the chime tightly in place while you use a drill with a 3/16″ bit to drill a shallow pilot hole for the left peg of the chime.  Don’t drill the right side hole yet.  Instead of drilling, you could use a very thin pencil to mark where the left hole will go.
  3. Using a drill press (I have something like a Microlux micro drill press) with a 1/8″ bit, drill the left peg hole.
  4. Cut a 1.5″ piece of 1/8″ dowel; this is one peg.  Insert this peg into the hole (but not all the way yet – you want to be able to pull the peg out soon).
  5. Place the chime on the left peg, and line up the right hole of the chime with the 1/2″ line. Center the left hole on the left peg, so there’s wiggle room.
  6. Mark the right hole in the same way you did the left (with a hand drill or pencil).
  7. Drill the right peg hole and insert a second peg.
  8. Place the chime on the left and right pegs. Double check that it is still a little loose.  If not, you have to putty up the right hole and try again (luckily I didn’t have to do this).
  9. Remove the pegs and chime, and go on to the next chime.
Drilling holes for one chime: after placing the left peg
Drilling holes for one chime: after placing the left peg

Now unscrew and remove that center piece. You don’t need it now that you’ve drilled all the peg holes.

Meanwhile, it’s time to cut the felt pads that will surround each peg.  These pads keep the chimes from rattling on the wood.

  1. Buy a set of felt pads with adhesive on the bottom; something like these Waxman pads from Amazon.
  2. Cut the felt pads into 3/4″ squares.
  3. Punch a 1/8″ hole in the center of each square.  I drilled the holes, which really gummed up the drill bit.
Drilling a hole in the felt
Drilling a hole in the felt

Now it’s time to glue everything together. For each peg:

  1. Put a small amount of wood glue on the peg
  2. Press the peg into its hole. Gently tap it home with a hammer.
  3. Clean off the excess glue
  4. Pick up a square of felt and peel off the adhesive backing
  5. Slide the felt over the peg and press it down onto the frame.
Gluing the pegs and felt
Gluing the pegs and felt

Once all the pegs and felt are in place, put the chimes on the pegs.

Your glockenspiel is complete! You can play it by hand with drumsticks, or you can do what I plan to do and add Arduino-controlled solenoids to play the chimes automatically.

See a video of me trying out my glockenspiel.

The finished (manual) glockenspiel
The finished (manual) glockenspiel

Next, I turn my attention to the circuit that will play the chimes.

A little info on using SSL in your web site

I recently made a rash Tweet saying that if you don’t have SSL (Secure Sockets Layer; what HTTPS uses) your WordPress site will be hacked. That’s an overstatement. I’ve known people who’ve run WP sites without SSL with no problems.

So here’s a little web SSL orientation for people who have better things to do than learn the details of computer security.

First, some people think that you need SSL on your web site/service only if you’re doing things you want to hide, or if you’re doing financial transactions.

My opinion is that your web needs SSL for anything that involves posting or login. As plenty of people have pointed out, if you as a web client don’t use SSL when you post or login to a site, an attacker – an eavesdropper – can easily post or login to that site as you. So if you don’t have SSL, you can’t trust that the poster or login isn’t somebody whose life goal is to sell counterfeit shoes, inflate the Google ratings of their bogus web site, or worse.

Another benefit of SSL is that it gives your web clients better assurance that when they go you your site, say “https://needhamia.com”, they are talking to the real site and not some imposter site (who wants to sell shoes, etc.)

SSL can involve a bit of sticker-shock on the scale of buying web hosting in the first place, so think about the costs to you and your users of your site getting hacked or their accounts or data being stolen. If your site and data need to be safe, you’ll probably choose to bite the bullet and pay the money necessary to make your site secure. (I know, I know, it sounds like a racket doesn’t it?)

SSL also involves a lot of lingo. Thankfully, you don’t have to understand the details of the meaning of the words or how SSL/HTTPS works to use it.

So here’s my recommended sequence of things you need to do to support SSL. I’ve left off the details because I just want to give you an orientation rather than a tutorial.

  1. Get a web host that supports SSL. Most do, but a surprising number don’t. If you’re building your own web host from scratch, I expect you’ll be able to setup SSL.
  2. Buy a domain name.  For example, my domain name is Needhamia.com. It will cost you a small amount per year. This name is necessary for SSL registration. Some web hosts support only domain names that they sell, vs. domain names sold by other domain name registration services.
  3. Once you have your domain name, connect it to your web site. If you bought your domain name from your web hosting service, they can help you do this.  It’s not complex.
  4. Once you have connected your domain name to your site, order an SSL certificate.  It can cost you a surprisingly large amount per year – but think about the costs to you of not having it: suppose somebody hacks your site or impersonates a user. There are several kinds of cerfiticates. Again, some web hosts support only SSL certificates that they sell vs. certificates sold by others or ‘signed’ by yourself. Filling out the paperwork can seem daunting, but it’s not so bad for the simpler types of certificates.
    • “self-signed”. These certificates are the simplest/cheapest because you can make one yourself. It gives your web clients the security they need to keep from being eavesdropped. It doesn’t give them any assurance that your site is real – but that’s ok in some applications.
    • “standard” certificates. These are often the least expensive certificates a web host will sell. They’re probably fine if your site doesn’t deal with money directly.  This is the level certificate I’m using on Needhamia.com.
    • Fancy certificates. These are more about what kind of logos various browsers will show for your site, and how much assurance the certificate gives web clients that your site is who it says and you are who you say you are. Useful if you’re selling stuff.
  5. Once you have your SSL certificate, install it on your web site. Your web hosting company can make this easy for you, especially if you bought the certificate from them.
  6. Ta Da! Now https://yourdomainname uses SSL!
  7. To make your browser-based clients safer, it’s a good idea to make your web site “redirect” http to https. That is, if they type “http://yourdomainname” their browser automatically goes to “https://yourdomainname” instead. I found it hard to find the instructions I needed for this step, but a little searching turned it up.

To sum up: you need to, in this order, get a site, get a domain name for it, get an SSL certificate for that domain, install the SSL certificate on your site, and (optionally) redirect http to https.

Now you can build some really cool web apps!

If at first you don’t succeed…

It was all going so well.

In an earlier post I tuned the chimes. In this post, I find one way not to make a frame for those chimes.

I created the frame for the glockenspiel, with a square frame on the outside so that I could mark the inner part of the frame with 1″ lines, one per chime.

Marking the lines for the chimes
Marking the lines for the chimes

I then aligned each chime in turn with the lines and with the centerline of the frame – the place where the solenoid will strike the chime.

Aligning each pipe with the center and edges
Aligning each pipe with the center and edges

I then drilled a pair of shallow marker holes for each aligned pipe. I could have instead marked the holes by slipping a thin pencil through the chime holes – but I didn’t have such a pencil, so the handy hand drill sufficed.

Drilling shallow marker holes
Drilling shallow marker holes

I then drilled smaller holes centered in the marker holes, and glued a short dowel in each hole.

Then I made the big mistake: I unscrewed the frame so that I could glue it back together – replacing the screws with glue.

Gluing the frame - after drilling the peg holes
Gluing the frame – after drilling the peg holes

Although I was careful to glue the frame pieces along marker lines I’d made when it had been screwed together, the long sides of the frame slipped enough that the dowels no longer aligned with the holes in the chimes.

You can see in the photo below that, for example, the “D” chime hole is nearly 1/4″ away from the peg above it, that it should have been able to rest on. This offset is true for almost all the chimes, indicating that the long parts of the frame shifted between the time I drilled the dowel holes and the time I tried to drop the chimes onto the dowels in those holes.

The holes don't line up
The holes don’t line up

So, my plan now is to make another frame, and to glue the frame together before I mark and drill the dowel holes (duh) that the chimes will fit over. If at first you don’t succeed, find the root cause of the problem and correct it :-)

My next post has the details of the working frame for the chimes.

I knew the project wasn’t unique, but…

Imagine my surprise, after working on my robotic glockenspiel for weeks, when I opened the Signals Catalog, and page two revealed Chimes of the Seasons Music Box, $90. The product has since been discontinued, but it was fun to see it in the catalog.

I knew the project was far from unique, but hadn’t seen such a thing in stores for years – cool that returned to the zeitgeist.

Cutting and Tuning Robotic Glockenspiel chimes

Cutting Glockenspiel pipes from 1/2" EMT Conduit
Cutting Glockenspiel pipes from 1/2″ EMT Conduit

After a few weeks of experimentation, I think I can now write sensible notes on how to cut and tune the chimes for a glockenspiel (metal xylophone) out of metal conduit. This is the first step of my Robotic Glockenspiel project, which I hope to end with a network-connected, Arduino-controlled set of chimes that can play Christmas carols.

Parts:

A few bits of background, which you can ignore if you like:

  • I’m talking about pipes what will each be suspended near each end and struck in the middle.
  • Physics of such pipes says that the suspension points – the Nodes – for a pipe of length L should be at L * 0.224 from each end of the pipe.
  • A chromatic tuner usually tunes to what’s called an Even-Tempered scale. In such a scale, a note that sounds one octave above another has twice the frequency of the first note. Further, that octave is divided into 12 steps, called semitones, each of which is equally spaced (in frequency ratios) from the previous one.
  • Modern even-tempered tuning is based on a standard frequency of 440Hz – cycles per second – as an “A” in the scale.
  • Mathematically, to divide a ratio of 2 into 12 equal ratios, each ratio is the 12th root of 2, or about 1.0594630. So the frequency of each semitone is 1.0594630 times the frequency of the next lower one. Similarly, each semitone’s frequency is 1 / 1.0594630 times the frequency of the next higher one.

First cut an arbitrary length of pipe that, when tuned, will be the reference for all your pipe lengths. For 1/2″ conduit, I’d suggest cutting a piece that’s something like 400mm long – you don’t have to be exact at this point.

Once the pipe is cut, calculate the suspension points – the Nodes.
N = L * 0.224, so for a 400mm pipe, the nodes are at 400mm * 0.224 from each end. That’s 89.6mm. Since the nodes don’t have to be exact, we can use 90mm instead.

Place the 400mm pipe on the two thin supports, each support at 90mm from an end of the pipe.

Measure the frequency of the pipe by turning on the tuner then striking the pipe with your mallet (or pen).

Now you’ll need to tune the pipe by repeatedly measuring the frequency and trimming off pieces of the pipe:

  • If the tuner says that the pipe is tuned sharp- that is, above the frequency of the nearest semitone – cut a few millimeters from the pipe length and measure the pipe frequency again. Repeat this process until the tuner says the pipe is flat – that is below the frequency of the nearest semitone.
  • If the tuner says that the pipe is tuned quite flat, cut 2 or 3mm from an end of the pipe and measure the pipe frequency again. Repeat this process until the tuner says that the pipe is either slightly flat or is in tune.
  • If the tuner says that the pipe is tuned only slightly flat, use the file to file off a very little bit from the pipe, then measure the frequency again. Repeat this process until the tuner says that the pipe is in tune.  If you happen to file off too much and the pipe is tuned sharp, cut off a few mm of pipe as in the step above.

Now you have one properly tuned pipe.  Next you’ll get to cut and tune all the other pipes, based on the length of this standard pipe.

Suppose that the tuner says that your tuned pipe is an F and that measuring the length with a meter stick, you find the pipe is 396mm long.

A little more math comes in here.  Given a pipe of length L1 that rings at frequency F1, you can calculate the length, L2 of a second pipe that will ring at a desired frequency, F2.  Put mathematically, with “sqrt” standing for square root, L2 = L1 / sqrt(F2 / F1).

A little music theory comes in here as well. One way of noting the order of semitones in an octave is with sharps: C, C#, D, D#, E, F, F#, G, G#, A, A#, B, C. Also, each octave (for example, a C to the C above it) is 2 times the frequency of the lower note. You can use this list and (and the fact that an octave is 12 semitones) to count the number of semitones between your first pipe’s frequency and the frequency you want.

Suppose that you want to find the length of a pipe that is the A above your F.  From our list above, we find that A is 4 semitones above F.  So our desired frequency, F2, is F1 * 1.0594630^4 (where ^ means “raised to the power”), or F1 * 1.0594630 * 1.0594630 * 1.0594630 * 1.0594630.

Substituting our calculation of F2 into the pipe length equation, we have
L2 = L1 / sqrt(F1 * 1.0594630^4 / F1), or
L2  = L1 / sqrt(1.0594630^4)

So, to calculate the length of a pipe that sounds N semitones above your first pipe (where semitones below are negative numbers), the math works out to
L2 = L1 / sqrt(1.0594630^N)

Plugging in all the stuff above, L1 = 396mm and N (of the A) is 4:
L2 = 396 / sqrt(1.0594630^4)

Wow, that was boring! At any rate, now you can cut and tune all the pipes in your glockenspiel, using these two formulae:

  1. L2 = L1 / sqrt(1.0594630^N)
  2. Nodes at = L2 * 0.224
  3. Strike the chime at the center = L2 / 2.0

Next I’ll describe how I’m mounting the pipes on a wooden frame.

 

References:

In my next post, I describe a build fail of the wooden frame. Making is not always as linear as it seems.