ruby-mext 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 765a069de73dccde01080cc08b7dc773781bc26f5dd5f3181a4d01e1bbdd04f2
4
- data.tar.gz: bd1a550f9cce944c99c0fdd863696d99784eca0536278d19ec7768df45a775b0
3
+ metadata.gz: 333432c69cb5c931d71693a145648130e816cdfb17bc5a2b72965fcee4e6760c
4
+ data.tar.gz: 3b2d6f694370f4864702ff8e3c7d46fbf3343438cb8f6475c430471a4007afb2
5
5
  SHA512:
6
- metadata.gz: f3de1073d0fb33c9dd5a1fd29eae1ea17ede3915a644478ed452780de20e75c2fe3ea15464fab9515e0c0c9331e83e879a77c55f382d156443c96022143a2016
7
- data.tar.gz: c601e4e6f42b862f06238878a2a55bed482311851fc05e8b30c74f3904ed24ae202d9c61c963d73e3bef6929661494533de7ad8409c4a95186a52c1caefc2e45
6
+ metadata.gz: ec7603a7aef497e79f6ac624f5ed3a988f0ab1842f2520739ef0ef863ca5e3ebf29dec52d790e119537cf1f4168c98cddbee1a8dc42c380a6ddf9bc3f32f8db5
7
+ data.tar.gz: 955887187d5738f7470b9d1999d412156d76f5ae561c8db89d9a912649375e9e1c51aff040c1e07447977e1b7f09e84feaa3487cbd81b7916b38e32fcfe0d9d8
@@ -1,10 +1,25 @@
1
1
  module Mext
2
2
  module Music
3
3
 
4
+ #
5
+ # +Mext::Music::PitchClass+
6
+ #
7
+ # this is a representation of pitches in the usual +PitchClass+
8
+ # representation of +csound+ and other software
9
+ #
10
+ # Internally, the representation is in semitones. In order to be
11
+ # consistent with the +csound+ representation, a +PitchClass+ of
12
+ # +8.00+ represents +central C4+ (+ca. 261.3 Hz+, depending on tuning),
13
+ # while a +PitchClass+ of +0.00+ is 96 semitones below (8 * 12).
14
+ #
15
+ # The internal representation allows for floating point values, negative
16
+ # values etc. However, since the +PitchClass+ is a geometric
17
+ # representation of pitches, it will never produce a negative
18
+ # frequency value (for semitones that go to -Inf., it'll produce
19
+ # infinitely small positive values.
20
+ #
4
21
  class PitchClass
5
22
 
6
- attr_reader :octave, :semi
7
-
8
23
  #
9
24
  # +Mext::Music::PitchClass.new(float_value, semi = nil)+:
10
25
  #
@@ -18,7 +33,40 @@ module Mext
18
33
  #
19
34
  #:nodoc:
20
35
  def initialize(fv, semi = nil)
21
- semi ? setup_with_two_arguments(fv, semi) : setup_with_one_argument(fv)
36
+ @semi = semi ? setup_with_two_arguments(fv, semi) : setup_with_one_argument(fv)
37
+ end
38
+
39
+ #:doc:
40
+ #
41
+ # +octave+
42
+ #
43
+ # returns the octave part of the pitch class
44
+ #
45
+ def octave
46
+ res = @semi / ::Numeric::CNPO
47
+ res >= 0.0 ? res.floor : res.ceil
48
+ end
49
+
50
+ #:doc:
51
+ #
52
+ # +semi+
53
+ #
54
+ # returns the semitone part of the pitch class
55
+ #
56
+ def semi
57
+ @semi >= 0.0 ? (@semi % 12.0) : (@semi % -12.0)
58
+ end
59
+
60
+ #:doc:
61
+ #
62
+ # +inner_representation+
63
+ #
64
+ # returns the inner representation in semitones (this is a public method
65
+ # since it is used in several comparison methods)
66
+ #
67
+ #:no_doc:
68
+ def inner_representation
69
+ @semi
22
70
  end
23
71
 
24
72
  #:doc:
@@ -29,11 +77,7 @@ module Mext
29
77
  #
30
78
  #:nodoc:
31
79
  def to_f
32
- #
33
- # we suppose here that the pitch_class data is well-formed inside the
34
- # object
35
- #
36
- self.octave + (self.semi / ::Numeric::PCC)
80
+ self.inner_representation.semitopch
37
81
  end
38
82
 
39
83
  #:doc:
@@ -44,7 +88,7 @@ module Mext
44
88
  #
45
89
  #:nodoc:
46
90
  def to_freq
47
- self.to_f.pchcps
91
+ self.inner_representation.semitopch.pchcps
48
92
  end
49
93
 
50
94
  #:doc:
@@ -55,7 +99,7 @@ module Mext
55
99
  #:nodoc:
56
100
  [:>, :>=, :<, :<=, :<=>, :==, :!=, :=== ].each do
57
101
  |op|
58
- define_method(op) { |other| self.to_freq.send(op, other.to_freq) }
102
+ define_method(op) { |other| self.inner_representation.send(op, other.inner_representation) }
59
103
  end
60
104
 
61
105
  #:doc:
@@ -66,11 +110,7 @@ module Mext
66
110
  #
67
111
  #:nodoc:
68
112
  def +(other)
69
- octave = ((self.to_semitones + other.to_semitones) / 12.0).cround
70
- octave = octave >= 0.0 ? octave.floor : octave.ceil
71
- semis = (self.to_semitones + other.to_semitones).cround % 12.0
72
- phase = octave >= 0.0 ? 1 : -1
73
- PitchClass.new(octave, phase*semis)
113
+ self.class.create_from_semitones((self.inner_representation + other.inner_representation).cround)
74
114
  end
75
115
 
76
116
  #:doc:
@@ -82,8 +122,7 @@ module Mext
82
122
  #
83
123
  #:nodoc:
84
124
  def -(other)
85
- tot_semi = self.to_semitones - other.to_semitones
86
- PitchClass.new(0.0, tot_semi)
125
+ self.class.create_from_semitones((self.inner_representation - other.inner_representation).cround)
87
126
  end
88
127
 
89
128
  #:doc:
@@ -92,9 +131,7 @@ module Mext
92
131
  #
93
132
  # returns the +PitchClass+ to a number of semitones
94
133
  #
95
- def to_semitones
96
- self.to_f.pchtosemi
97
- end
134
+ alias_method :to_semitones, :inner_representation
98
135
 
99
136
  #:doc:
100
137
  #
@@ -105,18 +142,30 @@ module Mext
105
142
  #
106
143
  #:nodoc:
107
144
  def interval(other)
108
- other.to_semitones - self.to_semitones
145
+ (other - self).inner_representation
109
146
  end
110
147
 
111
148
  #:doc:
112
149
  #
113
150
  # +transpose(semitones)+
114
151
  #
115
- # returns the transposition in semitones
152
+ # returns a +PitchClass+ transposed by +semitones+ semitones
116
153
  #
117
154
  #:nodoc:
118
155
  def transpose(semi)
119
- self + PitchClass.new(0.0, semi)
156
+ self.class.create_from_semitones((self.inner_representation + semi).cround)
157
+ end
158
+
159
+ #:doc:
160
+ #
161
+ # +transpose!(semitones)+
162
+ #
163
+ # transposes the receiver by +semitones+ semitones
164
+ #
165
+ #:nodoc:
166
+ def transpose!(semi)
167
+ @semi += semi
168
+ self
120
169
  end
121
170
 
122
171
  #:doc:
@@ -132,7 +181,46 @@ module Mext
132
181
  self.interval(other) * prop
133
182
  end
134
183
 
184
+ #:doc:
185
+ #
186
+ # +add_semitones(semi)+:
187
+ #
188
+ # returns a +PitchClass+ the addition of semitones required by argument
189
+ #
190
+ # - +semi+: the semitones to add; can be any sort of +Numeric+, also
191
+ # negative to subtract.
192
+ #
193
+ def add_semitones(semi)
194
+ self.class.create_from_semitones((self.inner_representation + semi).cround)
195
+ end
196
+
197
+ #:doc:
198
+ #
199
+ # +add_semitones!(semi)+:
200
+ #
201
+ # returns the addition of semitones required in the receiver's pitch class
202
+ #
203
+ # - +semi+: the semitones to add; can be any sort of +Numeric+, also
204
+ # negative to subtract.
205
+ #
206
+ def add_semitones!(semi)
207
+ @semi += semi
208
+ self
209
+ end
210
+
135
211
  class << self
212
+
213
+ #:doc:
214
+ #
215
+ # +create_from_semitones+
216
+ #
217
+ # creates a +PitchClass+ object out of a semitone representation
218
+ #
219
+ #:nodoc:
220
+ def create_from_semitones(s)
221
+ new(0.0, s)
222
+ end
223
+
136
224
 
137
225
  #:doc:
138
226
  #
@@ -172,17 +260,7 @@ module Mext
172
260
  # taking into account that pitch classes can be positive or negative
173
261
  #
174
262
  def setup_with_two_arguments(oct, s)
175
- octsemi = (s / 12.0)
176
- octsemi = case
177
- when octsemi > 0.0 then octsemi.floor
178
- when octsemi < 0.0 then octsemi.ceil
179
- when octsemi == 0.0 then 0.0
180
- end
181
- oct += octsemi
182
- remainder = s >= 0.0 ? (s % 12.0) : (s % -12.0)
183
- @octave = oct
184
- @semi = remainder
185
- self
263
+ (oct * ::Numeric::CNPO) + s
186
264
  end
187
265
 
188
266
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mext
2
- VERSION = '0.10.0'
2
+ VERSION = '0.10.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-mext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicola Bernardini
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-14 00:00:00.000000000 Z
11
+ date: 2019-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler