1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Added RandomPitchModifier and GetRandomPitch to SoundEffectInfo

This commit is contained in:
Ell 2022-05-20 16:59:28 +02:00
parent 161d44dbe0
commit bd9d3f970b
2 changed files with 25 additions and 3 deletions

View file

@ -16,6 +16,7 @@ Additions
- Added InputHandler.InvertPressBehavior - Added InputHandler.InvertPressBehavior
- Added ReverseInput, ReverseOutput and AndThen to Easings - Added ReverseInput, ReverseOutput and AndThen to Easings
- Added an Enum constructor to GenericInput - Added an Enum constructor to GenericInput
- Added RandomPitchModifier and GetRandomPitch to SoundEffectInfo
Improvements Improvements
- Allow comparing Keybind and Combination based on the amount of modifiers they have - Allow comparing Keybind and Combination based on the amount of modifiers they have

View file

@ -1,29 +1,39 @@
using System;
using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Audio;
using MLEM.Extensions; using MLEM.Extensions;
namespace MLEM.Sound { namespace MLEM.Sound {
/// <summary> /// <summary>
/// A sound effect info is a wrapper around <see cref="SoundEffect"/> that additionally stores <see cref="Volume"/>, <see cref="Pitch"/> and <see cref="Pan"/> information. /// A sound effect info is a wrapper around <see cref="SoundEffect"/> that additionally stores <see cref="Volume"/>, <see cref="Pitch"/> and <see cref="Pan"/> information.
/// Additionally, a <see cref="SoundEffectInstance"/> can be created using <see cref="CreateInstance"/>. /// Additionally, a <see cref="RandomPitchModifier"/> can be applied, a <see cref="SoundEffectInstance"/> can be created using <see cref="CreateInstance"/>, and more.
/// </summary> /// </summary>
public class SoundEffectInfo { public class SoundEffectInfo {
private static readonly Random Random = new Random();
/// <summary> /// <summary>
/// The <see cref="SoundEffect"/> that is played by this info. /// The <see cref="SoundEffect"/> that is played by this info.
/// </summary> /// </summary>
public readonly SoundEffect Sound; public readonly SoundEffect Sound;
/// <summary> /// <summary>
/// Volume, ranging from 0.0 (silence) to 1.0 (full volume). Volume during playback is scaled by SoundEffect.MasterVolume. /// Volume, ranging from 0.0 (silence) to 1.0 (full volume). Volume during playback is scaled by SoundEffect.MasterVolume.
/// </summary> /// </summary>
public float Volume; public float Volume;
/// <summary> /// <summary>
/// Pitch adjustment, ranging from -1.0 (down an octave) to 0.0 (no change) to 1.0 (up an octave). /// Pitch adjustment, ranging from -1.0 (down an octave) to 0.0 (no change) to 1.0 (up an octave).
/// To incorporate <see cref="RandomPitchModifier"/>, you can use <see cref="GetRandomPitch"/>.
/// </summary> /// </summary>
public float Pitch; public float Pitch;
/// <summary> /// <summary>
/// Pan value ranging from -1.0 (left speaker) to 0.0 (centered), 1.0 (right speaker). /// Pan value ranging from -1.0 (left speaker) to 0.0 (centered), 1.0 (right speaker).
/// </summary> /// </summary>
public float Pan; public float Pan;
/// <summary>
/// A value that allows randomly modifying <see cref="Pitch"/> every time <see cref="Play"/> or <see cref="CreateInstance"/> are used.
/// The random modifier that is added onto <see cref="Pitch"/> will be between -<see cref="RandomPitchModifier"/> and <see cref="RandomPitchModifier"/>.
/// </summary>
public float RandomPitchModifier;
/// <summary> /// <summary>
/// Creates a new sound effect info with the given values. /// Creates a new sound effect info with the given values.
@ -39,12 +49,23 @@ namespace MLEM.Sound {
this.Pan = pan; this.Pan = pan;
} }
/// <summary>
/// Returns a random pitch for this sound effect info that is between <see cref="Pitch"/> - <see cref="RandomPitchModifier"/> and <see cref="Pitch"/> + <see cref="RandomPitchModifier"/>.
/// If <see cref="RandomPitchModifier"/> is 0, this method always returns <see cref="Pitch"/>.
/// </summary>
/// <returns>A random pitch to use to play this sound effect</returns>
public float GetRandomPitch() {
if (this.RandomPitchModifier == 0)
return this.Pitch;
return this.Pitch + ((float) Random.NextDouble() * 2 - 1) * this.RandomPitchModifier;
}
/// <summary> /// <summary>
/// Plays this info's <see cref="Sound"/> once. /// Plays this info's <see cref="Sound"/> once.
/// </summary> /// </summary>
/// <returns>False if more sounds are currently playing than the platform allows</returns> /// <returns>False if more sounds are currently playing than the platform allows</returns>
public bool Play() { public bool Play() {
return this.Sound.Play(this.Volume, this.Pitch, this.Pan); return this.Sound.Play(this.Volume, this.GetRandomPitch(), this.Pan);
} }
/// <summary> /// <summary>
@ -53,7 +74,7 @@ namespace MLEM.Sound {
/// <param name="isLooped">The value to set the returned instance's <see cref="SoundEffectInstance.IsLooped"/> to. Defaults to false.</param> /// <param name="isLooped">The value to set the returned instance's <see cref="SoundEffectInstance.IsLooped"/> to. Defaults to false.</param>
/// <returns>A new sound effect instance, with this info's data applied</returns> /// <returns>A new sound effect instance, with this info's data applied</returns>
public SoundEffectInstance CreateInstance(bool isLooped = false) { public SoundEffectInstance CreateInstance(bool isLooped = false) {
return this.Sound.CreateInstance(this.Volume, this.Pitch, this.Pan, isLooped); return this.Sound.CreateInstance(this.Volume, this.GetRandomPitch(), this.Pan, isLooped);
} }
} }