Sounding Off

I was interested to read in the November '99 (p10) edition that sound was available on the Q40. I'd been quite interested in the possibility of sound on a QDOS platform ever since I wrote qvm (quintessential voice mail) in the mid-90s; it always seemed unfortunate that while the Linux and AmigaOS versions of this software could play back recorded voice messages through the systems' sound facilities, this was not available to QDOS users. This would now appear to be possible.

This article describes the current state of the qss-sox and soundtoy packages for manipulating and playing of a variety of sound formats on Q40.

Working it all out

Finding information on the Q40 sound scheme was the usual QDOS frustration saga that every time has me vowing 'never again'; I finally discovered a sound player on the Q40 web site http://www.q40.deThe readme file in qsplayer.zip contained enough information to discover that the format is stereo, 20kHz unsigned byte; the archive contained an example sound file, and the Linux command

play -tub -r20000 -c2 handel.ub

resulted in about seven seconds of the Royal Fireworks music to confirm the theory. I'll use the abbreviation QSS-UB to refer to this format.

Unsigned byte is quite a common sound format, however the sampling frequency is not, which proved to be rather an unfortunate (and strange) design decision, the vast majority of the world's sampled sound files are at the de-facto standard rate of 44.1kHz or fractions thereof. While this might seem like a petty point, in order to maintain quality when converting a 44.1kHz sound file to QSS-UB, the re-sampling required is quite CPU intensive and may even preclude real time conversion and playback of some high quality WAV files.

It is most likely that the majority of sound samples that users wish to play on Q40 will have originated as WAV (the sound file format of MS Windows and a de-facto standard everywhere) files. There is no such thing as a standard WAV file, as WAV is an extremely flexible wrapper for many types and formats of sound data, from concise 4 bit, mono samples through 16 bit stereo and beyond. This makes converting QSS-UB files to WAV very simple; just write the correct header as the data is an acceptable WAV format, while the converse is rather complex, as the data has to be converted (unless it is already an 8 bit, unsigned byte), and re-sampled to 20kHz.

In fact conversion to WAV is as simple as (less than) 10 lines of perl.

die "ub2wav ubfile wavfile ".$! unless $#ARGV == 1;
open(IN, shift) || die "input file ".$!;
open(OUT, ">".shift) || die "output file ".$!;
$siz = (stat(IN))[7];
$wavhdr = pack ("A4VA4A4VvvVVvvA4V",'RIFF', $siz+36, 'WAVE', 'fmt', 	16, 1, 2, 20000, 40000, 2, 8, 'data', $siz);
print OUT $wavhdr;
print OUT while(<IN>);
close IN;
close OUT;

ex perl;"ub2wav_pl sounds_ub sounds.wav"

    

You could do the same in BASIC in only a few more lines, no doubt.

In order to do the opposite trick, WAV to QSS-UB, it has been necessary to port a well known open source sound conversion program 'sox' (SOund eXchange) to QDOS. This program is capable of converting between almost all sound formats known to man, certainly popular formats for other computers (Unix, Windows, DOS, Amiga, Mac and Atari) are all provided, and as the the QSS-UB format is unsigned byte, then this is available by default. It is only necessary to remember to tell sox that you require stereo at 20kHz -tub -r20000 -c2).

For example:

WAV to QSS UB

ex sox;'sound.wav -tub -r20000 -c2 sound_ub resample'
    
QSS UB to WAV

ex sox;'-tub -c2 -r20000 sound_ub sound.wav'
    
Just remember that before any QSS-UB file name to add the magic parameters '-tub -c2 -r20000'.

Converting the sound formats proved relatively easy (sox is an extremely portable program), and the relatively few patches required for QDOS probably took less than an hour to engineer. The QDOS port of sox also included the optional GSM library, making sound conversion from GSM capable voice modems (USR Sportster for example) to QSS-UB very simple.

Producing an application to facilitate the playing of sound, not just QSS-UB format (which Claus Graf's qsplayer already does) proved rather more problematic. Unfortunately (and bizarrely in my opinion) the Q40 Sound System is not implemented as a (relatively trivial) device driver, but as a number of system calls. Getting hold of the information to implement these calls was helped by Claus Graf forwarding me the unpublished sound_asm module from qsplayer, and through the good offices of Jochen Merz, I receiving from Tony Tebby a MS Word document describing the system calls. Fortunately StarOffice for Linux handles Word documents with ease! I then made the fatal mistake of believing the TT documentation in preference to Claus' code; and the brave beta testers (Dave Westbury and Roy Wood) soon had locked up Q40's to show for my efforts (which of course worked perfectly in my uqlx/Linux 'emulation' library). Dave's report soon had me in the right direction, and a quick spot of plagiarism (only call it research1) of Claus' code, version two not only worked, but also enabled Dave to discover a (small but easily made) bug in the SMSQ/E sound system implementation. Tony Tebby is aware of this and will be able to fix it very easily in a future release of SMSQ/E. Current sound playback software is able to work around this bug very easily, without any affect on the quality of output.

Having cracked the secrets of the QSS sound system, the final implementation was to produce a player that 'piped' non-QSS-UB formats through sox in order to directly play most sound formats. I'm not convinced that Q40 is powerful enough to play all contemporary sound formats; while it should cope with most formats, I expect that the overhead of 16bit, 44.1 kHz, stereo WAV will take longer to convert that the time available to play it. This is just a problem of a slow processor (actually I'm looking forward to hearing the effect of this software on a real Q40; it is possible that the perceived slowness is a result of a the uqlx emulator, but I suspect not).

What and Where

What

Where

How

qsplayer.zip

http://www.q40.de/

Plays 20kHz, stereo, UB samples. Binary only (Claus Graf).

qss-sox.zip

http://www.bigfoot.com/~jrhudson/qdos/

Universal sound converter. Binary and source. Contains some other modem sound formats software and examples.

soundtoy.zip

http://www.bigfoot.com/~jrhudson/qdos/

Universal sound player. Requires qss-sox to do the hard work. Binary and source.

The soundtoy.zip archive contains some heavily commented source code for a c68 sound library that should enable implementation in BASIC or assembler (or even the 'trivial' device driver) quite easy.

The Future

Having reached the stage where Q40 can decode and play (subject to CPU constraints) just about all common sound formats, what else might be practical? After all this ability is only what one might have expected from any competent operating system over the last ten years. The rest of the world has moved on and subjects like audio recording and mixing, mp3 recording and playback are currently topical.

The future here is perhaps less rosy; the Q40 does not include a recording facility, so any sources for mixing and manipulated would have to be created externally or via a MIDI (quanta Nov 99/p10) interface, though once samples are available, there is no reason why they cannot, given suitable software, be manipulated on Q40.

I've always been of the opinion that the c68 code generator struggles on the 'bit manipulation' type of code (i.e. that required by sound manipulation software), producing code that is significantly slower than other compilers; this will hinder any effort to port existent open source solutions for mp32 and CD ripping and encoding.

Assuming that a CDROM driver is/maybe available that allowed direct sector access, then 'ripping' tracks of CD to some non-compressed intermediate format (usually, for convenience, WAV) would be possible, however the files are enormous. Applying mp3 compression to the ripped WAV files reduces the samples down to an acceptable size; however the CPU requirements to do this in real time (i.e. keeping up with the ripping software) I would expect to be way beyond the Q40. Playing back mp3 is significantly less CPU intensive than recording, however given the anticipated problems with 16bit/44.1kHz WAV to QSS-UB, I would not be surprised if this too were beyond a Q40.

Anyway, the basic tools are available; it will be interesting to see how sound on Q40 develops. I'm rather looking forward to hearing a real Q40 at the next workshop.

© 2000 Jonathan Hudson <jrhudson@bigfoot.com>

This article may be freely copied and published in entirety provided this notice is retained.

1 With apologies to Tom Lehrer, "Lobachevsky"

2 mp3 is a format for compressed sound. It offers impressive compression and is the format of much of the digitised sound available (legally and illegally) on the internet.