# Euterpea library to create music with Haskell

**URL:** <https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391>\
**Category:** Learn\
**Created:** [December 25, 2023, 1:06pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391 "2023-12-25T13:06:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toxypiks](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Toxypiks](https://discourse.haskell.org/u/Toxypiks)\
**Post date:** [December 25, 2023, 1:06pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391/1 "2023-12-25T13:06:06Z")

</div>

Hey guys,

I’m currently trying to play some notes with the Haskell Euterpea library. I’m using yoshimi as a synthesizer software. My Problem is, for some odd reason the first note gets skipped, only the second, third and so on are played. No matter what melody I try to play, its always the same. Someone has the same problem or an idea how to fix it?

```haskell

module Main (main) where                                                                                                                                                                                                                                                                                                                                                             

import Euterpea  

hNote :: Dur -> Pitch -> Int -> Music Pitch                                                                                                                                       
hNote d p h = note d p :=: note d (trans (-h) p)                                                                                                                
                                                                                                                                                                                             
mel :: Music Pitch                                                                                                                                                                    
mel = hNote qn (F,4) 3 :+: hNote qn (A,4) 4 :+: hNote qn (F,4) 3                                                                                                             
                                                                                                                                                                                                                                                                                                                                    
main :: IO()                                                                                                                                                                                 
main = do                                                                                                                                                                                                                                                                                                                                                                 
     play mel

```

Thank u for any help 🙂

---

<div class="post-metadata">

**Author:** ![f-a](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/f-a/32/2740_2.png) [@f-a](https://discourse.haskell.org/u/f-a)\
**Post date:** [December 25, 2023, 1:44pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391/2 "2023-12-25T13:44:23Z")

</div>

If you sandwich your code block between ```, your post will be easier to read.

I have a similar problem when I dabble with [ABC notation](https://en.wikipedia.org/wiki/ABC_notation#Example). The first note of the resulting `.midi` does not get played on _some_ synthesizers.

I work around it by adding a very small pause at the beginning of the piece. Maybe this will work with Euterpea too?

---

<div class="post-metadata">

**Author:** ![Toxypiks](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Toxypiks](https://discourse.haskell.org/u/Toxypiks)\
**Post date:** [December 25, 2023, 2:40pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391/3 "2023-12-25T14:40:38Z")

</div>

Thank u for your suggestion, I just edited the post 🙂

I also thought about some workaround like adding a small pause or a dummy note in front of my melody.  
Was just wondering if there’s a more elegant way to fix this, but maybe its a problem with yoshimi then.

---

<div class="post-metadata">

**Author:** ![Toxypiks](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Toxypiks](https://discourse.haskell.org/u/Toxypiks)\
**Post date:** [December 25, 2023, 4:12pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391/4 "2023-12-25T16:12:40Z")

</div>

Just tried to add a small pause at the beginning of my melody, but actually that’s not working. The next note will also be skipped and only the third and fourth note will be played. That’s fairly weird.

Here’s my code with the pause (I also added the same pause in between the third and fourth note just for testing if the pause works).

```haskell

import Euterpea  

hNote :: Dur -> Pitch -> Int -> Music Pitch                                                                                                                                       
hNote d p h = note d p :=: note d (trans (-h) p)                                                                                                                
                                                                                                                                                                                             
mel :: Music Pitch                                                                                                                                                                    
mel = wnr :+: hNote qn (F,4) 3 :+: hNote qn (A,4) 4 :+: wnr :+: hNote qn (F,4) 3                                                                                                             
                                                                                                                                                                                                                                                                                                                                    
main :: IO()                                                                                                                                                                                 
main = do                                                                                                                                                                                                                                                                                                                                                                 
     play mel

```

---

<div class="post-metadata">

**Author:** ![f-a](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/f-a/32/2740_2.png) [@f-a](https://discourse.haskell.org/u/f-a)\
**Post date:** [December 25, 2023, 5:13pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391/5 "2023-12-25T17:13:19Z")

</div>

Can Euterpea export a `.midi`?

---

<div class="post-metadata">

**Author:** ![setrar](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@setrar](https://discourse.haskell.org/u/setrar)\
**Post date:** [June 4, 2026, 11:02pm UTC](https://discourse.haskell.org/t/euterpea-library-to-create-music-with-haskell/8391/6 "2026-06-04T23:02:40Z")

</div>

in `ghci` :  
_- I played the file (i.e. saved in music.hs) using my piano (BlueTooth MIDI #7),_

- saved to a midi file called ` music.midi` and
- opened the file using MuseScore

```haskell
Loaded package environment from /Users/me/.ghc/aarch64-darwin-9.6.6/environments/default
GHCi, version 9.6.6: 
  :? for help
ghci> :l music.hs
 [1 of 2] Compiling Main ( music.hs, interpreted )
Ok, one module loaded.
ghci> devices
  Input devices: 
     InputDeviceID 0 IAC Driver Bus 1
     InputDeviceID 1 Network RTP Session 1
     InputDeviceID 2 UMP Network Network MIDI 2.0 Session 1
     InputDeviceID 3 FP-30 Bluetooth
  Output devices: 
     OutputDeviceID 4 IAC Driver Bus 1
     OutputDeviceID 5 Network RTP Session 1
     OutputDeviceID 6 UMP Network Network MIDI 2.0 Session 1
     OutputDeviceID 7 FP-30 Bluetooth
ghci> playDev 7 mel
ghci> writeMidi "music.midi" mel
ghci> :q
Leaving GHCi.

% open music.midi 

```

This is what is displayed in MuseScore

 ![image](https://us1.discourse-cdn.com/flex002/uploads/haskell/original/2X/6/6fa519f1dcbc7697eeba129c78f4fb67d9e0be73.png)

* * *

> [@Toxypiks](#):
>
> ```haskell
> import Euterpea  
> 
> hNote :: Dur -> Pitch -> Int -> Music Pitch                                                                                                                                       
> hNote d p h = note d p :=: note d (trans (-h) p)                                                                                                                
>                                                                                                                                                                                              
> mel :: Music Pitch                                                                                                                                                                    
> mel = wnr :+: hNote qn (F,4) 3 :+: hNote qn (A,4) 4 :+: wnr :+: hNote qn (F,4) 3                                                                                                             
>                                                                                                                                                                                                                                                                                                                                     
> main :: IO()                                                                                                                                                                                 
> main = do                                                                                                                                                                                                                                                                                                                                                                 
> play mel
> 
> ```
