// cd /blog/modsample-sound-effects

Sound effects that move: PLAY MODSAMPLE on the PicoMite and the Colour Maximite 2

MOD files have always been a good way to put music into an MMBasic game. They are small, they loop, and they have the right sound. Both the PicoMite and the Colour Maximite 2 can also play the samples inside the MOD file as sound effects while the music carries on, with PLAY MODSAMPLE. Until now, though, an effect could only be played once from its start. It couldn't be stopped, and changing its rate started it again. That was fine for a laser or a coin, but no use for the sounds that make a game feel alive: an engine whose note rises with its revs, a car that roars past, the wind.

The commands

The latest V8 betas of both the PicoMite (V8.00.00b2) and the Colour Maximite 2 (V8.00.00b4) add three things to PLAY MODSAMPLE: LOOP, CHANGE and STOP. Both betas have since been updated with a fourth, a pan. The commands are the same on both machines, so every example here runs unchanged on either.

PLAY MODSAMPLE sampleno, channel [, volume] [, samplerate] [, pan] [, LOOP]
PLAY MODSAMPLE CHANGE channel [, volume] [, samplerate] [, pan]
PLAY MODSAMPLE STOP [channel]
  • sampleno is the sample's number in the MOD file, 1 to 31.
  • channel is 1 to 4: up to four effects play at once, over the top of the music.
  • volume is 0 to 64 (the default).
  • samplerate is 150 to 500000, default 16000. It is the speed at which the sample is played, so it sets the pitch and the length together, like a tape played faster or slower: twice the rate is an octave higher and half as long. The Colour Maximite 2 has always had it; on the PicoMite it is new (every sample used to play at 16000).
  • pan is -100 (the left speaker only) to 100 (the right only). 0, the default, plays the effect in both speakers at full volume. Left to right: the pan shows it at work.
  • With LOOP the sample plays until you stop it: to the end of its own loop in the MOD file and back to the loop's start, as the MOD player plays it, or the whole sample over and over if it has no loop. LOOP can end the list after any argument from the channel on, so PLAY MODSAMPLE 5, 1, LOOP is enough.
  • CHANGE changes any of the volume, the rate and the pan of the sample playing on a channel, where it is, without starting it again. An argument that is left out or empty is kept, so PLAY MODSAMPLE CHANGE 1, , 20000 changes just the rate.
  • STOP stops one channel, or all four. The music carries on.

Effects are heard sooner, too. The MOD file is now mixed 512 samples at a time, so an effect, or a change to one, is heard 12 to 23 ms after the command where it used to take up to 93 ms. (The PicoMite's RP2040 builds always worked this way; with Bluetooth audio or a VS1053 the PicoMite keeps its bigger buffers.)

Every continuous sound follows the same pattern: start it with LOOP, change it each time round the game's main loop, and stop it when it's done. The examples below assume a MOD file is playing (PLAY MODFILE "game.mod") and use made-up sample numbers: use the ones in your own file.

On this page

  1. An engine that follows its revs
  2. A car going by: the Doppler effect
  3. Left to right: the pan
  4. A siren, a bomb, wind, a heartbeat and a helicopter
  5. Making the MOD file
  6. Good to know
  7. Getting it

An engine that follows its revs

The engine
PLAY MODSAMPLE 10, 1, 30, 8000, LOOP        ' sample 10: a looped engine, ticking over
revs = 0
DO
  k$ = INKEY$
  IF k$ = "a" THEN revs = MIN(revs + 100, 1000) ELSE revs = MAX(revs - 10, 0)
  PLAY MODSAMPLE CHANGE 1, 30 + revs / 30, 8000 + revs * 16
  PAUSE 20
LOOP UNTIL k$ = "q"
PLAY MODSAMPLE STOP 1

Hold A to rev it and let go to let it fall back. Because the rate is the speed the sample is played at, a higher rate raises the pitch and makes the engine's beat faster, which is exactly what a real engine does. Here the rate goes from 8000 to 24000, a rise of an octave and a half, and the engine gets louder as it revs.

A car going by: the Doppler effect

A car, a train or a spaceship sounds higher as it comes towards you and lower as it goes away. If it is moving away from you at away metres a second (negative while it approaches), the pitch you hear is its own pitch multiplied by c / (c + away), where c is the speed of sound, 343 m/s. Its volume falls with its distance. CHANGE can apply both every frame:

SUB CarPasses
CONST sound = 343, closest = 10, rate = 16000

' a car passes 10 m away at v m/s, from 4 s before its closest point to 4 s after it
SUB CarPasses s, v
  LOCAL t, x, d, away
  PLAY MODSAMPLE s, 1, 0, rate, LOOP        ' start it silent
  FOR t = -4 TO 4 STEP 0.02
    x = v * t : d = SQR(x * x + closest * closest)
    away = v * x / d                        ' its speed away from us
    PLAY MODSAMPLE CHANGE 1, MIN(64, 640 / d), rate * sound / (sound + away)
    PAUSE 20
  NEXT
  PLAY MODSAMPLE STOP 1
END SUB

CarPasses 10, 40 sends sample 10 past at 40 m/s (144 km/h). Its pitch starts about 13% high, drops through its own pitch as it passes and ends about 10% low, while its volume swells to full at the closest point and dies away. It's the classic "neeeee-owwww". In a game, work away and d out from the positions each frame instead: with the player at (px, py) and the car at (cx, cy) moving at (vx, vy) metres a second,

dx = cx - px : dy = cy - py : d = SQR(dx * dx + dy * dy)
away = (dx * vx + dy * vy) / d

A sound with a steady pitch, such as an engine or a horn, makes the effect easiest to hear.

Left to right: the pan

The updated betas of both firmwares add one more argument, a pan, which places an effect between the speakers. It goes after the rate, and CHANGE can move it while the effect plays:

PLAY MODSAMPLE sampleno, channel [, volume] [, samplerate] [, pan] [, LOOP]
PLAY MODSAMPLE CHANGE channel [, volume] [, samplerate] [, pan]

pan is -100 for the left speaker only and 100 for the right only. 0, the default, plays the effect in both speakers at full volume, just as before. In between, the far speaker is turned down in proportion: at -50 the effect plays at full volume on the left and at half volume on the right. As with the volume and the rate, an empty argument to CHANGE keeps the pan the effect has.

Left, centre and right
PLAY MODSAMPLE 10, 1, 40, 16000, -100, LOOP    ' the engine, on the left
PAUSE 2000
PLAY MODSAMPLE CHANGE 1, , , 0                 ' in the centre
PAUSE 2000
PLAY MODSAMPLE CHANGE 1, , , 100               ' on the right
PAUSE 2000
PLAY MODSAMPLE STOP 1

CHANGE moves the engine without starting it again, so it carries on without a break as it goes from one side to the other.

The car crosses over

Give CarPasses a direction, and the car goes from one speaker to the other as it passes, while its pitch drops. It uses the constants above:

SUB CarPasses, panned
' dir is 1 for left to right, -1 for right to left
SUB CarPasses s, v, dir
  LOCAL t, x, d, away
  PLAY MODSAMPLE s, 1, 0, rate, -100 * dir, LOOP   ' silent, on the side it comes from
  FOR t = -4 TO 4 STEP 0.02
    x = v * t : d = SQR(x * x + closest * closest)
    away = v * x / d                               ' its speed away from us
    PLAY MODSAMPLE CHANGE 1, MIN(64, 640 / d), rate * sound / (sound + away), 100 * dir * x / d
    PAUSE 20
  NEXT
  PLAY MODSAMPLE STOP 1
END SUB

x / d says which way the sound is coming from: close to -1 while the car is far off on one side, 0 as it passes in front of you and close to 1 once it is far off on the other. So the car stays on its side while it is far away and sweeps across quickly as it goes by, just when its pitch drops. CarPasses 10, 40, 1 sends it from left to right, and CarPasses 10, 40, -1 back again. In a game, 100 * dx / d, from the positions above, does the same for anything on the screen.

The PicoMite's repository has a demo of all this, doppler, with the MOD file it needs. Over a short tune it moves an engine between the speakers, then sends the engine and a car horn past at 20, 40 and 60 m/s, in each direction in turn.

A siren, a bomb, wind, a heartbeat and a helicopter

A siren

The siren
PLAY MODSAMPLE 11, 2, 48, 16000, LOOP       ' a looped tone
FOR i = 0 TO 499                            ' ten seconds
  PLAY MODSAMPLE CHANGE 2, , 16000 + 4000 * SIN(i / 20)
  PAUSE 20
NEXT
PLAY MODSAMPLE STOP 2

The rate swings 25% either side of 16000 and back every two and a half seconds: a police car's wail. Put the siren through CarPasses' Doppler shift as well and the police car can drive by.

A falling bomb

The falling bomb
PLAY MODSAMPLE 11, 3, 40, 30000, LOOP       ' a high, steady tone
FOR r = 30000 TO 12000 STEP -150            ' falling for about 2.4 seconds
  PLAY MODSAMPLE CHANGE 3, , r
  PAUSE 20
NEXT
PLAY MODSAMPLE 7, 3                         ' the explosion replaces it

The whistle drops by more than an octave as the bomb falls. Starting a new sample on the same channel replaces the old one, so the explosion cuts the whistle off without a STOP.

Wind and gusts

For wind, add a second or so of white noise to the MOD file in your tracker and loop it. Then let its strength wander:

Wind
PLAY MODSAMPLE 12, 4, 20, 12000, LOOP       ' looped noise
v = 20
DO
  v = MIN(64, MAX(8, v + RND * 6 - 3))      ' the strength drifts up and down...
  PLAY MODSAMPLE CHANGE 4, v, 10000 + v * 100   ' ...and a gust is higher as well as louder
  PAUSE 50
LOOP UNTIL INKEY$ <> ""
PLAY MODSAMPLE STOP 4

The same looped noise makes rain, the sea or a waterfall with a different rate, and steam escaping with a hiss that fades.

A heartbeat that quickens

The heartbeat
PLAY MODSAMPLE 13, 4, 50, 12000, LOOP       ' one beat, looped
' ... and each frame:
PLAY MODSAMPLE CHANGE 4, , 12000 + (100 - health) * 120

As the player's health falls from 100 to 0 the heart beats twice as fast. Because the rate also raises the pitch, the beat gets higher as it quickens, which only adds to the tension.

A helicopter's rotor

Changing the volume quickly instead of the rate gives a pulsing sound (tremolo):

The helicopter
PLAY MODSAMPLE 10, 1, 40, 9000, LOOP        ' the engine
FOR i = 0 TO 499
  PLAY MODSAMPLE CHANGE 1, 34 + 30 * SIN(i * 0.6)
  PAUSE 20
NEXT

The volume beats about five times a second: the whup-whup of a helicopter's blades. Wobble the rate a little instead (9000 + 300 * SIN(i * 0.6)) for a flying saucer's warble.

Making the MOD file

  • Use a tracker such as OpenMPT or MilkyTracker, and put the effects in spare sample slots: the high numbers keep them out of the way of the music.
  • Give each continuous sound a loop in the tracker, and make the loop a whole number of cycles of the sound so that it repeats without a click.
  • A MOD file doesn't store a rate for its samples. 16574 plays a sample at the tracker's note C-3, and 8287 at C-2. The default of 16000 is a little below C-3.
  • Keep the music quieter than your effects. An effect at volume 64 is as loud as one of the music's four channels at full volume, so turn the music's samples down in the tracker to let the effects stand out.

Good to know

  • There are four effect channels, and a sample started on a busy channel replaces what is there. Give each continuous sound a channel of its own, and share the others between one-shot effects.
  • The pan is a simple balance, so in the centre an effect plays at full volume in both speakers and sounds a little louder there than at either side.
  • A change is heard at the MOD file's next buffer. Once a frame is plenty: changes made much faster than about 20 times a second won't all be heard.
  • A single command that takes longer than one buffer (about 12 ms) to run, such as clearing a large screen, can make the music repeat a short piece.
  • The rate is close, not exact: about 1% low, in small steps that get coarser at high rates. That doesn't matter for effects, but don't tune a melody with it.

Getting it

LOOP, CHANGE and STOP are in these V8 betas and later:

  • PicoMite V8.00.00b2, for every build with sound. Read the release notes before updating, as some builds reset their options at the first boot of a V8 beta, and programs saved by an earlier firmware need to be loaded again.
  • Colour Maximite 2 V8.00.00b4.

The pan came in an update to both betas after they were first posted. If you downloaded either one before, download it again to get the pan.

These are betas, so reports of anything that doesn't sound right are very welcome, on The Back Shed forum. The PicoMite's User Manual and its Game Development Guide, in the supplementary manuals, describe the commands in full, and the guide has the engine and Doppler examples as well.

← All articles