// Filename : SOUND.C // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Sound effects library #include // The sound objects list OBJECT_LIST_HEADER sound_objects; word volume_control,sound_on; // Function : InitSoundData() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Load the sound data into sound memory // Parameters : se - pointer to sound effects data // vh - vab header main memory address // vb - vab body main memory address // Returns : Pointer to allocated structure, NULL for error // Notes : Will always allocate a sound data structure SOUND_DATA *InitSoundData(SOUND_EFFECT *se,u_byte *vh,u_byte *vb) { static SOUND_DATA sound_data[MAX_SOUND_DATA]; static u_byte sno=0; word vabid; // Initialise the sound objects list InitObjectList(&sound_objects); // Transfer sound data, wait for data to be transfered vabid=SsVabTransfer(vh,vb,-1,SS_WAIT_COMPLETED); #ifdef PSX SsStart(); #endif // Flag any problems if(vabid>=0) { // Allocate a sound data structure sno=++sno%MAX_SOUND_DATA; // Initialise the structure sound_data[sno].vabid=vabid; sound_data[sno].l=&sound_objects; sound_data[sno].seffects=se; // Set the default value of the volume control volume_control=0; // Allow sounds to be played sound_on=1; // Return a pointer to allocated data #ifdef DEBUG_INFO sprintf(fm_string,"Completed (%d)\n",vabid); PrintFM(fm_string); #endif return(&sound_data[sno]); } // A problem with the VAB so don't allow any sounds to be played sound_on=0; #ifdef DEBUG_INFO sprintf(fm_string,"Failed (%d)\n",vabid); PrintFM(fm_string); #endif return(0); } // Function : PlaySoundEffect() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Plays a sound effect // Parameters : sd - pointer to sound data // n - number of sound effect to play // Returns : The sound effect object, NULL for error // Notes : None OBJECT *PlaySoundEffect(SOUND_DATA *sd,VOLUME *vol,word n) { SOUND_EFFECT *se,*old; OBJECT *o,*s,*l; word lowest=SS_MAX_PRIORITY; word left,right; if(sound_on) { if((o=CreateObject(sd->l,SOUND_OBJECT))) { // Find the correct sound effect se=&sd->seffects[n]; // Assign this effect to the object o->type.sfx.effect=se; o->type.sfx.vabid=sd->vabid; o->type.sfx.priority=se->priority; // Make sure the object gets processed o->active=1; // Set the duration of the sound effect SetObjectsTimer(o,se->timer); // Set the sound handler SetObjectsHandler(o,HandleSoundEffect); // Calculate the volume left=vol->left-volume_control-se->left_vol; left=(left<0 ? 0 : left); right=vol->right-volume_control-se->right_vol; right=(right<0 ? 0 : right); // Try and play the sound effect if((o->type.sfx.voice=SsUtKeyOn(sd->vabid,se->prog_no,se->tone_no,se->pitch,se->fine_pitch,left,right))<0) { // Could not find a free voice, so kill the sound with the lowest priority s=sound_objects.start; while(s) { if(s->type.sfx.priority<=lowest && s!=o) { l=s; lowest=s->type.sfx.priority; } } old=(SOUND_EFFECT *)l->type.sfx.effect; // Free the voice used by the sound SsUtKeyOff(l->type.sfx.voice,l->type.sfx.vabid,old->prog_no,old->tone_no,old->pitch); #ifdef DEBUG_INFO PrintFM("Voice Off\n"); #endif // Now play the sound o->type.sfx.voice=SsUtKeyOn(sd->vabid,se->prog_no,se->tone_no,se->pitch,se->fine_pitch,left,right); } return(o); } } #ifdef DEBUG_INFO sprintf(fm_string,"Failed (%d)\n",n); PrintFM(fm_string); #endif return(0); } // Function : HandleSoundEffect() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Handles a sound effect object // Parameters : o - object to handle // Returns : None // Notes : None void HandleSoundEffect(OBJECT *o) { SOUND_EFFECT *se; // Has the sound expired yet if(o->timer) o->timer--; else { // Turn off the voice used by the sound se=(SOUND_EFFECT *)o->type.sfx.effect; SsUtKeyOff(o->type.sfx.voice,o->type.sfx.vabid,se->prog_no,se->tone_no,se->pitch); // Remove the object from the list RemoveObjectFromList(&sound_objects,o); } } // Function : UpdateSoundEffects() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Update all the sound effect objects // Parameters : None // Returns : None // Notes : None void UpdateSoundEffects(void) { UpdateObjects(&sound_objects); } // Function : CloseSound() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Closes the VAB and kills all the active sound objects // Parameters : None // Returns : None // Notes : None void CloseSound(SOUND_DATA *sd) { // Kill all the sound objects ClearObjectList(sd->l); // Close the VAB SsVabClose(sd->vabid); #ifdef DEBUG_INFO PrintFM("Completed\n"); #endif } // Function : GetVolumeControl() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Gets the value of the master volume control // Parameters : None // Returns : Value of volume control // Notes : None word GetVolumeControl(void) { return(volume_control); } // Function : SetVolumeControl() // Coded by : Scott Evans // Created/Modified : 8/7/98 // Description : Sets the value of the master volume control // Parameters : v - new value // Returns : None // Notes : None void SetVolumeControl(word v) { volume_control=v; } // Function : KillSounds() // Coded by : Scott Evans // Created/Modified : 10/7/98 // Description : Kills all sound objects and voices // Parameters : None // Returns : None // Notes : None void KillSounds(void) { Silence(); ClearObjectList(&sound_objects); }