From 243215ec5d333d937eed66466d2ed39e5b5c6b0f Mon Sep 17 00:00:00 2001 From: Peter Wedder Date: Wed, 25 Nov 2020 12:23:03 +0200 Subject: [PATCH] Adds a semitone shifting AudioHelper (#2608) * Add semitone audio helper * use semitone audio helper on bike horn * prettify * clamp this instead * Expose these to viewvars --- .../Sound/EmitSoundOnUseComponent.cs | 12 +++++- Content.Shared/Audio/AudioHelpers.cs | 37 +++++++++++++++++++ .../Entities/Objects/Fun/bike_horn.yml | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Content.Server/GameObjects/Components/Sound/EmitSoundOnUseComponent.cs b/Content.Server/GameObjects/Components/Sound/EmitSoundOnUseComponent.cs index f927d093a0..5fdc929f18 100644 --- a/Content.Server/GameObjects/Components/Sound/EmitSoundOnUseComponent.cs +++ b/Content.Server/GameObjects/Components/Sound/EmitSoundOnUseComponent.cs @@ -5,6 +5,7 @@ using Robust.Shared.Audio; using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Systems; using Robust.Shared.Serialization; +using Robust.Shared.ViewVariables; namespace Content.Server.GameObjects.Components.Sound { @@ -18,14 +19,16 @@ namespace Content.Server.GameObjects.Components.Sound /// public override string Name => "EmitSoundOnUse"; - public string _soundName; - public float _pitchVariation; + [ViewVariables(VVAccess.ReadWrite)] public string _soundName; + [ViewVariables(VVAccess.ReadWrite)] public float _pitchVariation; + [ViewVariables(VVAccess.ReadWrite)] public int _semitoneVariation; public override void ExposeData(ObjectSerializer serializer) { base.ExposeData(serializer); serializer.DataField(ref _soundName, "sound", string.Empty); serializer.DataField(ref _pitchVariation, "variation", 0.0f); + serializer.DataField(ref _semitoneVariation, "semitoneVariation", 0); } bool IUse.UseEntity(UseEntityEventArgs eventArgs) @@ -37,6 +40,11 @@ namespace Content.Server.GameObjects.Components.Sound EntitySystem.Get().PlayFromEntity(_soundName, Owner, AudioHelpers.WithVariation(_pitchVariation).WithVolume(-2f)); return true; } + if (_semitoneVariation > 0) + { + EntitySystem.Get().PlayFromEntity(_soundName, Owner, AudioHelpers.WithSemitoneVariation(_semitoneVariation).WithVolume(-2f)); + return true; + } EntitySystem.Get().PlayFromEntity(_soundName, Owner, AudioParams.Default.WithVolume(-2f)); return true; } diff --git a/Content.Shared/Audio/AudioHelpers.cs b/Content.Shared/Audio/AudioHelpers.cs index e996954be6..d03114e6f1 100644 --- a/Content.Shared/Audio/AudioHelpers.cs +++ b/Content.Shared/Audio/AudioHelpers.cs @@ -1,6 +1,8 @@ +using System; using Robust.Shared.Audio; using Robust.Shared.Interfaces.Random; using Robust.Shared.IoC; +using Robust.Shared.Maths; using Robust.Shared.Prototypes; using Robust.Shared.Random; @@ -16,6 +18,41 @@ namespace Content.Shared.Audio 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 + }; + + /// + /// Returns a pitch multiplier that shifts by the given number of semitones. + /// + /// 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. + public static AudioParams ShiftSemitone(int shift) + { + shift = MathHelper.Clamp(shift, -12, 12); + float pitchMult = SemitoneMultipliers[shift + 12]; + return AudioParams.Default.WithPitchScale(pitchMult); + } + + /// + /// Returns a pitch multiplier shifted by a random number of semitones within variation. + /// + /// Max number of semitones to shift in either direction. Values above 12 have no effect. + public static AudioParams WithSemitoneVariation(int variation) + { + variation = Math.Clamp(variation, 0, 12); + return ShiftSemitone(IoCManager.Resolve().Next(-variation, variation)); + } + public static string GetRandomFileFromSoundCollection(string name) { var soundCollection = IoCManager.Resolve().Index(name); diff --git a/Resources/Prototypes/Entities/Objects/Fun/bike_horn.yml b/Resources/Prototypes/Entities/Objects/Fun/bike_horn.yml index 0620ba7a4d..3960275bc8 100644 --- a/Resources/Prototypes/Entities/Objects/Fun/bike_horn.yml +++ b/Resources/Prototypes/Entities/Objects/Fun/bike_horn.yml @@ -17,7 +17,7 @@ - type: EmitSoundOnUse sound: /Audio/Items/bikehorn.ogg - variation: 0.2 + semitoneVariation: 6 - type: UseDelay delay: 0.5