An Introduction to Collective Algorithmic Music Composition

Musical Hello World!

In 1026, Guido d'Arezzo created a technique to set a text to music. This technique consisted of assigning a pitch to each vowel so the melody varied according to the vowels in the text. In this section of the book, we will do something similar we will assign each character a pitch. We are going to write our first musical script with Scripthica. This script is going to convert the "Hello World!" string to musical notes and save it as a MIDI file.

We need to know a couple of things before we start this script. The char->integer procedure converts a char into an ASCII number. We can map the letters to midi numbers using this procedure. The string->list procedure will also be very useful.

First, we must define a variable that will convert our string into a list of characters. We will use the string->list procedure to accomplish this:

(define hello (string->list "Hello World!"))

Now we must iterate our character list and convert each character to a number and store the numbers in a new list. We can define a new recursive method to accomplish this.

; define a function that converts 
; a list of chars to integers
(define (hw l)
 (if (null? l) '()
     (cons (char->integer (car l)) (hw (cdr l)))))

We can now define a variable to store the pitches. We call the hw function with hello as an argument to convert the string into a list of numbers.

;pitches
(define p (hw hello))

A note needs to have a duration or length. We can use the same numbers from the pitches but extend their duration using a function that will multiply each value by 3 or any number you want.

;note lengths
(define l (map (lambda (x) (* x 3)) p))

Or we can use a constant value:

;note lengths
(define l (constant-value (length p) 1000))

A note also needs to have a velocity, this means how loud or soft each note will be played. We can use the create-constant-velocities procedure to create constant velocities.

(define v (create-constant-velocities p 96))

Or we can use a constant value:

;velocities
(define v (constant-value (length p) 96))

Finally, we create the MIDI file.

(save-midi (create-events 0 p l v 1))
; musical hello world!
(define hello (string->list "Hello World!"))

(define (hw l)
 (if (null? l) '()
 (cons (char->integer (car l)) (hw (cdr l)))))

;pitches
(define p (hw hello))
;durations
(define l (constant-value (length p) 1000))
;velocities
(define v (constant-value (length p) 96))

(save-midi(create-events 0 p l v 1))

If we open the MIDI file in MuseScore we will get the following result: