Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Modest – musical harmony library for Lua (github.com/esbudylin)
76 points by esbudylin 8 days ago | hide | past | favorite | 7 comments
This is a project I've been building in my spare time over the past few months. It's a library that provides methods for working with musical harmony ‒ intervals, notes, chords. For example, it can parse almost any chord symbol (Fmaj7, CminMaj9, etc) and turn it into notes, or it can identify a chord from a given set of notes.

I started this project with the idea of using formal grammar to parse chord symbols. I wanted to use it instead of a hand-written parser, which is the common approach among similar libraries. Lua caught my attention because of Lpeg, a Parsing Expression Grammar library that is both fast and easy to use. An additional motivation for using Lua was the lack of comparable libraries for it, even though the language is commonly used in audio programming.

However, despite being a Lua library, the project itself is written in Fennel — a "lispy" language that transpiles to Lua. Fennel has features that make writing code for the Lua platform much more pleasant: a concise syntax, macros, and destructuring — a feature Lua sorely lacks!

In the process, I definitely learned a lot about music theory, although my new knowledge is quite one-sided. By working on this library, I know a thing or two about types and structure of chords, but I learned almost nothing about their composition and transformation. Perhaps these will be the directions I explore next in the project.






Things get more interesting when we explore musical tunings other than the 12 equal divisions of the octave (EDO) of Western music.

You can define interval structure as a sequence of large L, small s, and optionally medium M steps.

For example, the Major diatonic scale, a 7 note scale from 12 EDO, in Ls notation is:

   LLsLLLs  with L: 2  s: 1 (12=2+2+1+2+2+2+1)
A 6 note scale (Gorgo-6) in 16 EDO is:

   LLsLLL  with L: 3  s: 1 (16=3+3+1+3+3+3)
You can then explore frequency ratios beyond those available in 12 EDO, see this Lua file: https://github.com/robmckinnon/pitfalls/blob/main/lib/ratios...

E.g. the Gorgo-6 scale has the intervals S2 (septimal major second), d4 (Barbados third), N4 (undevicesimal wide fourth), s6 (septimal sixth), s7 (septimal minor seventh).

And chords based on those ratios, see Lua file: https://github.com/robmckinnon/pitfalls/blob/main/lib/chords...

The above links are Lua code files for a monome norns library for exploring microtonal tuning: https://llllllll.co/t/pitfalls/37795


What is the target use case of the library? Aren't chord, note and interval objects redundant (i.e. a chord is a set of notes of a specific distance, where the latter is an interval)? The chords themselves are just a subset of scales, of which a finite number exists (see e.g. https://github.com/rochus-keller/musictools/). Do you consider the relation of chords and scales, and modulations within and between scales? Concerning Lisp, are you aware of https://ccrma.stanford.edu/software/s7/s7.html or https://commonmusic.sourceforge.net/?

Thanks for sharing Common Music! I haven't heard of it, it seems worth studying.

Speaking of use cases, I started this project with the idea of making a flexible parser for chord symbols. In the process of solving this problem, I wrote some general-purpose utilities, which eventually took form of this library. I'm making it public in the hope that it will be useful to others who use Lua for music and audio programming.

I haven't yet implemented the functions related to scales. I'm still thinking about their relation to chords and how to express it in the library's API.


I wrote something like this for fun years ago, although I don’t think I published it. I think my library had a scale defined by a root note and a set of steps. A chord could be constructed from a scale and a root note, with modifiers such as inversions and others like X7, Xb5#13, etc. The scale has the blueprint and the notes follow. You already have the primitives with notes and intervals.

Parsing chords from notes is more difficult, as are most parsing tasks.


Nice work! Perhaps this could interest users of norns[1], many of which are active on the lines[2] message board.

1. https://github.com/monome/norns

2. https://llllllll.co/


Thanks for the pointers! I'll try to spread the word there as well.

Oh very neat. I had worked with Fennel inside touchOSC to create custom control surfaces and used it extensively for chord identification logic. The code is private, but here's an excerpt (e.g. norm-chord tries to identify chords based on MIDI notes): https://gist.github.com/turbo/264d44f4f820b5b32fdd13b8ef475d...

You're right it's very expressive. I wish there was something like Teal, but for Fennel. I know there are libraries for runtime typing, but my ideal language would be a gradually statically typed Fennel.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: