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.
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.
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.
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.
Increase number of bits per sample from 4 to 8
Change the sine wave audio sample to a real "ding" sound
Set an envelope on the audio sample to control the attack and decay volume speed
Use a real DAC (SPI, I2C, etc)
Use the serial port and build a true MIDI player sending commands to the synthesizer