peas 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ require 'peas/interval'
2
+
3
+ class Integer
4
+ def unisons
5
+ Peas::Interval::Diatonic::P1
6
+ end
7
+ alias unison unisons
8
+
9
+ def semitones
10
+ Peas::Interval::Semitone "S#{self}", self
11
+ end
12
+ alias semitone semitones
13
+
14
+ def minor_seconds
15
+ Peas::Interval::Diatonic "m2x#{self}", self
16
+ end
17
+ alias minor_second minor_seconds
18
+
19
+ def wholetones
20
+ Peas::Interval::Wholetone "W#{self}", self * 2
21
+ end
22
+ alias wholetone wholetones
23
+
24
+ def major_seconds
25
+ Peas::Interval::Diatonic "M2x#{self}", self * 2
26
+ end
27
+ alias major_second major_seconds
28
+
29
+ def minor_thirds
30
+ Peas::Interval::Diatonic "m3x#{self}", self * 3
31
+ end
32
+ alias minor_third minor_thirds
33
+
34
+ def major_thirds
35
+ Peas::Interval::Diatonic "M3x#{self}", self * 4
36
+ end
37
+ alias major_third major_thirds
38
+
39
+ def perfect_fourths
40
+ Peas::Interval::Diatonic "P4x#{self}", self * 5
41
+ end
42
+ alias perfect_fourth perfect_fourths
43
+
44
+ def tritones
45
+ # Peas::Interval::Latin "TTx#{self}", self * 6
46
+ self * 6
47
+ end
48
+ alias tritone tritones
49
+
50
+ def diminished_fifths
51
+ Peas::Interval::Diatonic "d5x#{self}", self * 6
52
+ end
53
+ alias diminished_fifth diminished_fifths
54
+
55
+ def augmented_fourths
56
+ Peas::Interval::Diatonic "S6x#{self}", self * 6
57
+ end
58
+ alias augmented_fourth augmented_fourths
59
+
60
+ def perfect_fifths
61
+ Peas::Interval::Diatonic "P5x#{self}", self * 7
62
+ end
63
+ alias perfect_fifth perfect_fifths
64
+
65
+ def minor_sixths
66
+ Peas::Interval::Diatonic "m6x#{self}", self * 8
67
+ end
68
+ alias minor_sixth minor_sixths
69
+
70
+ def augmented_fifths
71
+ Peas::Interval::Chromatic "S8x#{self}", self * 8
72
+ end
73
+ alias augmented_fifth augmented_fifths
74
+
75
+ def major_sixths
76
+ Peas::Interval::Diatonic "M6x#{self}", self * 9
77
+ end
78
+ alias major_sixth major_sixths
79
+
80
+ def minor_sevenths
81
+ Peas::Interval::Diatonic "m7x#{self}", self * 10
82
+ end
83
+ alias minor_seventh minor_sevenths
84
+
85
+ def major_sevenths
86
+ Peas::Interval::Diatonic "M7x#{self}", self * 11
87
+ end
88
+ alias major_seventh major_sevenths
89
+
90
+ def octaves
91
+ Peas::Interval::Diatonic "P8x#{self}", self * 12
92
+ end
93
+ alias octave octaves
94
+ end
95
+
96
+
@@ -0,0 +1,99 @@
1
+ require 'named_value_class'
2
+ require 'peas/pitch'
3
+
4
+ module Peas
5
+ module Interval
6
+ # see: http://en.wikipedia.org/wiki/Interval_(music)
7
+
8
+ NamedValueClass 'Semitone', Fixnum
9
+ 128.times do |x|
10
+ Semitone "S#{x}", x
11
+ end
12
+
13
+ NamedValueClass 'Wholetone', Fixnum
14
+ 64.times do |x|
15
+ Wholetone "W#{x}", x * 2
16
+ end
17
+
18
+ NamedValueClass 'Chromatic', Fixnum
19
+ Chromatic :d2, 0
20
+ Chromatic :A1, 1
21
+ Chromatic :d3, 2
22
+ Chromatic :A2, 3
23
+ Chromatic :d4, 4
24
+ Chromatic :A3, 5
25
+ Chromatic :d5, 6
26
+ Chromatic :A4, 6
27
+ Chromatic :d6, 7
28
+ Chromatic :A5, 8
29
+ Chromatic :d7, 9
30
+ Chromatic :A6, 10
31
+ Chromatic :d8, 11
32
+ Chromatic :A7, 12
33
+
34
+ NamedValueClass 'Latin', Fixnum
35
+ Latin 'S', 1
36
+ Latin 'T', 3
37
+ Latin 'TT', 6
38
+
39
+ NamedValueClass 'Diatonic', Fixnum do
40
+ def inverse
41
+ case @name
42
+ when 'P1' then Diatonic::P8
43
+ when 'm2' then Diatonic::M7
44
+ when 'M2' then Diatonic::m7
45
+ when 'm3' then Diatonic::M6
46
+ when 'M3' then Diatonic::m6
47
+ when 'P4' then Diatonic::P5
48
+ when 'A4' then Diatonic::d5
49
+ when 'd5' then Diatonic::A4
50
+ when 'P5' then Diatonic::P4
51
+ when 'm6' then Diatonic::M3
52
+ when 'M6' then Diatonic::m3
53
+ when 'm7' then Diatonic::M2
54
+ when 'M7' then Diatonic::m2
55
+ when 'P8' then Diatonic::P1
56
+ end
57
+ end
58
+
59
+ def interval_class
60
+ if (mod_12 = self % 12) > 6
61
+ 12 - mod_12
62
+ else
63
+ mod_12
64
+ end
65
+ end
66
+ end
67
+ Diatonic 'P1', 0
68
+ Diatonic 'm2', 1
69
+ Diatonic 'M2', 2
70
+ Diatonic 'm3', 3
71
+ Diatonic 'M3', 4
72
+ Diatonic 'P4', 5
73
+ Diatonic 'A4', 6
74
+ Diatonic 'd5', 6
75
+ Diatonic 'P5', 7
76
+ Diatonic 'm6', 8
77
+ Diatonic 'M6', 9
78
+ Diatonic 'm7', 10
79
+ Diatonic 'M7', 11
80
+ Diatonic 'P8', 12
81
+ end
82
+
83
+ module Intervals
84
+ include Interval::Semitone::NamedValues
85
+ extend Interval::Semitone::NamedValues
86
+
87
+ include Interval::Wholetone::NamedValues
88
+ extend Interval::Wholetone::NamedValues
89
+
90
+ include Interval::Chromatic::NamedValues
91
+ extend Interval::Chromatic::NamedValues
92
+
93
+ include Interval::Latin::NamedValues
94
+ extend Interval::Latin::NamedValues
95
+
96
+ include Interval::Diatonic::NamedValues
97
+ extend Interval::Diatonic::NamedValues
98
+ end
99
+ end
data/lib/peas/pitch.rb ADDED
@@ -0,0 +1,104 @@
1
+ require 'forwardable'
2
+ require 'peas/pitch_class'
3
+
4
+ module Peas
5
+ NamedValueClass 'Pitch', Fixnum do
6
+ extend Forwardable
7
+ def_delegators :pitch_class, :is_sharp?, :is_flat?, :is_natural?
8
+
9
+ def as_flat
10
+ self.class.flats(self)
11
+ end
12
+
13
+ def as_sharp
14
+ self.class.sharps(self)
15
+ end
16
+
17
+ def self.flats(value)
18
+ {
19
+ 0 => C_1 , 32 => Ab1 , 64 => E4 , 96 => C7 ,
20
+ 1 => Db_1 , 33 => A1 , 65 => F4 , 97 => Db7 ,
21
+ 2 => D_1 , 34 => Bb1 , 66 => Gb4 , 98 => D7 ,
22
+ 3 => Eb_1 , 35 => B1 , 67 => G4 , 99 => Eb7 ,
23
+ 4 => E_1 , 36 => C2 , 68 => Ab4 , 100 => E7 ,
24
+ 5 => F_1 , 37 => Db2 , 69 => A4 , 101 => F7 ,
25
+ 6 => Gb_1 , 38 => D2 , 70 => Bb4 , 102 => Gb7 ,
26
+ 7 => G_1 , 39 => Eb2 , 71 => B4 , 103 => G7 ,
27
+ 8 => Ab_1 , 40 => E2 , 72 => C5 , 104 => Ab7 ,
28
+ 9 => A_1 , 41 => F2 , 73 => Db5 , 105 => A7 ,
29
+ 10 => Bb_1 , 42 => Gb2 , 74 => D5 , 106 => Bb7 ,
30
+ 11 => B_1 , 43 => G2 , 75 => Eb5 , 107 => B7 ,
31
+ 12 => C0 , 44 => Ab2 , 76 => E5 , 108 => C8 ,
32
+ 13 => Db0 , 45 => A2 , 77 => F5 , 109 => Db8 ,
33
+ 14 => D0 , 46 => Bb2 , 78 => Gb5 , 110 => D8 ,
34
+ 15 => Eb0 , 47 => B2 , 79 => G5 , 111 => Eb8 ,
35
+ 16 => E0 , 48 => C3 , 80 => Ab5 , 112 => E8 ,
36
+ 17 => F0 , 49 => Db3 , 81 => A5 , 113 => F8 ,
37
+ 18 => Gb0 , 50 => D3 , 82 => Bb5 , 114 => Gb8 ,
38
+ 19 => G0 , 51 => Eb3 , 83 => B5 , 115 => G8 ,
39
+ 20 => Ab0 , 52 => E3 , 84 => C6 , 116 => Ab8 ,
40
+ 21 => A0 , 53 => F3 , 85 => Db6 , 117 => A8 ,
41
+ 22 => Bb0 , 54 => Gb3 , 86 => D6 , 118 => Bb8 ,
42
+ 23 => B0 , 55 => G3 , 87 => Eb6 , 119 => B8 ,
43
+ 24 => C1 , 56 => Ab3 , 88 => E6 , 120 => C9 ,
44
+ 25 => Db1 , 57 => A3 , 89 => F6 , 121 => Db9 ,
45
+ 26 => D1 , 58 => Bb3 , 90 => Gb6 , 122 => D9 ,
46
+ 27 => Eb1 , 59 => B3 , 91 => G6 , 123 => Eb9 ,
47
+ 28 => E1 , 60 => C4 , 92 => Ab6 , 124 => E9 ,
48
+ 29 => F1 , 61 => Db4 , 93 => A6 , 125 => F9 ,
49
+ 30 => Gb1 , 62 => D4 , 94 => Bb6 , 126 => Gb9 ,
50
+ 31 => G1 , 63 => Eb4 , 95 => B6 , 127 => G9
51
+ }[value]
52
+ end
53
+
54
+ def self.sharps(value)
55
+ {
56
+ 0 => C_1 , 32 => Gs1 , 64 => E4 , 96 => C7 ,
57
+ 1 => Cs_1 , 33 => A1 , 65 => F4 , 97 => Cs7 ,
58
+ 2 => D_1 , 34 => As1 , 66 => Fs4 , 98 => D7 ,
59
+ 3 => Ds_1 , 35 => B1 , 67 => G4 , 99 => Ds7 ,
60
+ 4 => E_1 , 36 => C2 , 68 => Gs4 , 100 => E7 ,
61
+ 5 => F_1 , 37 => Cs2 , 69 => A4 , 101 => F7 ,
62
+ 6 => Fs_1 , 38 => D2 , 70 => As4 , 102 => Fs7 ,
63
+ 7 => G_1 , 39 => Ds2 , 71 => B4 , 103 => G7 ,
64
+ 8 => Gs_1 , 40 => E2 , 72 => C5 , 104 => Gs7 ,
65
+ 9 => A_1 , 41 => F2 , 73 => Cs5 , 105 => A7 ,
66
+ 10 => As_1 , 42 => Fs2 , 74 => D5 , 106 => As7 ,
67
+ 11 => B_1 , 43 => G2 , 75 => Ds5 , 107 => B7 ,
68
+ 12 => C0 , 44 => Gs2 , 76 => E5 , 108 => C8 ,
69
+ 13 => Cs0 , 45 => A2 , 77 => F5 , 109 => Cs8 ,
70
+ 14 => D0 , 46 => As2 , 78 => Fs5 , 110 => D8 ,
71
+ 15 => Ds0 , 47 => B2 , 79 => G5 , 111 => Ds8 ,
72
+ 16 => E0 , 48 => C3 , 80 => Gs5 , 112 => E8 ,
73
+ 17 => F0 , 49 => Cs3 , 81 => A5 , 113 => F8 ,
74
+ 18 => Fs0 , 50 => D3 , 82 => As5 , 114 => Fs8 ,
75
+ 19 => G0 , 51 => Ds3 , 83 => B5 , 115 => G8 ,
76
+ 20 => Gs0 , 52 => E3 , 84 => C6 , 116 => Gs8 ,
77
+ 21 => A0 , 53 => F3 , 85 => Cs6 , 117 => A8 ,
78
+ 22 => As0 , 54 => Fs3 , 86 => D6 , 118 => As8 ,
79
+ 23 => B0 , 55 => G3 , 87 => Ds6 , 119 => B8 ,
80
+ 24 => C1 , 56 => Gs3 , 88 => E6 , 120 => C9 ,
81
+ 25 => Cs1 , 57 => A3 , 89 => F6 , 121 => Cs9 ,
82
+ 26 => D1 , 58 => As3 , 90 => Fs6 , 122 => D9 ,
83
+ 27 => Ds1 , 59 => B3 , 91 => G6 , 123 => Ds9 ,
84
+ 28 => E1 , 60 => C4 , 92 => Gs6 , 124 => E9 ,
85
+ 29 => F1 , 61 => Cs4 , 93 => A6 , 125 => F9 ,
86
+ 30 => Fs1 , 62 => D4 , 94 => As6 , 126 => Fs9 ,
87
+ 31 => G1 , 63 => Ds4 , 95 => B6 , 127 => G9
88
+ }[value]
89
+ end
90
+ end
91
+ PitchClass::NamedValues::Collection.each do |pclass|
92
+ Pitch "#{pclass}_1", pclass, pitch_class:pclass, octave:-1
93
+ 0.upto(9) do |octave|
94
+ name = pclass.to_s + octave.to_s
95
+ Pitch name, (((octave+1)*12)+pclass), pitch_class:pclass, octave:octave
96
+ end
97
+ end
98
+
99
+ module Pitches
100
+ include Pitch::NamedValues
101
+ include PitchClass::NamedValues
102
+ end
103
+ end
104
+
@@ -0,0 +1,69 @@
1
+ require 'named_value_class'
2
+
3
+ module Peas
4
+ NamedValueClass 'PitchClass', Fixnum do
5
+ def is_sharp?
6
+ %w|Bs Cs Ds Es Fs Gs As|.include? @name
7
+ end
8
+
9
+ def is_flat?
10
+ %w|Bb Cb Db Eb Fb Gb Ab|.include? @name
11
+ end
12
+
13
+ def is_natural?
14
+ %w|B C D E F G A|.include? @name
15
+ end
16
+
17
+ def self.flats(value)
18
+ {
19
+ 0 => C, 6 => Gb,
20
+ 1 => Db, 7 => G,
21
+ 2 => D, 8 => Ab,
22
+ 3 => Eb, 9 => A,
23
+ 4 => E, 10 => Bb,
24
+ 5 => F, 11 => B
25
+ }[value]
26
+ end
27
+
28
+ def self.sharps(value)
29
+ {
30
+ 0 => C, 6 => Fs,
31
+ 1 => Cs, 7 => G,
32
+ 2 => D, 8 => Gs,
33
+ 3 => Ds, 9 => A,
34
+ 4 => E, 10 => As,
35
+ 5 => F, 11 => B
36
+ }[value]
37
+ end
38
+
39
+ def as_flat
40
+ self.class.flats(self)
41
+ end
42
+
43
+ def as_sharp
44
+ self.class.sharps(self)
45
+ end
46
+ end
47
+
48
+ PitchClass 'Bs', 0 # B sharp is just Bs
49
+ PitchClass 'C', 0
50
+ PitchClass 'Cs', 1
51
+ PitchClass 'Db', 1
52
+ PitchClass 'D', 2
53
+ PitchClass 'Ds', 3
54
+ PitchClass 'Eb', 3
55
+ PitchClass 'E', 4
56
+ PitchClass 'Es', 5
57
+ PitchClass 'Fb', 4
58
+ PitchClass 'F', 5
59
+ PitchClass 'Fs', 6
60
+ PitchClass 'Gb', 6
61
+ PitchClass 'G', 7
62
+ PitchClass 'Gs', 8
63
+ PitchClass 'Ab', 8
64
+ PitchClass 'A', 9
65
+ PitchClass 'As', 10
66
+ PitchClass 'Bb', 10
67
+ PitchClass 'B', 11
68
+ PitchClass 'Cb', 11
69
+ end
@@ -0,0 +1,5 @@
1
+ module Peas
2
+ NAME = 'peas'
3
+ VERSION = '0.1.0'
4
+ SUMMARY = 'Do math with music pitches, intervals, scales, chords and progressions in Ruby.'
5
+ end
data/lib/peas.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'peas/pitch'
2
+ require 'peas/interval'
3
+ # require 'peas/scale'
4
+ # require 'peas/chord'
5
+ # require 'peas/progression'
6
+
7
+ require 'peas/integer_helper'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Garriss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: named_value_class
16
+ requirement: &2156329420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: 0.4.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: *2156329420
28
+ description: ! 'Peas defines constants from music theory and allows one to perform
29
+ math with them. Currently supports pitches (as MIDI values), pitch classes, and
30
+ intervals using semitones, wholetones, chromatic, latin or diatonic
31
+
32
+ abbreviations.'
33
+ email: mgarriss@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/peas/integer_helper.rb
39
+ - lib/peas/interval.rb
40
+ - lib/peas/pitch.rb
41
+ - lib/peas/pitch_class.rb
42
+ - lib/peas/version.rb
43
+ - lib/peas.rb
44
+ homepage: http://github.com/mgarriss/peas
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.11
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Peas defines constants from music theory and allows one to perform math with
68
+ them.
69
+ test_files: []