head_music 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/head_music.gemspec +30 -0
- data/lib/head_music/accidental.rb +37 -0
- data/lib/head_music/circle.rb +35 -0
- data/lib/head_music/interval.rb +51 -0
- data/lib/head_music/key_signature.rb +36 -0
- data/lib/head_music/letter.rb +58 -0
- data/lib/head_music/pitch.rb +62 -0
- data/lib/head_music/pitch_class.rb +45 -0
- data/lib/head_music/scale.rb +45 -0
- data/lib/head_music/spelling.rb +57 -0
- data/lib/head_music/version.rb +3 -0
- data/lib/head_music.rb +17 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 180ffc27218ca1ef9eacf64c6d93aff2ab81fc49
|
4
|
+
data.tar.gz: 76e58d1d8ed78ca58ebe899bedfed0756d7104fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 730ecdb5ebfadc4cc18c2776b3413a74c31629249e20376ad27928a762347b89ec32392fa4dc441bcd84724e41208915874cfbdacd3b75119c2910638339ba47
|
7
|
+
data.tar.gz: 1f5ed8080b5584f26702fe7f257459975923ddff48be3239050e721837f65abe3e3f8863dd865e39b2161900b1fd924eec03d2c6337860d7d27bbde69f10537e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at robert.head@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Rob Head
|
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,39 @@
|
|
1
|
+
# HeadMusic
|
2
|
+
|
3
|
+
The *head_music* ruby gem models the elements of western music theory, such as note names, scales, key signatures, intervals, and chords.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'head_music'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install head_music
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
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.
|
28
|
+
|
29
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/head_music. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
39
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "head_music"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/head_music.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'head_music/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "head_music"
|
8
|
+
spec.version = HeadMusic::VERSION
|
9
|
+
spec.authors = ["Rob Head"]
|
10
|
+
spec.email = ["robert.head@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{The rudiments of western music theory.}
|
13
|
+
spec.description = %q{Work with the elements of western music theory, such as pitches, scales, intervals, and chords.}
|
14
|
+
spec.homepage = "https://github.com/roberthead/head_music"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "activesupport", "~> 5.0"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "rspec-its", "~> 1.2"
|
30
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class HeadMusic::Accidental
|
2
|
+
ACCIDENTAL_SEMITONES = {
|
3
|
+
'#' => 1,
|
4
|
+
'##' => 2,
|
5
|
+
'b' => -1,
|
6
|
+
'bb' => -2
|
7
|
+
}
|
8
|
+
|
9
|
+
attr_reader :string
|
10
|
+
|
11
|
+
def self.get(string)
|
12
|
+
@accidentals ||= {}
|
13
|
+
@accidentals[string] ||= new(string)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.for_interval(semitones)
|
17
|
+
ACCIDENTAL_SEMITONES.key(semitones.to_i)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(string)
|
21
|
+
@string = string
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
string
|
26
|
+
end
|
27
|
+
|
28
|
+
def ==(value)
|
29
|
+
to_s == value.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def semitones
|
33
|
+
ACCIDENTAL_SEMITONES.fetch(string, 0)
|
34
|
+
end
|
35
|
+
|
36
|
+
private_class_method :new
|
37
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class HeadMusic::Circle
|
2
|
+
def self.of_fifths
|
3
|
+
by_interval(HeadMusic::Interval.named(:perfect_fifth))
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.of_fourths
|
7
|
+
by_interval(HeadMusic::Interval.named(:perfect_fourth))
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.by_interval(interval)
|
11
|
+
@circles ||= {}
|
12
|
+
@circles[interval.to_i] ||= new(interval)
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :interval, :pitch_classes
|
16
|
+
|
17
|
+
def initialize(interval)
|
18
|
+
@interval = HeadMusic::Interval.get(interval.to_i)
|
19
|
+
@pitch_classes = pitch_classes_by_interval(interval)
|
20
|
+
end
|
21
|
+
|
22
|
+
private_class_method :new
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def pitch_classes_by_interval(interval)
|
27
|
+
[HeadMusic::PitchClass.get(0)].tap do |list|
|
28
|
+
loop do
|
29
|
+
next_pitch_class = list.last + interval
|
30
|
+
break if next_pitch_class == list.first
|
31
|
+
list << next_pitch_class
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class HeadMusic::Interval
|
2
|
+
include Comparable
|
3
|
+
|
4
|
+
private_class_method :new
|
5
|
+
|
6
|
+
NAMES = %w{perfect_unison minor_second major_second minor_third major_third perfect_fourth tritone perfect_fifth minor_sixth major_sixth minor_seventh major_seventh perfect_octave}
|
7
|
+
|
8
|
+
attr_reader :semitones
|
9
|
+
|
10
|
+
def self.get(semitones)
|
11
|
+
@intervals_memo ||= {}
|
12
|
+
@intervals_memo[semitones] ||= new(semitones)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.named(name)
|
16
|
+
name = name.to_s
|
17
|
+
get(NAMES.index(name)) if NAMES.include?(name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(semitones)
|
21
|
+
@semitones = semitones
|
22
|
+
end
|
23
|
+
|
24
|
+
def simplified
|
25
|
+
HeadMusic::Interval.get(semitones % 12)
|
26
|
+
end
|
27
|
+
|
28
|
+
def simple?
|
29
|
+
(0..12).include?(semitones)
|
30
|
+
end
|
31
|
+
|
32
|
+
def compound?
|
33
|
+
semitones > 12
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_i
|
37
|
+
semitones
|
38
|
+
end
|
39
|
+
|
40
|
+
def +(value)
|
41
|
+
HeadMusic::Interval.get(to_i + value.to_i)
|
42
|
+
end
|
43
|
+
|
44
|
+
def -(value)
|
45
|
+
HeadMusic::Interval.get((to_i - value.to_i).abs)
|
46
|
+
end
|
47
|
+
|
48
|
+
def <=>(other)
|
49
|
+
self.to_i <=> other.to_i
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class HeadMusic::KeySignature
|
2
|
+
attr_reader :tonic_spelling
|
3
|
+
attr_reader :scale_type
|
4
|
+
|
5
|
+
SHARPS = %w{F# C# G# D# A# E# B#}
|
6
|
+
FLATS = %w{Bb Eb Ab Db Gb Cb Fb}
|
7
|
+
|
8
|
+
delegate :pitch_class, to: :tonic_spelling, prefix: :tonic
|
9
|
+
|
10
|
+
def initialize(tonic_spelling, scale_type = :major)
|
11
|
+
@tonic_spelling = tonic_spelling
|
12
|
+
@scale_type = scale_type
|
13
|
+
end
|
14
|
+
|
15
|
+
def sharps
|
16
|
+
SHARPS.first(num_sharps)
|
17
|
+
end
|
18
|
+
|
19
|
+
def flats
|
20
|
+
FLATS.first(num_flats)
|
21
|
+
end
|
22
|
+
|
23
|
+
def num_sharps
|
24
|
+
(HeadMusic::Circle.of_fifths.pitch_classes.index(tonic_pitch_class) - scale_type_adjustment) % 12
|
25
|
+
end
|
26
|
+
|
27
|
+
def num_flats
|
28
|
+
(HeadMusic::Circle.of_fourths.pitch_classes.index(tonic_pitch_class) + scale_type_adjustment) % 12
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def scale_type_adjustment
|
34
|
+
scale_type == :minor ? 3 : 0
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class HeadMusic::Letter
|
2
|
+
# Defines the natural relationship between the natural letter-named notes
|
3
|
+
|
4
|
+
NAMES = ('A'..'G').to_a
|
5
|
+
|
6
|
+
NATURAL_PITCH_CLASS_NUMBERS = {
|
7
|
+
'C' => 0,
|
8
|
+
'D' => 2,
|
9
|
+
'E' => 4,
|
10
|
+
'F' => 5,
|
11
|
+
'G' => 7,
|
12
|
+
'A' => 9,
|
13
|
+
'B' => 11,
|
14
|
+
}
|
15
|
+
|
16
|
+
def self.all
|
17
|
+
NAMES.map { |letter_name| get(letter_name)}
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.get(name)
|
21
|
+
@letters ||= {}
|
22
|
+
name = name.to_s.first.upcase
|
23
|
+
if NAMES.include?(name)
|
24
|
+
@letters[name] ||= new(name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.from_pitch_class(pitch_class)
|
29
|
+
@letters ||= {}
|
30
|
+
pitch_class = pitch_class.to_i % 12
|
31
|
+
name =
|
32
|
+
NAMES.detect { |name| pitch_class == NATURAL_PITCH_CLASS_NUMBERS[name] } ||
|
33
|
+
NAMES.detect { |name| pitch_class == NATURAL_PITCH_CLASS_NUMBERS[name]+1 }
|
34
|
+
if NAMES.include?(name)
|
35
|
+
@letters[name] ||= new(name)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_reader :name
|
40
|
+
|
41
|
+
delegate :to_s, to: :name
|
42
|
+
delegate :to_sym, to: :name
|
43
|
+
delegate :to_i, to: :pitch_class
|
44
|
+
|
45
|
+
def initialize(name)
|
46
|
+
@name = name
|
47
|
+
end
|
48
|
+
|
49
|
+
def pitch_class
|
50
|
+
HeadMusic::PitchClass.get(NATURAL_PITCH_CLASS_NUMBERS[name])
|
51
|
+
end
|
52
|
+
|
53
|
+
def ==(value)
|
54
|
+
to_s == value.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
private_class_method :new
|
58
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class HeadMusic::Pitch
|
2
|
+
include Comparable
|
3
|
+
|
4
|
+
attr_reader :spelling
|
5
|
+
attr_reader :octave
|
6
|
+
|
7
|
+
delegate :letter, :accidental, :pitch_class, to: :spelling
|
8
|
+
|
9
|
+
def self.get(value)
|
10
|
+
@pitches ||= {}
|
11
|
+
@pitches[value] ||= from_name(value) || from_number(value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.from_name(name)
|
15
|
+
return nil unless name == name.to_s
|
16
|
+
spelling = HeadMusic::Spelling.get(name)
|
17
|
+
octave = name.scan(/-?\d+$/).first.to_i
|
18
|
+
new(spelling, octave) if spelling && (-1..9).include?(octave)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_number(number)
|
22
|
+
return nil unless number == number.to_i
|
23
|
+
spelling = HeadMusic::Spelling.from_number(number)
|
24
|
+
octave = (number.to_i / 12) - 1
|
25
|
+
new(spelling, octave) if spelling && (-1..9).include?(octave)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(spelling, octave)
|
29
|
+
@spelling = HeadMusic::Spelling.get(spelling.to_s)
|
30
|
+
@octave = octave.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
def name
|
34
|
+
[spelling, octave].join
|
35
|
+
end
|
36
|
+
|
37
|
+
def midi_note_number
|
38
|
+
(octave + 1) * 12 + pitch_class.to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_s
|
42
|
+
name
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_i
|
46
|
+
midi_note_number
|
47
|
+
end
|
48
|
+
|
49
|
+
def enharmonic?(other)
|
50
|
+
self.midi_note_number == other.midi_note_number
|
51
|
+
end
|
52
|
+
|
53
|
+
def ==(value)
|
54
|
+
to_s == value.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def <=>(other)
|
58
|
+
self.midi_note_number <=> other.midi_note_number
|
59
|
+
end
|
60
|
+
|
61
|
+
private_class_method :new
|
62
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class HeadMusic::PitchClass
|
2
|
+
attr_reader :number
|
3
|
+
|
4
|
+
def self.get(number)
|
5
|
+
@pitch_classes ||= {}
|
6
|
+
number = number.to_i % 12
|
7
|
+
@pitch_classes[number] ||= new(number)
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
alias_method :[], :get
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(pitch_class_or_midi_number)
|
15
|
+
@number = pitch_class_or_midi_number % 12
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_i
|
19
|
+
number
|
20
|
+
end
|
21
|
+
|
22
|
+
def +(semitones)
|
23
|
+
HeadMusic::PitchClass.get(to_i + semitones.to_i)
|
24
|
+
end
|
25
|
+
|
26
|
+
def -(semitones)
|
27
|
+
HeadMusic::PitchClass.get(to_i - semitones.to_i)
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(value)
|
31
|
+
to_i == value.to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def intervals_to(other)
|
35
|
+
delta = other.to_i - to_i
|
36
|
+
inverse = delta > 0 ? delta - 12 : delta + 12
|
37
|
+
[delta, inverse].sort_by(&:abs).map { |interval| HeadMusic::Interval.get(interval) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def smallest_interval_to(other)
|
41
|
+
intervals_to(other).first
|
42
|
+
end
|
43
|
+
|
44
|
+
private_class_method :new
|
45
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class HeadMusic::Scale
|
2
|
+
PATTERNS = {
|
3
|
+
major: [0, 2, 4, 5, 7, 9, 11, 12],
|
4
|
+
minor: [0, 2, 3, 5, 7, 8, 10, 12],
|
5
|
+
minor_pentatonic: [0, nil, 3, 5, 7, nil, 10, 12],
|
6
|
+
}
|
7
|
+
|
8
|
+
attr_reader :pattern
|
9
|
+
|
10
|
+
def self.major
|
11
|
+
@scales ||= {}
|
12
|
+
@scales[:major] ||= new(PATTERNS[:major])
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.minor
|
16
|
+
@scales ||= {}
|
17
|
+
@scales[:minor] ||= new(PATTERNS[:minor])
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.minor_pentatonic
|
21
|
+
@scales ||= {}
|
22
|
+
@scales[:minor_pentatonic] ||= new(PATTERNS[:minor_pentatonic])
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(pattern)
|
26
|
+
@pattern = pattern
|
27
|
+
end
|
28
|
+
|
29
|
+
def in(spelling)
|
30
|
+
spelling = HeadMusic::Spelling.get(spelling.to_s)
|
31
|
+
spellings = []
|
32
|
+
letter_index = HeadMusic::Letter::all.index(spelling.letter)
|
33
|
+
starting_pitch_class = spelling.pitch_class
|
34
|
+
pattern.each do |interval_from_tonic|
|
35
|
+
letter = HeadMusic::Letter.all[letter_index]
|
36
|
+
if interval_from_tonic
|
37
|
+
accidental_interval = letter.pitch_class.smallest_interval_to(HeadMusic::PitchClass.get(starting_pitch_class + interval_from_tonic))
|
38
|
+
accidental = HeadMusic::Accidental.for_interval(accidental_interval)
|
39
|
+
spellings << HeadMusic::Spelling.get([letter, accidental].join)
|
40
|
+
end
|
41
|
+
letter_index = (letter_index + 1) % 7
|
42
|
+
end
|
43
|
+
spellings
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# A Spelling is a pitch class with a letter and an accidental
|
2
|
+
|
3
|
+
class HeadMusic::Spelling
|
4
|
+
attr_reader :letter
|
5
|
+
attr_reader :accidental
|
6
|
+
attr_reader :pitch_class
|
7
|
+
|
8
|
+
SPELLING_MATCHER = /([A-G])([b#]*)(\-?\d+)?/
|
9
|
+
|
10
|
+
def self.get(identifier)
|
11
|
+
@spellings ||= {}
|
12
|
+
@spellings[identifier] ||= from_name(identifier) || from_number(identifier)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.from_name(name)
|
16
|
+
return nil unless name == name.to_s
|
17
|
+
match = name.match(SPELLING_MATCHER)
|
18
|
+
if match
|
19
|
+
letter_name, accidental_string, _octave = match.captures
|
20
|
+
letter = HeadMusic::Letter.get(letter_name)
|
21
|
+
if letter
|
22
|
+
return new(letter, HeadMusic::Accidental.get(accidental_string))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.from_number(number)
|
28
|
+
return nil unless number == number.to_i
|
29
|
+
pitch_class_number = number % 12
|
30
|
+
letter = HeadMusic::Letter.from_pitch_class(pitch_class_number)
|
31
|
+
if letter.pitch_class != pitch_class_number
|
32
|
+
accidental = HeadMusic::Accidental.for_interval(pitch_class_number - letter.pitch_class.to_i)
|
33
|
+
end
|
34
|
+
new(letter, accidental)
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(letter, accidental = nil)
|
38
|
+
@letter = HeadMusic::Letter.get(letter.to_s)
|
39
|
+
@accidental = HeadMusic::Accidental.get(accidental.to_s)
|
40
|
+
accidental_semitones = @accidental ? @accidental.semitones : 0
|
41
|
+
@pitch_class = HeadMusic::PitchClass.get(letter.pitch_class + accidental_semitones)
|
42
|
+
end
|
43
|
+
|
44
|
+
def name
|
45
|
+
[letter, accidental].join
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
name
|
50
|
+
end
|
51
|
+
|
52
|
+
def ==(value)
|
53
|
+
to_s == value.to_s
|
54
|
+
end
|
55
|
+
|
56
|
+
private_class_method :new
|
57
|
+
end
|
data/lib/head_music.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "head_music/version"
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
require "active_support/core_ext/string/access"
|
5
|
+
|
6
|
+
require "head_music/accidental"
|
7
|
+
require "head_music/circle"
|
8
|
+
require "head_music/interval"
|
9
|
+
require "head_music/key_signature"
|
10
|
+
require "head_music/letter"
|
11
|
+
require "head_music/pitch_class"
|
12
|
+
require "head_music/pitch"
|
13
|
+
require "head_music/scale"
|
14
|
+
require "head_music/spelling"
|
15
|
+
|
16
|
+
module HeadMusic
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: head_music
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Head
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.2'
|
83
|
+
description: Work with the elements of western music theory, such as pitches, scales,
|
84
|
+
intervals, and chords.
|
85
|
+
email:
|
86
|
+
- robert.head@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- head_music.gemspec
|
102
|
+
- lib/head_music.rb
|
103
|
+
- lib/head_music/accidental.rb
|
104
|
+
- lib/head_music/circle.rb
|
105
|
+
- lib/head_music/interval.rb
|
106
|
+
- lib/head_music/key_signature.rb
|
107
|
+
- lib/head_music/letter.rb
|
108
|
+
- lib/head_music/pitch.rb
|
109
|
+
- lib/head_music/pitch_class.rb
|
110
|
+
- lib/head_music/scale.rb
|
111
|
+
- lib/head_music/spelling.rb
|
112
|
+
- lib/head_music/version.rb
|
113
|
+
homepage: https://github.com/roberthead/head_music
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.5.1
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: The rudiments of western music theory.
|
137
|
+
test_files: []
|