Avatar of paulfioravanti

paulfioravanti's solution

to Scale Generator in the Ruby Track

Published at May 09 2019 · 0 comments
Instructions
Test suite
Solution

Given a tonic, or starting note, and a set of intervals, generate the musical scale starting with the tonic and following the specified interval pattern.

Scales in Western music are based on the chromatic (12-note) scale. This scale can be expressed as the following group of pitches:

A, A#, B, C, C#, D, D#, E, F, F#, G, G#

A given sharp note (indicated by a #) can also be expressed as the flat of the note above it (indicated by a b) so the chromatic scale can also be written like this:

A, Bb, B, C, Db, D, Eb, E, F, Gb, G, Ab

The major and minor scale and modes are subsets of this twelve-pitch collection. They have seven pitches, and are called diatonic scales. The collection of notes in these scales is written with either sharps or flats, depending on the tonic. Here is a list of which are which:

No Sharps or Flats: C major a minor

Use Sharps: G, D, A, E, B, F# major e, b, f#, c#, g#, d# minor

Use Flats: F, Bb, Eb, Ab, Db, Gb major d, g, c, f, bb, eb minor

The diatonic scales, and all other scales that derive from the chromatic scale, are built upon intervals. An interval is the space between two pitches.

The simplest interval is between two adjacent notes, and is called a "half step", or "minor second" (sometimes written as a lower-case "m"). The interval between two notes that have an interceding note is called a "whole step" or "major second" (written as an upper-case "M"). The diatonic scales are built using only these two intervals between adjacent notes.

Non-diatonic scales can contain other intervals. An "augmented first" interval, written "A", has two interceding notes (e.g., from A to C or Db to E). There are also smaller and larger intervals, but they will not figure into this exercise.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby scale_generator_test.rb

To include color from the command line:

ruby -r minitest/pride scale_generator_test.rb

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

scale_generator_test.rb

require 'minitest/autorun'
require_relative 'scale_generator'

class ScaleGeneratorTest < Minitest::Test
  def test_naming_scale
    chromatic = Scale.new('c', :chromatic)
    expected = 'C chromatic'
    actual = chromatic.name
    assert_equal expected, actual
  end

  def test_chromatic_scale
    skip
    chromatic = Scale.new('C', :chromatic)
    expected = %w(C C# D D# E F F# G G# A A# B)
    actual = chromatic.pitches
    assert_equal expected, actual
  end

  def test_another_chromatic_scale
    skip
    chromatic = Scale.new('F', :chromatic)
    expected = %w(F Gb G Ab A Bb B C Db D Eb E)
    actual = chromatic.pitches
    assert_equal expected, actual
  end

  def test_naming_major_scale
    skip
    major = Scale.new('G', :major, 'MMmMMMm')
    expected = 'G major'
    actual = major.name
    assert_equal expected, actual
  end

  def test_major_scale
    skip
    major = Scale.new('C', :major, 'MMmMMMm')
    expected = %w(C D E F G A B)
    actual = major.pitches
    assert_equal expected, actual
  end

  def test_another_major_scale
    skip
    major = Scale.new('G', :major, 'MMmMMMm')
    expected = %w(G A B C D E F#)
    actual = major.pitches
    assert_equal expected, actual
  end

  def test_minor_scale
    skip
    minor = Scale.new('f#', :minor, 'MmMMmMM')
    expected = %w(F# G# A B C# D E)
    actual = minor.pitches
    assert_equal expected, actual
  end

  def test_another_minor_scale
    skip
    minor = Scale.new('bb', :minor, 'MmMMmMM')
    expected = %w(Bb C Db Eb F Gb Ab)
    actual = minor.pitches
    assert_equal expected, actual
  end

  def test_dorian_mode
    skip
    dorian = Scale.new('d', :dorian, 'MmMMMmM')
    expected = %w(D E F G A B C)
    actual = dorian.pitches
    assert_equal expected, actual
  end

  def test_mixolydian_mode
    skip
    mixolydian = Scale.new('Eb', :mixolydian, 'MMmMMmM')
    expected = %w(Eb F G Ab Bb C Db)
    actual = mixolydian.pitches
    assert_equal expected, actual
  end

  def test_lydian_mode
    skip
    lydian = Scale.new('a', :lydian, 'MMMmMMm')
    expected = %w(A B C# D# E F# G#)
    actual = lydian.pitches
    assert_equal expected, actual
  end

  def test_phrygian_mode
    skip
    phrygian = Scale.new('e', :phrygian, 'mMMMmMM')
    expected = %w(E F G A B C D)
    actual = phrygian.pitches
    assert_equal expected, actual
  end

  def test_locrian_mode
    skip
    locrian = Scale.new('g', :locrian, 'mMMmMMM')
    expected = %w(G Ab Bb C Db Eb F)
    actual = locrian.pitches
    assert_equal expected, actual
  end

  def test_harmonic_minor
    skip
    harmonic_minor = Scale.new('d', :harmonic_minor, 'MmMMmAm')
    expected = %w(D E F G A Bb Db)
    actual = harmonic_minor.pitches
    assert_equal expected, actual
  end

  def test_octatonic
    skip
    octatonic = Scale.new('C', :octatonic, 'MmMmMmMm')
    expected = %w(C D D# F F# G# A B)
    actual = octatonic.pitches
    assert_equal expected, actual
  end

  def test_hexatonic
    skip
    hexatonic = Scale.new('Db', :hexatonic, 'MMMMMM')
    expected = %w(Db Eb F G A B)
    actual = hexatonic.pitches
    assert_equal expected, actual
  end

  def test_pentatonic
    skip
    pentatonic = Scale.new('A', :pentatonic, 'MMAMA')
    expected = %w(A B C# E F#)
    actual = pentatonic.pitches
    assert_equal expected, actual
  end

  def test_enigmatic
    skip
    enigmatic = Scale.new('G', :enigma, 'mAMMMmM')
    expected = %w(G G# B C# D# F F#)
    actual = enigmatic.pitches
    assert_equal expected, actual
  end
end
# frozen_string_literal: true

class Scale
  FLAT_SCALE_TONICS = %w[F Bb Eb Ab Db Gb d g c f bb eb].freeze
  private_constant :FLAT_SCALE_TONICS
  FLAT_SCALE = %w[C Db D Eb E F Gb G Ab A Bb B].freeze
  private_constant :FLAT_SCALE
  INITIAL_SCALE_INDEX = 0
  private_constant :INITIAL_SCALE_INDEX
  INTERVALS = { "m" => 1, "M" => 2, "A" => 3 }.freeze
  private_constant :INTERVALS
  NAME_FORMAT = "%<tonic>s %<scale_name>s"
  private_constant :NAME_FORMAT
  SHARP_SCALE = %w[C C# D D# E F F# G G# A A# B].freeze
  private_constant :SHARP_SCALE

  def initialize(tonic, scale_name, intervals = nil)
    @tonic = tonic.capitalize
    @scale_name = scale_name
    @intervals = intervals
    @scale = FLAT_SCALE_TONICS.include?(tonic) ? FLAT_SCALE : SHARP_SCALE
  end

  def name
    format(NAME_FORMAT, tonic: tonic, scale_name: scale_name)
  end

  def pitches
    tonic_scale =
      scale
      .index(tonic)
      .then { |index| scale.rotate(index) }
    return tonic_scale unless intervals

    generate_pitches(tonic_scale)
  end

  private

  attr_reader :tonic, :scale_name, :intervals, :scale

  def generate_pitches(tonic_scale)
    intervals
      .chars
      .each
      .with_object(tonic_scale)
      .reduce([INITIAL_SCALE_INDEX, []], &method(:add_tonic_scale))
      .last
  end

  def add_tonic_scale((last_index, acc), (interval, tonic_scale))
    acc << tonic_scale[last_index]
    last_index += INTERVALS[interval]
    [last_index, acc]
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?