Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TriangleSoundChannel generate a broken wave in Blazor WASM #26

Open
AutumnSky1010 opened this issue Jun 30, 2024 · 0 comments
Open

TriangleSoundChannel generate a broken wave in Blazor WASM #26

AutumnSky1010 opened this issue Jun 30, 2024 · 0 comments
Assignees
Labels
bug Something isn't working Ver 2.3.x

Comments

@AutumnSky1010
Copy link
Owner

Workaround

Create a original sound channel.

FixedTriangleChannel class

public class FixedTriangleChannel : SoundChannelBase
{
    /// <summary>
    /// constructor コンストラクタ
    /// </summary>
    /// <param name="format">format of the sound. 音のフォーマット</param>
    /// <param name="tempo">quarter note/rest per minute. 一分間の四分音符・休符の数</param>
    /// <param name="panType">sound direction. 左右どちらから音が出るか</param>
    /// <param name="capacity">the total number of sound components the internal data structure can hold without resizing. 内部データ構造がリサイズされずに保持できるサウンドコンポーネントの総数。</param>
    /// <exception cref="ArgumentOutOfRangeException">Tempo must be non-negative and greater than 0.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Capacity must be non-negative.</exception>
    public FixedTriangleChannel(int tempo, SoundFormat format, PanType panType, int capacity) : base(tempo, format, panType, capacity) { }

    /// <summary>
    /// constructor コンストラクタ
    /// </summary>
    /// <param name="format">format of the sound. 音のフォーマット</param>
    /// <param name="tempo">quarter note/rest per minute. 一分間の四分音符・休符の数</param>
    /// <param name="panType">sound direction. 左右どちらから音が出るか</param>
    /// <exception cref="ArgumentOutOfRangeException">Tempo must be non-negative and greater than 0.</exception>
    public FixedTriangleChannel(int tempo, SoundFormat format, PanType panType) : base(tempo, format, panType) { }

    public override ushort[] GenerateWave()
    {
        var result = new List<ushort>();
        foreach (var soundComponent in SoundComponents)
        {
            var wave = soundComponent.GenerateWave(Format, Tempo, new FixedTriangleWave());
            FadeInOut(wave);
            result.AddRange(wave);
        }
        return result.ToArray();
    }
}

FixedTriangleWave

/// <summary>
/// the triangle wave. 三角波
/// </summary>
public class FixedTriangleWave : WaveTypeBase
{
    [Obsolete("Use 'GenerateWave(SoundFormat format, int length, int volume, double hertz)'")]
    public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz)
    {
        CheckGenerateWaveArgs(tempo, length, volume, hertz);
        return GenerateWave(format, length, volume, hertz);
    }

    public override ushort[] GenerateWave(SoundFormat format, int length, int volume, double hertz)
    {
        CheckGenerateWaveArgs(length, volume, hertz);
        var result = new List<ushort>(length);
        var unitWave = GenerateUnitWave(format, volume, hertz);
        for (var i = 0; i < length / unitWave.Count; i++)
        {
            result.AddRange(unitWave);
        }
        for (var i = 0; i < length % unitWave.Count; i++)
        {
            result.Add(0);
        }
        return result.ToArray();
    }

    private List<ushort> GenerateUnitWave(SoundFormat format, int volume, double hertz)
    {
        var repeatNumber = (int)format.SamplingFrequency / hertz;

        var result = new ushort[(int)repeatNumber];

        var slope = ushort.MaxValue / (repeatNumber / 2);

        var volumeMagnification = volume / 100d;

        var halfCount = (int)(repeatNumber / 2);

        for (var i = 0; i < halfCount; i++)
        {
            var sound = (ushort)(slope * (i + 1));
            sound = (ushort)(sound * volumeMagnification);
            result[i] = sound;
            result[result.Length - i - 1] = sound;
        }

        if (result.Length % 2 == 1)
        {
            result[halfCount] = (ushort)(ushort.MaxValue * volumeMagnification);
        }

        return result.ToList();
    }
}
@AutumnSky1010 AutumnSky1010 added bug Something isn't working Ver 2.3.x labels Jun 30, 2024
@AutumnSky1010 AutumnSky1010 self-assigned this Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Ver 2.3.x
Projects
Status: Todo
Development

No branches or pull requests

1 participant