Playing SFX

From dreamcast.wiki
Revision as of 04:56, 30 May 2020 by Unknown user (talk) (First commit. Adding example code next)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Author: Andress Barajas

Introduction

This guide will focus solely on playing sound effects on the Sega Dreamcast. The Sega Dreamcast has 2MB of sound RAM and supports 64 channels which gives you the ability to play many sound effects at once (1 sound effect per channel). A sound effect can be either stereo or mono, and must either be 16-bit uncompressed PCM samples or 4-bit Yamaha ADPCM. All sounds played in this manner must be at most 65534 samples in length. That is samples, not bytes (samples != bytes). Each sound effect has a sample rate. The standard sample rate of a sound is 44.1 kHz (44100 Hz/sec => 44100 samples/sec). So mathematically you're getting 65534/44100 => 1.486 secs of sound per sound effect using the standard sample rate. You can lesson the sample rate of your sound to get a longer sound effect but audio quality will be affected.

http://gamedev.allusion.net/docs/kos-2.0.0/sfxmgr_8h.html https://github.com/KallistiOS/KallistiOS/blob/master/kernel/arch/dreamcast/sound/snd_sfxmgr.c

This guide does not cover playing music from CD or RAM. If you want to know how to do that, checkout this guide instead(LINK LATER).

API

Initializing Sound System

 int snd_init()

Initializes the sound system. It is usually called at the beginning of your program. You can't play sound effects before calling this function.

Loading Sound Effects

 sfxhnd_t snd_sfx_load(const char* fn)

After initializing the sound system you can start loading sound effects. This function loads a sound effect from a WAV file and returns a handle to it. The sound effect can be either stereo or mono, and must either be 16-bit uncompressed PCM samples or 4-bit Yamaha ADPCM. Keep in mind that you have less than 2MB of sound RAM to work with.

Playing Sound Effects

 int snd_sfx_play(sfxhnd_t idx, int vol, int pan)

This function plays a loaded sound effect with the specified volume (for both stereo or mono) and panning values (for mono sounds only). Behind the scenes this picks a channel that is not currently in use and plays it on that channel. This is important because no two sound effects can play on the same channel at the same time.

  • The volume to play at (between 0 and 255 254). 255 will crash your sound system.
  • The panning value of the mono sound effect. 0 is all the way to the left, 128 is center, 255 is all the way to the right.
 int snd_sfx_play_chn(int chn, sfxhnd_t idx, int vol, int pan)

This function works similar to snd_sfx_play(), but allows you to specify the channel to play on.

  • The channel value can be between 0 and 63.

Stop Sound Effects

 void snd_sfx_stop(int chn)

This function stops the specified channel of sound from playing

 void snd_sfx_stop_all()

This function stops all channels currently allocated to sound effects from playing

Unload Sound Effects

 void snd_sfx_unload(sfxhnd_t idx)

This function unloads a previously loaded sound effect, and frees the memory associated with it.

 void snd_sfx_unload_all()

This function unloads all previously loaded sound effect, and frees the memory associated with them.

Shutdown Sound System

 void snd_shutdown()

Shut down the sound system and frees all allocated memory.

Code Example