stave 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 298ec095fbd8df1e9ee1f77db4708598d0fbb433a3d364810884787451dcec21
4
+ data.tar.gz: 1c89449bb41bdabb016ef555b0cc8962eb9d6cf99fe90f35e689632c326ff48a
5
+ SHA512:
6
+ metadata.gz: 134a5a015273135682beec053a9985eb6f0084e972a516f26da08a221b3c16edee37c3d4ac1ec46c2ffedc4d7b77206ccd539b041a772214ca31ae295ead4c64
7
+ data.tar.gz: 3d7b9d994d481785c8625757caa253fd4ae2f70e1f9a24531758a7ed392bccbe24a10912069ed2fa2ef51f13be4bf22d09a7c93ad72e267ee487773547435140
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,71 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+ SuggestExtensions: false
7
+
8
+ Gemspec/RequireMFA:
9
+ Enabled: false
10
+
11
+ Layout/ExtraSpacing:
12
+ AllowForAlignment: true
13
+ Layout/LineLength:
14
+ Max: 80
15
+ Layout/MultilineMethodCallIndentation:
16
+ Enabled: false
17
+
18
+ Lint/AmbiguousBlockAssociation:
19
+ AllowedMethods:
20
+ - change
21
+ Lint/ConstantDefinitionInBlock:
22
+ Exclude:
23
+ - spec/spec_helper.rb
24
+ Lint/MissingSuper:
25
+ Enabled: false
26
+
27
+ Naming/MethodParameterName:
28
+ Enabled: false
29
+
30
+ Style/ArgumentsForwarding:
31
+ Enabled: false
32
+ Style/CharacterLiteral:
33
+ Enabled: false
34
+ Style/Documentation:
35
+ Enabled: false
36
+ Style/ExplicitBlockArgument:
37
+ Enabled: false
38
+ Style/FormatStringToken:
39
+ EnforcedStyle: template
40
+ Style/FrozenStringLiteralComment:
41
+ Enabled: false
42
+ Style/RescueStandardError:
43
+ EnforcedStyle: implicit
44
+ Style/StringLiterals:
45
+ EnforcedStyle: double_quotes
46
+
47
+ RSpec/AnyInstance:
48
+ Enabled: false
49
+ RSpec/ContextWording:
50
+ Prefixes:
51
+ - when
52
+ - with
53
+ - without
54
+ - if
55
+ - unless
56
+ - and
57
+ - but
58
+ RSpec/DescribedClass:
59
+ SkipBlocks: true
60
+ RSpec/ExpectChange:
61
+ EnforcedStyle: block
62
+ RSpec/LetSetup:
63
+ Enabled: false
64
+ RSpec/MultipleMemoizedHelpers:
65
+ Max: 15
66
+ RSpec/NestedGroups:
67
+ Enabled: false
68
+ RSpec/SharedExamples:
69
+ Enabled: false
70
+ RSpec/ExampleLength:
71
+ Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Chris Welham
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Stave
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/stave`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stave.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rubocop/rake_task"
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
@@ -0,0 +1,51 @@
1
+ module Stave
2
+ module Core
3
+ class DegreeCollection < Lookup
4
+ # Set via the variants interface.
5
+ def degrees
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def count
10
+ uniq.count
11
+ end
12
+
13
+ def intervals
14
+ degrees.map(&:interval)
15
+ end
16
+
17
+ def rotate(position)
18
+ return degrees if position == 1
19
+
20
+ uniq.rotate(position - 1).tap do |degrees|
21
+ degrees << degrees.first
22
+ end
23
+ end
24
+
25
+ def relative_rotate(position)
26
+ return degrees if position == 1
27
+
28
+ rotated_degrees = rotate(position)
29
+
30
+ rotated_degrees.map do |degree|
31
+ degree - rotated_degrees.first
32
+ end
33
+ end
34
+
35
+ def steps
36
+ intervals.each_cons(2).map { |pair| pair.reverse.reduce(:-) }
37
+ end
38
+
39
+ def uniq
40
+ degrees.uniq(&:position)
41
+ end
42
+
43
+ def self.class_key
44
+ to_s.delete_prefix("Stave::Theory::")
45
+ .gsub(/([a-z])([A-Z])/, "\\1_\\2")
46
+ .downcase
47
+ .to_sym
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,110 @@
1
+ module Stave
2
+ module Core
3
+ class Lookup
4
+ extend Forwardable
5
+
6
+ attr_reader :variant
7
+
8
+ def_delegators :variant, :to_s
9
+
10
+ def initialize(variant)
11
+ @variant = variant.to_sym
12
+
13
+ validate_variant!
14
+ set_attributes!
15
+ end
16
+
17
+ def ==(other)
18
+ case other
19
+ when String, Symbol then variant == other.to_sym
20
+ when Lookup then variant == other.variant
21
+ else false
22
+ end
23
+ end
24
+
25
+ alias eql? ==
26
+
27
+ class << self
28
+ def keys = variant_lookup.keys
29
+
30
+ def string_keys = keys.map(&:to_s)
31
+
32
+ def each_key(&)
33
+ keys.each(&)
34
+ end
35
+
36
+ def variant(name, prefix: nil, suffix: nil, **attributes)
37
+ name = :"#{prefix}_#{name}" if prefix
38
+ name = :"#{name}_#{suffix}" if suffix
39
+
40
+ variant_lookup[name] = attributes
41
+
42
+ define_singleton_method name, -> { new(name) }
43
+ end
44
+
45
+ def variant?(name)
46
+ variant_lookup.keys.include?(name)
47
+ end
48
+
49
+ def variants
50
+ variant_lookup.keys.map { |variant| new(variant) }
51
+ end
52
+
53
+ def variant_lookup
54
+ @variant_lookup ||= {}
55
+ end
56
+
57
+ def where(**attributes)
58
+ variants.select do |variant|
59
+ attributes.all? do |attribute, value|
60
+ variant.send(attribute) == value
61
+ end
62
+ end
63
+ end
64
+
65
+ def find_by(**attributes)
66
+ where(**attributes).first
67
+ end
68
+
69
+ def with_options(scope: nil, **options, &)
70
+ merger = OptionMerger.new(self, options)
71
+
72
+ merger.instance_eval(&)
73
+
74
+ return if scope.nil?
75
+
76
+ define_singleton_method scope, -> { where(**options) }
77
+ end
78
+ end
79
+
80
+ class InvalidVariantError < StandardError
81
+ def initialize(variant)
82
+ super("Variant :#{variant} is invalid")
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def validate_variant!
89
+ return if self.class.variant?(variant)
90
+
91
+ raise InvalidVariantError, variant
92
+ end
93
+
94
+ def set_attributes!
95
+ self.class.variant_lookup[variant].each do |attribute, value|
96
+ set_attribute!(attribute, value)
97
+ end
98
+ end
99
+
100
+ def set_attribute!(name, value)
101
+ ivar_name = name.to_s.delete_suffix("?")
102
+ instance_variable_set(:"@#{ivar_name}", value)
103
+
104
+ self.class.define_method name do
105
+ instance_variable_get(:"@#{ivar_name}")
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,47 @@
1
+ module Stave
2
+ module Core
3
+ class NoteCollection
4
+ attr_reader :root, :type
5
+
6
+ def initialize(root:, type:)
7
+ @root = root
8
+ @type = type
9
+ end
10
+
11
+ def notes
12
+ type.intervals.map { |interval| root + interval }
13
+ end
14
+
15
+ def note_at(position)
16
+ return if position.zero? || position.negative?
17
+
18
+ position -= 7 while position >= 8
19
+
20
+ notes[position - 1]
21
+ end
22
+
23
+ def uniq
24
+ notes.uniq(&:variant)
25
+ end
26
+
27
+ def to_h
28
+ {
29
+ self.class.type_class.class_key => { variant: type.variant },
30
+ root: root.variant
31
+ }
32
+ end
33
+
34
+ def self.type_class
35
+ const_get("#{self}Type")
36
+ end
37
+
38
+ def self.inherited(note_class)
39
+ note_class.type_class.variants.each do |type|
40
+ self.class.define_method(
41
+ :"#{type.variant}_from", ->(root) { new(type:, root:) }
42
+ )
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ module Stave
2
+ module Core
3
+ class OptionMerger
4
+ attr_reader :context, :options
5
+
6
+ def initialize(context, options)
7
+ @context = context
8
+ @options = options
9
+ end
10
+
11
+ private
12
+
13
+ def method_missing(method, *args, **kwargs, &)
14
+ kwargs ||= {}
15
+ kwargs = kwargs.merge(options) unless options.empty?
16
+
17
+ context.send(method, *args, **kwargs, &)
18
+ end
19
+
20
+ def respond_to_missing?(...)
21
+ context.respond_to?(...)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ module Stave
2
+ module Core
3
+ class ScaleHarmoniser
4
+ attr_reader :scale_type, :chord_set
5
+
6
+ def initialize(scale_type:, chord_set:)
7
+ @scale_type = scale_type
8
+ @chord_set = chord_set
9
+ end
10
+
11
+ def harmonise!
12
+ scale_type.uniq.map do |degree|
13
+ Theory::ChordType.find_by(intervals: intervals(degree))
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def scale_intervals(degree)
20
+ scale_type.rotate(degree.position)
21
+ .each_slice(chord_set.slice_size)
22
+ .map(&:first)
23
+ .map(&:interval)
24
+ .take(chord_set.note_count)
25
+ end
26
+
27
+ def intervals(degree)
28
+ intervals = scale_intervals(degree)
29
+
30
+ intervals.map { |interval| interval - intervals.first }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ module Stave
2
+ module Theory
3
+ class Accidental < Core::Lookup
4
+ variant :double_flat, transform: -2, symbol: "♭♭"
5
+ variant :flat, transform: -1, symbol: "♭"
6
+ variant :natural, transform: 0, symbol: ""
7
+ variant :sharp, transform: 1, symbol: "♯"
8
+ variant :double_sharp, transform: 2, symbol: "♯♯"
9
+
10
+ def flat?
11
+ transform.negative?
12
+ end
13
+
14
+ def natural?
15
+ transform.zero?
16
+ end
17
+
18
+ def sharp?
19
+ transform.positive?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module Stave
2
+ module Theory
3
+ class Chord < Core::NoteCollection
4
+ def symbol
5
+ "#{root.symbol}#{type.symbol}"
6
+ end
7
+
8
+ def inversions
9
+ notes.drop(1).zip(type.inversion_types).map do |root, type|
10
+ ChordInversion.new(root:, type:)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Stave
2
+ module Theory
3
+ class ChordInversion < Chord
4
+ def symbol
5
+ "#{root_chord.symbol}#{type.symbol}"
6
+ end
7
+
8
+ def root_chord
9
+ root = notes[type.root_position]
10
+
11
+ Chord.new(root:, type: type.chord_type)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module Stave
2
+ module Theory
3
+ class ChordInversionType < Core::DegreeCollection
4
+ ChordType.triads.each do |chord_type|
5
+ with_options chord_type:, suffix: chord_type.variant do
6
+ variant :first_inversion, inversion: 1
7
+ variant :second_inversion, inversion: 2
8
+ end
9
+ end
10
+
11
+ ChordType.sevenths.each do |chord_type|
12
+ with_options chord_type:, suffix: chord_type.variant do
13
+ variant :first_inversion, inversion: 1
14
+ variant :second_inversion, inversion: 2
15
+ variant :third_inversion, inversion: 3
16
+ end
17
+ end
18
+
19
+ def position = inversion + 1
20
+
21
+ def root_position = degrees.count - inversion
22
+
23
+ def degrees
24
+ chord_type.relative_rotate(position).uniq(&:variant)
25
+ end
26
+
27
+ def symbol
28
+ "(#{'i' * inversion})"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,70 @@
1
+ module Stave
2
+ module Theory
3
+ class ChordType < Core::DegreeCollection
4
+ class Set < Core::Lookup
5
+ with_options slice_size: 2 do
6
+ variant :triad, note_count: 3
7
+ variant :seventh, note_count: 4
8
+ end
9
+ end
10
+
11
+ with_options set: Set.triad, scope: :triads do
12
+ variant :major_triad, symbol: "", degrees: [
13
+ Degree.root,
14
+ Degree.three,
15
+ Degree.five
16
+ ]
17
+
18
+ variant :minor_triad, symbol: "m", degrees: [
19
+ Degree.root,
20
+ Degree.flat_three,
21
+ Degree.five
22
+ ]
23
+
24
+ variant :diminished_triad, symbol: "o", degrees: [
25
+ Degree.root,
26
+ Degree.flat_three,
27
+ Degree.flat_five
28
+ ]
29
+ end
30
+
31
+ with_options set: Set.seventh, scope: :sevenths do
32
+ variant :major_seventh, symbol: "∆7", degrees: [
33
+ Degree.root,
34
+ Degree.three,
35
+ Degree.five,
36
+ Degree.seven
37
+ ]
38
+
39
+ variant :dominant_seventh, symbol: "7", degrees: [
40
+ Degree.root,
41
+ Degree.three,
42
+ Degree.five,
43
+ Degree.flat_seven
44
+ ]
45
+
46
+ variant :minor_seventh, symbol: "m7", degrees: [
47
+ Degree.root,
48
+ Degree.flat_three,
49
+ Degree.five,
50
+ Degree.flat_seven
51
+ ]
52
+
53
+ variant :half_diminished_seventh, symbol: "ø7", degrees: [
54
+ Degree.root,
55
+ Degree.flat_three,
56
+ Degree.flat_five,
57
+ Degree.flat_seven
58
+ ]
59
+ end
60
+
61
+ def note_count
62
+ set.note_count
63
+ end
64
+
65
+ def inversion_types
66
+ ChordInversionType.where(chord_type: self)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,22 @@
1
+ module Stave
2
+ module Theory
3
+ class Circle
4
+ attr_reader :type, :root
5
+
6
+ def initialize(type:, root:)
7
+ @type = type
8
+ @root = root
9
+ end
10
+
11
+ def notes
12
+ 7.times.inject([root]) do |notes, _|
13
+ notes + [notes.last + type.step]
14
+ end
15
+ end
16
+
17
+ def key_signatures
18
+ notes.map { |root| Scale.major_from(root).key_signature }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ module Stave
2
+ module Theory
3
+ class CircleType < Core::Lookup
4
+ variant :fourths, step: Interval.perfect_fourth
5
+ variant :fifths, step: Interval.perfect_fifth
6
+ end
7
+ end
8
+ end