Files
tbd-station-14/Content.Shared/Audio/AudioHelpers.cs
Visne 9b94d5c195 Added nullable to most Content.Shared files (#3238)
* Add nullable to some Content.Shared files.

* Use [NotNullWhen(true)]

* Undo adding now redundant !'s

* Forgot one

* Add a ton more nullable

* You can guess

* Fix some issues

* It actually compiles now

* Auto stash before merge of "null2" and "origin/master"

* I lied

* enable annotations -> enable

* Revert ActionBlockerSystem.cs to original

* Fix ActionBlockerSystem.cs

* More nullable

* Undo some added exclamation marks

* Fix issues

* Update Content.Shared/Maps/ContentTileDefinition.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Resolve some issues

* Remove unused method

* Fix more issues

* Fix more issues

* Fix more issues

* Fix more issues

* Fix issue, rollback SharedGhostComponent.cs

* Update submodule

* Fix issue, invert some if-statements to reduce nesting

* Revert RobustToolbox

* FIx things broken by merge

* Some fixes

- Replaced with string.Empty
- Remove some exclamation marks
- Revert file

* Some fixes

* Trivial #nullable enable

* Fix null ables

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-02-27 14:12:09 +11:00

63 lines
2.6 KiB
C#

#nullable enable
using System;
using Robust.Shared.Audio;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Shared.Audio
{
public static class AudioHelpers{
/// <summary>
/// Returns a random pitch.
/// </summary>
public static AudioParams WithVariation(float amplitude)
{
var scale = (float)(IoCManager.Resolve<IRobustRandom>().NextGaussian(1, amplitude));
return AudioParams.Default.WithPitchScale(scale);
}
// Might as well just hardcode these because the audio system is limited to pitching up and down
// by 12 semitones anyway (ie. 0.5 to 2.0 multiplier).
private static readonly float[] SemitoneMultipliers =
{
0.5f, 233.08f/440f, 246.94f/440f, 261.63f/440f,
277.18f/440f, 293.66f/440f, 311.13f/440f, 329.63f/440f,
349.23f/440f, 369.99f/440f, 392.00f/440f, 415.30f/440f,
1.0f,
466.16f/440f, 493.88f/440f, 523.25f/440f, 554.37f/440f,
587.33f/440f, 622.25f/440f, 659.26f/440f, 698.46f/440f,
739.99f/440f, 783.99f/440f, 830.61f/440f, 2.0f
};
/// <summary>
/// Returns a pitch multiplier that shifts by the given number of semitones.
/// </summary>
/// <param name="shift">Number of semitones to shift, positive or negative. Clamped between -12 and 12
/// which correspond to a pitch multiplier of 0.5 and 2.0 respectively.</param>
public static AudioParams ShiftSemitone(int shift)
{
shift = MathHelper.Clamp(shift, -12, 12);
float pitchMult = SemitoneMultipliers[shift + 12];
return AudioParams.Default.WithPitchScale(pitchMult);
}
/// <summary>
/// Returns a pitch multiplier shifted by a random number of semitones within variation.
/// </summary>
/// <param name="variation">Max number of semitones to shift in either direction. Values above 12 have no effect.</param>
public static AudioParams WithSemitoneVariation(int variation)
{
variation = Math.Clamp(variation, 0, 12);
return ShiftSemitone(IoCManager.Resolve<IRobustRandom>().Next(-variation, variation));
}
public static string GetRandomFileFromSoundCollection(string name)
{
var soundCollection = IoCManager.Resolve<IPrototypeManager>().Index<SoundCollectionPrototype>(name);
return IoCManager.Resolve<IRobustRandom>().Pick(soundCollection.PickFiles);
}
}
}