Rambling about PLAY

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
equinox
Dynamite Dan
Posts: 1032
Joined: Mon Oct 08, 2018 1:57 am
Location: SE England

Rambling about PLAY

Post by equinox »

As a kid with a Spectrum +2, I used to mess about a lot with the PLAY command, trying to produce various sound effects. Stuff like a doorbell, a chain of explosions, and (a bit trickier) helicopter rotors. I had the vague feeling that a complex enough PLAY statement should be able to synthesise speech. (I now realise that it can't, but only because PLAY can't produce infinitesimally short notes.)

Anyway, one of the "tricks" I would use in my sound effects was PLAY "1", which isn't documented in the manual but produces a very short click or pop. I'm still not sure why it worked, but I suspect it's related to this item in Ian Collier's bug list: "A volume instruction in a PLAY command attempts to output the volume to the sound chip even for channels 4 to 8 [... meaning] that PLAY "V10&" and PLAY "&","V10" both make spurious sounds.

I had a fairly strong recollection that if you used PLAY "1" or similar number-based tricks, and combined them with the ")" that causes PLAY to repeat the bar, then the program would sometimes get stuck: it would keep playing the sound forever and you couldn't stop it by pressing Break. You had to reset the machine.

I was messing about today trying to reproduce this, without success, and can only conclude that I should have held down Break, instead of tapping it. PLAY only lets you break between notes, so if you are inadvertently playing a very long note (N9) then you have to hit Break at just the right time (or hold it down) in order to get back to BASIC. I suppose I was bashing away at the key at the wrong moments and assumed the machine had hung.

I don't have a question or anything. This was just a bit of nostalgia :)

Oh yes, the other good trick (also exploiting a bug) was to do something like PLAY "V15aV15aV15a" which would give you a nice smooth long tone instead of a triple bip-bip-bip. I don't think that's otherwise possible in BASIC.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Rambling about PLAY

Post by djnzx48 »

Interesting. I was reading about the PLAY command a while ago, and there's a seemingly little-known feature where it's not just limited to playing three channels, but can actually play up to eight (IIRC). This was intended for hardware MIDI devices and the like, but if you use certain commands in the strings, you can have multiple channels playing to the same AY channel.

Now one way I thought of abusing this was to use six PLAY channels, two mapped to each AY channel. At the start of one triplet of channels there would be a very short pause, to offset the following notes. Then the two channel triplets would play interleaved with each other, resulting in twice the normal playback rate. I can't remember the exact details, but the calculations I did showed that at the highest tempo, it would then be possible to play 48Hz AY tunes in BASIC. I did think of writing a PT3 to PLAY command converter, but it seemed like too much work and the resulting tune would take up a huge amount of memory, so I never got around to starting it.

About PLAY being used to synthesize speech, I think it could technically be possible. There are WAV to 50Hz AY converters using frequency analysis that produce vaguely recognisable words. If you've ever seen one of those videos where they get MIDI pianos to 'sing', it's the same principle behind that. There was also an approach using the built-in envelopes and format synthesis here. If you're thinking of PCM sampled speech however, I don't think there's any way to do that just with PLAY.
equinox
Dynamite Dan
Posts: 1032
Joined: Mon Oct 08, 2018 1:57 am
Location: SE England

Re: Rambling about PLAY

Post by equinox »

Thanks for your reply dj.

I think it would be very interesting to hook up a Speccy to a MIDI instrument and see how well it actually works. I don't know why they bothered trying to make the Spectrum send MIDI instructions but I would assume it's because they felt they had to compete with the Atari. (Did the CPC do this too? And the pre-Amstrad 128? Was it Sugar or Sinclair?)

So anyway I think 8-channel is supported but only for MIDI reasons. And if you look at Ian Collier's bug list, the MIDI was so fundamentally broken (like a key-up on one channel would send to all channels, so stopping a single note would stop all the notes -- OH! THANKS!) ... that I don't suppose it was ever meaningfully used. But this opens a really interesting can of worms, like what CAN we do with it? Have emulator authors hooked up a keyboard or guitar to test the MIDI output? (And you say "if you use certain commands in the strings, you can have multiple channels playing to the same AY channel"-- which needs more exploration too.)

> About PLAY being used to synthesize speech, I think it could technically be possible. There are WAV to 50Hz AY converters using frequency analysis that produce vaguely recognisable words.

Hmm, you mean that the minimum duration of a note doesn't matter because you could offset every note by a tiny degree? I don't quite get it.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Rambling about PLAY

Post by djnzx48 »

equinox wrote: Fri Apr 19, 2019 8:52 am I think it would be very interesting to hook up a Speccy to a MIDI instrument and see how well it actually works. I don't know why they bothered trying to make the Spectrum send MIDI instructions but I would assume it's because they felt they had to compete with the Atari. (Did the CPC do this too? And the pre-Amstrad 128? Was it Sugar or Sinclair?)
Yeah, it would be quite interesting to see. One of the magazines (I think Sinclair User) mentioned that one of the demos Sinclair used to show off the Spanish 128 had it hooked up to a Casio synth through MIDI, and playing music loaded from the RAMdisk. It's a shame MIDI input doesn't work, but I think it's fundamentally a software problem rather than a hardware problem: monitoring for MIDI events would take up too much CPU time to be able to do anything else. I guess other computers had dedicated hardware to handle this?

Are there any games that use MIDI for sound? For DOS games in the '90s, MIDI music was basically the standard and many games supported external MIDI devices. It would be pretty cool to have multi-channel MIDI music playing in tandem with the regular 3-channel AY. The built-in ROM implementation seems quite limited and buggy, but would new MIDI drivers allow for more advanced capabilities?
equinox wrote: Fri Apr 19, 2019 8:52 am Hmm, you mean that the minimum duration of a note doesn't matter because you could offset every note by a tiny degree? I don't quite get it.
Ah, well basically an audio waveform can be modelled as the result of adding together sine waves of different frequencies and amplitudes. What these converters do is analyse the waveform using FFT and pick out a number of the strongest frequency waves, three of them in the case of the AY. If this process is repeated every 50th of a second then these selected waves make a reasonable approximation to the original waveform. The AY however cannot (easily) play sine waves, so square waves must be used instead.

Using this technique, you don't need arbitrarily short notes because the waveform is being continuously generated. The advantage is low CPU usage compared to PCM samples, and theoretically these converted waves could be played back using the BASIC PLAY command. I don't think any of the converters make use of the envelope, so there may be extra potential with that also.
equinox
Dynamite Dan
Posts: 1032
Joined: Mon Oct 08, 2018 1:57 am
Location: SE England

Re: Rambling about PLAY

Post by equinox »

Well, speech add-ons for the Speccy were a toy, because having voice coming out of your computer was very strange and interesting in the 80s. I also had a BBC Micro with the Superior Software "!SPEECH" ROM and it sounded like a crazy robot but it was definitely recognisable speech (and actually a lot better than Currah on a Speccy) so -- yeah.

And: sure, I understand that you can make complex waveforms like speech, by adding smaller ones, but I don't think you can do it with PLAY :D If you are able to do this with little offsets (maybe generating the BASIC from external code) then the whole WoS forum will stand open-mouthed. (And please do: that would be another little amazing Speccy trick.) But we know it's not easy.

So let me take this a bit further -- I am very surprised that we have been writing emulators for decades but nobody has checked the MIDI support. I think there are two issues here: (i) maybe emulator programmers don't own MIDI kit and cables, (ii) maybe the entire Speccy implementation of MIDI is broken and shitty.

How much does it actually cost to pick up a basic 80s synth that supports whatever MIDI was supposed to do in the 80s. It doesn't really matter but I feel like we should emulate this stuff properly. In fact it seems like a strange omission whereas people have spent any amount of time trying to count border cycles or whatever. Maybe MIDI is easy and you just have to send certain notes at certain times? But has anyone listened to it?
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Rambling about PLAY

Post by djnzx48 »

Nevermind, it didn't work. Looks like AY channels on PLAY command are hardcoded. Oh well.

Anyway, I could only find a single emulator with MIDI support: https://bitbucket.org/jiri_svoboda/gzx/overview. There are no prebuilt binaries though so I haven't tried it yet.

And I also found this: https://www.youtube.com/watch?v=flGrT2Hf6do
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Rambling about PLAY

Post by djnzx48 »

Not much interest in this?

Well, I managed to get gzx built on Windows and it mostly works, except for the MIDI output. Strangely, only one out of three MIDI buffers get submitted successfully with midiStreamOut, while the other two fail with the error:

The current MIDI Mapper setup refers to a MIDI device that is not installed on the system. Use MIDI Mapper to edit the setup.

Then as soon as any MIDI data gets sent, it quickly runs out of available buffers and playback fails. No idea what's causing all the errors but I'll make another attempt at getting it to work.
User avatar
ZXDunny
Manic Miner
Posts: 498
Joined: Tue Nov 14, 2017 3:45 pm

Re: Rambling about PLAY

Post by ZXDunny »

BASin does MIDI from the PLAY command. So does ZXSpin, iirc.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Rambling about PLAY

Post by djnzx48 »

Thanks, I had forgotten that option was there. Got it working now.
Post Reply