
Guitar Chord Voicings with Prolog - edavis
http://www.petecorey.com/blog/2020/04/21/guitar-chord-voicings-with-prolog/
======
elihu
Interesting that this intersects with something I've been experimenting with
lately. A friend of mine came up with a way of doing 41-tone equal temperament
on a modified guitar [1]. One of the tricks is that since 41-TET would require
the frets to be so tightly packed that the guitar would be difficult to play,
you simply omit every other fret and tune the guitar so that each string has
the notes its neighbors on either side lack.

It's a little strange not having all the notes on all the strings, but it
actually works out surprisingly well. Chord intonation is a bit nicer than
12-TET, and you get pretty good approximations of the 7-limit just intonation
intervals as well.

[1]
[https://en.xen.wiki/w/The_Kite_Guitar](https://en.xen.wiki/w/The_Kite_Guitar)
(The 7-string Ibanez is mine.)

~~~
cousin_it
There's a similar trick with a normally fretted guitar: tune the strings in
alternating major and minor thirds (something like ACEGBD) and lower every
other string by 15 cents. Then any three adjacent strings will be a just
intonation major or minor triad, you can mini-barre them on any fret to get
more triads, and there are just intonation scales in every key.

~~~
elihu
I haven't heard of that one, but I have heard of simpler tricks like re-tuning
one string so the third of certain barre chords comes out right. There's
always a bit of a trade-off; some intervals get better and others get worse so
in effect you're optimizing the instrument for a particular playing style or
key or song.

If you're willing to modify the guitar to put frets in different places, it
allows for a lot of options you wouldn't have otherwise. (This is especially
true if you're willing to use partial frets.)

Kite tuning is the best scheme I've encountered for having closer-to-just
intervals than 12-TET and yet you can play in any key. If you want perfect
intervals, plain just intonation is better but then you're locked into a
particular key and scale.

The design space of guitar fingerboards is surprisingly huge and open-ended
when you stray even a little from the twelfth root of two as the fundamental
unit of musical interval width.

------
hardmath123
Once, when I was taking a music theory class, I tried to use the Z3 constraint
solver to automatically harmonize melodies… it worked surprisingly well!
[https://github.com/kach/recreational-
rosette/tree/master/mus...](https://github.com/kach/recreational-
rosette/tree/master/music)

------
wgetch
Always interesting to see logic programming applied to music, it is in my
experience a good fit for the musical problem space. I spent some time
exploring the analysis of chords using answer set programming. On the surface
ASP programs look very similar to Prolog but the semantics differ; you query
Prolog to answer a question or series of questions related to your program,
whereas ASP produces complete "stable models" of your program (it answers "all
questions").

The first version of my analysis program would most likely have been a valid
Prolog program, and furthermore the program was completely positive (contained
no negation). After several weeks of rewrites, I ended up with a much smaller
program that also took advantage of ASP "choice" and "optimization" rules.
Much of the complexity was reduced by one fundamental improvement in the
representation of internal facts and adjusting the rules to accommodate. In
hindsight, the original version used a rather foolish approach, but like many
things it took some effort to realize why and how to do better.

This process of rewriting and incremental improvement (and initially
struggling in a less-familiar programming paradigm) gave me a great
appreciation for logic systems, and also a desire to use them more often.
There is something very beautiful in reducing a problem into logical
relationships, and instead of focusing on algorithms just focusing on the
problem space and how different pieces interrelate. It does take practice and
experimentation to produce an elegant solution to a problem, but with
experience it gets easier. I have no doubt the author of this article could
make it feasible to discover chords and tunings as he describes in the Future
Work section. Though I would recommend starting over with this goal in mind;
even a naive but viable solution might reveal some property of the system that
would transfer over to the original program and increase its versatility.

~~~
carapace
I'd love to see that code. Sounds awesome.

~~~
wgetch
I intend to do a proper writeup for this, in the meantime I published a short
blog[1] with the program along with some background information.

[1] [https://blog.phazon.xyz/answer-set-
programming/2020/04/22/mu...](https://blog.phazon.xyz/answer-set-
programming/2020/04/22/musical-logic-program.html)

~~~
pcorey
This is great, thanks for posting! I'll definitely take your advice to heart.
I've been noticing that finding different data representations of the problem
at hand is a big part of finding the "core relations" that describe the
problem. My first attempt at the voicing/4 predicate was attempted with a data
structure used in a procedural version of the algorithm, and I just couldn't
get it to work in Prolog. The second iteration is what you've seen, and I'm
working on a third iteration with a simpler data representation that is
simplifying the problem even further.

------
valdiorn
Tangentially related; I wrote some python code to come up with every possible
pentatonic scale (including some restrictions for musicality) - If nothing
else, I learnt a lot about scales and how they relate to one another

[https://github.com/ValdemarOrn/PentatonicScales](https://github.com/ValdemarOrn/PentatonicScales)

------
hpoe
So I've been interested in learning prolog ever since I found out it was a
language used for AI. I am wondering if it is worth picking up now?

Does it change the way one things about interacting with a computer or a
problem?

Is it something that will soon grow?

~~~
simplify
Prolog is fundamentally well established (~40 years old), and you can start
using it today in several programming languages. For example:

JavaScript [http://tau-prolog.org](http://tau-prolog.org)

Ruby [https://github.com/preston/ruby-prolog](https://github.com/preston/ruby-
prolog)

Rust [https://github.com/mthom/scryer-prolog](https://github.com/mthom/scryer-
prolog)

Erlang [https://github.com/rvirding/erlog](https://github.com/rvirding/erlog)

If your language is missing one, implementing a basic Prolog is fairly simple.
Take a look at the Ruby one: it's core is not even 500 lines of code!

There are also great options like SWI Prolog if you want to build your entire
app in Prolog. But I imagine it would be much easier to convince a work boss
to pull in a library instead :)

Re AI: It's true that Prolog's origins involved AI, but nowadays Prolog is
better suited for more general logic programming tasks (there are better tools
out there for AI specifically).

~~~
MisterTea
> Erlang
> [https://github.com/rvirding/erlog](https://github.com/rvirding/erlog)

Fun fact: Erlang started out as a modified version of Prolog. The original
Erlang VM was written in Prolog as well.

------
qppo
I kinda want to see a riff generator, searchable by chord base/tonality, time
signature, and length. Also a scale exercise generator. Lot of space to
explore in terms of permutations. I seem to recall some research on
probability of usage of a particular set of notes/chords based on information
entropy but I can't find that anywhere.

------
raverbashing
It's an interesting problem

An even harder problem is figuring out the chord corresponding to a given set
of notes. And it is surprisingly hard (though humans seem to do it "easily"
(with experience).

Even harder because for some there is no right answer, or better, the right
answer is the simplest. Yes C-E-G is a CMajor chord, but what is D-F-Bb-E ?

------
adamnemecek
I'm working n an IDE for music composition. Launching soon.
[http://ngrid.com](http://ngrid.com)

~~~
speps
Redirects to National Grid US.

~~~
adamnemecek
Fuck is [http://ngrid.io](http://ngrid.io).

~~~
partomniscient
Just so you know - you can edit your previous post.

~~~
adamnemecek
You can't after 2 hours.

~~~
partomniscient
TIL : Today I learned. I didn't know that.

Although I have made your kind of mistake before.

This is why I do a post-proof read / link test after I post.

------
msaltz
Re: Future Work: I didn’t think about the particular predicates in detail, but
the author could consider using the cut predicate (!) to prevent some
unnecessary backtracking. Without more thought I can’t guarantee that actually
could be effectively applied here, though

------
Rochus
Prolog is rather limiting; that's why Lisp has established itself in
traditional AI and especially in algorithmic music composition/generation too
(see e.g. Experiments in Musical Intelligence, EMI, or OpenMusic or CLM); even
today there are very interesting tools based on Lisp for music generation,
e.g.
[https://en.wikipedia.org/wiki/Impromptu_(programming_environ...](https://en.wikipedia.org/wiki/Impromptu_\(programming_environment\)).

~~~
hjek
> Prolog is rather limiting;

How is it limiting?

~~~
Rochus
Lisp is a general purpose programming language, supports any paradigm and the
implementation of domain specific extensions. Tool and library support is very
good and there is abundance of tutorials, books and people supporting it.
Prolog is not a general purpos programming language, but you have to write
relations and queries which are then resolved by SLD. You can implement that
in Lisp and it runs very fast e.g. on SBCL which has a JIT compiler; I'm not
aware of similar performance with Prolog compilers. Just to name a few
limitations.

~~~
hjek
Thanks for your response.

I love Lisps (especially Racket and Clojure) so let me just be clear that I am
not going to argue that one is "better" than the other.

> library support is very good

That depends mostly on the implementation. SWI Prolog has a decent number of
libraries available. Tau Prolog can interoperate with JavaScript.

> Prolog is not a general purpos programming language

I've made a HN clone in SWI Prolog. You can represent HTML as lists, like you
would in many Lisps. In some sense, Prolog can occasionally feel _more_
general purpose because you don't need a separate language (like SQL) for your
database.

> but you have to write relations and queries which are then resolved by SLD.

That is true, yet in some sense this is like stating about Scheme that "you
have to write functions which are then evaluated". Yep, you _have to do that_
but it doesn't mean the language can't be used for X.

> there is abundance of tutorials, books and people supporting it.

Yup, I dig SICP, The Little Schemer and Rich Hickey's talks. When it comes to
Prolog, Markus Triska's presentations are amazing[0].

If it was all about _which language has the most books written about it_ , I'd
worry that the "winner" would be Java. (I haven't particularly enjoyed the
books I've read about Java though.)

> supports any paradigm

I'm not sure that is necessarily a good thing. JavaScript kinda does this, and
I feel like I can never tell if a function or method call is pure or mutates
something. Does `foo.sort()` modify `foo` itself, or does it just return an a
sorted collection of the elements in `foo`?[1]

I also find it confusing in Common Lisp that `sort` is destructive[2]. I think
Clojure fares better there by sticking to immutability.

[0]:
[https://www.metalevel.at/prolog/videos/](https://www.metalevel.at/prolog/videos/)

[1]: [https://medium.com/@winwardo/the-principle-of-least-
astonish...](https://medium.com/@winwardo/the-principle-of-least-astonishment-
and-javascripts-sort-e98a734a30c9)

[2]:
[http://clhs.lisp.se/Body/f_sort_.htm](http://clhs.lisp.se/Body/f_sort_.htm)

~~~
Rochus
> Prolog can occasionally feel more general purpose

That's subjective; Prolog is/was not intended to be a general purpose
programming language, as little as e.g. SQL. And human imagination and
perseverance make it possible to reach the goal even under the most adverse
circumstances. If you make an effort, you can also build an HN clone in
Assembler or Brainf*ck. You cannot conclude from this that the language is
intended/suited for this.

> yet in some sense this is like stating about Scheme that "you have to write
> functions which are then evaluated"

No. There are different ways to evaluate rules. If you don't like SLD then the
tinkering begins if you want to stay in Prolog.

> I'm not sure that is necessarily a good thing

It is. E.g. MOP in Lisp is something extremely powerful. With macros you can
do things that languages like JavaScript (or Prologue) can only dream of; but
you know that yourself for sure.

~~~
hjek
Lol, it's nothing like assembler or brainfuck.

