The title of this post was a bit obscure as I didn’t really know what I was going to do for this entry on the #FsAdvent Calendar Make sure you check out the previous and future entries in the series.
For today, I thought we would celebrate with sound, in the Key of F# :D
To that effect we can use OpenAl. To be honest this was a little more difficult to do that it needed to be, mostly because there was plenty of playing around with values to make sure stuff worked and sounded somewhat ok.
The good thing about this sample is that you can run the sample either via a console app or in F# interactive (make sure you change the path to your location of OpenAl, you should get the NuGet package called OpenTKWithOpenAL) Disclaimer this is the first time I play with vanilla OpenAl (it will probably show).
The end goal is to transform something like this
"F# F# G# F# F# D# D# D# F# F# G# F# F# D# D# D#" into a collection of numbers that represent a wave, then we can feed that into OpenAl for it to play.
The graph below represent one note (generated with F# Charting)
Step by Step
To start off you need a context, a buffer and a source, these are OpenAl requirements.
Then we need to get something to play, if our input is a string with the whole song then we can do something like this to get the frequencies calculated for each note
I was thinking we could have a discriminated union here, however this seemed to serve the purpose of this example in a simple and concise way.
You might be wondering how I came to those numbers, well turns up the internet is great for this sort of thing, they can be found here. Tried a few octaves and these were the ones I preferred.
As mentioned in the first section, we are using 44100 as the base frequency, to simplify things the note length is set to half a second, however this is something that could be easily changed to support different lengths. Then we are creating a sequence that is half the frequency, and the reason for that is this is the number of samples required. Each item in that sequence is part of a curve that represents the sound for that given note (the freq parameter).
With those two key functions you can create the complete collection. In this case Seq.collect really shines, it is perfectly suited to what we need to do:
Combines the given enumeration-of-enumerations as a single concatenated enumeration.
Finally we use OpenAl to actually play the wave:
There is a mystery song you can play (in the key of F#)
You can see it all together in this sample here
Try it, you will probably love it. Another little toy to play with during the jolly season.
Thanks to Sergey and the F# community for this cool FsAdvent.
Also thanks to everyone (yes you, you rock!) for a truly amazing year.
The gif below seems completely unsuitable but you are here already :)