Skip to main content

All Insights – Page 36

Jane McGonigal: Gamers can change the future

Jane McGonigal wants to see a game developer get nominated fortheNobel Prize. I daresaymost of the audience first went “yeah, sure.” That was before

Clojure + ClojureScript + Vim + REPL

Learn how to set up a seamless Clojure and ClojureScript REPL integration with Vim, enabling easy evaluation and live updates directly in your browser.

Internet all the things: Hacking Android TV

The Internet of Things is coming, or so they tell me. We will soon be surrounded by "smart" devices that bend the world to our slightest whims. And it's

Building Roguelike in F#

Explore the journey of building a roguelike game in F# from scratch, focusing on hero movement, game logic, and overcoming initial challenges.

Code == UX

Developers: whencoding, you are not only creating user experience (UX) for the end user. All your code actuallyisUX. The gym I go to just moved to new

Generating music in F# part 3: Note values

Previous posts in series:Part 1Part 2 After last post the program can generate melodies using Markov chains generated from seed data. Next important aspect of a catchy melody is note value. Note value means a relative duration of a note. Just like notes note values are modelled as a type: type value = | Full | Half | Quarter | Eighth The actual duration of a note value in seconds is relative to tempo. A map of durations for values can be created using this function: let time beatsPerMinute = let quarterLength = 60. / beatsPerMinute Map.ofList Beats per minute(bpm) means the amount of quarter notes in a minute. Dividing minute(60 seconds) with bpm yields length of quarter note in seconds. Other time values can be counted from that quarter note value. I wanted some more complex seed data: let yesterday = Melody is now represented as list of tuples of note and value. Both types will have their own Markov chain structure let createMarkovChains data = data |> Seq.windowed 2 |> Seq.groupBy (fun x -> x.) |> Seq.map (fun x -> (fst x, x |> snd |> Seq.map (Seq.nth 1))) |> Map.ofSeq let noteData = yesterday |> List.map(fst) |> createMarkovChains let timingsData = yesterday |> List.map(snd) |> createMarkovChains Creation of random sequence is not dependent of the type of item in this case. Functions can be made more generic by removing type declarations referring to note-type. let getNextElement (random : System.Random) (data : Map<_,_>) currentElement = let nextSet = data. let nextElementIndex = random.Next(0, Seq.length nextSet) nextSet |> Seq.skip nextElementIndex |> Seq.head let r = System.Random() let nextElementFromData x y = getNextElement r x y let rec randomSequence wantedLength seedData currentElement (acc : 'a list) = if acc.Length = wantedLength then acc else let nextElement = nextElementFromData seedData currentElement randomSequence wantedLength seedData nextElement (acc @ ) Here is the main now: let main argv = let availableNotes = noteData |> Map.toSeq |> Seq.map fst |> List.ofSeq let firstNote = availableNotes. let melody = randomSequence 24 noteData firstNote |> List.map(fun x -> frequency.) let timingsForTempo = (time 120.) let availableTimes = timingsData |> Map.toSeq |> Seq.map fst |> List.ofSeq let firstTime = availableTimes. let timings = randomSequence 24 timingsData firstTime |> List.map(fun x -> timingsForTempo.) List.zip melody timings |> Synth.writeMelody 0 Random sequences are created for both notes and time values. Then they are zipped into list of tuples and written to wav-file. Full source on GitHub. Here is an example output:

Generating music with F# part 2: Markov chains

Continuing from the previous post. To make melody generation more interesting I decided to try applying statistical process called Markov chain. In the context of this post by Markov chain I mean that the next state of the system is not completely random but next states have different probabilities that depend on the current state. There is a Finnish children's song and a Swedish drinking song called "Ukko-Nooa" or "Gubben Noak" that has a simple melody that goes like this: let nooaMelody = We create a map of notes to sequence of notes. Sequence consists of notes that can follow the note that is used as key. let noteData = nooaMelody |> Seq.windowed 2 |> Seq.groupBy (fun (x : note []) -> x.) |> Seq.map (fun x -> (fst x, x |> snd |> Seq.map (Seq.nth 1))) |> Map.ofSeq This is how to get next random note based on current note. let getNextNote (random : System.Random) (data : Map<note,seq<note>>) (currentNote : note) = let nextSet : seq<note> = data. let nextNoteIndex = random.Next(0, Seq.length nextSet) nextSet |> Seq.skip nextNoteIndex |> Seq.head The more times a note is contained in the sequence where currentNote acts as a key, the more likely it is to become next note. Rest of the program looks like this: let r = System.Random() let nextNoteFromData : Map<note,seq<note>> -> note -> note = getNextNote r let rec randomMelody wantedLength noteData currentNote (melody : note list) = if melody.Length = wantedLength then melody else let nextNote = nextNoteFromData noteData currentNote randomMelody wantedLength noteData nextNote (melody @ ) let main argv = let availableNotes = noteData |> Map.toSeq |> Seq.map fst |> List.ofSeq let firstNote = availableNotes. randomMelody 8 noteData firstNote |> Seq.map (fun x -> frequency.) |> Synth.writeMelody 0 // return an integer exit code nextNoteFromData is partially applied function of getNextNote that can be used without passing an instance of System.Random along. randomMelody recursively creates list of notes until given length is achieved. That's it! Here is an example of the output. Much better than the completely random version from previous post. The full source code is on GitHub

Generating music with F#

For a long time I have been fascinated with the idea of generating music programmatically. I decided to give it a go using F# and this is what I came up with. I wrote a program that generates a short melody in the key of C major. Not exactly Mozart, but it's a start. I created a type for notes and a Map of notes to their frequencies: type note = C | Cis | D | Dis | E | F | Fis | G | Gis | A | Ais | B let frequency = Map.ofList A scale of C major can be modelled as a list consisting of the notes of the scale: let cMajor = Creation of a melody is only picking wanted amount of notes from the scale randomly let randomMelody () = let r = System.Random() let length = cMajor.Length seq {0 .. 7} |> Seq.map (fun x -> cMajor.) Frequencies for the notes can be extracted and then written to wave file randomMelody() |> Seq.map (fun x -> frequency.) |> Synth.writeMelody I took code to create wav-files from Liam McClennan's blog. All I did was wrap it in a module called Synth and created an public function that takes a list of frequencies. Here is an example output: That's it so far. Nothing fancy but hopefully something to build on in the future. The full source can be found on Github