Difference between revisions of "Playing SFX"

From dreamcast.wiki
Jump to navigation Jump to search
(Fixed KOS bug)
 
Line 3: Line 3:
 
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.
 
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.
  
This guide does not cover playing music from CD or RAM, but a guide for this will be coming soon.
+
This guide does not cover playing music from CD or RAM, look at [[Streaming audio]] instead.
  
 
= API =
 
= API =

Latest revision as of 05:29, 28 June 2020

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.

This guide does not cover playing music from CD or RAM, look at Streaming audio instead.

API

The KOS reference manual for this API contains more detail on each function and also has more functions not listed here. The source code for most of these methods can be found within snd_sfxmgr.c.

Initializing Sound System

int snd_init()

The snd_init() function initializes the sound system. It is usually called at the beginning of your program. You can't play sound effects before calling this function.

Load Sound Effect

sfxhnd_t snd_sfx_load(const char* fn)

After initializing the sound system you can start loading sound effects. The snd_sfx_load() 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.

Play Sound Effect

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

The snd_sfx_play() 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).
  • 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)

The snd_sfx_play_chn() 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)

The snd_sfx_stop() function stops the specified channel of sound from playing.

void snd_sfx_stop_all()

The snd_sfx_stop_all() function stops all channels currently allocated to sound effects from playing.

Unload Sound Effects

void snd_sfx_unload(sfxhnd_t idx)

The snd_sfx_unload() function unloads a previously loaded sound effect, and frees the memory associated with it.

void snd_sfx_unload_all()

The snd_sfx_unload_all() function unloads all previously loaded sound effect, and frees the memory associated with them.

Shutdown Sound System

void snd_shutdown()

The snd_shutdown() function shuts down the sound system and frees all allocated memory.

Code Example

The following example demonstrates how to play sound effects on their own channel (using snd_sfx_play()) and on the same channel(using snd_sfx_play_chn()). By playing the sound effects on their own channel (pressing A,B,X,Y), you will notice that the sound effects can overlap each other; while playing sound effects on the same channel (with the d-pad), you will only hear one sound effect at a time. Another thing to point out is that all of the beep WAV files referenced here are MONO so I also pan the sound with hard coded values (LEFT, CENTER, RIGHT). You can also adjust the volume of all the sound effects using the left and right triggers of the controller.

Error handling is non-existent in this example. You'll want to check if the sound effects are loaded correctly and that you have a controller plugged in when running this demo.

You can find all the code and resources of this example on Github.

NOTE: This code sample may not work in Sega Dreamcast emulators.

  1 #include <kos.h>
  2 
  3 #define LEFT 0
  4 #define CENTER 128
  5 #define RIGHT 255
  6 
  7 extern uint8 romdisk[];
  8 KOS_INIT_ROMDISK(romdisk);
  9 
 10 static void draw_instructions(uint8_t volume);
 11 
 12 static cont_state_t* get_cont_state();
 13 static int button_pressed(unsigned int current_buttons, unsigned int changed_buttons, unsigned int button);
 14 
 15 int main(int argc, char **argv) {
 16     uint8_t volume = 128;
 17     int volume_changed = 1;
 18     cont_state_t* cond;
 19 
 20     vid_set_mode(DM_640x480, PM_RGB555);
 21     // Initialize sound system
 22     snd_init();
 23 
 24     // Load wav files found in romdisk
 25     sfxhnd_t beep1 = snd_sfx_load("/rd/beep-1.wav");
 26     sfxhnd_t beep2 = snd_sfx_load("/rd/beep-2.wav");
 27     sfxhnd_t beep3 = snd_sfx_load("/rd/beep-3.wav");
 28     sfxhnd_t beep4 = snd_sfx_load("/rd/beep-4.wav");
 29 
 30     unsigned int current_buttons = 0;
 31     unsigned int changed_buttons = 0;
 32     unsigned int previous_buttons = 0;
 33 
 34     for(;;) {
 35         cond = get_cont_state();
 36         current_buttons = cond->buttons;
 37         changed_buttons = current_buttons ^ previous_buttons;
 38         previous_buttons = current_buttons;
 39         
 40         // Play sounds on different channels
 41         if(button_pressed(current_buttons, changed_buttons, CONT_A)) {
 42             snd_sfx_play(beep1, volume, CENTER);
 43         }
 44         if(button_pressed(current_buttons, changed_buttons, CONT_B)) {
 45             snd_sfx_play(beep2, volume, RIGHT);
 46         }
 47         if(button_pressed(current_buttons, changed_buttons, CONT_X)) {
 48             snd_sfx_play(beep3, volume, LEFT);
 49         }
 50         if(button_pressed(current_buttons, changed_buttons, CONT_Y)) {
 51             snd_sfx_play(beep4, volume, CENTER);
 52         }
 53 
 54         // Play sounds on same channel
 55         if(button_pressed(current_buttons, changed_buttons, CONT_DPAD_DOWN)) {
 56             snd_sfx_play_chn(0, beep1, volume, CENTER);
 57         }
 58         if(button_pressed(current_buttons, changed_buttons, CONT_DPAD_RIGHT)) {
 59             snd_sfx_play_chn(0, beep2, volume, RIGHT);
 60         }
 61         if(button_pressed(current_buttons, changed_buttons, CONT_DPAD_LEFT)) {
 62             snd_sfx_play_chn(0, beep3, volume, LEFT);
 63         }
 64         if(button_pressed(current_buttons, changed_buttons, CONT_DPAD_UP)) {
 65             snd_sfx_play_chn(0, beep4, volume, CENTER);
 66         }
 67 
 68         // Adjust Volume
 69         if(cond->ltrig > 0) {
 70             volume_changed = 1;
 71 
 72             if(volume < 254)
 73                 volume++;
 74         }
 75         if(cond->rtrig > 0) {
 76             volume_changed = 1;
 77 
 78             if(volume > 0)
 79                 volume--;
 80         }
 81 
 82         // Exit Program
 83         if(button_pressed(current_buttons, changed_buttons, CONT_START))
 84             break;
 85 
 86         if(volume_changed) {
 87             volume_changed = 0;
 88             draw_instructions(volume);
 89         }
 90     }
 91 
 92     // Unload all sound effects from sound RAM
 93     snd_sfx_unload(beep1);	
 94     snd_sfx_unload(beep2);
 95     snd_sfx_unload(beep3);	
 96     snd_sfx_unload(beep4);
 97     // OR
 98     // snd_sfx_unload_all();	
 99 
100     snd_shutdown();
101 
102     return 0;
103 }
104 
105 static void draw_instructions(uint8_t volume) {
106     int x = 20, y = 20+24;
107     int color = 1;
108     char current_volume_str[32];
109     memset(current_volume_str, 0, 32);
110     snprintf(current_volume_str, 32, "Current Volume: %3i", volume);
111     
112     bfont_draw_str(vram_s + y*640+x, 640, color, "Press A,B,X,Y to play beeps on separate channels");
113     y += 48;
114     bfont_draw_str(vram_s + y*640+x, 640, color, "Press UP,DOWN,LEFT,RIGHT on D-Pad to play beeps");
115     y += 24;
116     bfont_draw_str(vram_s + y*640+x, 640, color, "on the same channel");
117     y += 48;
118     bfont_draw_str(vram_s + y*640+x, 640, color, "Press L-Trigger/R-Trigger to +/- volume");
119     y += 24;
120     bfont_draw_str(vram_s + y*640+x, 640, color, current_volume_str);
121     y += 48;
122     bfont_draw_str(vram_s + y*640+x, 640, color, "Press Start to exit program");
123 }
124 
125 static cont_state_t* get_cont_state() {
126     maple_device_t* cont;
127     cont_state_t* state;
128 
129     cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
130     if(cont) {
131         state = (cont_state_t*)maple_dev_status(cont);
132         return state;
133     }
134 
135     return NULL;
136 }
137 
138 static int button_pressed(unsigned int current_buttons, unsigned int changed_buttons, unsigned int button) {
139     if (changed_buttons & button) {
140         if (current_buttons & button)
141             return 1;
142     }
143 
144     return 0;
145 }