1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 20:37:26 +08:00
osu-lazer/osu.Game/Audio/HitSampleInfo.cs

115 lines
4.3 KiB
C#
Raw Normal View History

2019-06-30 20:58:30 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
2020-12-01 17:09:28 +08:00
using Newtonsoft.Json;
2023-05-16 15:29:24 +08:00
using osu.Game.Beatmaps.ControlPoints;
2020-12-01 14:37:51 +08:00
using osu.Game.Utils;
2019-06-30 20:58:30 +08:00
namespace osu.Game.Audio
{
/// <summary>
/// Describes a gameplay hit sample.
/// </summary>
[Serializable]
public class HitSampleInfo : ISampleInfo, IEquatable<HitSampleInfo>
2019-06-30 20:58:30 +08:00
{
public const string HIT_NORMAL = @"hitnormal";
2019-06-30 20:58:30 +08:00
public const string HIT_WHISTLE = @"hitwhistle";
public const string HIT_FINISH = @"hitfinish";
public const string HIT_CLAP = @"hitclap";
2022-10-19 19:34:41 +08:00
public const string BANK_NORMAL = @"normal";
public const string BANK_SOFT = @"soft";
public const string BANK_DRUM = @"drum";
// new sample used exclusively by taiko for now.
public const string HIT_FLOURISH = "hitflourish";
// new bank used exclusively by taiko for now.
public const string BANK_STRONG = @"strong";
/// <summary>
/// All valid sample addition constants.
/// </summary>
public static IEnumerable<string> AllAdditions => new[] { HIT_WHISTLE, HIT_FINISH, HIT_CLAP };
2022-10-19 19:34:41 +08:00
/// <summary>
/// All valid bank constants.
/// </summary>
public static IEnumerable<string> AllBanks => new[] { BANK_NORMAL, BANK_SOFT, BANK_DRUM };
2019-06-30 20:58:30 +08:00
/// <summary>
2020-12-01 14:37:51 +08:00
/// The name of the sample to load.
2019-06-30 20:58:30 +08:00
/// </summary>
2020-12-01 14:37:51 +08:00
public readonly string Name;
2019-06-30 20:58:30 +08:00
/// <summary>
2020-12-01 14:37:51 +08:00
/// The bank to load the sample from.
2019-06-30 20:58:30 +08:00
/// </summary>
2023-05-16 15:29:24 +08:00
public readonly string Bank;
2019-06-30 20:58:30 +08:00
/// <summary>
/// An optional suffix to provide priority lookup. Falls back to non-suffixed <see cref="Name"/>.
/// </summary>
2020-12-01 14:37:51 +08:00
public readonly string? Suffix;
2019-06-30 20:58:30 +08:00
/// <summary>
/// The sample volume.
/// </summary>
2020-12-01 14:37:51 +08:00
public int Volume { get; }
2024-08-30 03:54:47 +08:00
/// <summary>
/// Whether this sample should automatically assign the bank of the normal sample whenever it is set in the editor.
/// </summary>
public bool EditorAutoBank { get; }
public HitSampleInfo(string name, string bank = SampleControlPoint.DEFAULT_BANK, string? suffix = null, int volume = 100, bool editorAutoBank = true)
2020-12-01 14:37:51 +08:00
{
Name = name;
Bank = bank;
Suffix = suffix;
Volume = volume;
2024-08-30 03:54:47 +08:00
EditorAutoBank = editorAutoBank;
2020-12-01 14:37:51 +08:00
}
2019-06-30 20:58:30 +08:00
/// <summary>
/// Retrieve all possible filenames that can be used as a source, returned in order of preference (highest first).
/// </summary>
[JsonIgnore]
2019-06-30 20:58:30 +08:00
public virtual IEnumerable<string> LookupNames
{
get
{
if (!string.IsNullOrEmpty(Suffix))
yield return $"Gameplay/{Bank}-{Name}{Suffix}";
2019-06-30 20:58:30 +08:00
yield return $"Gameplay/{Bank}-{Name}";
yield return $"Gameplay/{Name}";
2019-06-30 20:58:30 +08:00
}
}
2020-12-01 14:37:51 +08:00
/// <summary>
/// Creates a new <see cref="HitSampleInfo"/> with overridden values.
/// </summary>
2020-12-02 09:55:48 +08:00
/// <param name="newName">An optional new sample name.</param>
/// <param name="newBank">An optional new sample bank.</param>
/// <param name="newSuffix">An optional new lookup suffix.</param>
/// <param name="newVolume">An optional new volume.</param>
2024-08-30 03:54:47 +08:00
/// <param name="newEditorAutoBank">An optional new editor auto bank flag.</param>
2020-12-01 14:37:51 +08:00
/// <returns>The new <see cref="HitSampleInfo"/>.</returns>
2024-08-30 03:54:47 +08:00
public virtual HitSampleInfo With(Optional<string> newName = default, Optional<string> newBank = default, Optional<string?> newSuffix = default, Optional<int> newVolume = default, Optional<bool> newEditorAutoBank = default)
=> new HitSampleInfo(newName.GetOr(Name), newBank.GetOr(Bank), newSuffix.GetOr(Suffix), newVolume.GetOr(Volume), newEditorAutoBank.GetOr(EditorAutoBank));
Fix argon volume-aware hitsounds not correctly playing immediately after object placement Closes https://github.com/ppy/osu/issues/29832. The underlying reason for the incorrect sample playback was an equality comparer failure. Samples are contained in several pools which are managed by the playfield. In particular, the pools are keyed by `ISampleInfo` instances. This means that for correct operation, `ISampleInfo` has to implement `IEquatable<ISampleInfo>` and also provide an appropriately correct `GetHashCode()` implementation. Different audible samples must not compare equal to each other when represented by `ISampleInfo`. As it turns out, `VolumeAwareHitSampleInfo` failed on this, due to not overriding equality members. Therefore, a `new HitSampleInfo(HitSampleInfo.HIT_NORMAL, HitSampleInfo.BANK_NORMAL, volume: 70)` was allowed to compare equal to a `VolumeAwareHitSampleInfo` wrapping it, *even though they correspond to completely different sounds and go through entirely different lookup path sequences*. Therefore, to fix, provide more proper equality implementations for `VolumeAwareHitSampleInfo`. When testing note that this issue *only occurs immediately after placing an object*. Saving and re-entering editor makes this issue go away. I haven't looked too long into why, but the general gist of it is ordering; it appears that a `normal-hitnormal` pool exists at point of query of a new object placement, but does not seem to exist when entering editor afresh. That said I'm not sure that ordering aspect of this bug matters much if at all, since the two `IHitSampleInfo`s should never be allowed to alias with each other at all wrt equality.
2024-09-23 19:27:36 +08:00
public virtual bool Equals(HitSampleInfo? other)
=> other != null && Name == other.Name && Bank == other.Bank && Suffix == other.Suffix;
public override bool Equals(object? obj)
=> obj is HitSampleInfo other && Equals(other);
public override int GetHashCode() => HashCode.Combine(Name, Bank, Suffix);
2019-06-30 20:58:30 +08:00
}
}