/
pitch_class_spec.rb
231 lines (193 loc) · 5.45 KB
/
pitch_class_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
require 'spec_helper'
describe MTK::PitchClass do
let(:names) {
[
['B#', 'C', 'Dbb'],
['B##', 'C#', 'Db'],
['C##', 'D', 'Ebb'],
['D#', 'Eb', 'Fbb'],
['D##', 'E', 'Fb'],
['E#', 'F', 'Gbb'],
['E##', 'F#', 'Gb'],
['F##', 'G', 'Abb'],
['G#', 'Ab'],
['G##', 'A', 'Bbb'],
['A#', 'Bb', 'Cbb'],
['A##', 'B', 'Cb']
].flatten
}
describe 'NAMES' do
it "is the 12 note names in western chromatic scale" do
PitchClass::NAMES =~ ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']
end
it "is immutable" do
lambda{ PitchClass::NAMES << 'H' }.should raise_error
end
end
describe 'VALID_NAMES' do
it "is all enharmonic spellings of NAMES including sharps, flats, double-sharps, and double-flats" do
PitchClass::VALID_NAMES =~ names
end
it "is immutable" do
lambda{ PitchClass::VALID_NAMES << 'H' }.should raise_error
end
end
describe '.new' do
it "is private" do
lambda{ PitchClass.new('C',0) }.should raise_error
end
end
describe '.from_s' do
context "the argument is a valid name" do
it "returns a PitchClass" do
names.each { |name| PitchClass[name].should be_a PitchClass }
end
it "returns an object with that name" do
names.each { |name| PitchClass[name].name.should == name }
end
it "ignores case" do
for name in names
PitchClass[name.upcase].name.should == name
PitchClass[name.downcase].name.should == name
end
end
end
context "the argument is not a valid name" do
it "returns nil, if the name doesn't exist" do
PitchClass['z'].should be_nil
end
end
end
describe '.from_s' do
it "acts like .[]" do
for name in ['C', 'bbb', 'z']
PitchClass.from_s(name).should == PitchClass[name]
end
end
end
describe '.from_name' do
it "acts like .[]" do
for name in ['C', 'bbb', 'z']
PitchClass.from_name(name).should == PitchClass[name]
end
end
end
describe '.from_i' do
it "returns the PitchClass with that value" do
PitchClass.from_i(2).should == D
end
it "returns the PitchClass with that value mod 12" do
PitchClass.from_i(14).should == D
PitchClass.from_i(-8).should == E
end
end
describe '#name' do
it "is the name of the pitch class" do
C.name.should == 'C'
end
end
describe '#to_i' do
it "is the integer value of the pitch class" do
C.to_i.should == 0
D.to_i.should == 2
E.to_i.should == 4
end
end
describe '#to_s' do
it "returns the name" do
C.to_s.should == C.name
for name in names
PitchClass.from_s(name).to_s.should == name
end
end
end
describe '#==' do
it "checks for equality" do
C.should == C
C.should_not == D
end
it "treats enharmonic names as equal" do
C.should == PitchClass('B#')
C.should == PitchClass('Dbb')
end
end
describe "#<=>" do
it "compares the underlying int value" do
(C <=> D).should < 0
(B <=> C).should > 0
end
end
describe '#+' do
it "adds the integer value of the argument and #to_i" do
(C + 4).should == E
end
it "'wraps around' the range 0-11" do
(D + 10).should == C
end
end
describe "#transpose" do
it "behaves like #+" do
C.transpose(2.semitones).should == C + 2
end
end
describe '#-' do
it "subtracts the integer value of the argument from #to_i" do
(E - 2).should == D
end
it "'wraps around' the range 0-11" do
(C - 8).should == E
end
end
describe "#invert" do
it 'inverts the pitch class around the given center pitch class' do
E.invert(D).should == C
end
it 'wraps around octaves as needed (always returns a valid pitch class)' do
E.invert(B).should == Gb
end
it "returns the pitch class when the argument is the same pitch class" do
E.invert(E).should == E
end
end
describe "#distance_to" do
it "is the distance in semitones between 2 PitchClass objects" do
C.distance_to(D).should == 2
end
it "is the shortest distance (accounts from octave 'wrap around')" do
B.distance_to(C).should == 1
end
it "is a negative distance in semitones when the cloest given PitchClass is at a higher Pitch" do
D.distance_to(C).should == -2
C.distance_to(B).should == -1
end
it "is (positive) 6 for tritone distances, when this PitchClass is C-F" do
for pc in [C,Db,D,Eb,E,F]
pc.distance_to(pc+TT).should == 6
end
end
it "is -6 for tritone distances, when this PitchClass is Gb-B" do
for pc in [Gb,G,Ab,A,Bb,B]
pc.distance_to(pc+TT).should == -6
end
end
end
end
describe MTK do
describe '#PitchClass' do
it "acts like from_s if the argument is a String" do
PitchClass('D').should == PitchClass.from_s('D')
end
it "acts like from_s if the argument is a Symbol" do
PitchClass(:D).should == PitchClass.from_s('D')
end
it "acts like from_i if the argument is a Numberic" do
PitchClass(3).should == PitchClass.from_i(3)
end
it "returns the argument if it's already a PitchClass" do
PitchClass(C).should be_equal C
end
it "raises an error for types it doesn't understand" do
lambda{ PitchClass({:not => :compatible}) }.should raise_error
end
end
end