Reusable code in TidalCycles

Nick Tchayka
POTAC
Published in
3 min readJun 1, 2017
Renick Bell live coding during the 2013 Linux Audio Conference in Graz (Austria) at the Forum Stadtpark on 11 May 2013.

A lot of the time, while live coding music, we make awesome sounds, awesome melodies, awesome patterns or even awesome effects.

But, what happens with them after we finish our piece/performance?

Most of the time, they get lost, forgotten, or just saved for the future, but lost in our file systems. Sometimes we want to bring them back to life, but copying and pasting is not an option.

TidalCycles (or Tidal only) is an embedded domain specific language in Haskell, for the purpose of creating music, patterns or just sound. It depends on the Haskell interpreter to execute its code, so we get access to it.

Because it’s just Haskell, we can make a Haskell library to save our sounds in a library that we can just import later. Let’s begin.

Stack

Stack is a program for initializing, building, testing and doing many other things with Haskell project with ease. If you are in OS X or Linux, you can install it by issuing the following command in your prompt:

curl -sSL https://get.haskellstack.org/ | sh

If you are in Windows, you can download an appropriate installer from this page.

Our library

Let’s create a new Haskell library project by executing:

stack new <name-of-our-library> simple-library

I named mine ns-sounds, you can name yours whatever you like.

This command will generate a directory like so:

├── LICENSE
├── README.md
├── Setup.hs
├── <name-of-our-library>.cabal
├── src
│ └── Lib.hs
└── stack.yaml

Let’s open the cabal file with a text editor and make a few tweaks to it. First, we will add tidal to the build-depends field, then, we will change the exposed-modules field to the name we want to import and, finally, we will add the field default-extensions: OverloadedStrings after default-language:

name:                ns-sounds
version: 0.1.0.0
--synopsis:
--description:
homepage: https://github.com/githubuser/ns-sounds#readme
license: BSD3
license-file: LICENSE
author: Author name here
maintainer: example@example.com
copyright: 2017 Author name here
category: Web
build-type: Simple
extra-source-files: README.md
cabal-version: >=1.10
library
hs-source-dirs: src
exposed-modules: Sound.Tidal.NickSeagull.Sounds
build-depends: base >= 4.7 && < 5,
tidal
default-language: Haskell2010
default-extensions: OverloadedStrings
source-repository head
type: git
location: https://github.com/githubuser/ns-sounds

After this, delete the file inside the src folder called Lib.hs and make a folder hierarchy according to your library name, ending with a hs file, in my case:

src
└── Sound
└── Tidal
└── NickSeagull
└── Sounds.hs

Each dot separates a directory level.

Saving our sounds

Now, open your library file with a text editor, in my case src/Sound/Tidal/NickSeagull/Sounds.hs.

At the top of the file, we will put the name of the module and import tidal by doing the following:

module Sound.Tidal.NickSeagull.Sounds whereimport Sound.Tidal.Context
import Sound.Tidal.Scales
import Sound.Tidal.Chords

After this, we can just add our sounds as we do in regular tidal:

mySound = s "supersaw" # sustain 10

Save the file, and issue the following command from the root of the library project, where the cabal file is located:

cabal install

This will compile and install your library in a system-wide level.

Now, just open a new tidal session and issue the line

import <name-of-our-library>

You’re ready for rocking the stage with your sounds, whenever you update your sound library, just run cabal install again.

Keep rocking!

--

--

Nick Tchayka
POTAC
Writer for

Functional magician 🎩 I make Monads disappear ✨ Serverless Architect @theagilemonkeys 📐 Yerba Mate addict 🥤