MIDI in BASIC?

All aspects of programming on the Commander X16.
Post Reply
Mike Yocum
Posts: 2
Joined: Wed Nov 09, 2022 3:12 am

MIDI in BASIC?

Post by Mike Yocum »

Forgive me if this question was answered previously. With the MIDI card, is it possible to handle MIDI I/O in BASIC? Is BASIC fast enough to process the MIDI data stream with no perceptible latency?

Thanks,
Mike
TomXP411
Posts: 1802
Joined: Tue May 19, 2020 8:49 pm

Re: MIDI in BASIC?

Post by TomXP411 »

It depends on what you're doing.

If you're trying to play classical music with 32 instruments in a complicated pattern, then no - BASIC simply isn't fast enough. But for a simple melody line or sound effects using just a few channels, you can pull something off.

Music that uses the PSG or FM synth should have similar performance; you could try writing some test code that writes cords and patterns to the PSG to see how many notes you can play before the time between notes starts to be an issue.
Mike Yocum
Posts: 2
Joined: Wed Nov 09, 2022 3:12 am

Re: MIDI in BASI

Post by Mike Yocum »

It depends on what you’re doing.
Tom,

I appreciate your reply! No, I’m not talking about a DAW in BASIC, more like playing a saved chord based upon what note is hit, sending simple MIDI message(s) (e.g., All Notes Off), or transposing. From what you’ve stated, the X16 should have enough horsepower to do this?

Thanks,
Mike
TomXP411
Posts: 1802
Joined: Tue May 19, 2020 8:49 pm

Re: MIDI in BASI

Post by TomXP411 »

Mike Yocum wrote: Thu Jan 23, 2025 8:34 pm
It depends on what you’re doing.
Tom,

I appreciate your reply! No, I’m not talking about a DAW in BASIC, more like playing a saved chord based upon what note is hit, sending simple MIDI message(s) (e.g., All Notes Off), or transposing. From what you’ve stated, the X16 should have enough horsepower to do this?

Thanks,
Mike
Well, there's going to be some delay between activating notes. I just don't know how much of a delay to expect.

The good news is that Kevin used the same UART for the MIDI interface that he does for the serial/network card. So if you look at the BASIC terminal, you can re-use that I/O code to write to the MIDI port.

My only concern is that you may find that chords don't strike at exactly the same time. There may be an audible delay between each note - which is why I'm suggesting using the FM or PSG chip for some testing before investing in the MIDI card and spending a lot of time writing code.
Xiphod
Posts: 568
Joined: Thu Apr 15, 2021 8:05 am

Re: MIDI in BASIC?

Post by Xiphod »

The X16 has the horsepower, but doing so from BASIC perhaps not. The X16 has a lot of great features, but one thing it has sort of failed so far to achieve is that vision of making visual/audio games in BASIC "easy". One still has to invest quite a bit in the learning curve of VERA and interrupts (POKE-magic).

From BASIC, there isn't readily available double-buffering for animation (like an extra parameter on LINE, RING, etc. go to another screen buffer then SwAP). And the FM, PSG keywords don't really lend themselves to setting up background audio sequences. Then there is also the challenge of multiple keys at a time (like to move and fire at the same time) - though you can use the gamepad (JOY keyword) to get around that. (see X16SYNTH project on how to setup reading multiple keys in BASIC)


These things aren't impossible - so it's both a Pro and Con: the system challenges you to figure out workarounds (good for those who like a technical challenge), but to casually make a stick figure walk across the screen in sync with footstep audio (in BASIC) may take more than an afternoon (maybe cycling sprites and one of the audio BASIC keywords has a short duration background play).

ZSMKIT has shown how a software library extension can be made to interoperate with BASIC. For an example on doing that, seek out the Lower Decks project (or also CrazyLander, which I think uses the older ZSOUND library, but both are done from BASIC). We just don't have an example yet of MIDI (from BASIC). Some of the essentials to get started are there (like BLOAD and MCIOUT), but BASIC also doesn't have ready-made keywords for setting up background tasks (conceptually such a thing would be like an "ON TIMER 20 GOSUB 1000" (prep a background timer, and when hit near the timer, auto-GOSUB to a branch). The work around is to inject your own ISR (but then you can't easily work with BASIC variables- there are workarounds to that also), or just do your best at inline processing in a main loop (for examples on that see, see the RATE GROUP project [ viewtopic.php?p=29526&hilit=rate+groups#p29526 ]).
Last edited by Xiphod on Sun Jan 26, 2025 11:54 pm, edited 2 times in total.
User avatar
ahenry3068
Posts: 1190
Joined: Tue Apr 04, 2023 9:57 pm

Re: MIDI in BASIC?

Post by ahenry3068 »

Xiphod wrote: Sun Jan 26, 2025 7:43 pm The X16 has the horsepower, but doing so from BASIC perhaps not. The X16 has a lot of great features, but one thing it has sort of failed so far to achieve is that vision of making visual/audio games in BASIC "easy". One still has to invest quite a bit in the learning curve of VERA and interrupts (POKE-magic).

From BASIC, there isn't readily available double-buffering for animation (like an extra parameter on LINE, RING, etc. go to another screen buffer then SwAP). And the FM, PSG keywords don't really lend themselves to setting up background audio sequences. Then there is also the challenge of multiple keys at a time (like to move and fire at the same time) - though you can use the gamepad (JOY keyword) to get around that. (see X16SYNTH project on how to setup reading multiple keys in BASIC)


These things aren't impossible - so it's both a Pro and Con: the system challenges you to figure out workarounds (good for those who like a technical challenge), but to casually make a stick figure walk across the screen in sync with footstep audio (in BASIC) may take more than an afternoon (maybe cycling sprites and one of the audio BASIC keywords has a short duration background play).

ZSMKIT has shown how a software library extension can be made to interoperate with BASIC. For an example on doing that, seek out the Lower Decks project (or also CrazyLander, which I think uses the older ZSOUND library, but both are done from BASIC). We just don't have an example yet of MIDI (from BASIC). Some of the essentials to get started are there (like BLOAD and MCIOUT), but BASIC also doesn't have ready-made keywords for setting up background tasks (conceptually such a thing would be like an "ON TIMER 20 GOSUB 1000" (prep a background timer, and when hit near the timer, auto-GOSUB to a branch). The work around is to inject your own ISR (but then you can't easily work with BASIC variables- there are workarounds to that also), or just do your best at inline processing in a main loop.
I won't say it's IMPOSSIBLE. I got PCM from Memory playing straight from BASIC. (If you don't disqualify me for doing a SYS to memory_copy).
Post Reply