Idea for my own DSL to produce music
Tags: discuss, sideprojects, music
Published: 2020-02-12 16:40:14

I have been having an idea recently about whether someone (maybe myself) could create a DSL (domain specific language) that could serve as somesort of scripting language to produce music, more specifically TABS.

# What are TABS? 
Okay yeah - I should backup and start from the beginning, one of the things that I do for fun iis play tehe guitar, I have always loved music and this is just a natural extension of that, now there are ways to show musicians what notes to play:

![Music notation](https://dev-to-uploads.s3.amazonaws.com/i/r426dy0x7oo3gygw6in0.png)

This sort of notation works greta for some instraments, such as piano for example, but for guitar there are subtleties that have been introduced that make this a bad way of reading music for players.

For example how would you know to slide from one note to another? How would you know when to bend a string from one note to another?

The Answer is, is that you can't so obviously people invented a different version of the same thing but for guitar players, this is normally referred to as TAB, or Tablature:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3ocqydcycckxxvrpa2gm.png)

This is not perfect, because it doesn't give time signatures and standards can vary - a lot between who is writing them.

# What is a DSL?
So a DSL is a domain specific language, let me quote a fellow countryman here:

> A Domain-Specific Language (DSL) is a computer language that's targeted to a particular kind of problem, rather than a general purpose language that's aimed at any kind of software problem. Domain-specific languages have been talked about, and used for almost as long as computing has been done.
- Martin Fowler (https://www.martinfowler.com/dsl.html)

The way I like to imagine this is that it is some form of abstraction layer that allows you to take a problem add a layer of DSL and make it easier for people to do what they are already doing

A GREAT example of this is HTML, think about it, it's an XML language that allows us to describe to the browser how we would like the page to display to our user. But we don't explicitly tell the browser "Render this button here", we just give it a vague outline.

# So what are we doing?
I think this is more of a mental exercise than anything else, I'm wondering if there could be some sort of language that could esentially produce TAB, I have thought about it a bit let me show you my first run through:

```
section intro
    let MyNewChord :e 7 :B 7 :G 7 :D 7 :A 7 :E 7

    repeat 4
        play :OpenA

    play MyNewChord

    play :e 7 :B 7 :G 7 :D 7 :A 7 :E 7
    slide :e 7 :B 7 :G 7 :D 7 :A 7 :E 7, :e 8 :B 8 :G 8 :D 8 :A 8 :E 8

    play :wait

main:
    diagram "A", :e 7 :B 7 :G 7 :D 7 :A 7 :E 7

    play intro

```

Hopefully some of this syntax will be familier to most if not all coders. But let me take you through some of the implementation that I have in my head:

`play`: this would print out the tab like the example above:

```
e|------------------
B|------------------
G|------------------
D|------------------
A|------------------
E|------------------
```

The `:e` operators are referring to a string so if we say something like `play :e 7` for example then it would print 

```
e|-7----------------
B|------------------
G|------------------
D|------------------
A|------------------
E|------------------
```

`section` is much like a function, music is composed of sections that you may or may not want to repeat, talking specfically about western music here, music is normally in some sort of 


Verse, Chorus, Verse, Chorus, Middle 8th, Outro,

structure so we could say some thing like "this is section 1" and "chorus 1":

`section 1`

so we could repeat it more easily:

```
play verse_1
play chorus_1
play verse_1
```

and it esentially "calls" the section to print out it's chords.

`let MyNewChord :e 7 :B 7 :G 7 :D 7 :A 7 :E 7`

This may speak for itself but I am defining a "new chord" and assigning it to the variable "MyNewChord" this can be referred to later and "played"

```
play MyNewChord
```

And it would print out the notes you would need to play:

```
e|-7----------------
B|-7----------------
G|-7----------------
D|-7----------------
A|-7----------------
E|-7----------------
```

`repeat x` -Simple looping mechanism, some time you want to just say "play this chord x amount of times, or play this part of the music x amount of times.

# Different sinks

What's cool about abstracting away the writing of the tabs you could make it so TABS are only a sink of data, much like logging where a file is a sink and a queue is a sink, you could make a sink that is TABS, and a sink that is I don't know MusicXML https://www.musicxml.com/ which is apparently a thing, or have it export a file for guitar pro.


Maybe I'm onto something, maybe this is to niche