Audio - VictorAlvizo/Ventura GitHub Wiki

Audio class is what adds sounds to your game and thus very important.

Audio(bool deathAllowed)

The constructer for the audio class takes in a boolean that should always be left as true unless adding the audio object as a component or copying. If you're using the Audio class normally and don't leave it as true it will cause a memory leak as irrklang's (The audio library used) sound engine is never freed. If you're adding the audio object as a component and leave it as a true you won't hear anything coming from that audio source and the component class needs to copy the object resulting in the sound engine getting freed so no sound engine will be available. So in essence, Use Audio() as the constructer unless adding it as a component, then do Audio(false)

Example: Audio audio = new Audio(false); //Not adding the audio as a component nor copying

~Audio()

Frees up the sound engine if it's death is allowed

void AddSound(const std::string& referenceName, const std::string& soundFileName, bool loop, float volumePercentage)

  1. Name of the audio source
  2. File path to the audio file
  3. Should the audio loop
  4. What volume (Range: 0.0 - 1.0) should the audio be on

Call this method when adding a sound, if the reference name already exists it will be overwritten. The audio must be within the valid range or else it will be set automatically to 1.0.

Example: AddSound("Grunt", "Audio/GruntNoise.wav", false, 0.3f) Adding a grunt sound that does not loop with it's volume at 30%

void PlaySound(const std::string& soundName)

  1. Reference name of the sound you want to play

Method must be called to play a sound. If the sound does not exist it will prompt an error indicating so. If the sound given to play is already playing it will restart from the beginning, not doing so would give you the same audio playing at the same time.

Example: PlaySound("Grunt") Plays the grunt sound

void Mute(const std::string& soundName)

  1. Reference name of the sound you want to mute

Something important to take note of here, if you just all Mute() with nothing passed in, all the sounds from this sound engine will be muted. Same thing will happen if you call Mute(""), passing "" mutes everything. If you give it a valid sound source it will stop the sound. If the source can not be found it will prompt an error message.

Example: Mute() Mutes everything from the audio class Mute("BackgroundMusic") Mutes the audio source named background music

void Unmute()

Calling this method unmutes any sound that has been muted

Example: Mute("swing"); Unmute();

void PauseSound(bool pause)

Passing in true will pause the current sound; passing in false will unpause the current sound. If there are no sounds currently playing, nothing will occur.

Example: PauseSound(true); //Pause the current sound

void Volume(const std::string& soundName, float volume)

This method will allow the volume of a certain sound to be changed. If the sound does not exist, the program will prompt the error. The volume must be between 0.0f - 1.0f, otherwise, an error message will prompt indicating the value inputted is out of range. At 0.0, the sound will be completely mute, at 1.0, the sound will be at max volume

Example: Volume("Swing", 0.6f);

void SetSoundPos(unsigned int newPos)

Adjust the sound position of the current playing sound. The position is measured in miliseconds (1000 miliseconds = 1 second). if no sound is currently playing, an error message will be prompted. If the new position is larger than the length of the current playing audio an error will be prompted with how long the current playing audio is (in miliseconds)

Example: SetSoundPos(3400); //Move the audio to 3.4 seconds

Sound& getSound(const std::string& soundName)

  1. Name of the sound source you want returned

Returns the actual sound struct. The Sound struct has some important variables such as: the volume, loop control, name given to the audio source by the Irrklang library (usually the file path), and most importantly the ISoundSource pointer which gives you greater access to the Irrklang library. Read up on their documentation if you want to take that further.

Example: Sound gruntSound = audioObject.getSound("Grunt"); //Return the sound struct for the grunt sound

bool isPlaying(const std::string& soundName)

  1. Name of the sound source you want checked

Will return true if the sound is currently playing, false if it's not. Will prompt an error if the sound source could not be found

if(audioObject.isPlaying("Grunt") == true) { //code } Run a line of code if the sound is currently playing

bool isFinished()

Returns whether the current audio is finished playing or not. It will also return false if there is no audio playing

Example: if(isFinished()) { Mute(); }

bool isPaused()

Determines whether the current audio playing is paused or not. Returns false as well if there is nothing currently playing

unsigned int getPlayPos()

Get the current position of the current audio in miliseconds (1000 miliseconds = 1 second). If there are no sounds currently playing or the sound is done playing it will return 0

Example: if(getPlayPos() == 0 && currentSoundPlaying() != "") { PlaySound("ThemeSong"); }

unsigned int getAudioLength(const std::string& soundName)

Returns the full audio length (in miliseconds) of the sound specified. (1000 miliseconds = 1 second). The method will return 0 if there were any errors in retrieving the data, such as the audio not existing

Example: float playSpot = 5000;

if(playSpot < getAudioLength("ThemeSong")) { SetSoundPos(playSpot); }

float getVolume(const std::string& soundName)

Returns the volume of the specified audio in a range of 0.0 to 1.0. If the audio does not exist it will return 0

std::string currentSoundPlaying()

Returns the name of the current sound playing. If there is no sound playing, it will return ""

Example: if(currentSoundPlaying() == "") { PlaySound("Music"); }

bool soundsMuted()

Will return whether or not sounds are muted

Example: if(soundsMuted() == true) { Unmute(); }