Sound_FX and Patterns

By JulienGio, 6 years ago

This tutorial explains how to make and use sound patterns and sound_FXs to implement in your games


A sound pattern is composed of a series of sound_FXs that are played in a sequence. Each sound_FX has 7 parameters that affect how it sounds:

  • Type: Either SQUARE or NOISE, determines the waveform
  • Continue_flag: Set to 0 if this is the last sound_FX in the sound. Otherwise 1.
  • Volume start: Volume (amplitude) at the start of the sound_FX
  • Volume sweep: Volume (amplitude) variation over time (negative values dim volume over time, positive values amplify)
  • Period sweep: Period variation over time (negative values shrink the period over time, positive values extend)
  • Period start: Period at the start of the sound_FX
  • Length: Duration of the sound_FX.

I recommend using Valden's GSFX tool to quickly make sounds ;)

Implementing sound patterns in code

A sound pattern is an array of sound_FX. To implement a sound pattern in code, you need to create an array containing the sequence of sound_FXs like so:

const Gamebuino_Meta::Sound_FX mySfx[] = {
  {uint8_t Type, 
   uint8_t Continue_flag, 
   uint8_t Volume start, 
   int8_t Volume sweep, 
   int8_t Period Sweep, 
   uint8_t Period Start, 
   uint8_t Length},  // 1st sound
  {...},  // 2nd sound

Type can be Gamebuino_Meta::Sound_FX_Wave::NOISE or Gamebuino_Meta::Sound_FX_Wave::SQUARE.

If Continue_flag is set to 0, then the pattern stops at this sound_FX. Always set the last sound_FX's continue_flag to 0.

Volume start and volume sweep determine the amplitude of the wave during the sound_FX. Using a high start volume with a negative sweep will make the sound fade out. You could also have a low starting volume and a positive sweep to make the sound fade in.

Period sweep and Period start are similar except they affect the period. Just in case for those who may not know this, but the period is directly tied to the inverse of the frequency. So a high period corresponds to a low frequency and vice-versa.

Lastly, the length is how long the sound lasts. In milliseconds, the sound will last 20 times the value you put for length. So a length of 40 will make your sound_FX last for 800ms (or 0.8s).

Here is an example of a pattern composed of 3 sound_FXs: one noise and two square.

const Gamebuino_Meta::Sound_FX mySfx[] = {

Then, play your pattern by calling the gb.sound.fx() (reference here) function whenever you want:

// Play the FX

Note: you cannot play multiple sound_FX / patterns at the same time. Well, you could, but because there is only one sound_FX audio channel, playing a sound when another is still playing will cut the older sound midway through.