/// <summary> /// (Private) Private method handling low-level OpenAL calls required to play a sound, used by PlaySound() and PlayLoopingSound(). /// </summary> /// <param name="channel">The sound channel in which to play the sound</param> /// <param name="file">The sound file, as it appears in the file sourced used by FileSystem</param> /// <param name="volume">Volume at which the sound should be played, 1.0 means normal, 0.5 means half volume, 2.0 means twice as loud</param> /// <param name="pitch">Pitch at which the sound should be played. 1.0 means normal, 0.5 means 2x slower, 2.0 means 2x faster.</param> /// <param name="looping">Should the sound be looping?</param> /// <returns>True if everything went well, false otherwise</returns> private bool PlaySoundInChannel(int channel, string file, float volume, float pitch, bool looping) { if (!Enabled) { return(false); } if ((channel < 0) || (channel >= TOTAL_CHANNELS)) { return(false); // Channel index is out of bounds } if (!PrecacheSound(file)) { return(false); // Failed to precache the sound (wrong data or file do not exist) } AudioPlayerSound sound = SoundCache[file]; StopSound(channel); SoundChannels[channel] = AL.GenSource(); AL.Source(SoundChannels[channel], ALSourcef.Gain, OneBitOfTools.Clamp(volume, 0f, 10f)); AL.Source(SoundChannels[channel], ALSourcef.Pitch, OneBitOfTools.Clamp(pitch, 0.01f, 10f)); AL.Source(SoundChannels[channel], ALSourcei.Buffer, sound.Buffer); AL.Source(SoundChannels[channel], ALSourceb.Looping, looping); AL.SourcePlay(SoundChannels[channel]); return(true); }
/// <summary> /// Loads a sound from the engine's FileSystem and stores it in the cache. /// </summary> /// <param name="file">The name of the file, as found in the FileSystem file source</param> /// <returns>True if the sound was precached properly, false otherwise</returns> public bool PrecacheSound(string file) { if (!Enabled) { return(false); } if (string.IsNullOrEmpty(file)) { return(false); } if (SoundCache.ContainsKey(file)) { return(true); // File was already precached } byte[] waveFileBytes = Files.GetFile(file); if (waveFileBytes == null) { return(false); } AudioPlayerSound source = new AudioPlayerSound(waveFileBytes); if (!source.IsValid) { return(false); } SoundCache.Add(file, source); return(true); }