music-transcription 0.13.0 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/music-transcription/model/note.rb +1 -1
- data/lib/music-transcription/model/pitch.rb +36 -54
- data/lib/music-transcription/version.rb +1 -1
- data/spec/model/pitch_spec.rb +45 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2519d2fe69e6b6d5944695bae502dece8dd456c9
|
4
|
+
data.tar.gz: b4da4b389692c5eb70bb2505d003a783fd565833
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80a849288ffc1c279b5083ea6d878bb7de606d9f69b6885263945a5b95e1617a5c33cea586639fa8560c6ef72c4669892c2a4c913a0a0234d2b38d46693d36dd
|
7
|
+
data.tar.gz: a4ca68fad70b5cee5939fa319e23a0fc3580ed0b3016e03b2046ea3e56f1f84d5610c9857f31ac29f409c8b673f4b6b4d3b762f02366dd02132bd72543a1c995
|
@@ -43,12 +43,6 @@ class Pitch
|
|
43
43
|
return self.ratio() * BASE_FREQ
|
44
44
|
end
|
45
45
|
|
46
|
-
# Set the pitch according to the given frequency. Uses the current base_freq
|
47
|
-
# to determine what the pitch ratio should be, and sets it accordingly.
|
48
|
-
def freq= freq
|
49
|
-
self.ratio = freq / BASE_FREQ
|
50
|
-
end
|
51
|
-
|
52
46
|
# Calculate the total semitone count. Converts octave to semitone count
|
53
47
|
# before adding to existing semitone count.
|
54
48
|
# @return [Fixnum] total semitone count
|
@@ -56,17 +50,6 @@ class Pitch
|
|
56
50
|
return (@octave * SEMITONES_PER_OCTAVE) + @semitone
|
57
51
|
end
|
58
52
|
|
59
|
-
# Set the Pitch ratio according to a total number of semitones.
|
60
|
-
# @param [Fixnum] semitone The total number of semitones to use.
|
61
|
-
# @raise [NonIntegerError] if semitone is not an Integer
|
62
|
-
def total_semitone= semitone
|
63
|
-
unless semitone.is_a?(Integer)
|
64
|
-
raise NonIntegerError, "semitone #{semitone} is not a Integer"
|
65
|
-
end
|
66
|
-
@octave, @semitone = 0, semitone
|
67
|
-
normalize!
|
68
|
-
end
|
69
|
-
|
70
53
|
# Calculate the pitch ratio. Raises 2 to the power of the total semitone
|
71
54
|
# count divided by semitones-per-octave.
|
72
55
|
# @return [Float] ratio
|
@@ -74,16 +57,6 @@ class Pitch
|
|
74
57
|
2.0**(self.total_semitone.to_f / SEMITONES_PER_OCTAVE)
|
75
58
|
end
|
76
59
|
|
77
|
-
# Represent the Pitch ratio according to a ratio.
|
78
|
-
# @param [Numeric] ratio The ratio to represent.
|
79
|
-
# @raise [NonPositiveError] unless ratio is > 0
|
80
|
-
def ratio= ratio
|
81
|
-
raise NonPositiveError, "ratio #{ratio} is not > 0" unless ratio > 0
|
82
|
-
|
83
|
-
x = Math.log2 ratio
|
84
|
-
self.total_semitone = (x * SEMITONES_PER_OCTAVE).round
|
85
|
-
end
|
86
|
-
|
87
60
|
# Round to the nearest semitone.
|
88
61
|
def round
|
89
62
|
self.clone.round!
|
@@ -119,19 +92,23 @@ class Pitch
|
|
119
92
|
# Add pitches by adding the total semitone count of each.
|
120
93
|
# @param [Pitch] other The pitch object to add.
|
121
94
|
def + (other)
|
122
|
-
|
123
|
-
octave:
|
124
|
-
|
125
|
-
|
95
|
+
if other.is_a? Integer
|
96
|
+
return Pitch.new(octave: @octave, semitone: @semitone + other)
|
97
|
+
else
|
98
|
+
return Pitch.new(octave: (@octave + other.octave),
|
99
|
+
semitone: (@semitone + other.semitone))
|
100
|
+
end
|
126
101
|
end
|
127
102
|
|
128
103
|
# Add pitches by subtracting the total semitone count.
|
129
104
|
# @param [Pitch] other The pitch object to subtract.
|
130
105
|
def - (other)
|
131
|
-
|
132
|
-
octave:
|
133
|
-
|
134
|
-
|
106
|
+
if other.is_a? Integer
|
107
|
+
return Pitch.new(octave: @octave, semitone: @semitone - other)
|
108
|
+
else
|
109
|
+
return Pitch.new(octave: (@octave - other.octave),
|
110
|
+
semitone: (@semitone - other.semitone))
|
111
|
+
end
|
135
112
|
end
|
136
113
|
|
137
114
|
# Produce an identical Pitch object.
|
@@ -139,17 +116,6 @@ class Pitch
|
|
139
116
|
Marshal.load(Marshal.dump(self)) # is this cheating?
|
140
117
|
end
|
141
118
|
|
142
|
-
# Balance out the octave and semitone count.
|
143
|
-
def normalize!
|
144
|
-
semitoneTotal = (@octave * SEMITONES_PER_OCTAVE) + @semitone
|
145
|
-
|
146
|
-
@octave = semitoneTotal / SEMITONES_PER_OCTAVE
|
147
|
-
semitoneTotal -= @octave * SEMITONES_PER_OCTAVE
|
148
|
-
|
149
|
-
@semitone = semitoneTotal
|
150
|
-
return self
|
151
|
-
end
|
152
|
-
|
153
119
|
def to_s(sharpit = false)
|
154
120
|
letter = case semitone
|
155
121
|
when 0 then "C"
|
@@ -169,16 +135,32 @@ class Pitch
|
|
169
135
|
return letter + octave.to_s
|
170
136
|
end
|
171
137
|
|
172
|
-
def self.
|
173
|
-
|
174
|
-
|
175
|
-
|
138
|
+
def self.from_ratio ratio
|
139
|
+
raise NonPositiveError, "ratio #{ratio} is not > 0" unless ratio > 0
|
140
|
+
x = Math.log2 ratio
|
141
|
+
semitones = (x * SEMITONES_PER_OCTAVE).round
|
142
|
+
from_semitones(semitones)
|
143
|
+
end
|
144
|
+
|
145
|
+
def self.from_freq freq
|
146
|
+
from_ratio(freq / BASE_FREQ)
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.from_semitones semitones
|
150
|
+
Pitch.new(semitone: semitones)
|
176
151
|
end
|
177
152
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
153
|
+
private
|
154
|
+
|
155
|
+
# Balance out the octave and semitone count.
|
156
|
+
def normalize!
|
157
|
+
semitoneTotal = (@octave * SEMITONES_PER_OCTAVE) + @semitone
|
158
|
+
|
159
|
+
@octave = semitoneTotal / SEMITONES_PER_OCTAVE
|
160
|
+
semitoneTotal -= @octave * SEMITONES_PER_OCTAVE
|
161
|
+
|
162
|
+
@semitone = semitoneTotal
|
163
|
+
return self
|
182
164
|
end
|
183
165
|
end
|
184
166
|
|
data/spec/model/pitch_spec.rb
CHANGED
@@ -17,11 +17,11 @@ describe Pitch do
|
|
17
17
|
]
|
18
18
|
end
|
19
19
|
|
20
|
-
it "should be
|
20
|
+
it "should be constructable with no parameters (no error raised)" do
|
21
21
|
lambda { Pitch.new }.should_not raise_error
|
22
22
|
end
|
23
23
|
|
24
|
-
it "should
|
24
|
+
it "should take keyword args" do
|
25
25
|
obj = Pitch.new octave: 4, semitone: 3
|
26
26
|
obj.octave.should eq(4)
|
27
27
|
obj.semitone.should eq(3)
|
@@ -41,25 +41,36 @@ describe Pitch do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
describe '.from_ratio' do
|
45
|
+
it 'should return a Pitch with given ratio' do
|
46
|
+
@cases.each do |case_data|
|
47
|
+
p = Pitch.from_ratio case_data[:ratio]
|
48
|
+
|
49
|
+
p.octave.should eq case_data[:octave]
|
50
|
+
p.semitone.should eq case_data[:semitone]
|
51
|
+
p.total_semitone.should eq case_data[:total_semitone]
|
52
|
+
end
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
56
|
+
describe '.from_semitones' do
|
57
|
+
it 'should return a Pitch with given total semitones' do
|
58
|
+
@cases.each do |case_data|
|
59
|
+
p = Pitch.from_semitones case_data[:total_semitone]
|
60
|
+
|
61
|
+
p.octave.should eq case_data[:octave]
|
62
|
+
p.semitone.should eq case_data[:semitone]
|
63
|
+
p.total_semitone.should eq case_data[:total_semitone]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '.from_freq' do
|
69
|
+
it 'should make a pitch whose freq is approximately the given freq' do
|
70
|
+
[16.35, 440.0, 987.77].each do |given_freq|
|
71
|
+
pitch = Pitch.from_freq given_freq
|
72
|
+
pitch.freq.should be_within(0.01).of(given_freq)
|
73
|
+
end
|
63
74
|
end
|
64
75
|
end
|
65
76
|
|
@@ -96,6 +107,22 @@ describe Pitch do
|
|
96
107
|
(p3 - p1).should eq(Pitch.new semitone: 2)
|
97
108
|
end
|
98
109
|
|
110
|
+
it "should be addable and subtractable with integers" do
|
111
|
+
p1 = Pitch.new semitone: 1
|
112
|
+
p2 = Pitch.new semitone: 2
|
113
|
+
p3 = Pitch.new semitone: 3
|
114
|
+
|
115
|
+
(p1 + 2).should eq(Pitch.new semitone: 3)
|
116
|
+
(p1 + 3).should eq(Pitch.new semitone: 4)
|
117
|
+
(p2 + 3).should eq(Pitch.new semitone: 5)
|
118
|
+
|
119
|
+
(p1 - 2).should eq(Pitch.new semitone: -1)
|
120
|
+
(p1 - 3).should eq(Pitch.new semitone: -2)
|
121
|
+
(p2 - 3).should eq(Pitch.new semitone: -1)
|
122
|
+
(p3 - 2).should eq(Pitch.new semitone: 1)
|
123
|
+
(p3 - 1).should eq(Pitch.new semitone: 2)
|
124
|
+
end
|
125
|
+
|
99
126
|
it "should have freq of 440 for A4" do
|
100
127
|
a4 = Pitch.new octave: 4, semitone: 9
|
101
128
|
a4.freq.should be_within(0.01).of(440.0)
|
@@ -139,23 +166,4 @@ describe Pitch do
|
|
139
166
|
end
|
140
167
|
end
|
141
168
|
end
|
142
|
-
|
143
|
-
describe '.make_from_freq' do
|
144
|
-
it 'should make a pitch whose freq is approximately the given freq' do
|
145
|
-
[16.35, 440.0, 987.77].each do |given_freq|
|
146
|
-
pitch = Pitch.make_from_freq given_freq
|
147
|
-
pitch.freq.should be_within(0.01).of(given_freq)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
describe '.make_from_semitone' do
|
153
|
-
context 'given an integer less than 12' do
|
154
|
-
before(:all) { @pitch = Pitch.make_from_semitone(11) }
|
155
|
-
|
156
|
-
it 'semitone should equal given integer' do
|
157
|
-
@pitch.semitone.should eq 11
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
169
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: music-transcription
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Tunnell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|