mtk 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.yardopts +3 -2
- data/DEVELOPMENT_NOTES.md +114 -0
- data/INTRO.md +64 -8
- data/LICENSE.txt +1 -1
- data/README.md +31 -102
- data/Rakefile +56 -18
- data/bin/mtk +215 -0
- data/examples/crescendo.rb +5 -5
- data/examples/drum_pattern1.rb +23 -0
- data/examples/dynamic_pattern.rb +8 -11
- data/examples/gets_and_play.rb +26 -0
- data/examples/notation.rb +22 -0
- data/examples/play_midi.rb +8 -10
- data/examples/random_tone_row.rb +2 -2
- data/examples/syntax_to_midi.rb +28 -0
- data/examples/test_output.rb +8 -0
- data/examples/tone_row_melody.rb +6 -6
- data/lib/mtk.rb +52 -40
- data/lib/mtk/chord.rb +55 -0
- data/lib/mtk/constants/durations.rb +57 -0
- data/lib/mtk/constants/intensities.rb +61 -0
- data/lib/mtk/constants/intervals.rb +73 -0
- data/lib/mtk/constants/pitch_classes.rb +29 -0
- data/lib/mtk/constants/pitches.rb +52 -0
- data/lib/mtk/duration.rb +211 -0
- data/lib/mtk/events/event.rb +119 -0
- data/lib/mtk/events/note.rb +112 -0
- data/lib/mtk/events/parameter.rb +54 -0
- data/lib/mtk/helpers/collection.rb +164 -0
- data/lib/mtk/helpers/convert.rb +36 -0
- data/lib/mtk/helpers/lilypond.rb +162 -0
- data/lib/mtk/helpers/output_selector.rb +67 -0
- data/lib/mtk/helpers/pitch_collection.rb +23 -0
- data/lib/mtk/helpers/pseudo_constants.rb +26 -0
- data/lib/mtk/intensity.rb +156 -0
- data/lib/mtk/interval.rb +155 -0
- data/lib/mtk/lang/mtk_grammar.citrus +190 -13
- data/lib/mtk/lang/parser.rb +29 -0
- data/lib/mtk/melody.rb +94 -0
- data/lib/mtk/midi/dls_synth_device.rb +144 -0
- data/lib/mtk/midi/dls_synth_output.rb +62 -0
- data/lib/mtk/midi/file.rb +67 -32
- data/lib/mtk/midi/input.rb +97 -0
- data/lib/mtk/midi/jsound_input.rb +36 -17
- data/lib/mtk/midi/jsound_output.rb +48 -46
- data/lib/mtk/midi/output.rb +195 -0
- data/lib/mtk/midi/unimidi_input.rb +117 -0
- data/lib/mtk/midi/unimidi_output.rb +121 -0
- data/lib/mtk/{_numeric_extensions.rb → numeric_extensions.rb} +12 -0
- data/lib/mtk/patterns/chain.rb +49 -0
- data/lib/mtk/{pattern → patterns}/choice.rb +14 -8
- data/lib/mtk/patterns/cycle.rb +18 -0
- data/lib/mtk/patterns/for_each.rb +71 -0
- data/lib/mtk/patterns/function.rb +39 -0
- data/lib/mtk/{pattern → patterns}/lines.rb +11 -17
- data/lib/mtk/{pattern → patterns}/palindrome.rb +11 -8
- data/lib/mtk/patterns/pattern.rb +171 -0
- data/lib/mtk/patterns/sequence.rb +20 -0
- data/lib/mtk/pitch.rb +7 -6
- data/lib/mtk/pitch_class.rb +124 -46
- data/lib/mtk/pitch_class_set.rb +58 -35
- data/lib/mtk/sequencers/event_builder.rb +131 -0
- data/lib/mtk/sequencers/legato_sequencer.rb +24 -0
- data/lib/mtk/sequencers/rhythmic_sequencer.rb +28 -0
- data/lib/mtk/{sequencer/abstract_sequencer.rb → sequencers/sequencer.rb} +37 -11
- data/lib/mtk/{sequencer → sequencers}/step_sequencer.rb +4 -4
- data/lib/mtk/timeline.rb +39 -22
- data/lib/mtk/variable.rb +32 -0
- data/spec/mtk/chord_spec.rb +83 -0
- data/spec/mtk/{_constants → constants}/durations_spec.rb +12 -41
- data/spec/mtk/{_constants → constants}/intensities_spec.rb +13 -37
- data/spec/mtk/{_constants → constants}/intervals_spec.rb +14 -32
- data/spec/mtk/{_constants → constants}/pitch_classes_spec.rb +8 -4
- data/spec/mtk/{_constants → constants}/pitches_spec.rb +5 -1
- data/spec/mtk/duration_spec.rb +372 -0
- data/spec/mtk/events/event_spec.rb +234 -0
- data/spec/mtk/events/note_spec.rb +174 -0
- data/spec/mtk/events/parameter_spec.rb +220 -0
- data/spec/mtk/{helper → helpers}/collection_spec.rb +86 -3
- data/spec/mtk/{helper → helpers}/pseudo_constants_spec.rb +2 -2
- data/spec/mtk/intensity_spec.rb +289 -0
- data/spec/mtk/interval_spec.rb +265 -0
- data/spec/mtk/lang/parser_spec.rb +597 -0
- data/spec/mtk/melody_spec.rb +223 -0
- data/spec/mtk/midi/file_spec.rb +16 -16
- data/spec/mtk/midi/jsound_input_spec.rb +11 -0
- data/spec/mtk/midi/jsound_output_spec.rb +11 -0
- data/spec/mtk/midi/output_spec.rb +102 -0
- data/spec/mtk/midi/unimidi_input_spec.rb +11 -0
- data/spec/mtk/midi/unimidi_output_spec.rb +11 -0
- data/spec/mtk/{_numeric_extensions_spec.rb → numeric_extensions_spec.rb} +1 -0
- data/spec/mtk/patterns/chain_spec.rb +110 -0
- data/spec/mtk/{pattern → patterns}/choice_spec.rb +20 -30
- data/spec/mtk/{pattern → patterns}/cycle_spec.rb +25 -35
- data/spec/mtk/patterns/for_each_spec.rb +136 -0
- data/spec/mtk/{pattern → patterns}/function_spec.rb +17 -30
- data/spec/mtk/{pattern → patterns}/lines_spec.rb +11 -27
- data/spec/mtk/{pattern → patterns}/palindrome_spec.rb +13 -29
- data/spec/mtk/patterns/pattern_spec.rb +132 -0
- data/spec/mtk/patterns/sequence_spec.rb +203 -0
- data/spec/mtk/pitch_class_set_spec.rb +23 -21
- data/spec/mtk/pitch_class_spec.rb +151 -39
- data/spec/mtk/pitch_spec.rb +22 -1
- data/spec/mtk/sequencers/event_builder_spec.rb +245 -0
- data/spec/mtk/sequencers/legato_sequencer_spec.rb +45 -0
- data/spec/mtk/sequencers/rhythmic_sequencer_spec.rb +84 -0
- data/spec/mtk/sequencers/sequencer_spec.rb +215 -0
- data/spec/mtk/{sequencer → sequencers}/step_sequencer_spec.rb +35 -13
- data/spec/mtk/timeline_spec.rb +109 -16
- data/spec/mtk/variable_spec.rb +52 -0
- data/spec/spec_coverage.rb +2 -0
- data/spec/spec_helper.rb +3 -0
- metadata +188 -91
- data/lib/mtk/_constants/durations.rb +0 -80
- data/lib/mtk/_constants/intensities.rb +0 -81
- data/lib/mtk/_constants/intervals.rb +0 -85
- data/lib/mtk/_constants/pitch_classes.rb +0 -35
- data/lib/mtk/_constants/pitches.rb +0 -49
- data/lib/mtk/event.rb +0 -70
- data/lib/mtk/helper/collection.rb +0 -114
- data/lib/mtk/helper/event_builder.rb +0 -85
- data/lib/mtk/helper/pseudo_constants.rb +0 -26
- data/lib/mtk/lang/grammar.rb +0 -17
- data/lib/mtk/note.rb +0 -63
- data/lib/mtk/pattern/abstract_pattern.rb +0 -132
- data/lib/mtk/pattern/cycle.rb +0 -51
- data/lib/mtk/pattern/enumerator.rb +0 -26
- data/lib/mtk/pattern/function.rb +0 -46
- data/lib/mtk/pattern/sequence.rb +0 -30
- data/lib/mtk/pitch_set.rb +0 -84
- data/lib/mtk/sequencer/rhythmic_sequencer.rb +0 -29
- data/lib/mtk/transform/invertible.rb +0 -15
- data/lib/mtk/transform/mappable.rb +0 -18
- data/lib/mtk/transform/set_theory_operations.rb +0 -34
- data/lib/mtk/transform/transposable.rb +0 -14
- data/spec/mtk/event_spec.rb +0 -139
- data/spec/mtk/helper/event_builder_spec.rb +0 -92
- data/spec/mtk/lang/grammar_spec.rb +0 -100
- data/spec/mtk/note_spec.rb +0 -115
- data/spec/mtk/pattern/abstract_pattern_spec.rb +0 -45
- data/spec/mtk/pattern/note_cycle_spec.rb.bak +0 -116
- data/spec/mtk/pattern/pitch_cycle_spec.rb.bak +0 -47
- data/spec/mtk/pattern/pitch_sequence_spec.rb.bak +0 -37
- data/spec/mtk/pattern/sequence_spec.rb +0 -151
- data/spec/mtk/pitch_set_spec.rb +0 -198
- data/spec/mtk/sequencer/abstract_sequencer_spec.rb +0 -159
- data/spec/mtk/sequencer/rhythmic_sequencer_spec.rb +0 -49
data/lib/mtk.rb
CHANGED
@@ -1,70 +1,82 @@
|
|
1
|
-
##############################################
|
2
1
|
# Description of modules for documentation:
|
3
2
|
|
4
3
|
# The top level module for this library
|
5
4
|
module MTK
|
6
5
|
|
6
|
+
# Constants for modeling frequency, intensity, and duration.
|
7
|
+
module Constants
|
8
|
+
end
|
9
|
+
|
7
10
|
# Internal helper classes used to avoid duplicating code in this library.
|
8
|
-
module
|
11
|
+
module Helpers
|
9
12
|
end
|
10
13
|
|
11
|
-
#
|
12
|
-
|
13
|
-
# The core interface for Pattern classes is {Pattern::Enumerator#next} and {Pattern::Enumerator#rewind}.
|
14
|
-
module Pattern
|
14
|
+
# Musical events, such as {Events::Note}s and {Events::Parameter} changes, that are arranged in time via a {Timeline}.
|
15
|
+
module Events
|
15
16
|
end
|
16
17
|
|
17
|
-
# Classes that
|
18
|
-
module
|
18
|
+
# Classes that emit elements one at a time. Used by {MTK::Sequencers::Sequencer}s to construct {Timeline}s.
|
19
|
+
module Patterns
|
20
|
+
end
|
21
|
+
|
22
|
+
# Classes that assemble {Patterns::Pattern}s into {Timeline}s.
|
23
|
+
module Sequencers
|
19
24
|
end
|
20
25
|
|
21
26
|
# Optional classes for the "MTK language", which let's you compose music via MTK without writing any Ruby code
|
22
27
|
module Lang
|
23
28
|
end
|
24
29
|
|
25
|
-
# Optional classes for MIDI input and
|
30
|
+
# Optional classes for MIDI input and {Output}.
|
26
31
|
module MIDI
|
27
32
|
end
|
28
33
|
|
29
34
|
end
|
30
35
|
|
31
|
-
require 'mtk/helper/collection'
|
32
|
-
require 'mtk/helper/pseudo_constants'
|
33
|
-
|
34
|
-
require 'mtk/transform/mappable'
|
35
|
-
require 'mtk/transform/transposable'
|
36
|
-
require 'mtk/transform/invertible'
|
37
|
-
require 'mtk/transform/set_theory_operations'
|
38
|
-
|
39
36
|
require 'mtk/pitch_class'
|
40
|
-
require 'mtk/pitch_class_set'
|
41
37
|
require 'mtk/pitch'
|
42
|
-
require 'mtk/
|
38
|
+
require 'mtk/duration'
|
39
|
+
require 'mtk/intensity'
|
43
40
|
|
44
|
-
require 'mtk/
|
45
|
-
require 'mtk/note'
|
46
|
-
require 'mtk/timeline'
|
41
|
+
require 'mtk/interval'
|
47
42
|
|
48
|
-
require 'mtk/
|
49
|
-
require 'mtk/_constants/pitches'
|
50
|
-
require 'mtk/_constants/intervals'
|
51
|
-
require 'mtk/_constants/intensities'
|
52
|
-
require 'mtk/_constants/durations'
|
43
|
+
require 'mtk/variable'
|
53
44
|
|
54
|
-
require 'mtk/
|
55
|
-
require 'mtk/
|
56
|
-
require 'mtk/
|
57
|
-
require 'mtk/
|
58
|
-
require 'mtk/pattern/choice'
|
59
|
-
require 'mtk/pattern/function'
|
60
|
-
require 'mtk/pattern/lines'
|
61
|
-
require 'mtk/pattern/palindrome'
|
45
|
+
require 'mtk/helpers/collection'
|
46
|
+
require 'mtk/helpers/pitch_collection'
|
47
|
+
require 'mtk/helpers/pseudo_constants'
|
48
|
+
require 'mtk/helpers/convert'
|
62
49
|
|
63
|
-
require 'mtk/
|
64
|
-
require 'mtk/
|
65
|
-
require 'mtk/
|
66
|
-
require 'mtk/sequencer/rhythmic_sequencer'
|
50
|
+
require 'mtk/pitch_class_set'
|
51
|
+
require 'mtk/melody'
|
52
|
+
require 'mtk/chord'
|
67
53
|
|
68
|
-
require 'mtk/
|
54
|
+
require 'mtk/events/event'
|
55
|
+
require 'mtk/events/note'
|
56
|
+
require 'mtk/events/parameter'
|
69
57
|
|
58
|
+
require 'mtk/timeline'
|
70
59
|
|
60
|
+
require 'mtk/constants/pitch_classes'
|
61
|
+
require 'mtk/constants/pitches'
|
62
|
+
require 'mtk/constants/intervals'
|
63
|
+
require 'mtk/constants/intensities'
|
64
|
+
require 'mtk/constants/durations'
|
65
|
+
|
66
|
+
require 'mtk/patterns/pattern'
|
67
|
+
require 'mtk/patterns/sequence'
|
68
|
+
require 'mtk/patterns/cycle'
|
69
|
+
require 'mtk/patterns/choice'
|
70
|
+
require 'mtk/patterns/function'
|
71
|
+
require 'mtk/patterns/lines'
|
72
|
+
require 'mtk/patterns/palindrome'
|
73
|
+
require 'mtk/patterns/chain'
|
74
|
+
require 'mtk/patterns/for_each'
|
75
|
+
|
76
|
+
require 'mtk/sequencers/event_builder'
|
77
|
+
require 'mtk/sequencers/sequencer'
|
78
|
+
require 'mtk/sequencers/step_sequencer'
|
79
|
+
require 'mtk/sequencers/rhythmic_sequencer'
|
80
|
+
require 'mtk/sequencers/legato_sequencer'
|
81
|
+
|
82
|
+
require 'mtk/lang/parser'
|
data/lib/mtk/chord.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module MTK
|
2
|
+
|
3
|
+
# A sorted collection of distinct {Pitch}es.
|
4
|
+
#
|
5
|
+
# The "vertical" (simultaneous) pitch collection.
|
6
|
+
#
|
7
|
+
# @see Melody
|
8
|
+
# @see PitchClassSet
|
9
|
+
#
|
10
|
+
class Chord < Melody
|
11
|
+
|
12
|
+
# @param pitches [#to_a] the collection of pitches
|
13
|
+
# @note duplicate pitches will be removed. See #{Melody} if you want to maintain duplicates.
|
14
|
+
# @see MTK#Chord
|
15
|
+
#
|
16
|
+
def initialize(pitches)
|
17
|
+
pitches = pitches.to_a.clone
|
18
|
+
pitches.uniq!
|
19
|
+
pitches.sort!
|
20
|
+
@pitches = pitches.freeze
|
21
|
+
end
|
22
|
+
|
23
|
+
# Generate a chord inversion (positive numbers move the lowest notes up an octave, negative moves the highest notes down)
|
24
|
+
def inversion(number)
|
25
|
+
number = number.to_i
|
26
|
+
pitch_set = Array.new(@pitches.uniq.sort)
|
27
|
+
if number > 0
|
28
|
+
number.times do |count|
|
29
|
+
index = count % pitch_set.length
|
30
|
+
pitch_set[index] += 12
|
31
|
+
end
|
32
|
+
else
|
33
|
+
number.abs.times do |count|
|
34
|
+
index = -(count + 1) % pitch_set.length # count from -1 downward to go backwards through the list starting at the end
|
35
|
+
pitch_set[index] -= 12
|
36
|
+
end
|
37
|
+
end
|
38
|
+
self.class.new pitch_set.sort
|
39
|
+
end
|
40
|
+
|
41
|
+
# Transpose the chord so that it's lowest pitch is the given pitch class.
|
42
|
+
def nearest(pitch_class)
|
43
|
+
self.transpose @pitches.first.pitch_class.distance_to(pitch_class)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Construct an ordered {Melody} with no duplicates.
|
48
|
+
# @see #Chord
|
49
|
+
# @see #Melody
|
50
|
+
def Chord(*anything)
|
51
|
+
Chord.new Helpers::Convert.to_pitches(*anything)
|
52
|
+
end
|
53
|
+
module_function :Chord
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rational'
|
2
|
+
|
3
|
+
module MTK
|
4
|
+
module Constants
|
5
|
+
|
6
|
+
# Defines duration constants using abbreviations for standard rhythm values ('w' for whole note, 'h' for half note, etc).
|
7
|
+
#
|
8
|
+
# In order to avoid conflict with pitch class 'e', the constant for eighth note is 'i'
|
9
|
+
#
|
10
|
+
# These can be thought of like constants, but they
|
11
|
+
# use lower-case names and therefore define them as "pseudo constant" methods.
|
12
|
+
# The methods are available either through the module (MTK::Durations::e) or via mixin (include MTK::Durations; q)
|
13
|
+
#
|
14
|
+
# These values assume the quarter note is one beat (1.0), so they work best with 4/4 and other */4 time signatures.
|
15
|
+
#
|
16
|
+
# @note Including this module defines a bunch of single-character variables, which may shadow existing variable names.
|
17
|
+
# Just be mindful of what is defined in this module when including it.
|
18
|
+
#
|
19
|
+
# @see Note
|
20
|
+
module Durations
|
21
|
+
extend Helpers::PseudoConstants
|
22
|
+
|
23
|
+
# NOTE: the yard doc macros here only fill in [$2] with the actual value when generating docs under Ruby 1.9+
|
24
|
+
|
25
|
+
# whole note
|
26
|
+
# @macro [attach] durations.define_constant
|
27
|
+
# @attribute [r]
|
28
|
+
# @return [$2] number of beats for $1
|
29
|
+
define_constant 'w', MTK::Duration[4]
|
30
|
+
|
31
|
+
# half note
|
32
|
+
define_constant 'h', MTK::Duration[2]
|
33
|
+
|
34
|
+
# quarter note
|
35
|
+
define_constant 'q', MTK::Duration[1]
|
36
|
+
|
37
|
+
# eight note
|
38
|
+
define_constant 'i', MTK::Duration[Rational(1,2)]
|
39
|
+
|
40
|
+
# sixteenth note
|
41
|
+
define_constant 's', MTK::Duration[Rational(1,4)]
|
42
|
+
|
43
|
+
# thirty-second note
|
44
|
+
define_constant 'r', MTK::Duration[Rational(1,8)]
|
45
|
+
|
46
|
+
# sixty-fourth note
|
47
|
+
define_constant 'x', MTK::Duration[Rational(1,16)]
|
48
|
+
|
49
|
+
# The values of all "psuedo constants" defined in this module
|
50
|
+
DURATIONS = [w, h, q, i, s, r, x].freeze
|
51
|
+
|
52
|
+
# The names of all "psuedo constants" defined in this module
|
53
|
+
DURATION_NAMES = MTK::Duration::NAMES
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module MTK
|
2
|
+
module Constants
|
3
|
+
|
4
|
+
# Defines intensity constants using standard dynamic symbols.
|
5
|
+
#
|
6
|
+
# These can be thought of like constants, but in order to distinguish 'f' (forte) from the {PitchClass} 'F'
|
7
|
+
# it was necessary to use lower-case names and therefore define them as "pseudo constant" methods.
|
8
|
+
# The methods are available either through the module (MTK::Intensities::f) or via mixin (include MTK::Intensities; f)
|
9
|
+
#
|
10
|
+
# These values are intensities in the range 0.125 - 1.0 (in increments of 1/8), so they can be easily scaled (unlike MIDI velocities).
|
11
|
+
#
|
12
|
+
# It is also possible to retrieve values in increments of 1/24 by using the '+' and '-' suffix when looking
|
13
|
+
# up values via the {.[]} method.
|
14
|
+
#
|
15
|
+
# @note Including this module shadows Ruby's built-in p() method.
|
16
|
+
# If you include this module, you can access the built-in p() method via Kernel.p()
|
17
|
+
#
|
18
|
+
# @see Note
|
19
|
+
module Intensities
|
20
|
+
extend Helpers::PseudoConstants
|
21
|
+
|
22
|
+
# NOTE: the yard doc macros here only fill in [$2] with the actual value when generating docs under Ruby 1.9+
|
23
|
+
|
24
|
+
# pianississimo
|
25
|
+
# @macro [attach] intensities.define_constant
|
26
|
+
# @attribute [r]
|
27
|
+
# @return [$2] intensity value for $1
|
28
|
+
define_constant 'ppp', MTK::Intensity[0.125]
|
29
|
+
|
30
|
+
# pianissimo
|
31
|
+
define_constant 'pp', MTK::Intensity[0.25]
|
32
|
+
|
33
|
+
# piano
|
34
|
+
# @note Including this module shadows Ruby's built-in p() method.
|
35
|
+
# If you include this module, you can access the built-in p() method via Kernel.p()
|
36
|
+
define_constant 'p', MTK::Intensity[0.375]
|
37
|
+
|
38
|
+
# mezzo-piano
|
39
|
+
define_constant 'mp', MTK::Intensity[0.5]
|
40
|
+
|
41
|
+
# mezzo-forte
|
42
|
+
define_constant 'mf', MTK::Intensity[0.625]
|
43
|
+
|
44
|
+
# forte
|
45
|
+
define_constant 'o', MTK::Intensity[0.75]
|
46
|
+
|
47
|
+
# fortissimo
|
48
|
+
define_constant 'ff', MTK::Intensity[0.875]
|
49
|
+
|
50
|
+
# fortississimo
|
51
|
+
define_constant 'fff', MTK::Intensity[1.0]
|
52
|
+
|
53
|
+
# The values of all "psuedo constants" defined in this module
|
54
|
+
INTENSITIES = [ppp, pp, p, mp, mf, o, ff, fff].freeze
|
55
|
+
|
56
|
+
# The names of all "psuedo constants" defined in this module
|
57
|
+
INTENSITY_NAMES = MTK::Intensity::NAMES
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module MTK
|
2
|
+
module Constants
|
3
|
+
|
4
|
+
# Defines a constant for intervals up to an octave using diatonic naming conventions (see http://en.wikipedia.org/wiki/Interval_(music)#Main_intervals)
|
5
|
+
#
|
6
|
+
# Naming conventions
|
7
|
+
# P#: perfect interval
|
8
|
+
# M#: major interval
|
9
|
+
# m#: minor interval
|
10
|
+
# TT: tritone (AKA augmented 4th or diminished 5th)
|
11
|
+
#
|
12
|
+
# These can be thought of like constants, but in order to succinctly distinguish 'm2' (minor) from 'M2' (major),
|
13
|
+
# it was necessary to use lower-case names for some of the values and therefore define them as "pseudo constant" methods.
|
14
|
+
# The methods are available either through the module (MTK::Intervals::m2) or via mixin (include MTK::Intervals; m2)
|
15
|
+
module Intervals
|
16
|
+
extend Helpers::PseudoConstants
|
17
|
+
|
18
|
+
# NOTE: the yard doc macros here only fill in [$2] with the actual value when generating docs under Ruby 1.9+
|
19
|
+
|
20
|
+
# perfect unison
|
21
|
+
# @macro [attach] interval.define_constant
|
22
|
+
# @attribute [r]
|
23
|
+
# @return [$2] number of semitones in the interval $1
|
24
|
+
define_constant 'P1', MTK::Interval[0]
|
25
|
+
|
26
|
+
# minor second
|
27
|
+
# @macro [attach] interval.define_constant
|
28
|
+
# @attribute [r]
|
29
|
+
# @return [$2] number of semitones in the interval $1
|
30
|
+
define_constant 'm2', MTK::Interval[1]
|
31
|
+
|
32
|
+
# major second
|
33
|
+
define_constant 'M2', MTK::Interval[2]
|
34
|
+
|
35
|
+
# minor third
|
36
|
+
define_constant 'm3', MTK::Interval[3]
|
37
|
+
|
38
|
+
# major third
|
39
|
+
define_constant 'M3', MTK::Interval[4]
|
40
|
+
|
41
|
+
# pefect fourth
|
42
|
+
define_constant 'P4', MTK::Interval[5]
|
43
|
+
|
44
|
+
# tritone (AKA augmented fourth or diminished fifth)
|
45
|
+
define_constant 'TT', MTK::Interval[6]
|
46
|
+
|
47
|
+
# perfect fifth
|
48
|
+
define_constant 'P5', MTK::Interval[7]
|
49
|
+
|
50
|
+
# minor sixth
|
51
|
+
define_constant 'm6', MTK::Interval[8]
|
52
|
+
|
53
|
+
# major sixth
|
54
|
+
define_constant 'M6', MTK::Interval[9]
|
55
|
+
|
56
|
+
# minor seventh
|
57
|
+
define_constant 'm7', MTK::Interval[10]
|
58
|
+
|
59
|
+
# major seventh
|
60
|
+
define_constant 'M7', MTK::Interval[11]
|
61
|
+
|
62
|
+
# pefect octave
|
63
|
+
define_constant 'P8', MTK::Interval[12]
|
64
|
+
|
65
|
+
# The values of all "psuedo constants" defined in this module
|
66
|
+
INTERVALS = [P1, m2, M2, m3, M3, P4, TT, P5, m6, M6, m7, M7, P8].freeze
|
67
|
+
|
68
|
+
# The names of all "psuedo constants" defined in this module
|
69
|
+
INTERVAL_NAMES = MTK::Interval::NAMES
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MTK
|
2
|
+
module Constants
|
3
|
+
|
4
|
+
# Defines a constant for each {PitchClass} in the Western chromatic scale.
|
5
|
+
module PitchClasses
|
6
|
+
|
7
|
+
# The values of all constants defined in this module
|
8
|
+
PITCH_CLASSES = MTK::PitchClass::PITCH_CLASSES
|
9
|
+
|
10
|
+
# The names of all constants defined in this module
|
11
|
+
PITCH_CLASS_NAMES = MTK::PitchClass::NAMES
|
12
|
+
|
13
|
+
PITCH_CLASSES.each { |pc| const_set pc.name, pc }
|
14
|
+
|
15
|
+
# Lookup the value of an pitch class constant by name.
|
16
|
+
# @example lookup value of 'C'
|
17
|
+
# MTK::PitchClasses['C']
|
18
|
+
# @see PitchClass.[]
|
19
|
+
def self.[](name)
|
20
|
+
begin
|
21
|
+
const_get name
|
22
|
+
rescue
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module MTK
|
2
|
+
module Constants
|
3
|
+
|
4
|
+
# Defines a constants for each {Pitch} in the standard MIDI range using scientific pitch notation.
|
5
|
+
#
|
6
|
+
# See http://en.wikipedia.org/wiki/Scientific_pitch_notation
|
7
|
+
#
|
8
|
+
# @note Because the character '#' cannot be used in the name of a constant,
|
9
|
+
# the "black key" pitches are all named as flats with 'b' (for example, Gb3 or Cb4)
|
10
|
+
# @note Because the character '-' (minus) cannot be used in the name of a constant,
|
11
|
+
# the low pitches use '_' (underscore) in place of '-' (minus) (for example C_1).
|
12
|
+
module Pitches
|
13
|
+
|
14
|
+
# The values of all constants defined in this module
|
15
|
+
PITCHES = []
|
16
|
+
|
17
|
+
# The names of all constants defined in this module
|
18
|
+
PITCH_NAMES = []
|
19
|
+
|
20
|
+
128.times do |note_number|
|
21
|
+
pitch = Pitch.from_i( note_number )
|
22
|
+
PITCHES << pitch
|
23
|
+
|
24
|
+
octave_str = pitch.octave.to_s.sub(/-/,'_') # '_1' for -1
|
25
|
+
name = "#{pitch.pitch_class}#{octave_str}"
|
26
|
+
PITCH_NAMES << name
|
27
|
+
|
28
|
+
# TODO? also define lower case pseudo constants for consistency with the grammar?
|
29
|
+
|
30
|
+
const_set name, pitch
|
31
|
+
end
|
32
|
+
|
33
|
+
PITCHES.freeze
|
34
|
+
PITCH_NAMES.freeze
|
35
|
+
|
36
|
+
# Lookup the value of an pitch constant by name.
|
37
|
+
# @example lookup value of 'C3'
|
38
|
+
# MTK::Pitches['C3']
|
39
|
+
# @see Pitch.from_s
|
40
|
+
# @note Unlike {Pitch.from_s} this method will accept either '_' (underscore) or '-' (minus) and treat it like '-' (minus)
|
41
|
+
# @note Unlike {Pitch.from_s} this method only accepts the accidental 'b'
|
42
|
+
def self.[](name)
|
43
|
+
begin
|
44
|
+
const_get name.sub('-','_')
|
45
|
+
rescue
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|