# Music composition in Elm

**URL:** <https://discourse.elm-lang.org/t/music-composition-in-elm/3956>\
**Category:** Request Feedback\
**Created:** [July 8, 2019, 1:49pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956 "2019-07-08T13:49:06Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [July 12, 2019, 2:44am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/21 "2019-07-12T02:44:36Z")

</div>

Hi Leif,

I looked at your code. Very, very nice!! (Listened to the demo as well). I think in most respects you are farther along with an Elm version of Euterpea than am I. I’ve been working on this for a short while – it grew out of my wacky drum language project – first commit on June 27.

About a library – this is most definitely what should happen. Let’s talk some more. There is plenty to do to make a really good Elm Euterpea, and collaboration might be a good way to accomplish that. I’m interested. I think that @Lucas_Payr would be also. He’s helped me several times already.

Oh – one more point. Despite my experiments, I agree that it is desirable to stay as close to Hudak’s design as possible while writing idiomatic Elm. He has put a lot of thought into this.

PS. Here is the latest version of my test app: [https://jxxcarlson.github.io/app/euterpia-test.html](https://jxxcarlson.github.io/app/euterpia-test.html) – not very impressive. It really only plays one voice at the moment. I’m using Tone.js as the backend.

I

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [July 12, 2019, 5:01am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/22 "2019-07-12T05:01:30Z")

</div>

> [@jxxcarlson](#):
>
> There is plenty to do to make a really good Elm Euterpea, and collaboration might be a good way to accomplish that. I’m interested. I think that @Lucas_Payr would be also. He’s helped me several times already.

Jup

> [@leif](#):
>
> I just recently started this so this is still much WIP and I don’t know yet where this should go. But maybe it would be nice to create a library out of this at some point and maybe an accompanying npm package?

Yes that really should happen. Personally, I’d rather call it elm-music than elm-euterpea though.

> [@leif](#):
>
> My first attempt was to do playback with webaudiofonts

We currently use [tone.js](https://tonejs.github.io/). It seems like webaudiofont is sample/waveTable based while tone.js simulates a synthesizer. I personally like having a synthesizer but on the other hand webAudioFont has Midi-In and Midi-Out. Maybe we want to join both libarays? tune.js has a Midi format. It shouldn’t be that difficult to compine both…🤔

---

<div class="post-metadata">

**Author:** ![leif](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/leif/32/2339_2.png) [@leif](https://discourse.elm-lang.org/u/leif)\
**Post date:** [July 12, 2019, 12:36pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/23 "2019-07-12T12:36:48Z")

</div>

> [@Lucas\_Payr](#):
>
> Yes that really should happen. Personally, I’d rather call it elm-music than elm-euterpea though.

Sure, I don’t feel attached to that name at all.

> [@Lucas\_Payr](#):
>
> We currently use [tone.js](https://tonejs.github.io/). It seems like webaudiofont is sample/waveTable based while tone.js simulates a synthesizer. I personally like having a synthesizer but on the other hand webAudioFont has Midi-In and Midi-Out. Maybe we want to join both libarays? tune.js has a Midi format. It shouldn’t be that difficult to compine both…🤔

I worked with tone.js a bit and I like it so far. (I think it is pretty much state of the art for web audio.)  
For what I have seen, Euterea has extensive MDI support. It would be really cool to be able to make use of this, like instrumentation of voices, etc. webaudiofont was just a first attempt. I am experimenting and I’m not really an expert in web audio and MIDI.

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [July 12, 2019, 4:03pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/24 "2019-07-12T16:03:31Z")

</div>

Re names: yes, elm-music sounds good to me. Perhaps the best is to use a neutral name to but to credit Euterpea. I also liked the Mousikea name.

Re output: Having different backends or sound renderers or whatever they should be called is an attractive feature that should make the library useful for a wider set of people and interests – Tone.js, MIDI, maybe more. In the latter half of the Hudak-Quick book, they talk about “Sounds and Signals”. Tone.js or something of that nature could be useful for this

---

<div class="post-metadata">

**Author:** ![pianomanfrazier](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pianomanfrazier/32/2926_2.png) [@pianomanfrazier](https://discourse.elm-lang.org/u/pianomanfrazier)\
**Post date:** [July 12, 2019, 7:27pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/25 "2019-07-12T19:27:40Z")

</div>

I would go further. Make notes more like  
type `NoteName = C D E F G A B` and the accidental `type Accidental = Sharp Flat None ...` I wrote more about representing music stuff on my blog at [https://pianomanfrazier.com/post/music-theory-in-elm/#designing-with-elm-types](https://pianomanfrazier.com/post/music-theory-in-elm/#designing-with-elm-types)

---

<div class="post-metadata">

**Author:** ![pianomanfrazier](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pianomanfrazier/32/2926_2.png) [@pianomanfrazier](https://discourse.elm-lang.org/u/pianomanfrazier)\
**Post date:** [July 12, 2019, 7:36pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/26 "2019-07-12T19:36:47Z")

</div>

The problem with `as fs ...` is what about double sharps and flats? What about naturals? Now you have an explosion of types 7 x 5 instead of 7 x 2.

What if I wanted to render your generated music to print it? You could possibly generate LilyPond output from this so you would want a musical representation sophisticated enough to describe readable music.

Btw I’m speaking at Elm conf this year about doing music theory stuff with Elm.

---

<div class="post-metadata">

**Author:** ![Chadtech](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/chadtech/32/609_2.png) [@Chadtech](https://discourse.elm-lang.org/u/Chadtech)\
**Post date:** [July 12, 2019, 11:55pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/27 "2019-07-12T23:55:05Z")

</div>

Hey @jxxcarlson,

Thanks for sharing those data types. It looks really interesting to model music that way in code.

[I happen to be working on my own music project in Elm/Haskell](https://github.com/Chadtech/Radler-ui) The UI is in Elm, ([here is a picture of what it looks like](https://imgur.com/a9q6Z9w)) and the backend that generates all my audio is in Haskell ([like this audio](https://www.youtube.com/watch?v=_T2KHSreoKc&feature=youtu.be)). Maybe our interests overlap a little bit? I’m not sure.

In my project, the front end is really just providing a spreadsheet, where the Y axis represents time, X axis represents different voices, and the strings in the cells of the spreadsheet are notes, containing pitch, volume, and duration information. The spreadsheet is sent to the backend, as a long list of notes paired times. The backend parses the notes, and turns it into audio.

Its a lot less abstract than your music types. Just a lot of lists of notes and voices. Heres a data type for all the different kinds of voices I have so far:

```auto
data Voice
    = Sin (Osc.Model Sin.Model)
    | Saw (Osc.Model Saw.Model)
    | Harmonics (Osc.Model Harmonics.Model)
    | DullSaw (Osc.Model DullSaw.Model)
    | Percussion Percussion.Model
    | Test Test.Model

```

Each voice type has its own note type, and its own function for converting a list of notes into audio.

I guess one thing that seems interesting seeing you all talk about music code, compared to my own project, is the data types. My project isn’t trying to do much more than compile human written notes into sound, and so I guess I can get by just fine with lots of `List Note` types. But maybe what you all are doing is more like mutating the shape of musical structures in code automatically and algorithmically, so you need those structures _in_ your code to begin with.

I dont know. Does that sound about right?

---

<div class="post-metadata">

**Author:** ![leif](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/leif/32/2339_2.png) [@leif](https://discourse.elm-lang.org/u/leif)\
**Post date:** [July 13, 2019, 2:25am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/28 "2019-07-13T02:25:25Z")

</div>

> [@pianomanfrazier](#):
>
> I would go further. Make notes more like  
> type `NoteName = C D E F G A B` and the accidental `type Accidental = Sharp Flat None ...`

Hi,

I like that approach. It is about the compiler helping to write correct code and making illegal state unrepresentable.

I found that there are two concerns that are related but also quite different, that is music notation and music performance (as in playback).

The fundamental difference is that in notation you have to handle the problem of enharmonic spelling well. Whereas in performance you only care about pitch, and this makes things a lot easier.

Euterpea is primarily focussed on music generation, modification, interpretation and performance as far as I can tell.  
So e.g. it doesn’t know the concept of intervals with names and qualities (like e.g. minor third, where “minor” is the quality and “third” the name) at all. In Euterpea intervals are represented as number semitones.

---

<div class="post-metadata">

**Author:** ![pianomanfrazier](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pianomanfrazier/32/2926_2.png) [@pianomanfrazier](https://discourse.elm-lang.org/u/pianomanfrazier)\
**Post date:** [July 13, 2019, 3:23am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/29 "2019-07-13T03:23:12Z")

</div>

That is a great distinction. It would be hard to type all that out in real time for a performance.

It would be cool however to generate harmony and more complex music programmatically. I’m imagining something like generating a lead sheet harmony and generate random bebop lines over the top. If you had a concept of tonality and harmony you could generate patterns that fit the chords.

---

<div class="post-metadata">

**Author:** ![pianomanfrazier](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pianomanfrazier/32/2926_2.png) [@pianomanfrazier](https://discourse.elm-lang.org/u/pianomanfrazier)\
**Post date:** [July 13, 2019, 3:34am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/30 "2019-07-13T03:34:01Z")

</div>

You could adopt a notation similar to lilypond. For example `aes'4 bisis,2`. I know it strays from the original api but the way of representing notes is very compact.

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [July 13, 2019, 1:17pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/31 "2019-07-13T13:17:10Z")

</div>

Hey @Chadtech looks very interesting! I’m currently in the process of a family move from the US to France. I want to study your code in detail as soon as I get a chance. I am very much interested in your Haskell interop.

Lots of interest in music, it seems. This is good!

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [July 13, 2019, 1:30pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/32 "2019-07-13T13:30:20Z")

</div>

@chadtech, yes, sounds about right, I’m primarly interested in algorithmic composition (as well as the conventional kind). Of course, to enjoy that kind of composition, you need some way of rendering it in to audio. My first crude attempt: [https://jxxcarlson.github.io/app/euterpia-test.html](https://jxxcarlson.github.io/app/euterpia-test.html) – at the moment voice 2 overwrites voice 1, so it isn’t really doing two voices.

There is nothing original in what I’ve done so far. The data types are taken from the book _The Haskell School of Music_, by Paul Hudak and Donya Quick.

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [July 14, 2019, 8:46pm UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/33 "2019-07-14T20:46:46Z")

</div>

I think that one could have various input languages that work with the underlying data structure.

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [July 15, 2019, 4:47am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/34 "2019-07-15T04:47:47Z")

</div>

Yeah, I think so too. Our focus should be a package and a .js file that are intuitive and easy to use. From there everyone can build upon.

Personally I’ve not got enough experience with ports to know how we can include the Js-side with the package. I would hope that its possible that we can have a Js-File in the same folder as the compiled HTML file and then everything works.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [July 25, 2019, 4:47am UTC](https://discourse.elm-lang.org/t/music-composition-in-elm/3956/35 "2019-07-25T04:47:51Z")

</div>

Closed on July 25, 2019, 4:47am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.elm-lang.org/t/music-composition-in-elm/3956.md?page=1)
