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.8.14 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.13 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.12 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.11 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.10 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.9 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.8 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.7 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.6 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.5 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.4 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.3 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.2 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.8.1 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.7.0 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.6.44 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.6.43 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.6.42 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.6.41 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.0.6.40 tracks/csharp/exercises/scale-generator/Example.cs