Idea for a Music Box Player

Share:

Overview

brown music boxHave you ever tried to make a music box?

Well this is my attempt at creating a circuit that plays the musical notes of a song every time someone pushes a switch. For simplicity, both song and player code are compiled together and uploaded to the PIC flash.

The song consists of notes and their duration and thus is stored in a very compact form and the player code occupies around 370 bytes. It's possible to store very long songs using this format which reminds me of MIDI.




Schematic

The circuit has few components. On the left the usual 4k7 MCLR pull-up resistor and the 100nF decoupling capacitor. On the right a 4k7 pull-up resistor and push-button on INT0 and a logarithmic 4 bit DAC on pins RB7..RB4 with a bypass capacitor and a small 32Ω speaker.

The values of R6 and R7 are not critical. The values of R1, R2, R3 and R4 ideally must be a geometric progression with a common ratio of 2. The E12 series values 1k, 2k2, 3k9 and 8k2 provide a good approximation.

schematic

PCB

No PCB was designed since this is an idea to incorporate in other projects.

Software

High level description of the Music Box Player

 

Timer 0: note duration
  • All durations are a multiple of 48 (CGD)
  • A quarter note figure (crotchet) corresponds to 48 ticks
  • All other figures are multiples of 48 and they are all integer
Timer 1: tone of the musical note
  • 12 tones times 3 octaves (C2..B2, C3..B3, C4..B4)
  • All tones are pre-calculated as a timer value
  • Its interrupt loads the next sample of the sound wave
  • Sound wave is a Sine Wave with 16 samples and 4 bit values
  • Sound wave can be expanded with more samples and larger values 
 

 

Music encoding scheme:
  • One byte per note
  • Most significant nibble contains the following operations:
    • 0..11 are notes C, C#, D, D#, E, ..., A#, B
    • 12 is a silence
    • 13 is an octave up operation for every note that follows
    • 14 is an octave down operation for every note that follows
    • 15 is the end of song operation
  • Least significant nibble contains the note duration as follows:
    • 0 whole-note
    • half note with dot
    • half note
    • quarter note with dot
    • quarter note
    • eighth note with dot
    • eighth note
    • sixteenth note with dot
    • sixteenth note
    • thirtysecond with dot
    • 10 thirty-second note
    • 11 sixty-fourth note
    • 12 triplets

 

Example: 0xc0, 0x05, 0x08, 0x24, 0x04, 0x54, 0x42

  • 0xc0 = 13 | 0 : octave up (following notes will be played at octave C3..B3)
  • 0x05 = 0 | 5 : note = C, duration = eighth note with dot
  • 0x08 = 0 | 8 : note = C, duration = sixteenth note
  • 0x24 = 2 | 4 : note = D, duration = quarter note
  • 0x04 = 0 | 4 : note = C, duration = quarter note
  • 0x54 = 5 | 4 : note = F, duration = eighth note with dot
  • 0x42 = 4 | 2 : note = E, duration = half note

And yes, this is the beginning of the happy birthday song:
(still under copyright as of 2015)

   happy birthday music sheet

 
Player file structure: 
File name Description
main.c player code
sinewave.h the sound we'll ear (sine wave with 16 samples and 4 bits per sample)
stdint.h several integer type definitions (may not be needed depending on the compiler)
music-brahms-lullaby.h lullaby song (currently included by the player)
music-happybirthday.h happy birthday song (optional)

 

 

Pictures and Videos

The following picture shows the prototype assembled on the breadboard with a 5V regulator PCB on the left side.

 

And below is a video demonstration. You'll probably find the quality of the sound to be quite annoying. To be considered a true music box, the sound quality must be improved.

 

Improving this project

There are several things that can be done to improve the project. Below is a list of those I remember. If you like the project go ahead and implement a few of them.

 

Downloads

Right click on the files you want and select Save Link As...

 

References

  1. MIDI file format
  2. Outline of the Standard MIDI File Structure
  3. Frequencies, Periods and Wavelengths
  4. Weighted Resistor DAC - Analog/Digital Conversions, Howard Austerlitz, 1991, Data Acquisition Techniques Using PCs, 2nd edition, Academic Press, San Diego, California, pp 57-59.
  5. Electronic Music Box by Nathan Reed

 

Published on Sunday 2015/07/12, last modified on Monday 2015/07/20