Converting text to music can be fun. Opusmodus has a few built-in functions to work with text. Look up the description of text-to-pitch, for example. As described in its documentation,
(text-to-pitch ‘(“To be, or not to be, that is the question”))
=> (((g5 d5) (cs4 e4) (d5 f5) (cs5 d5 g5) (g5 d5)
(cs4 e4) (g5 g4 c4 g5) (gs4 fs5) (g5g4 e4)
(e5 gs5 e4 fs5 g5 gs4 d5 cs5)))
For today’s experiment, I wanted to do something more adventurous. I am a great enthusiast of NLP and text analysis, so instead of mapping letters to pitch, decided to determine the part of speech of each word and then map that part of speech to pitch. For example, we can map all Verbs to one pitch and all Adjectives to another. Of course, there is no guarantee that this simple algorithm yields memorable melody, but this gave me an opportunity to check out one of the popular text analysis engines – TextRazor. You can register with them and get a free API key (you need the key to test my program).
TextRazor has convenient APIs in Java, Python, etc., but since I am using Lisp, I decided to use their REST API. I needed two other Lisp libraries for this – Drakma and Yason, both of which can be easily obtained via Quicklisp. You can visit Opusmodus forum to learn how to install Quicklisp.
The following figure shows the code fragment.
You can download the full source code here. Do not forget to substitute your API key in the marked place in the source code, otherwise, the program will not work.
Recent Comments