A lot of (nerd) humour in this title 🙂 It can be read in two ways. The first is: “Am I a smart music composer ?” (the answer is: no) and the second is: “Artificial musical Intelligence – a smart music composer”. In practice, the purpose of this small project is to test an algorithm able to randomly generate a “decent” music. It could be easily done with a PC, using its huge HW / SW resources, but I want to do it with a very small microcontroller circuit based on STM8S001J micro, using my own firmware written in C language.

The software project is splitted in two parts: the first is “the player”, the part that just plays a melody stored in the microcontroller’s memory; the second is the algorithm that generates such melody in a random way. Let’s start from the first part, the “music player”.

First of all, we must build the circuit and test the music player firmware. The schematic is very simple (see below)

Differences from the prototype picture: R6 is 330R on the prototype and R3 was added to reduce sound level while recording the song with PC audio software.

You can download the schematic in PDF format from the "files" section. The circuit is very simple and can be easily built on a prototype board. The piezo buzzer LS1 has low audio volume (it’s just a local “monitor”), but you can take the audio signal from J2 connector and send it to an amplifier. I decided to create a very small ADSR (Attack-Decay-Sustain-Release) in order to have a sound like a “ding” and not the usual “beep”. The I/Os PC5-PC4-PC3 (pin 7) are used all together to give the current necessary to charge the 100uF capacitor used in ADSR. The envelope of such ADSR circuit has the shape that you can see in the next image.

This is a screenshot of the Audacity open-source software used to record the default song “Happy Birthday to you” produced by the test firmware (you can download the .mp3 from the "files" section).

Here is how such “song” is stored in the microcontroller’s memory. In this first section, the notes and their durations are stored as a constants array (FLASH memory), but in the final version the random song will be generated in a RAM array. The player, however, will act in the same way. The duration of every note is expressed in 50mS steps. The note “0” is a pause.

If you want to build this circuit, and improve or modify the first version of the firmware, download the file AmI.zip. It contains the full project folder, with C sources, ready to be edited, compiled and debugged using the free Cosmic-C and STM tools. For more info please read my previous article (italian) on this subject. The working folder is zipped, so use the 7Z program with password eficara to extract it. The two buttons “like” and “unlike”, in this test firmware, are used just to start and stop the sequence. Note that the stop button is sampled only at the end of a note, so you must press and hold it down up to the end of a note to stop the song.

The algorithm and how it improves itself

Artificial musical Intelligence: what do we need to create a machine that can autonomously produce an acceptable music or melody ? First of all, we must implement a random numbers generator. This is the heart of the algorithm and simulates that magical element that we have no control over, like fate or destiny. After this, we have to set some rules that will be picked up randomly and used to generate a sequence of notes and pauses. Finally, the most important element: the feedback. The machine needs a feedback to improve itself. The feedback, in this case, is supplied by the human that listens to the music (you) via two buttons. Such buttons are labelled “like” and “unlike”. After listening to the music, you can press “like” if you appreciated the...

Read more »