# Teach, Show, then Consult: Make GPT a Music Composition Guide

I tried an odd idea: have ChatGPT learn a music programming language (Alda), give it some great code examples, and then ask it for advice on how to create music.

Surprisingly, it somehow turned out to be work! Maybe it is a misrepresentation, but the attempt itself is quite interesting. Code: teach-show-consult.

## Teach 🎓

We will teach ChatGPT to learn a language called Alda, It introduces itself as follows:

Alda is a text-based programming language for music composition. It allows you to write and play back music using only a text editor and the command line.

An example:

piano:
o3
g8 a b > c d e f+ g | a b > c d e f+ g4
g8 f+ e d c < b a g | f+ e d c < b a g4
<< g1/>g/>g/b/>d/g


In this step, all I have to do is to use Prompt Engineering to give ChatGPT as many syntax rules as possible for the Alda language. You can find this part in teach.py:

ALDA_LANG_RULE = """You are a musician as well as a technologist who is well versed in programming.
Now you've been asked to learn a new language called Alda, which allows you to create music as if you were programming.
I will now tell you its rules:
1.The alda program usually starts with (tempo! number), which is stating the tempo of the music as this number.
2.Next, the instrument is usually specified, e.g. "piano:", which means that the music will be played on a piano. Other instruments supported are: acoustic-guitar, cello, flute, violin, etc.
3.Immediately after that, comes the part of the notes. Let me illustrate the main features of this program.
a) The default is quarter notes, which means that you type "c d e f", which represents a measure that has four quarter notes: C, D, E and F.
b) The ">" symbol means...


Thanks to LangChain, I can easily test the results. Now try to see if it understands the rules:

>>> python teach.py
Let's test Alda language learning!
Human:
c d e+ f-
AI:
This is a measure of four quarter notes written in the key of C Major. The notes are C, D, E-sharp, and F-flat.

Human:
c d > e4 f8
AI:
This paragraph of code represents a measure of music that begins at the C major scale and moves up one octave to E.
The first two notes are quarter notes (C and D), and the third note is an eighth note (E). The last note is a quarter note (F).


It looks like it basically understands the rules.

## Show 👉📺

Now the second step: show it some subtle code examples for ChatGPT to learn from.

I found some examples from Alda repo, downloaded and parsed then encode these Alda code files into a vectorestore using OpenAI Embedding.

Here, a local database is actually established. Before you ask ChatGPT a question, it can obtain some relevant local information. The code is in show.py, and heavily based on chat-your-data.

## Consult 🤔❓

Now, we have come to the final step: consulting ChatGPT to create music.

Since ChatGPT has already learned the rules, if we provide it some musical inspiration, perhaps it can extend the music we create by following the works of giants and coming up with new melodies or phrases.

Then I gave it a try:

>>> python consult.py
Let's consult the music master!
You:
c d8 > g

Music Master:
(tempo! 120)
piano:
c4 d8 > g4 c4. <g4~4. >c4. d4~4. c4. <g4~4. >c4. d4~4. c8 r d e g b2~12 > d12 g | d8 r < b4~8~ > d8 < {b > d g}4 |
g8 r f+4 f {e_ g b}4 | g8 r f+4 f {e_ g b}4 | (quant 40) > d4 (quant 90) e-4~8 e8 d- c | < b_ > d4~ f8 < b_4 > c |
< b_8 > (ff) d4~ e8 (f) c2 | < a-2. > c4 < a2~ f b_2 > (mf) c8 c4~ d8 | < a-2 r8 > c4~ d8 | < a-2 r8 f4~ (p) g8 |
(pp) a_16~ b~ a_~ b_ (p) > c~

[INFO] Notes saved to: output.alda


I used an online converter to turn output.alda into a .midi file. Then again using another online midi2mp3 tool to get the .wav file music starting with my input of c d8 > g.

You can listen to it here:

## Have Fun! 🎉

This project is probably just a joke. It uses too little samples and the rules I taught are only a subset of the entire grammar. However, it seems to have led to interesting results. Perhaps, in other matters, this approach can produce more interesting things.

I made the code open sourc: have fun!