note_frequencies 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +10 -0
- data/lib/note_frequencies.rb +75 -0
- data/lib/note_frequencies/version.rb +3 -0
- data/note_frequencies.gemspec +23 -0
- data/test/test_note_frequencies.rb +79 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Choan Galvez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# NoteFrequencies
|
2
|
+
|
3
|
+
A library to get frequencies from pitch names. Mainly:
|
4
|
+
|
5
|
+
NoteFrequencies.frequency_from_name("e'") # => 326.93
|
6
|
+
|
7
|
+
The naming accepts both Helmholtz (middle C is `c'`) and scientific notacion (middle C is `c4`).
|
8
|
+
|
9
|
+
Alterations accepted include Lilypond’s style (`es`, `is`, `eses`, `isis`) and ASCII style (`b`, `#`, `bb`, `##`, `x`).
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'note_frequencies'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install note_frequencies
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To get the frequency from a named note:
|
28
|
+
|
29
|
+
NoteFrequencies.frequency_from_name("e'") # => 326.93
|
30
|
+
|
31
|
+
To get the frequency from a delta (from middle A)
|
32
|
+
|
33
|
+
NoteFrequencies.frequency_from_delta(-5) # => 326.93
|
34
|
+
|
35
|
+
Fork tuning is configurable:
|
36
|
+
|
37
|
+
NoteFrequencies.frequency_from_name("a", :tuning => 443) # => 221.5
|
38
|
+
|
39
|
+
Frequencies are rounded by default to 2 decimal places. Should you need more precission:
|
40
|
+
|
41
|
+
NoteFrequencies.frequency_from_name("e'", :round => 5) # => 329.62756
|
42
|
+
|
43
|
+
|
44
|
+
Both scientific notation and Helmholtz’s notation are accepted:
|
45
|
+
|
46
|
+
NoteFrequencies.frequency_from_name("a4") # => 440.0
|
47
|
+
NoteFrequencies.frequency_from_name("a'") # => 440.0
|
48
|
+
|
49
|
+
Alterations are accepted as `b` (flat), `bb` (double flat), `es` (flat), `eses` (double flat), `#` (sharp), `##` (double sharp), `x` (double sharp), `is` (sharp) and `isis` (double sharp).
|
50
|
+
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require "note_frequencies/version"
|
2
|
+
|
3
|
+
module NoteFrequencies
|
4
|
+
|
5
|
+
DEFAULT_TUNING = 440
|
6
|
+
DEFAULT_ROUND = 2
|
7
|
+
|
8
|
+
# frequency from delta
|
9
|
+
def self.frequency(delta_from_a, conf = {})
|
10
|
+
conf = {
|
11
|
+
:tuning => DEFAULT_TUNING,
|
12
|
+
:round => DEFAULT_ROUND
|
13
|
+
}.update(conf)
|
14
|
+
tuning = conf[:tuning]
|
15
|
+
freq = tuning * (2**(delta_from_a.to_f/12))
|
16
|
+
round(freq, conf[:round])
|
17
|
+
end
|
18
|
+
|
19
|
+
# delta in steps from a' (absolute)
|
20
|
+
def self.delta_from_name(name)
|
21
|
+
note, alt, octave = name.scan(/([a-g])(eses|isis|bb|##|es|is|b|#|x)?(\d|(,+)|('+))?/).first
|
22
|
+
# puts octave.inspect
|
23
|
+
octave_delta = if octave
|
24
|
+
if octave.start_with?("'")
|
25
|
+
octave.length - 1
|
26
|
+
elsif octave.start_with?(",")
|
27
|
+
-octave.length - 1
|
28
|
+
else
|
29
|
+
octave.to_i - 4
|
30
|
+
end
|
31
|
+
else
|
32
|
+
-1
|
33
|
+
end
|
34
|
+
# puts octave_delta.inspect
|
35
|
+
alt_delta = if (alt == 'is' || alt == '#')
|
36
|
+
1
|
37
|
+
elsif (alt == 'isis' || alt == '##' || alt == 'x')
|
38
|
+
2
|
39
|
+
elsif (alt == 'es' || alt == 'b')
|
40
|
+
-1
|
41
|
+
else
|
42
|
+
0
|
43
|
+
end
|
44
|
+
delta(note) + alt_delta + 12 * octave_delta
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.round(q, r)
|
48
|
+
pow = 10**r
|
49
|
+
(q*pow).round/pow.to_f
|
50
|
+
end
|
51
|
+
|
52
|
+
# delta in steps from A (relative)
|
53
|
+
def self.delta(name)
|
54
|
+
@deltas ||= begin
|
55
|
+
{
|
56
|
+
:a => 0,
|
57
|
+
:b => 2,
|
58
|
+
:c => -9,
|
59
|
+
:d => -7,
|
60
|
+
:e => -5,
|
61
|
+
:f => -4,
|
62
|
+
:g => -2
|
63
|
+
}
|
64
|
+
end
|
65
|
+
@deltas[name.to_sym]
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.frequency_from_name(name, conf = {})
|
69
|
+
conf = {
|
70
|
+
:tuning => DEFAULT_TUNING,
|
71
|
+
:round => DEFAULT_ROUND
|
72
|
+
}.update(conf)
|
73
|
+
frequency(delta_from_name(name), conf)
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'note_frequencies/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "note_frequencies"
|
8
|
+
spec.version = NoteFrequencies::VERSION
|
9
|
+
spec.authors = ["Choan Gálvez"]
|
10
|
+
spec.email = ["choan.galvez@gmail.com"]
|
11
|
+
spec.description = %q{Convert musical note names to frequencies}
|
12
|
+
spec.summary = %q{A tool for converting musical note names to it vibrating frequencies. Accepts input in both Helmholtz (`a'`, `a,`) and scientific notation (`a4`, `a3`)}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'note_frequencies'
|
3
|
+
|
4
|
+
class NoteFrequenciesTest < Test::Unit::TestCase
|
5
|
+
def test_round_up
|
6
|
+
assert_equal 333.34, NoteFrequencies.round(333.335, 2)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_round_down
|
10
|
+
assert_equal 333.33, NoteFrequencies.round(333.334, 2)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_a_from_delta
|
14
|
+
assert_equal 440, NoteFrequencies.frequency(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_low_a_from_delta
|
18
|
+
assert_equal 220, NoteFrequencies.frequency(-12)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_high_a_from_delta
|
22
|
+
assert_equal 880, NoteFrequencies.frequency(12)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_e_from_delta
|
26
|
+
assert_equal 329.63, NoteFrequencies.frequency(-5)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_delta_octave
|
30
|
+
assert_equal 0, NoteFrequencies.delta_from_name("a'")
|
31
|
+
assert_equal 12, NoteFrequencies.delta_from_name("a''")
|
32
|
+
assert_equal -12, NoteFrequencies.delta_from_name("a")
|
33
|
+
assert_equal -24, NoteFrequencies.delta_from_name("a,")
|
34
|
+
assert_equal -36, NoteFrequencies.delta_from_name("a,,")
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_delta_alt
|
38
|
+
assert_equal 1, NoteFrequencies.delta_from_name("ais'")
|
39
|
+
assert_equal 1, NoteFrequencies.delta_from_name("a#'")
|
40
|
+
assert_equal -1, NoteFrequencies.delta_from_name("aes'")
|
41
|
+
assert_equal -1, NoteFrequencies.delta_from_name("ab'")
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_delta_double_alt
|
45
|
+
assert_equal 2, NoteFrequencies.delta_from_name("aisis'")
|
46
|
+
assert_equal 2, NoteFrequencies.delta_from_name("ax'")
|
47
|
+
assert_equal 2, NoteFrequencies.delta_from_name("a##'")
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_jump_octave
|
51
|
+
assert_equal 3, NoteFrequencies.delta_from_name("bis'")
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_delta_other
|
55
|
+
assert_equal -9, NoteFrequencies.delta_from_name("c'")
|
56
|
+
assert_equal -5, NoteFrequencies.delta_from_name("e'")
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_delta_enharmonics
|
60
|
+
assert_equal NoteFrequencies.delta_from_name("g#'"), NoteFrequencies.delta_from_name("ab'")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_scientific_octave
|
64
|
+
assert_equal 0, NoteFrequencies.delta_from_name('a4')
|
65
|
+
assert_equal -9, NoteFrequencies.delta_from_name('c4')
|
66
|
+
assert_equal -12, NoteFrequencies.delta_from_name('a3')
|
67
|
+
assert_equal 12, NoteFrequencies.delta_from_name('a5')
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_frequency_from_name
|
71
|
+
assert_equal 440, NoteFrequencies.frequency_from_name("a'")
|
72
|
+
assert_equal 880, NoteFrequencies.frequency_from_name("a''")
|
73
|
+
assert_equal 220, NoteFrequencies.frequency_from_name("a")
|
74
|
+
assert_equal 110, NoteFrequencies.frequency_from_name("a,")
|
75
|
+
assert_equal 329.63, NoteFrequencies.frequency_from_name("e'")
|
76
|
+
assert_equal 440, NoteFrequencies.frequency_from_name("gx'")
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: note_frequencies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Choan Gálvez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Convert musical note names to frequencies
|
47
|
+
email:
|
48
|
+
- choan.galvez@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/note_frequencies.rb
|
59
|
+
- lib/note_frequencies/version.rb
|
60
|
+
- note_frequencies.gemspec
|
61
|
+
- test/test_note_frequencies.rb
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
hash: 234481389
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
hash: 234481389
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.25
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: A tool for converting musical note names to it vibrating frequencies. Accepts
|
93
|
+
input in both Helmholtz (`a'`, `a,`) and scientific notation (`a4`, `a3`)
|
94
|
+
test_files:
|
95
|
+
- test/test_note_frequencies.rb
|