mailto: blog -at- heyrick -dot- eu

Not interested in MIDI for RISC OS? Skip to the other stuff.

USB-MIDI v0.08

If you came here from MidiMon's GitHub page,
please note that this release is not the latest.
Look at the "Geekery" section of the column on
the right (in desktop mode) and you'll find a
link to the latest versions of things.

Impatient? Skip to the downloads...

At the end of the week, somebody called Lauren <waves> prompted me with some questions about my replacement MIDI module. One of them was the thing that gives the amount of free space in the receive buffer was incorrectly returning the size of the receive buffer.

A simple fix, right?

Then I had the crazy idea of updating the user guide, dating from the 17th of March 2015 (pre-Brexit!), to match what the module does now. Along the way, I found myself a decent OCRed copy of the original Acorn MIDI guide and then went to compare the two implementations.

Changing one line of code suddenly took several hours. Hmmm...

However, I wish to present you the new shiny version 0.08 of my MIDI module. It is for RISC OS 5 only (it uses some HAL features) and allows any compliant USB MIDI device to be connected. Many modern keyboards support USB directly. For older keyboards, you can get a USB to serial adaptor, but do please note that many of these are awful.

MIDI module source code
MIDI module source code.


This module provides an emulation of the original Acorn MIDI module API, so it will "just work" with software written with the understanding of that. For example Maestro supplied with RISC OS, and the infinitely superior Rhapsody4 that you can now download.
· It has not been tested with the Liquid Silicon/ESP MIDI system as I have never seen this.

There are obviously implementation differences. For instance, there is no attempt to provide a MIDI synth. Given the limitations of the Archimedes sound system (only eight channels, only nine voices none of which bore any resemblence to actual instruments) I can't imagine that it sounded particularly good...
I suspect, also, the beat/bar timing is messed up. As far as I'm aware, nothing actually uses this as the Fast Clock mode provides timing to millisecond accuracy. This part works, obviously. ☺
Also, there's no concept of Running Status as USB works with distinct four byte packets (a type byte followed by 1-3 bytes of the original MIDI data). For this reason, it's not possible to put bytes on the wire directly. The module will assemble bytes received until they are a valid command, and then it'll be send. But, really, better to just send entire commands.
Also, MIDI ports can come and go (USB supports hotplugging) and don't have to be contiguous. This means some assumptions that worked in 1987 might fail today. That being said, it has been tested by playing songs in Maestro, and working in both directions (playing complex pieces and transcribing played music) with Rhapsody4.


What has changed in this release

  • The original bug fix, MIDI_InqBufferSize now returns the free bytes in the receive buffer rather than the buffer size.
  • Fixed MIDI_SetChannel to be able to support all four ports.
    The original Acorn API says: "If the selected MIDI port is not installed, then it is undefined which port this and other SWIs will use instead. Use MIDI_Init to find the maximum port number installed, and never exceed it."
    Since we are using USB and it could be possible to have port 1 and no port 0, and since leaving the status "undefined" is a lazy and dumb thing to do, this module simply verifies the chosen MIDI device is available and if not, then the currently selected MIDI channel will not be changed.
  • Note: The default initial setting is 0 (channel 1 on port 0) even if no MIDI devices are connected. This is because the channel needs to be set to something.
  • Added MIDI_Remap SWI. This doesn't do anything (it will return an error) but is added to allow for future functionality with port mapping to not be a part of the MIDI_Options SWI, which should be for controlling the behaviour of the module as a whole.
  • Changed MIDI_Options as follows:
      R0 = If 0, then use the millisecond timer (if available)
           If 1, then fake millisecond timing using the 100Hz ticker        
      R1 = Bitmap of which ports to apply delay to for cheap serial adaptors.
           Bit 0 = port 0 ... bit 3 = port 3.
      In both cases, using -1 on entry will report the current setting without
      changing anything.
  • MIDI_InqSongPositionPointer now also sets bit 3 if in Fast Clock mode; and now correctly flags whether we're using internal timing (Fast Clock mode) or external timing (not Fast Clock, with MIDI clock events and the clock active - following MIDI_Start).
  • Fixed MIDI_TxCommand to return the number of scheduler slots available.
  • MIDI_Init bit 3 can now be used to purge the scheduler buffer.
  • Changed MIDI_USBInfo to be more useful.
  • Added some things to *MidiUSBDebug.
  • Updated user guide for current version of MIDI module, so it isn't a mere eight years out of date.


Known bugs

  • Using MIDI_Init bit 3 does indeed clear the schedule buffer, but one note may sound as it is stored elsewhere for comparison against the millisecond ticker, it's no longer technically in the buffer. ☺
  • The non-FastClock timing is wrong. It looks like we might need to multiply the MIDI clock ticks by six, and then use the Sound_QBeat SWI to work out break this into bars and microbeats, with bars incrementing in the upper 16 bits and microbeats in the lower 16 bits.
    The module currently just returns the number of clock ticks received. Does anybody or anything actually use the beat timing?

The first is a bug that'll need fixing. The second, I don't plan to spend time fixing it if nothing uses it. Certainly, it lacks accuracy. Given that Dave Higton made his much needed modifications because some people were saying that using the centisecond ticker (100 ticks per second!) just wasn't accurate enough!



Download the module (v0.08) (zip, 138.98KiB)
Includes a 26 page user guide PDF which can be use as a basic reference if you're not familiar with how the Acorn original worked.

Download the source code (zip, 93.25KiB)
Builds with the DDE, unpack and then double-click on the "Mk" file.

Download the user guide originals (zip, 32.22KiB)
The user guide is an OvationPro file (find it on !Store), and has a bookmarks file for use with Stephen Fryatt's PrintPDF.

The module is licenced EUPL v1.1 only. A PDF copy of this licence is supplied. If you've never heard of the EUPL, it's like the GPL only without the nutty politics and the unpleasant viral behaviour.


Installing MIDI

Place the file in $.!Boot.Resources.!System.500.Modules.

If a software package doesn't automatically load MIDI, take a look in the app's !Run file. You may see a line like:

RMEnsure MIDI 3.14 RMLoad System:Modules.MIDI314

Change this to:

RMEnsure MIDI 0.08 RMLoad System:Modules.MIDI

If you can't see that line at all (like Maestro), then add the second line (MIDI 0.08) above somewhere before the WimpSlot line.

Uninstalling MIDI just requires you to find that MIDI file and delete it. You shouldn't need to undo any of the !Run changes as those only load the module if it isn't loaded.


MIDI hardware not working?

The user guide gives a very simple command that should play a middle C:
*MIDIUSBSend 144 60 80

If you don't hear anything, then check to see that the device is being recognised by the USB system.

No. Bus Dev Class Description
  1   1   1  9/ 0 Synopsys DWC OTG root hub
  2   1   2  9/ 0 SMSC USB 2.0 4-Port Hub
  3   1   3  9/ 0 SMSC USB 2.0 4-Port Hub
  4   1   4  0/ 0 USB USB Keykoard
  5   1   5  0/ 0 Logitech USB-PS/2 Optical Mouse
  6   1   6 FF/ 0 SMSC LAN7800 USB 3.1 gigabit ethernet device
  8   1   8  0/ 0 Prolific Technology Inc. USB-Serial Controller
 18   1   7  0/ 0 QinHeng Electronics USB2.0-MIDI

Here, we can see the MIDI adaptor (a cheap crappy one) is appearing as device 18.

Unfortunately it seems that some USB commands are bugged and don't correctly return the class/subclass information. So we can't use *USBDevInfo. But there is a call that works, USBConfInfo, to which you give the USB device number (in my case, 18).

*USBConfInfo 18
Current config  : 1
# of interfaces : 2
Config value    : 1
Name            : ''
Attributes      : Bus powered
Maximum power   : 96mA

Interface 0.0 class 1.1:0 ''

Interface 1.0 class 1.3:0 ''
Endpoint 2, Bulk OUT, 32 bytes 0 frames
Endpoint 2, Bulk IN, 32 bytes 0 frames

The second entry, with the endpoints, is the one we are interested in. It needs to say class 1.3, which means audio class (1), midi streaming subclass (3).
If your device says something different (some devices use non-standard MIDI-like interfaces), then it won't work.

If all looks good, you can ask the MIDI module if it has picked up on the device:

MIDI USB information:
  Timestamp type is Fast Clock
  MIDI Clock is 0 (Song Position = 0)
  Current TX channel is 0 (real channel 1 on real port 0).

Information for port 0:
  Current MIDI device is USB18, VID 1A86, PID 752D,
  using IN file handle 254,
  and OUT file handle 243.

If your device shows up here, it's potentially a problem with it being selective regarding which MIDI channels it chooses to respond to. Sending data to channel 1 should work, but some (older) devices are... weird.

If, for some reason, your device shows up on a different port, then you can repeat the test command specifying a different port, say port 2.

*MIDIUSBSend P2 144 60 80

If you should need more detailled information on the device, there's another command available.

MIDI USB debug information:
  RealTime -> RX?  No
  RealTime ignore? No
  Ticker in use    Yes
  RX interval      2 cs
  Clock stopped?   No
  Timer1           57394659, 1 (2), 3, 1000000; 1

  Device #0:
    Is valid?        Yes
    USB device       "USB18"
    Manufacturer     "USB2.0-MIDI"
    Product          ""
    Lasterror flag   65
    IN file handle   254
    OUT file handle  243
    RX virt buffer   &2017E334 (2048 bytes)
    RX buffer head   0
    RX buffer tail   0
    RX byte offset   0
    Periodic RX?     Yes
    TX virt buffer   &20003F74 (8 bytes)
    TX buffer ptr    0
    Ignoring clock?  No
    RX ActiveSense?  No
    TX ActiveSense?  No

  Device #1:
    Is valid?        No
      ** device not valid, no data to report **

  Device #2:
    Is valid?        No
      ** device not valid, no data to report **

  Device #3:
    Is valid?        No
      ** device not valid, no data to report **


It's a trickier question if your device is not sending notes to the computer. This is frequently because different keyboards use different channels to send back data and if you're having problems with, say, Rhapsody4, it's probably a question of configuration.
Try setting the end of !Rhapsody4.MidiSetup.!Setup to say:

#1/1-9,11-16            :"GM"
#1/10                   :"GMDrumKit"
#2/1-9,11-16            :"GM"
#2/10                   :"GMDrumKit"
#3/1-9,11-16            :"GM"
#3/10                   :"GMDrumKit"
#4/1-9,11-16            :"GM"
#4/10                   :"GMDrumKit"

This should have all four ports be recognised as a standard MIDI device. Once you have something that works, you can start to narrow it down to the specifics of your particular keyboard, but you're on your own here. I've still not figured out how to set it up for my many XGLite voices.


Reading device information

The MIDI_USBInfo SWI has been expanded to provide much more useful information.

Entry:  R0 = 0

Exit:   R0 = Number of devices connected
        R1 = Bitmap of available devices (bit 0 = port 0, etc).
        R2-R7 are corrupted.
Actually, R2-R7 provide internal information. This is for testing/development so it will remain "undocumented" as what is here may be changed in a future version. Refer to the source (c.midi, function midi_usbinfo() (towards the end)) if you need to know.

Entry:  R0 = 1 - 4 (corresponding to ports 0-3)

Exit:   R0 = Bytes unread in RX buffer.
        R1 = Pointer to USB device ID (string)
        R2 = Pointer to product name string
        R3 = Pointer to RX buffer
        R4 = RX buffer head pointer (offset)
        R5 = RX buffer tail pointer (offset)
        R6 = Non-zero if hardware needs delays
        R7 = Pointer to device descriptor block

This means, as described in the user guide, that you can read the USB device name and product name as follows:

>SYS "MIDI_USBInfo", 1 TO , id$, product$
>PRINT id$, product$
USB15     USB2.0-MIDI


ESP32Cam order

I've packaged up my faulty ESP32Cam to return it to Amazon. If you didn't see the comment I left on the previous blog entry, Amazon rejected my one star review. Funny, that...
As I was looking through Amazon, it looked like the vast majority were the same cheap Chinese board at all sorts of price points.
The one that I'm sending back cost a little over €12.

If I'm going to get cheap Chinese crap, I really ought to pop over to a place that specialises in such things. So, here, my first AliExpress order.

Just a few
Just a few.

It was the cheapest one I could find. I ordered three, with the expectation that I ought to get two that work. And the price? About average for one on Amazon.
I would have liked a proper AI-Thinker board, but Amazon and Google were no help there. The world is flooded with these cheap modules. So, if it's going to be cheap crap I expect to pay a cheap crap price. This is practically a Happy Meal for each, and less than a decent takeaway meal for the lot (including postage).

A big problem I had with AliExpress is that vendors would bundle up all sorts of different options, and Ali would list the cheapest one on the horrible (loud and messy) results page. So you go thinking "€0,81 for an ESP32?" and when you tap on it, it's almost as expensive as Amazon because the thing that's €0,81 is a WiFi antenna.
It makes trying to compare prices of things a time consuming affair. Now, Ali might be "excellent! he spent twenty five minutes on our site", but the truth is more "bloody hell, that took nearly half an hour, I probably won't go back...".

It's a bit like Playmobil, really. I would like to order stuff directly from them (it's not as if it's cheaper with Amazon), but their insistence on using UPS and to not leave parcels, UPS who run their own pickup point network which is different to the local supermarket that everybody else uses, it turns a simple thing into a hassle. So, just, no.

So, to Ali. Ordering from them wasn't difficult. I used a virtual credit card and it worked without problems. They gave prices in euros, it was all quite simple. But the searching was a mess, the results cluttered like one of those awful AOL sites from way back when (when people tried using every font and colour they had at the same time), and pretty much everybody bundled different things together so they could promote a dirt cheap price instead of the actual price that the thing cost. It made me need to immediately go and drink a cup of tea in order to calm my brain.
Also, the suggestions were completely unrelated to the article in question. I really do not need to know that there's actually a device that a guy can shove up his arse to stimulate his prostate. And let's not even talk about that thing for women that is shaped to fit into both holes. In fact, I think I feel another cup of tea coming on. And maybe a dose of mind-bleach. "Ewww!" somehow just doesn't cover it.



I didn't spend all afternoon doing that. I also planted a raspberry in the bin where a vine (long since died) used to live. Because of ivy and weeds, I turfed out the dirt and replaced it with something claiming to favour growth. It looks like finely ground pine trees to me.

Raspberry, type "Marastar".

I noticed that the Sakura is finally waking up. Lots of buds, looking forward to seeing this in flower again.

Cherry buds
Cherry buds.

Now to throw some tea down the hatch, and finally go do something with those logs...



Your comments:

Please note that while I check this page every so often, I am not able to control what users write; therefore I disclaim all liability for unpleasant and/or infringing and/or defamatory material. Undesired content will be removed as soon as it is noticed. By leaving a comment, you agree not to post material that is illegal or in bad taste, and you should be aware that the time and your IP address are both recorded, should it be necessary to find out who you are. Oh, and don't bother trying to inline HTML. I'm not that stupid! ☺ ADDING COMMENTS DOES NOT WORK IF READING TRANSLATED VERSIONS.
You can now follow comment additions with the comment RSS feed. This is distinct from the b.log RSS feed, so you can subscribe to one or both as you wish.

J.G.Harston, 19th March 2023, 23:18
I've always been puzzled by people who see "computer MIDI" and expect it to be the *computer* playing music. No! The whole point is for the computer to *send* to purpose-built music-making machines, not for the computer itself to make the music. I wrote MIDI intercept code to translate SOUND commands into *outgoing* MIDI commands, not to convert incoming MIDI into SOUND commands. 
If my computer had the capability of playing high quality synth music, why on earth would I be buying a synth to plug into it in the first place? Just use the computer itself. The whole point of MIDI-IN is to *record* data from synths to edit and play back later, not for the computer to respond to that data. The computer does the thing the computer is best at, the synth does the thing the synth is best at. 
Rick, 19th March 2023, 23:44
I can understand the computer playing the MIDI as a rough and ready check in the absence of an actual synth connected (I used the software synth with MuseScore on the PC) but I really don't see how the original Acorn player could have been [much|any] use at all... 
Maybe these days software can put in a passable attempt, but not back in '87. 
Anon, 20th March 2023, 11:52
On the other hand, software MIDI synths these days are pretty fantastic. If you've ever had a play around with Logic Pro on the Mac you'll know what I mean. 
Modern versions of Cubase on the PC are also fairly mind-blowing (or at least the software VST modules that generate the sounds). You no longer need a rack full of synths, the whole thing (and effects) can be done 'in the box'. 
And yes, you can play 'live' into Logic or Cubase from a connected MIDI keyboard. 
(As an aside - Rick, it looks like you've forgotten to close a tag in one of the code blocks somewhere further up, everything in the last third of the page is appearing in monospaced font!)
Rick, 20th March 2023, 15:18
Thanks for pointing out the rendering issue, fixed. 
Yes, software synthesis these days is pretty good (re. SoundFont). Perhaps the only thing against using a PC is that it's doing loads of other things at the same time, so one can't guarantee the latency. 
That being said, there's enough processing power and maths engines to do all that hardware did in the eighties. 
I wouldn't be surprised if my keyboard wasn't just a microcontroller running firmware to play the right samples at the right time... 
The advances in MIDI synthesis isn't so much the processing power, it's using better and better audio generation. No longer making noises that "sound like a grand piano", they can play recordings of actual pianos.
Anon, 20th March 2023, 19:54
That's exactly what Logic's piano VST does. (And Cubase.) 16 samples per key, at different velocities, for each of the 88 keys. With some DSP to deal with things like the sustain pedal. 
What Logic / Cubase didn't do on the versions I used a while back (but might well do on a newer version), but my digital piano does, is something called 'damper resonance simulation'. 
On my digital piano, when I press down the right hand (sustain) pedal with no keys pressed, you can actually hear the 'dampers' lifting off the strings. All done digitally of course. And if you gently press down middle C (not hard enough to make a noise), keep it held, then belt the bottom C key for a second or so, when you let go of the bottom C you'll hear middle C ringing. Just like on an acoustic piano. It simulates this for every string, so when you're playing 'legato' (with the sustain pedal down) you get all the rich harmonics and resonances that you would on an acoustic piano. It also has fully weighted keys, with graduated weighting (so the lower pitch keys give more resistance) and the same bounce-back you get on an acoustic. 
Yet it's digital, it never goes out of tune, ever. 
Gotta love technology.
Rick, 20th March 2023, 21:14
I think my keyboard uses the same sample for groups of keys (6-8?) and just fiddles it digitally for the actual key pressed. There's nothing fancy like hearing the damper, the keys aren't weighted, and I don't think the touch sensitivity is fine enough to press a key gently enough that it won't sound. I've not tried the C keys thing. 
But, hey, my keyboard cost like €200 so it's not going to be quite like the more expensive models. 
Rick, 20th March 2023, 21:17
As for tuning... that's the big question. What do you set A4 to? 440Hz? 444Hz? Something else?
Anon, 20th March 2023, 22:10
I have a "cheap" Yamaha PSR-282 that I use for gigs, 5-octave (61-key), doesn't have the damper resonance simulation but does have multiple samples per key. 
By default A=440Hz, but you can tune it manually. Doing this just alters the sample playback frequency. It'll go (I think) up to 2 semitones each way. 
Looks like the PSR-282 sold for around £399 back in 2001. I got mine off eBay for £40, complete with a stand and 'wall wart' power brick (although not an official Yamaha one, works fine though). 
The digital piano is a Casio Celviano AP-250, bought new in November 2014. List price was £699, got it for £349 in the pre-Xmas sale at the local music shop. 
Bargain of the week though - my friend's kid is learning piano, so she wanted to get him a keyboard for his birthday. I saw a Casio CTK-611 in the charity shop and grabbed it. Full size keys, stand, genuine Casio wall-wart, it has touch response, General MIDI, and they wanted £30 for it. 
Bargain of the millenium though - a few years ago I picked up a Casio CT-680 off eBay for £7. Again came complete with stand and PSU. This is the full-size key version of the 'legendary' Casio MT-750, which has ROM samples of just about every cheesy synth sound that ever existed in the 1980s. (I think Casio were cheeky enough to buy a Yamaha DX7 and use it to create the samples.) 
No touch response on the '680, but it does have digital reverb and pitch bend. You can get away without touch response if you're using it for 'synth' type sounds, only a handful of the 80s synths had touch response anyway (the DX7 did, the DX9 didn't, the Casio CZ-101, 1000 and 3000 didn't, the CZ-1 and CZ-5000 did, etc etc).
David Pilling, 22nd March 2023, 14:57
I agree with the Ali comments. It can be impossible to find the lowest price - post the changes in shipping rules, carriage cost is another obstacle. 
I have no complaints, I bought a lot of stuff over the years, mostly with a real credit card. 
Knowing when your right to complain about non-delivery runs out is important. 
It is often cheaper to place a number of small orders rather than one big order.
Rick, 22nd March 2023, 15:25
It looks to me like one can complain in a 14 day period after a delay of 90 days from posting (at which point they'll assume the delivery has arrived). Is this correct? 
David Pilling, 23rd March 2023, 15:46
Rick - for my part I don't know. They may have changed things. My experience was a fixed time of say 90 days, and you had to complain within that time. Now I never wanted to complain too early, because some vendors took a long time to supply stuff. But equally didn't want to end up on the wrong side of the 90 days. 
Ali keep vendors on a short leash. It always seemed one had to confirm receipt before the money was released to the vendor. I'd rate Ali vendors more reliable than ebay. 
Modern Ali, often buying from multiple suppliers and everything would come in one parcel - because at some point they bulk things up to reduce costs. PayPal is also cost effective now - used to be a charge for using it on Ali. 
Not that worried about the Ali time out because you can always claim on credit card insurance. 
Ali is mix of types, some are small scale, others serious businesses. Some will be super fast delivery, others push the limits to keep costs low. 
There is a mechanism where the seller can extend the delivery time, and I saw that operate a few times. 
David Pilling, 23rd March 2023, 21:41
Another thing about modern day Ali, is that most things are "recorded delivery", almost always I get an email saying XXX has been delivered please confirm. 
Signing for stuff as with old time recorded delivery has mostly vanished because Covid. 
They tend to take a photo of the parcel on the doorstep. An amusing incident, recently a neighbours parcel was delivered here by error - he knew where it was because he recognised the doorstep. Alas I was not in. 
Rob, 24th March 2023, 17:12
I've had a few things from Ali lately. They have definitely sped up - they all arrived within about three weeks, and they used some UK aggregation service that drops then into Royal Mail tracked. I then get an email from Ali saying its been delivered. But I agree about the difficulty in searching - the loading up the listing with a cheap item is a pain in the neck. More so than with eBay, as they at least show the range of prices of the options. 
Another thing to watch is Ali now add VAT on top, which doesn't feature in the listings, so it is a bit of a surprise at checkout, but that's the law now, and at least it stops unexpected customs charges (and the handling charge that can exceed them.)

Add a comment (v0.11) [help?] . . . try the comment feed!
Your name
Your email (optional)
Validation Are you real? Please type 04824 backwards.
Your comment
French flagSpanish flagJapanese flag
«   March 2023   »

(Felicity? Marte? Find out!)

Last 5 entries

List all b.log entries

Return to the site index



Search Rick's b.log!

PS: Don't try to be clever.
It's a simple substring match.


Last read at 22:48 on 2024/07/22.

QR code

Valid HTML 4.01 Transitional
Valid CSS
Valid RSS 2.0


© 2023 Rick Murray
This web page is licenced for your personal, private, non-commercial use only. No automated processing by advertising systems is permitted.
RIPA notice: No consent is given for interception of page transmission.


Have you noticed the watermarks on pictures?
Next entry - 2023/03/25
Return to top of page