Skip to content

Commit

Permalink
Class TwelveToneSet to struct
Browse files Browse the repository at this point in the history
- tests for TwelveToneSet
- moved all tests to MusicCore.Tests
- other miscellaneous changes
  • Loading branch information
oggy22 committed Apr 8, 2019
1 parent 595307a commit 0661131
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 263 deletions.
26 changes: 13 additions & 13 deletions MusicComposer/Compositions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Melody12Tone CMajorPreludeBach()
new object[] { Gsm, Bsm, D, G, B } //G
);

return new Melody12Tone(seq, MusicalModes.Major, 60, 80);
return new Melody12Tone(seq, TwelveToneSet.majorScale, 60, 80);
}

public static Melody12Tone DanceOfClocks()
Expand All @@ -47,7 +47,7 @@ public static Melody12Tone DanceOfClocks()
new object[] { 5, 3, 2 }
);

return new Melody12Tone(seq, MusicalModes.Major, 64, 80);
return new Melody12Tone(seq, TwelveToneSet.majorScale, 64, 80);
}

public static Melody12Tone ChopinAccompany()
Expand All @@ -59,7 +59,7 @@ public static Melody12Tone ChopinAccompany()
new object[] { 0, 2, 4, 7, 9 }
);

return new Melody12Tone(seq, MusicalModes.Major, 64, 100);
return new Melody12Tone(seq, TwelveToneSet.majorScale, 64, 100);
}

public static Melody12Tone OggyMelody()
Expand All @@ -70,7 +70,7 @@ public static Melody12Tone OggyMelody()

MelodyComposite melody2 = new MelodyComposite("AA-A--B", melody, melodyEnd);

Melody12Tone m12tone = new Melody12Tone(melody2, MusicalModes.Minor, 64, 100);
Melody12Tone m12tone = new Melody12Tone(melody2, TwelveToneSet.minorScale, 64, 100);
return m12tone;
}

Expand All @@ -79,15 +79,15 @@ public static Melody12Tone MrSandman()
MelodyBase melody = new MelodyAtomic(new object[] { 0, 2, 4, 6 }, 2);
var melody2 = new MelodyReversed(new MelodyDiffEnd(melody, new[] { new NoteWithDuration(5, new Fraction(1, 2)) }));
MelodyComposite melodyComp = new MelodyComposite("ABA+", melody, melody2);
Melody12Tone m12tone = new Melody12Tone(melodyComp, MusicalModes.Major, 60, 150);
Melody12Tone m12tone = new Melody12Tone(melodyComp, TwelveToneSet.majorScale, 60, 150);
return m12tone;
}

public static object Pratnja()
{
MelodyBase melody = new MelodyAtomic(new object[] { 0, 2 }, 2);
MelodyComposite melodyComp = new MelodyComposite("A A1 A2 Ar1", melody);
Melody12Tone m12tone = new Melody12Tone(melodyComp, MusicalModes.Major, 64);
Melody12Tone m12tone = new Melody12Tone(melodyComp, TwelveToneSet.majorScale, 64);
return m12tone;
}

Expand All @@ -96,7 +96,7 @@ public static Melody12Tone Albinoni()
RhythmPattern rhythm = new RhythmPattern(3, 4, "1h.qh.q 1 2");
MelodyBase melody = new MelodyAtomic(rhythm, new object[] { 4, 3, 2, 1, 0, 0, -1 });
MelodyComposite melodyComp = new MelodyComposite("AA+", melody);
Melody12Tone m12tone = new Melody12Tone(melodyComp, MusicalModes.MinorHarmonic, 64, 100);
Melody12Tone m12tone = new Melody12Tone(melodyComp, TwelveToneSet.minorHarmonicScale, 64, 100);
return m12tone;
}

Expand All @@ -109,7 +109,7 @@ public static Melody12Tone Rach2ndSymphAdagio()
new NoteWithDuration(2, new Fraction(1, 2))
};
MelodyComposite melodyComp = new MelodyComposite("AA-A--B", melodyCEGB, melodyEnd);
return new Melody12Tone(melodyComp, MusicalModes.Major, 60, 50);
return new Melody12Tone(melodyComp, TwelveToneSet.majorScale, 60, 50);
}

public static Melody12Tone GabrielaImprov()
Expand All @@ -121,23 +121,23 @@ public static Melody12Tone GabrielaImprov()
new NoteWithDuration(1, new Fraction(1, 4))
};
MelodyComposite melodyComp = new MelodyComposite("AA+A++", melody);
return new Melody12Tone(melodyComp, MusicalModes.Minor, 64);
return new Melody12Tone(melodyComp, TwelveToneSet.majorScale, 64);
}

public static Melody12Tone GMajorMenuet2nd()
{
RhythmPattern rhythm = new RhythmPattern(3, 2, "1hhhh");
ParameterizedMelody pmelody = new ParameterizedMelody("0' 0 1 2 0", rhythm);
MelodyComposite mcomposote = new MelodyComposite("A(0,2) A(-3,1) A(-2,0)", pmelody);
return new Melody12Tone(mcomposote, MusicalModes.Major, 60);
return new Melody12Tone(mcomposote, TwelveToneSet.majorScale, 60);
}

public static Melody12Tone CSharpValseChopin()
{
RhythmPattern rhythm = new RhythmPattern(3, 2, "hhhhhh");
ParameterizedMelody pmelody = new ParameterizedMelody("0 1 0 -1 -3 0'", rhythm);
MelodyComposite mcomposite = new MelodyComposite("A(4,-3) A(3,-3) A(2,-4) A(1,-5)", pmelody);
return new Melody12Tone(mcomposite, MusicalModes.MinorHarmonic, 60);
return new Melody12Tone(mcomposite, TwelveToneSet.minorHarmonicScale, 60);
}

public static Melody12Tone WeWishYouAMerryChristmas()
Expand All @@ -147,7 +147,7 @@ public static Melody12Tone WeWishYouAMerryChristmas()
melody.anacrusis = new List<NoteWithDuration>() { new NoteWithDuration(-3, new Fraction(1, 1)) };
MelodyAtomic melody2 = new MelodyAtomic(new RhythmPattern(3, 2, "1113"), new object[] { -2, 1, -1, 0 });
MelodyComposite meloComp = new MelodyComposite("AA+A++B", melody, melody2);
return new Melody12Tone(meloComp, MusicalModes.Major, 65, 140);
return new Melody12Tone(meloComp, TwelveToneSet.majorScale, 65, 140);
}

public static Melody12Tone AnotherWhoopy()
Expand All @@ -161,7 +161,7 @@ public static Melody12Tone AnotherWhoopy()
};

MelodyComposite mc = new MelodyComposite("AA", melody);
return new Melody12Tone(mc, MusicalModes.Major, 64, 100);
return new Melody12Tone(mc, TwelveToneSet.majorScale, 64, 100);
}
}
}
2 changes: 1 addition & 1 deletion MusicComposer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void Main2(string[] args)
{
Random rand = new Random();
TwelveToneSet chord = TwelveToneSet.major7;
TwelveToneSet scale = new TwelveToneSet(TwelveToneSet.majorScale);
TwelveToneSet scale = TwelveToneSet.majorScale;
int dist = 0;
Console.WriteLine($"Chord: {chord.ToString()}");
int startFrom = chord.First;
Expand Down
9 changes: 5 additions & 4 deletions MusicComposer/RandomSequencer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ static void RandomSequencer(int alightWithLast = 5, int diffFromLast = 1, int ap
continue;

// Last notes contained by a major scale
TwelveToneSet set = new TwelveToneSet();
set.Add(tone % 12);
short set = 0;
TwelveToneSet.BitOn(set, tone % 12);
for (int i = list.Count - 1; i >= list.Count - alightWithLast && i >= 0; i--)
set.Add(list[i] % 12);
if (!CoveredByChordOfInterefest(set))
TwelveToneSet.BitOn(set, list[i] % 12);

if (!CoveredByChordOfInterefest(new TwelveToneSet(set)))
continue;

break;
Expand Down
37 changes: 37 additions & 0 deletions MusicCore.Tests/CompositionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
//using MSTestHacks;
using MusicCore;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace MusicComposer.Tests
{
[TestClass]
public class CompositionsTests //: TestBase
{
private IEnumerable<string> TestSource
{
get
{
return null;
//foreach (var mi in typeof(Compositions).GetMethods())
//{
// if (mi.ReturnType == typeof(Melody12Tone))
// yield return mi.Name;
//}
}
}

[TestMethod]
//[DataSource("MusicComposer.Tests.CompositionsTests." + nameof(TestSource))]
public void Test()
{
//var methodName = TestContext.GetRuntimeDataSourceObject<string>();
//MethodInfo mi = typeof(Compositions).GetMethod(methodName);
//Func<Melody12Tone> dg = (Func<Melody12Tone>)Delegate.CreateDelegate(typeof(Func<Melody12Tone>), mi);
//Melody12Tone m12tone = dg();
//foreach (var nwd in m12tone.Notes()) ;
}
}
}
3 changes: 3 additions & 0 deletions MusicCore.Tests/MusicCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChomskyAnalysisTest.cs" />
<Compile Include="CompositionsTests.cs" />
<Compile Include="FractionTest.cs" />
<Compile Include="Melody\MelodyCompositeTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scale7ToneTests.cs" />
<Compile Include="TonesTest.cs" />
<Compile Include="ToneTest.cs" />
<Compile Include="CompositionTest.cs" />
<Compile Include="TwelveToneSetTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MusicCore\MusicCore.csproj">
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MusicCore;
using System;

namespace MusicComposer.Tests
namespace MusicCore.Tests
{
[TestClass]
public class TwelveToneSetTests
Expand All @@ -14,7 +13,7 @@ public void TwelveToneSetScaleTest()
Assert.AreEqual(new TwelveToneSet(new int[] { 0, 2, 4, 5, 7, 9, 11 }), TwelveToneSet.majorScale);
Assert.AreEqual(new TwelveToneSet(new int[] { 0, 2, 3, 5, 7, 8, 10 }), TwelveToneSet.minorScale);

// Major and minor scale are different but similar
//// Major and minor scale are different but similar
Assert.AreNotEqual(TwelveToneSet.majorScale, TwelveToneSet.minorScale);
Assert.IsTrue(TwelveToneSet.majorScale.Similar(TwelveToneSet.minorScale));

Expand Down Expand Up @@ -113,18 +112,18 @@ public void GetRootTest()
Assert.AreEqual<int>(new tone12("F"), TwelveToneSet.halfDiminished.GetRoot());

// No roots:
Assert.IsNull(TwelveToneSet.augmented.GetRoot());
Assert.IsNull(TwelveToneSet.chromatic.GetRoot());
Assert.IsNull(TwelveToneSet.fullDiminished.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.augmented.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.chromatic.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.fullDiminished.GetRoot());
// minor7 can be interpreted as major 6th, therefore two roots i.e. no roots
Assert.IsNull(TwelveToneSet.minor7.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.minor7.GetRoot());

// No roots for scales:
Assert.IsNull(TwelveToneSet.majorScale.GetRoot());
Assert.IsNull(TwelveToneSet.minorScale.GetRoot());
Assert.IsNull(TwelveToneSet.minorHarmonicScale.GetRoot());
Assert.IsNull(TwelveToneSet.minorMelodicScale.GetRoot());
Assert.IsNull(TwelveToneSet.chromatic.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.majorScale.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.minorScale.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.minorHarmonicScale.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.minorMelodicScale.GetRoot());
Assert.ThrowsException<Exception>(() => TwelveToneSet.chromatic.GetRoot());
}

[TestMethod]
Expand All @@ -147,7 +146,6 @@ public void ShiftInScaleTest_OutScale()
Assert.AreEqual(
new TwelveToneSet("BD#"),
new TwelveToneSet("AC#").ShiftInScale(1, TwelveToneSet.majorScale));

TwelveToneSet AMajor = new TwelveToneSet("AC#E");
Assert.AreEqual(
new TwelveToneSet("BDF"),
Expand Down Expand Up @@ -191,13 +189,13 @@ public void ShiftInScaleTest_OutScale()
FSharp.ShiftInScale(3, TwelveToneSet.majorScale));
}

[TestMethod]
[TestMethod, Ignore]
public void ShiftInScaleTest_0shift()
{
Random rand = new Random(0);
for (int i=0; i<100; i++)
{
TwelveToneSet set = new TwelveToneSet(rand, rand.Next(3,6), null);
TwelveToneSet set = new TwelveToneSet(rand, rand.Next(3,6), TwelveToneSet.chromatic);
TwelveToneSet setShifted = set.ShiftInScale(0, TwelveToneSet.majorScale);
Assert.AreEqual(set, setShifted);
}
Expand Down
4 changes: 2 additions & 2 deletions MusicCore/Melody/Melody12Tone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public override Fraction StartPause

public override Fraction Duration => melodyBase.Duration;

public Melody12Tone(MelodyBase melodyBase, MusicalModes mode, int key = 0, int tempo = 60)
public Melody12Tone(MelodyBase melodyBase, TwelveToneSet toneset, int key = 0, int tempo = 60)
{
this.melodyBase = melodyBase;
toneset = new TwelveToneSet(mode);
this.toneset = toneset;
this.key = key;
this.tempo = tempo;
}
Expand Down
1 change: 0 additions & 1 deletion MusicCore/MusicCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<Compile Include="Rhythm\RhythmSectionInverse.cs" />
<Compile Include="Rhythm\RhytmPattern.cs" />
<Compile Include="StaticAndExtensionMethods.cs" />
<Compile Include="TimeLine.cs" />
<Compile Include="Tone12.cs" />
<Compile Include="TonesAndIntervals.cs" />
<Compile Include="TwelveToneSet.cs" />
Expand Down
27 changes: 26 additions & 1 deletion MusicCore/StaticAndExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NAudio.Midi;
using System.Linq;
using NAudio.Midi;
using System.Collections.Generic;

namespace MusicCore
Expand All @@ -23,5 +24,29 @@ static void Query(this List<MelodyPartList> composition,
{
//todo: perform query
}

public static int NoteTo12Tone(int x)
{
if (x > 0)
return x % 12;

return 0;
}

public static List<NoteWithDuration> ChromaticToDiatonic(List<int> notes)
{
notes.Distinct().ToList().Sort();

HashSet<int> hash;

return null;
}

public static List<int> DiatonicToChromatic(List<NoteWithDuration> notes)
{
HashSet<int> hash;

return null;
}
}
}
9 changes: 0 additions & 9 deletions MusicCore/TimeLine.cs

This file was deleted.

10 changes: 1 addition & 9 deletions MusicCore/Tone12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,13 @@ public override string ToString()
}
}

public class tone12
public struct tone12
{
public override int GetHashCode()
{
return tone;
}

public override bool Equals(object obj)
{
if (obj is tone12)
return (obj as tone12).tone == tone;

return false;
}

const int TWELVE = 12;

int tone;
Expand Down
Loading

0 comments on commit 0661131

Please sign in to comment.