music-utils 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.0.1 (May 6, 2011)
2
+
3
+ Features:
4
+
5
+ - Added benchmarks to project
6
+ - Performance increased about 30 percent to classify intervals
7
+
8
+
1
9
  ## 1.0.0 (May 6, 2011)
2
10
 
3
11
  Features:
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'music-utils'
4
+ require 'music-utils/scales/scales'
5
+ require 'benchmark'
6
+
7
+ n = 100_000
8
+
9
+ Benchmark.bmbm(15) do |b|
10
+ b.report('with strings:') do
11
+ n.times { MusicUtils.short('do', 'mi') }
12
+ end
13
+ b.report('with symbols:') do
14
+ n.times { MusicUtils.short(:do, :mi) }
15
+ end
16
+ b.report('with scales:') do
17
+ n.times { MusicUtils.short(Scales::DO, Scales::MI) }
18
+ end
19
+ end
data/lib/music-utils.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'music-utils/interval/interval'
2
+ require 'music-utils/scales/scales'
2
3
 
3
4
  # Music utils
4
5
  module MusicUtils
6
+ include Scales
5
7
 
6
8
  # Returns the number of the interval
7
9
  def MusicUtils.number(note1, note2, step = 0)
@@ -7,8 +7,10 @@ module MusicUtils
7
7
  include Scales
8
8
 
9
9
  def initialize(note1, note2, step)
10
- @note1 = note1
11
- @note2 = note2
10
+ @note1 = note1[0..1].to_sym
11
+ @note1_alt = note1[2..3]
12
+ @note2 = note2[0..1].to_sym
13
+ @note2_alt = note2[2..3]
12
14
  @step = step
13
15
  end
14
16
 
@@ -36,9 +38,9 @@ module MusicUtils
36
38
  count, i = no_unison(count, i)
37
39
 
38
40
  # counting semi-tones
39
- until_find_note2(i) do |i|
41
+ until_find_note2(i) do |ii|
40
42
  # from 'mi' to 'fa' and 'si' to 'do' there 1 semi-tone
41
- if DIATONIC_SCALE[i] == 'fa' or DIATONIC_SCALE[i] == 'do'
43
+ if DIATONIC_SCALE[ii] == Scales::FA or DIATONIC_SCALE[ii] == Scales::DO
42
44
  count += 1
43
45
  else
44
46
  count += 2
@@ -48,19 +50,15 @@ module MusicUtils
48
50
  count = count + (12 * @step) if @step > 0
49
51
 
50
52
  # counting notes alterations
51
- alter = @note1[2..3]
52
-
53
- alter.each_char do |c|
54
- count += 1 if c == 'b'
55
- count -= 1 if c == '#'
56
- end
53
+ count += 1 if @note1_alt == Scales::FLAT
54
+ coutn += 2 if @note1_alt == Scales::DFLAT
55
+ count -= 1 if @note1_alt == Scales::SHARP
56
+ count -= 1 if @note1_alt == Scales::DSHARP
57
57
 
58
- alter = @note2[2..3]
59
-
60
- alter.each_char do |c|
61
- count -= 1 if c == 'b'
62
- count += 1 if c == '#'
63
- end
58
+ count -= 1 if @note2_alt == Scales::FLAT
59
+ count -= 2 if @note2_alt == Scales::DFLAT
60
+ count += 1 if @note2_alt == Scales::SHARP
61
+ count -= 2 if @note2_alt == Scales::DSHARP
64
62
 
65
63
  count
66
64
  end
@@ -82,7 +80,7 @@ module MusicUtils
82
80
  # Common loop to search note 2
83
81
  def until_find_note2(i)
84
82
  # search note2
85
- while DIATONIC_SCALE[i] != @note2[0..1]
83
+ while DIATONIC_SCALE[i] != @note2
86
84
  i += 1
87
85
  if i > DIATONIC_SCALE.length
88
86
  i = 0; next
@@ -93,7 +91,7 @@ module MusicUtils
93
91
 
94
92
  # Jumps to the next note if note 1 and note 2 are the same
95
93
  def no_unison(count, i)
96
- if @note1[0..1] == @note2[0..1]
94
+ if @note1 == @note2
97
95
  count += 1; i += 1
98
96
  end
99
97
  [count, i]
@@ -101,7 +99,7 @@ module MusicUtils
101
99
 
102
100
  # Returns index of note 1
103
101
  def note1_index
104
- DIATONIC_SCALE.index(@note1[0..1])
102
+ DIATONIC_SCALE.index(@note1)
105
103
  end
106
104
 
107
105
  end
@@ -1,13 +1,41 @@
1
1
  # Scales module
2
2
  module Scales
3
- DIATONIC_SCALE = ['do', 're', 'mi', 'fa', 'sol', 'la', 'si']
3
+
4
+ # Notes
5
+ DO = :do
6
+ RE = :re
7
+ MI = :mi
8
+ FA = :fa
9
+ SOL = :sol
10
+ LA = :la
11
+ SI = :si
12
+
13
+ DIATONIC_SCALE = [DO, RE, MI, FA, SOL, LA, SI]
14
+
15
+ # Alterations:
16
+ FLAT = 'b'
17
+ DFLAT = FLAT + FLAT
18
+ SHARP = '#'
19
+ DSHARP = SHARP + SHARP
20
+
21
+ # Qualities
22
+ PERF = 'P'
23
+ MAJ = 'M'
24
+ MIN = 'm'
25
+ AUG = 'A'
26
+ AUGP = AUG + AUG
27
+ DIM = 'd'
28
+ DIMP = DIM + DIM
29
+
30
+ # Qualities hash
4
31
  QUALITIES = {
5
- 2 => {0 => 'd', 1 => 'm', 2 => 'M', 3 => 'A', 4 => 'AA'},
6
- 3 => {1 => 'dd', 2 => 'd', 3 => 'm', 4 => 'M', 5 => 'A', 6 => 'AA'},
7
- 4 => {3 => 'dd', 4 => 'd', 5 => 'P', 6 => 'A', 7 => 'AA'},
8
- 5 => {5 => 'dd', 6 => 'd', 7 => 'P', 8 => 'A', 9 => 'AA'},
9
- 6 => {6 => 'dd', 7 => 'd', 8 => 'm', 9 => 'M', 10 => 'A', 11 => 'AA'},
10
- 7 => {8 => 'dd', 9 => 'd', 10 => 'm', 11 => 'M', 12 => 'A', 13 => 'AA'},
11
- 8 => {10 => 'dd', 11 => 'd', 12 => 'P', 13 => 'A', 14 => 'AA'}
32
+ 2 => {0 => DIM, 1 => MIN, 2 => MAJ, 3 => AUG, 4 => AUGP},
33
+ 3 => {1 => DIMP, 2 => DIM, 3 => MIN, 4 => MAJ, 5 => AUG, 6 => AUGP},
34
+ 4 => {3 => DIMP, 4 => DIM, 5 => PERF, 6 => AUG, 7 => AUGP},
35
+ 5 => {5 => DIMP, 6 => DIM, 7 => PERF, 8 => AUG, 9 => AUGP},
36
+ 6 => {6 => DIMP, 7 => DIM, 8 => MIN, 9 => MAJ, 10 => AUG, 11 => AUGP},
37
+ 7 => {8 => DIMP, 9 => DIM, 10 => MIN, 11 => MAJ, 12 => AUG, 13 => AUGP},
38
+ 8 => {10 => DIMP, 11 => DIM, 12 => PERF, 13 => AUG, 14 => AUGP}
12
39
  }
40
+
13
41
  end
@@ -1,3 +1,3 @@
1
1
  module MusicUtils
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: music-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-05-06 00:00:00.000000000Z
12
+ date: 2011-05-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &20979048 !ruby/object:Gem::Requirement
16
+ requirement: &83068570 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.8'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *20979048
24
+ version_requirements: *83068570
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &20978820 !ruby/object:Gem::Requirement
27
+ requirement: &83068380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *20978820
35
+ version_requirements: *83068380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &20978544 !ruby/object:Gem::Requirement
38
+ requirement: &83109790 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20978544
46
+ version_requirements: *83109790
47
47
  description: Utils for music. At the moment only to classify intervals.
48
48
  email:
49
49
  - jorgeluis700@gmail.com
@@ -57,6 +57,7 @@ files:
57
57
  - LICENSE
58
58
  - README.md
59
59
  - Rakefile
60
+ - benchmark/music-utils_bm.rb
60
61
  - lib/music-utils.rb
61
62
  - lib/music-utils/interval/interval.rb
62
63
  - lib/music-utils/scales/scales.rb