Sound_FX and Patterns

By JulienGio, 5 months ago

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

Overview

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[] = {
    {Gamebuino_Meta::Sound_FX_Wave::NOISE,1,100,2,5,96,3},
    {Gamebuino_Meta::Sound_FX_Wave::SQUARE,1,100,10,0,126,10},
    {Gamebuino_Meta::Sound_FX_Wave::SQUARE,0,120,-6,0,84,10},
};

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

// Play the FX
gb.sound.fx(mySfx);

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.

Last comments

Sorunome

NEW 2 weeks ago

makerSquirrel makerSquirrel

It's OK, don't worry!

makerSquirrel

NEW 2 weeks ago

yop, I saw that lateron, sorry. I mentioned that in the other thread...

Sorunome

2 weeks ago

It's OK, don't worry!

Sorunome

NEW 2 weeks ago

makerSquirrel makerSquirrel

It is already linked in gb.sound.fx!