C64 Music Player using CC65 C Cross Compiler

 

Developing a Christmas demo program on the Commodore 64 and Atari platforms using the C language and the CC65 C Compiler.  Below is a working music play component written two different ways.  Both methods use a special Header file which maps each note in each octave to the correct frequency.  As a result, the songs themselves are loaded into arrays as natural notes as the following:

Song Arrays with Notes

The first method uses FOR loop delays for the tone and shutoff time.  It's simple to implement and if no other major tasks for graphics or animation are required to process then this will work well.  The trade-off to simplicity is that the FOR loops (red below) effectively delay and hang any other task the program might need to execute:

Music Play Example using For Loop Delays

The second method uses a State Machine and although it requires more programming, it will not delay or hang other tasks that the program might need to perform while playing music.  Since every line of code can be executed quickly, the latency effect is minimal allowing one to add graphics and animation and other tasks to have a greater degree of independence.  For example, responding to joystick inputs would not adversely affect the music play and vice versa.  We are fortunate to have a runtime clock with a resolution of 60 Hz accessible via hardware which is enough resolution to handle fast musical notes to exact time.  For example, a quarter note would need a 60Hz/4 or 15 Hz delay.  As a result DELAY1 below is set to a constant value of 15.  Below is the State Machine example which will play all three songs in sequence continuously until a key is pressed by the user.

State Flag Definition in Header

Music Player State Machine in Main.c




Popular posts from this blog

Indy House Updates

Young Great Horned Owls: Morning Walk, April 24

Olympic Photo: Steve Prefontaine & Lasse Viren