peas 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/peas/interval.rb CHANGED
@@ -4,18 +4,33 @@ require 'peas/pitch'
4
4
  module Peas
5
5
  module Interval
6
6
  # see: http://en.wikipedia.org/wiki/Interval_(music)
7
+ module Math
8
+ def -(rhs)
9
+ if [Pitch,PitchClass].include?(rhs.class)
10
+ raise Peas::InvalidOperation, '(Interval - Pitch) has no meaning'
11
+ else
12
+ super
13
+ end
14
+ end
15
+ end
7
16
 
8
- NamedValueClass 'Semitone', Fixnum
17
+ NamedValueClass 'Semitone', Fixnum do
18
+ include Peas::Interval::Math
19
+ end
9
20
  128.times do |x|
10
21
  Semitone "S#{x}", x
11
22
  end
12
23
 
13
- NamedValueClass 'Wholetone', Fixnum
24
+ NamedValueClass 'Wholetone', Fixnum do
25
+ include Peas::Interval::Math
26
+ end
14
27
  64.times do |x|
15
28
  Wholetone "W#{x}", x * 2
16
29
  end
17
30
 
18
- NamedValueClass 'Chromatic', Fixnum
31
+ NamedValueClass 'Chromatic', Fixnum do
32
+ include Peas::Interval::Math
33
+ end
19
34
  Chromatic :d2, 0
20
35
  Chromatic :A1, 1
21
36
  Chromatic :d3, 2
@@ -31,12 +46,15 @@ module Peas
31
46
  Chromatic :d8, 11
32
47
  Chromatic :A7, 12
33
48
 
34
- NamedValueClass 'Latin', Fixnum
49
+ NamedValueClass 'Latin', Fixnum do
50
+ include Peas::Interval::Math
51
+ end
35
52
  Latin 'S', 1
36
53
  Latin 'T', 3
37
54
  Latin 'TT', 6
38
55
 
39
56
  NamedValueClass 'Diatonic', Fixnum do
57
+ include Peas::Interval::Math
40
58
  def inverse
41
59
  case @name
42
60
  when 'P1' then Diatonic::P8
@@ -96,4 +114,12 @@ module Peas
96
114
  include Interval::Diatonic::NamedValues
97
115
  extend Interval::Diatonic::NamedValues
98
116
  end
117
+
118
+ def self.is_an_interval?(obj)
119
+ [Interval::Semitone,
120
+ Interval::Wholetone,
121
+ Interval::Chromatic,
122
+ Interval::Latin,
123
+ Interval::Diatonic].include? obj.class
124
+ end
99
125
  end
data/lib/peas/pitch.rb CHANGED
@@ -1,10 +1,31 @@
1
1
  require 'forwardable'
2
2
  require 'peas/pitch_class'
3
+ require 'peas/interval'
3
4
 
4
5
  module Peas
5
6
  NamedValueClass 'Pitch', Fixnum do
6
7
  extend Forwardable
7
8
  def_delegators :pitch_class, :is_sharp?, :is_flat?, :is_natural?
9
+
10
+ alias _minus :-
11
+
12
+ def -(rhs)
13
+ if rhs.class == Pitch
14
+ if self >= rhs
15
+ Interval::Diatonic[super(rhs)] || Interval::Semitone[super(rhs)]
16
+ else
17
+ rhs - self
18
+ end
19
+ elsif rhs.class == PitchClass
20
+ self.pitch_class - rhs
21
+ elsif Peas.is_an_interval?(rhs)
22
+ if is_sharp?
23
+ Pitch.sharps(self._minus(rhs))
24
+ else
25
+ Pitch.flats(self._minus(rhs))
26
+ end
27
+ end
28
+ end
8
29
 
9
30
  def as_flat
10
31
  self.class.flats(self)
@@ -1,7 +1,34 @@
1
1
  require 'named_value_class'
2
2
 
3
3
  module Peas
4
+ class InvalidOperation < RuntimeError; end
5
+
4
6
  NamedValueClass 'PitchClass', Fixnum do
7
+ alias _minus :-
8
+
9
+ def -(rhs)
10
+ if rhs.class == PitchClass
11
+ if self >= rhs
12
+ # TODO: stop the insanity
13
+ Interval::Diatonic[
14
+ Interval::Diatonic[self._minus(rhs)].interval_class] ||
15
+ Interval::Semitone[
16
+ Interval::Semitone[self._minus(rhs)].interval_class]
17
+ else
18
+ Interval::Diatonic[rhs - self]
19
+ end
20
+ elsif rhs.class == Pitch
21
+ self - rhs.pitch_class
22
+ elsif Peas.is_an_interval?(rhs)
23
+ result = self._minus(rhs) % 12
24
+ if is_sharp?
25
+ PitchClass.sharps(result)
26
+ else
27
+ PitchClass.flats(result)
28
+ end
29
+ end
30
+ end
31
+
5
32
  def is_sharp?
6
33
  %w|Bs Cs Ds Es Fs Gs As|.include? @name
7
34
  end
data/lib/peas/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Peas
2
2
  NAME = 'peas'
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  SUMMARY = 'Do math with music pitches, intervals, scales, chords and progressions in Ruby.'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: named_value_class
16
- requirement: &2156329420 !ruby/object:Gem::Requirement
16
+ requirement: &2155967180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.4'
22
22
  - - ! '>='
23
23
  - !ruby/object:Gem::Version
24
- version: 0.4.0
24
+ version: 0.4.2
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *2156329420
27
+ version_requirements: *2155967180
28
28
  description: ! 'Peas defines constants from music theory and allows one to perform
29
29
  math with them. Currently supports pitches (as MIDI values), pitch classes, and
30
30
  intervals using semitones, wholetones, chromatic, latin or diatonic