Sha256: eeeee7dc8801af9c1f2d5f158f397742a58fca081f47aa3ff374e3367e45d101

Contents?: true

Size: 1.45 KB

Versions: 253

Compression:

Stored size: 1.45 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.OrdinalIgnoreCase));
        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

253 entries across 253 versions & 1 rubygems

Version Path
trackler-2.2.1.151 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.150 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.149 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.148 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.147 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.146 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.145 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.144 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.143 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.142 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.141 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.140 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.139 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.138 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.137 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.136 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.135 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.134 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.133 tracks/csharp/exercises/scale-generator/Example.cs
trackler-2.2.1.132 tracks/csharp/exercises/scale-generator/Example.cs