Login
Xmas Magic
Title:Xmas Magic
Category:Demo/BasicExample
Release Date:
Language:(none)
Size:16K
Machine:PAL & NTSC
Code Type:Basic
Distribution:Freeware
 Play Online!
Code by:Csabo
Notes:A small program that plays the "Deck the halls" melody. For an explanation of the origins and how it works, see below.
User Rating: 10/10 (5 votes)
Xmas Magic Screenshot


Description
This small BASIC "demo" was written in October 2022, in response to this forum post. (In short, it's a call-to-action for Christmas themed BASIC programs.) I sent it to the organizers to be included in the 2022 magazine, but after following up on 2022-12-24, I turned out that my email was overlooked and the magazine was published without it. Below is the original description I submitted:

Xmas Magic

This small BASIC program will play the melody of the popular Christmas carol "Deck the Halls". But what's this? Only 6 lines of code? Where are the pages and pages of sound and data statements? Well, as you can see from the first line: it's MAGIC! :-)

The program is less than 256 bytes long. I hope it will be fun to figure how it works! Since it doesn't draw anything, perhaps enterprising users can combine it with other programs here with visuals that do not have any music.


How Does It Work?

This code uses two novel ideas: storing frequency values as single characters, and using strings as a simple tracker. I picked a song which had a relatively small interval between the lowest and the highest note in the melody: the notes are between E5 and F6, just over one octave. The corresponding frequency values are from 856 - 945, that's an (integer) interval of 89. That's a pretty narrow range, but even so it's a bit of a problem to represent these with plain PETSCII characters. However, we can "cheat" a tiny bit, and divide the range by two. This causes some frequency values to be off by one, but - unless you have perfect hearing - this is not noticeable. I picked "Z" to be the highest note, and everything else is counting from that note backwards.

Therefore, the notes are stored as: one PETSCII character, which we can easily type into a BASIC program, e.g. "Z" is converted to its PETSCII code (90), then multiplied by 2 (180), and 764 is added, resulting in frequency value 944, which is F6.

Shift + letter characters are used as "note length" control characters, e.g. Shift + A sets the note length to 1, etc.

The resulting strings can be easily added together for repetition. This is a form of compression, and similar to how "patterns" and "orders" work in music trackers.

But given all that, how come that the first three notes in the melody, plus the control characters spell out "MaGiC"? Well, that's Xmas Magic :-)

Copyright © Plus/4 World Team, 2001-2024