#include "audio.h" #include <SDL2/SDL.h>#include <SDL2/SDL_mixer.h>#include "loader.h"#include "datatypes.h" /*Copyright (c) 2013-2021 Devine Lu Linvega2018 web port by rezmason2021 C89 port by rezmason Permission to use, copy, modify, and distribute this software for anypurpose with or without fee is hereby granted, provided that the abovecopyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIESWITH REGARD TO THIS SOFTWARE.*/ #define loadEffect(effectName, path) success &= Loader_LoadSound(Loader_CreateSound(&effects[effectName], "audio/effect/" path, 0.125))#define createMusic(chapter, path) Loader_CreateSound(&musicByChapter[chapter], "audio/record/" path, 1.0)#define createAmbience(zone, path) Loader_CreateSound(&ambienceByZone[zone], "audio/ambience/" path, 0.2) static Sound *effects[NumberOfEffects];static Sound *musicByChapter[NumberOfChapters];static Sound *ambienceByZone[NumberOfZones];static int _active = 0; static Sound *currentAmbience = NULL;static Sound *currentMusic = NULL;static int footstep = 0; intAudio_PreloadAssets(void){ int success = 1; /* sound effects are loaded immediately */ loadEffect(EFFECT_ACTION_DOOR_ACTIVE, "action_DoorActive.mp3"); loadEffect(EFFECT_ACTION_DOOR_INIT, "action_DoorInit.mp3"); loadEffect(EFFECT_ACTION_ENERGY_ACTIVE, "action_EnergyActive.mp3"); loadEffect(EFFECT_ACTION_ENERGY_INIT, "action_EnergyInit.mp3"); loadEffect(EFFECT_ACTION_ENERGY_STACK, "action_EnergyStack.mp3"); loadEffect(EFFECT_ACTION_SEAL_ACTIVE, "action_SealActive.mp3"); loadEffect(EFFECT_ACTION_SEAL_INACTIVE, "action_SealInactive.mp3"); loadEffect(EFFECT_ACTION_SEAL_INIT, "action_SealInit.mp3"); loadEffect(EFFECT_FOOTSTEP_COLLIDE, "footstep_collide.mp3"); loadEffect(EFFECT_FOOTSTEP_LEFT, "footstep_left.mp3"); loadEffect(EFFECT_FOOTSTEP_RIGHT, "footstep_right.mp3"); loadEffect(EFFECT_FOOTSTEP_TURN, "footstep_turn.mp3"); /* music is loaded on demand */ createMusic(CHAPTER_ACT1, "music_act1.mp3"); createMusic(CHAPTER_ACT2, "music_act2.mp3"); createMusic(CHAPTER_ACT3, "music_act3.mp3"); createMusic(CHAPTER_ACT4, "music_act4.mp3"); createMusic(CHAPTER_ACT5, "music_act5.mp3"); createMusic(CHAPTER_END, "music_credit.mp3"); /* ambience is loaded on demand */ createAmbience(ZONE_ANTECHANNEL, "ambient_antechannel.mp3"); createAmbience(ZONE_CAPSULE, "ambient_capsule.mp3"); createAmbience(ZONE_CIRCULAR, "ambient_circular.mp3"); /* (entente has no ambience) */ createAmbience(ZONE_FOREST, "ambient_forest.mp3"); createAmbience(ZONE_METAMONDST, "ambient_metamondst.mp3"); createAmbience(ZONE_NATANIEV, "ambient_nataniev.mp3"); createAmbience(ZONE_RAINRE, "ambient_rainre.mp3"); createAmbience(ZONE_STONES, "ambient_stones.mp3"); createAmbience(ZONE_STUDIO, "ambient_studio.mp3"); return success;} voidAudio_Init(void){ Mix_ReserveChannels(RESERVED_CHANNELS);} voidAudio_Quit(void){ int i; for(i = 0; i < NumberOfEffects; i++) { Loader_DestroySound(effects[i]); effects[i] = NULL; } for(i = 0; i < NumberOfChapters; i++) { Loader_DestroySound(musicByChapter[i]); musicByChapter[i] = NULL; } for(i = 0; i < NumberOfZones; i++) { Loader_DestroySound(ambienceByZone[i]); ambienceByZone[i] = NULL; } currentAmbience = NULL; currentMusic = NULL;} voidAudio_SetAmbience(Zone zone){ Sound *ambience = ambienceByZone[zone]; if(currentAmbience == ambience) { return; } Loader_LoadSound(ambience); if(currentAmbience != NULL) { Loader_UnloadSound(currentAmbience); } currentAmbience = ambience; if(currentAmbience != NULL) { Mix_PlayChannel(AMBIENCE_CHANNEL, currentAmbience->chunk, -1); if(!_active) { Mix_Pause(AMBIENCE_CHANNEL); } }} voidAudio_SetMusic(Chapter chapter){ Sound *music = musicByChapter[chapter]; if(currentMusic == music) { return; } Loader_LoadSound(music); if(currentMusic != NULL) { Loader_UnloadSound(currentMusic); } currentMusic = music; if(currentMusic != NULL) { Mix_PlayChannel(MUSIC_CHANNEL, currentMusic->chunk, -1); if(!_active) { Mix_Pause(MUSIC_CHANNEL); } }} voidAudio_PlayEffect(EffectName name){ if(_active) { Mix_PlayChannel(-1, effects[name]->chunk, 0); }} voidAudio_PlayFootstep(void){ footstep = (footstep + 1) % 2; Audio_PlayEffect(footstep == 1 ? EFFECT_FOOTSTEP_LEFT : EFFECT_FOOTSTEP_RIGHT);} intAudio_GetActive(void){ return _active;} voidAudio_SetActive(int active){ if(_active != active) { _active = active; if(_active) { Mix_Resume(-1); } else { Mix_Pause(-1); } }}
#include "audio.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include "loader.h"
#include "datatypes.h"
/*
Copyright (c) 2013-2021 Devine Lu Linvega
2018 web port by rezmason
2021 C89 port by rezmason
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
#define loadEffect(effectName, path) success &= Loader_LoadSound(Loader_CreateSound(&effects[effectName], "audio/effect/" path, 0.125))
#define createMusic(chapter, path) Loader_CreateSound(&musicByChapter[chapter], "audio/record/" path, 1.0)
#define createAmbience(zone, path) Loader_CreateSound(&ambienceByZone[zone], "audio/ambience/" path, 0.2)
static Sound *effects[NumberOfEffects];
static Sound *musicByChapter[NumberOfChapters];
static Sound *ambienceByZone[NumberOfZones];
static int _active = 0;
static Sound *currentAmbience = NULL;
static Sound *currentMusic = NULL;
static int footstep = 0;
int
Audio_PreloadAssets(void)
{
int success = 1;
/* sound effects are loaded immediately */
loadEffect(EFFECT_ACTION_DOOR_ACTIVE, "action_DoorActive.mp3");
loadEffect(EFFECT_ACTION_DOOR_INIT, "action_DoorInit.mp3");
loadEffect(EFFECT_ACTION_ENERGY_ACTIVE, "action_EnergyActive.mp3");
loadEffect(EFFECT_ACTION_ENERGY_INIT, "action_EnergyInit.mp3");
loadEffect(EFFECT_ACTION_ENERGY_STACK, "action_EnergyStack.mp3");
loadEffect(EFFECT_ACTION_SEAL_ACTIVE, "action_SealActive.mp3");
loadEffect(EFFECT_ACTION_SEAL_INACTIVE, "action_SealInactive.mp3");
loadEffect(EFFECT_ACTION_SEAL_INIT, "action_SealInit.mp3");
loadEffect(EFFECT_FOOTSTEP_COLLIDE, "footstep_collide.mp3");
loadEffect(EFFECT_FOOTSTEP_LEFT, "footstep_left.mp3");
loadEffect(EFFECT_FOOTSTEP_RIGHT, "footstep_right.mp3");
loadEffect(EFFECT_FOOTSTEP_TURN, "footstep_turn.mp3");
/* music is loaded on demand */
createMusic(CHAPTER_ACT1, "music_act1.mp3");
createMusic(CHAPTER_ACT2, "music_act2.mp3");
createMusic(CHAPTER_ACT3, "music_act3.mp3");
createMusic(CHAPTER_ACT4, "music_act4.mp3");
createMusic(CHAPTER_ACT5, "music_act5.mp3");
createMusic(CHAPTER_END, "music_credit.mp3");
/* ambience is loaded on demand */
createAmbience(ZONE_ANTECHANNEL, "ambient_antechannel.mp3");
createAmbience(ZONE_CAPSULE, "ambient_capsule.mp3");
createAmbience(ZONE_CIRCULAR, "ambient_circular.mp3");
/* (entente has no ambience) */
createAmbience(ZONE_FOREST, "ambient_forest.mp3");
createAmbience(ZONE_METAMONDST, "ambient_metamondst.mp3");
createAmbience(ZONE_NATANIEV, "ambient_nataniev.mp3");
createAmbience(ZONE_RAINRE, "ambient_rainre.mp3");
createAmbience(ZONE_STONES, "ambient_stones.mp3");
createAmbience(ZONE_STUDIO, "ambient_studio.mp3");
return success;
}
void
Audio_Init(void)
{
Mix_ReserveChannels(RESERVED_CHANNELS);
}
void
Audio_Quit(void)
{
int i;
for(i = 0; i < NumberOfEffects; i++) {
Loader_DestroySound(effects[i]);
effects[i] = NULL;
}
for(i = 0; i < NumberOfChapters; i++) {
Loader_DestroySound(musicByChapter[i]);
musicByChapter[i] = NULL;
}
for(i = 0; i < NumberOfZones; i++) {
Loader_DestroySound(ambienceByZone[i]);
ambienceByZone[i] = NULL;
}
currentAmbience = NULL;
currentMusic = NULL;
}
void
Audio_SetAmbience(Zone zone)
{
Sound *ambience = ambienceByZone[zone];
if(currentAmbience == ambience) {
return;
}
Loader_LoadSound(ambience);
if(currentAmbience != NULL) {
Loader_UnloadSound(currentAmbience);
}
currentAmbience = ambience;
if(currentAmbience != NULL) {
Mix_PlayChannel(AMBIENCE_CHANNEL, currentAmbience->chunk, -1);
if(!_active) {
Mix_Pause(AMBIENCE_CHANNEL);
}
}
}
void
Audio_SetMusic(Chapter chapter)
{
Sound *music = musicByChapter[chapter];
if(currentMusic == music) {
return;
}
Loader_LoadSound(music);
if(currentMusic != NULL) {
Loader_UnloadSound(currentMusic);
}
currentMusic = music;
if(currentMusic != NULL) {
Mix_PlayChannel(MUSIC_CHANNEL, currentMusic->chunk, -1);
if(!_active) {
Mix_Pause(MUSIC_CHANNEL);
}
}
}
void
Audio_PlayEffect(EffectName name)
{
if(_active) {
Mix_PlayChannel(-1, effects[name]->chunk, 0);
}
}
void
Audio_PlayFootstep(void)
{
footstep = (footstep + 1) % 2;
Audio_PlayEffect(footstep == 1 ? EFFECT_FOOTSTEP_LEFT : EFFECT_FOOTSTEP_RIGHT);
}
int
Audio_GetActive(void)
{
return _active;
}
void
Audio_SetActive(int active)
{
if(_active != active) {
_active = active;
if(_active) {
Mix_Resume(-1);
} else {
Mix_Pause(-1);
}
}
}