Sha256: 22cf3b92b243bf86b123c39ba3ef3f144f2ab3c6e75dbd9d415b698d0ea8635c

Contents?: true

Size: 1.46 KB

Versions: 115

Compression:

Stored size: 1.46 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public static class ScaleGenerator
{
    private static readonly string[] ChromaticScale = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
    private static readonly string[] FlatChromaticScale = { "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" };
    private static readonly string[] FlatKeys = { "F", "Bb", "Eb", "Ab", "Db", "Gb", "d", "g", "c", "f", "bb", "eb" };

    private static readonly Dictionary<char, int> Intervals = new Dictionary<char, int> { ['m'] = 1, ['M'] = 2, ['A'] = 3 };
    
    private static string[] Scale(string tonic) => FlatKeys.Contains(tonic) ? FlatChromaticScale : ChromaticScale;
    private static string[] SkipInterval(char interval, string[] scale) => scale.Skip(Intervals[interval]).ToArray();
    private static string[] Shift(int index, string[] scale) => scale.Skip(index).Concat(scale.Take(index)).ToArray();

    public static string[] Pitches(string tonic, string pattern)
    {
        var scale = Scale(tonic);
        var index = Array.FindIndex(scale, pitch => string.Equals(pitch, tonic, StringComparison.InvariantCultureIgnoreCase));
        var shiftedScale = Shift(index, scale);

        var pitches = new List<string>();

        foreach (var interval in pattern)
        {
            pitches.Add(shiftedScale[0]);
            shiftedScale = SkipInterval(interval, shiftedScale);
        }

        return pitches.ToArray();
    }
}

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
trackler-2.0.5.18 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.17 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.16 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.15 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.14 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.13 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.12 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.11 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.10 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.9 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.8 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.7 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.6 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.5 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.4 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.3 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.2 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.1 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.5.0 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.4.0 tracks/csharp/exercises/scale-generator/Example.cs