Skip to main content

Homebrew game for C64 - part 3: generating assembly straight from sheet music

This post is going to combine musical notation, music XML, cocoa programming (objective-C for MacOS) and retro game programming (specifically the music) in assembly for C64.

That might qualify this as an extremely niche post, so if you're reading because you're interested in all or some of those things, or if there's some other reason why you'd like to convert sheet music to another format, do let me know if the comments so that I know I'm not entirely alone!

In the last couple of posts in this series I wrote about how I was tinkering around with a little tune for a game I'm writing for C64. Writing out the notation is something I'm very comfortable with.

I eventually arrived at a great and efficient way to store and play the music within my game. This involved using a kind of 'bytecode', combining instructions and data. (A development of code that came with Derek Morris' book). That looks like this:
That looks pretty laborious to write out by hand, particularly looking up the high and low oscillator settings for each note. And I can tell you that it is.

Anyway, for this game that's done now,  I have something I'm pretty happy with, other than the voice settings. You can hear that in the last part of this series if you're interested.

But with future composing efforts in mind, it occurred to me that I could seriously turbocharge this process, and pretty much generate the assembly language straight from the sheet music.

First step: parse the music XML (which any music notation software should be able to export to). At first, to save time, I downloaded an open-source class for turning XML into NSDictionary. That appeared to work but after some work I realised that it wasn't handling self-closing tags in the XML, which my musicXML was full of. So that wasn't a time-saver and to be fair the author of the xml -> NSDictionary class has made it clear that it's now out of date and unsupported.

So back to square one and writing code using NSXMLParser. That involves writing a lot of code, but it's useful in some ways - it serialises the data, you can take what you want from it and ignore what you don't want. It was probably no more code than I had to write to extract the data after the XML had been converted to an NSDictionary, and there are no dependencies on 3rd party / open source code.

Here's where we are, in one afternoon (including the abandoned effort and restarting) I can open any musicXML file and see this:
[later update]
... and here we are with the app now converting the notes to high and low frequency values and generating the assembly. Sweet!
Yes I did have to type in the table of values for each note from good old Appendix E (just the decimal value, the high and low values are calculated from that) but now that I've done that, it's the last time I'll have to refer to that table.

It looks like a lot of code, but each of those lines of assembly represents just two bytes in the compiled code, so my entire tune with three voices takes around 1.8k.

Comments

Popular posts from this blog

Homebrew game for C64 - part 5: C64 / 6502 IDE for Mac

So far I've not written very much about the actual gameplay. That's mostly because I had done a bit of that before I started writing this blog. I will write some of that up 'retro'spectively. (geddit?)

But I've been choosing to do other things such as the music and whilst doing that I got hung up on writing a system for converting music notation to assembly.

I've been muddling by with a miscellany of tools (TMPx, an online sprite editor, XCode etc). This has been working pretty efficiently, with a few frustrations. So today I decided to begin work on an IDE for Mac, which AFAIK doesn't exist.
Not bad for a day's work.  The syntax colouring alone makes the assembly so much easier to read. The line numbering probably took more time than the rest put together. You never know what's going to go smoothly and what you'll be stuck on for hours.

But is this just displacement activity?? Am I avoiding getting on with the gameplay because I'm having do…

Comparing ARMSID and real 6581 SID

I've set up a YouTube channel for posting my music, mostly to give myself an incentive to keep writing and hopefully improve those skills.

I recently posted a version of this eastern-sounding piece rendered by the notation software. An exercise in using the pentatonic minor for that eastern flavour.

I've now made recordings of the C64 playing the piece. First of all I made a 3-voice version (not yet posted) and then an 8-voice version using multi-tracking.

That's the first version you hear in this video, played on my 'bionic 64' which is fitted with an ARMSID.

Curious about how this would compare with the same music played by a real 6581 SID, I did the same recording using an all-original 64 and made a video with one recording after the other. My observations are below but judge for yourself.


In the 'pure' tones it's hard to hear a difference. But there is definitely a difference when there are many voices playing together and using pulse-wave modulatio…

Homebrew game for C64 - part 1: SID tune

It's funny how interests from your teens remain with you, how easy it is to pick them up again and how comfortable they feel.

I'll probably write more about the revival of this hobby and my collection of 8-bit computers. Let's just say for now that I've begun a C64 puzzle game based on my character Yvonne the Sheep and her friends - and enemies.

Writing for 6502 (or 6510 in this case, which makes no difference as far as the assembly language is concerned) is like putting on an old familiar comfortable jumper.

I wasn't particularly keen on doing music, I did think about just forgetting it and just going with sound effects.

But a silly tune came into my head that seemed suitable for sheep pootling around a field. I'm not a composer of any great experience but I am at home with sheet music. Notating it (in 3 voices and possibly a fourth later) was the easiest way for me to get the tune out of my head and onto 'paper'.

Before I knew it I was really hooked o…