From 796150053b386f71b1fb55dafd25a55ddb2aae21 Mon Sep 17 00:00:00 2001
From: AutumnSky1010 <66455966+AutumnSky1010@users.noreply.github.com>
Date: Sun, 10 Mar 2024 14:11:14 +0900
Subject: [PATCH] :bug: Fix a bug of scale number
---
src/SoundMaker/Sounds/Score/Note.cs | 47 +++++++++++++++++-----
test/IntegrationTests/TestFormatBuilder.cs | 2 +-
test/SoundMakerTests.csproj | 10 +++--
test/UnitTests/Sounds/Score/TestNote.cs | 21 ++++++----
4 files changed, 58 insertions(+), 22 deletions(-)
diff --git a/src/SoundMaker/Sounds/Score/Note.cs b/src/SoundMaker/Sounds/Score/Note.cs
index c8082c5..928d0c1 100644
--- a/src/SoundMaker/Sounds/Score/Note.cs
+++ b/src/SoundMaker/Sounds/Score/Note.cs
@@ -6,6 +6,43 @@ namespace SoundMaker.Sounds.Score;
///
public class Note : BasicSoundComponentBase
{
+ private static readonly IReadOnlyDictionary<(Scale, int), double> _hertz = new Dictionary<(Scale, int), double>()
+ {
+ // A0 ~
+ {(Scale.A, 0), 27.500 },
+ {(Scale.ASharp, 0), 29.135 },
+ {(Scale.B, 0), 30.868 },
+ // C1 ~ C2 ~ C3 ~ C4 ~
+ {(Scale.C, 1), 32.703 }, {(Scale.C, 2), 65.406 }, {(Scale.C, 3), 130.813 }, {(Scale.C, 4), 261.626 },
+ {(Scale.CSharp, 1), 34.648 }, {(Scale.CSharp, 2), 69.296 }, {(Scale.CSharp, 3), 138.591 }, {(Scale.CSharp, 4), 277.183 },
+ {(Scale.D, 1), 36.708 }, {(Scale.D, 2), 73.416 }, {(Scale.D, 3), 146.832 }, {(Scale.D, 4), 293.665 },
+ {(Scale.DSharp, 1), 38.891 }, {(Scale.DSharp, 2), 77.782 }, {(Scale.DSharp, 3), 155.563 }, {(Scale.DSharp, 4), 311.127 },
+ {(Scale.E, 1), 41.203 }, {(Scale.E, 2), 82.407 }, {(Scale.E, 3), 164.814 }, {(Scale.E, 4), 329.628 },
+ {(Scale.F, 1), 43.654 }, {(Scale.F, 2), 87.307 }, {(Scale.F, 3), 174.614 }, {(Scale.F, 4), 349.228 },
+ {(Scale.FSharp, 1), 46.249 }, {(Scale.FSharp, 2), 92.499 }, {(Scale.FSharp, 3), 184.997 }, {(Scale.FSharp, 4), 369.994 },
+ {(Scale.G, 1), 48.999 }, {(Scale.G, 2), 97.999 }, {(Scale.G, 3), 195.998 }, {(Scale.G, 4), 391.995 },
+ {(Scale.GSharp, 1), 51.913 }, {(Scale.GSharp, 2), 103.826 }, {(Scale.GSharp, 3), 207.652 }, {(Scale.GSharp, 4), 415.305 },
+ {(Scale.A, 1), 55.000 }, {(Scale.A, 2), 110.000 }, {(Scale.A, 3), 220.000 }, {(Scale.A, 4), 440.000 },
+ {(Scale.ASharp, 1), 58.270 }, {(Scale.ASharp, 2), 116.541 }, {(Scale.ASharp, 3), 233.082 }, {(Scale.ASharp, 4), 466.164 },
+ {(Scale.B, 1), 61.735 }, {(Scale.B, 2), 123.471 }, {(Scale.B, 3), 246.942 }, {(Scale.B, 4), 493.883 },
+ // C5 ~ C6 ~ C7 ~
+ {(Scale.C, 5), 523.251 }, {(Scale.C, 6), 1046.502 }, {(Scale.C, 7), 2093.005 },
+ {(Scale.CSharp, 5), 554.365 }, {(Scale.CSharp, 6), 1108.731 }, {(Scale.CSharp, 7), 2217.461 },
+ {(Scale.D, 5), 587.330 }, {(Scale.D, 6), 1174.659 }, {(Scale.D, 7), 2349.318 },
+ {(Scale.DSharp, 5), 622.254 }, {(Scale.DSharp, 6), 1244.508 }, {(Scale.DSharp, 7), 2489.016 },
+ {(Scale.E, 5), 659.255 }, {(Scale.E, 6), 1318.510 }, {(Scale.E, 7), 2637.020 },
+ {(Scale.F, 5), 698.456 }, {(Scale.F, 6), 1396.913 }, {(Scale.F, 7), 2793.826 },
+ {(Scale.FSharp, 5), 739.989 }, {(Scale.FSharp, 6), 1479.978 }, {(Scale.FSharp, 7), 2959.955 },
+ {(Scale.G, 5), 783.991 }, {(Scale.G, 6), 1567.982 }, {(Scale.G, 7), 3135.963 },
+ {(Scale.GSharp, 5), 830.609 }, {(Scale.GSharp, 6), 1661.219 }, {(Scale.GSharp, 7), 3322.438 },
+ {(Scale.A, 5), 880.000 }, {(Scale.A, 6), 1760.000 }, {(Scale.A, 7), 3520.000 },
+ {(Scale.ASharp, 5), 932.328 }, {(Scale.ASharp, 6), 1864.655 }, {(Scale.ASharp, 7), 3729.310 },
+ {(Scale.B, 5), 987.767 }, {(Scale.B, 6), 1975.533 }, {(Scale.B, 7), 3951.066 },
+ // C8
+ {(Scale.C, 8), 4186.009 },
+ };
+
+
///
/// constructor コンストラクタ
///
@@ -18,15 +55,7 @@ public Note(Scale scale, int scaleNumber, LengthType length, bool isDotted = fal
: base(length, isDotted)
{
CheckArgument(scale, scaleNumber);
- if ((int)scale >= 3)
- {
- scaleNumber--;
- }
- Hertz += AHertz[scaleNumber];
- for (var i = 0; i < (int)scale; i++)
- {
- Hertz *= 1.059463094;
- }
+ Hertz += _hertz[(scale, scaleNumber)];
Scale = scale;
ScaleNumber = scaleNumber;
}
diff --git a/test/IntegrationTests/TestFormatBuilder.cs b/test/IntegrationTests/TestFormatBuilder.cs
index 205da5b..4a2e0e6 100644
--- a/test/IntegrationTests/TestFormatBuilder.cs
+++ b/test/IntegrationTests/TestFormatBuilder.cs
@@ -17,7 +17,7 @@ public void TestToSoundFormat()
}
[Fact()]
- public void TestToFormatChunc()
+ public void TestToFormatChunk()
{
var formatBuilder = FormatBuilder.Create()
.WithFrequency(48000)
diff --git a/test/SoundMakerTests.csproj b/test/SoundMakerTests.csproj
index fb3be2a..0f17d1a 100644
--- a/test/SoundMakerTests.csproj
+++ b/test/SoundMakerTests.csproj
@@ -8,6 +8,12 @@
false
+
+
+
+
+
+
@@ -21,10 +27,6 @@
-
-
-
-
diff --git a/test/UnitTests/Sounds/Score/TestNote.cs b/test/UnitTests/Sounds/Score/TestNote.cs
index 0eb8609..46f0029 100644
--- a/test/UnitTests/Sounds/Score/TestNote.cs
+++ b/test/UnitTests/Sounds/Score/TestNote.cs
@@ -3,26 +3,31 @@
namespace SoundMakerTests.UnitTests.Sounds.Score;
public class TestNote
{
- [Fact(DisplayName = "初期化が正しく行えるかのテスト")]
- public void InitializeTest()
+ [Theory(DisplayName = "初期化が正しく行えるかのテスト")]
+ [InlineData(Scale.A, 4, LengthType.Whole, true)]
+ [InlineData(Scale.C, 4, LengthType.SixtyFourth, true)]
+ public void InitializeTest(Scale scale, int scaleNumber, LengthType lengthType, bool isDotted)
{
- var isDotted = true;
- var lengthType = LengthType.Whole;
- var scaleNum = 4;
- var scale = Scale.A;
- var note = new Note(scale, scaleNum, lengthType, isDotted);
+ var note = new Note(scale, scaleNumber, lengthType, isDotted);
Assert.Equal(scale, note.Scale);
- Assert.Equal(scaleNum, note.ScaleNumber);
+ Assert.Equal(scaleNumber, note.ScaleNumber);
Assert.Equal(lengthType, note.Length);
Assert.Equal(isDotted, note.IsDotted);
Assert.Equal(100, note.Volume);
+ }
+ [Fact(DisplayName = "例外のテスト")]
+ public void InitializeExceptionTest()
+ {
+ var isDotted = true;
+ var lengthType = LengthType.Whole;
// 例外のテスト
_ = Assert.Throws(() => new Note(Scale.G, 0, lengthType, isDotted));
_ = Assert.Throws(() => new Note(Scale.D, 8, lengthType, isDotted));
_ = Assert.Throws(() => new Note(Scale.G, 9, lengthType, isDotted));
_ = Assert.Throws(() => new Note(Scale.G, -1, lengthType, isDotted));
}
+
[Fact(DisplayName = "簡単なコンストラクタで初期化が正しく行えるかのテスト")]
public void EasinessInitializeTest()
{