ruby-mext 0.21.4 → 0.21.5

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: 38c48bcd24251667ce5add29d5fdce2e84bbe79e96a28a8186908fbd497135fb
4
- data.tar.gz: 2c5b300ff3d78301ebcabd3103c45f2b10cb70873e43403fea362d6010b44d00
3
+ metadata.gz: f2f4f4f25273a53383d136a74e37bc86532da799ffcda4a158216d87e68e4eaf
4
+ data.tar.gz: 006b52ca22341b85fe7f3e6bfb2a53d8242880fb66e5e577ead3a1fe437e29f3
5
5
  SHA512:
6
- metadata.gz: 4214e793102277046d77c518771cf14d2edd2f499557f613c9e615f6639cff7f3897c9aa9a4a9e7572e7034703c6d8249ad8d69779f8df8135b9a4568c069db3
7
- data.tar.gz: 7bc0794803b8c1659b9e669bd9ca60ed1fb6135393a9dc0f7ea207047fb317bdfa32920429afc6f7464765831ec23ec621a72cda4c4f9a1ce5c317f70e55aae0
6
+ metadata.gz: c0aede90dcd997f577eea7f96280c38e1bd815983f746a026f22b0ebe3e7a8a588b762c37a3e76345b7d8159181bf5c11af08e5f8bcf844d362f10a9b3172db3
7
+ data.tar.gz: 56c1b1346179f2daee885cfb0d1e68c929d79b42a5260a618892a226348b9cc3b36fbc1e83f2612e97cec6b87a6166df4db654980a32c6edc6089cc6b0c807c9
data/README.md CHANGED
@@ -10,6 +10,11 @@ These are extensions of usual idiomatic `ruby` objects (i.e. `Numeric`,
10
10
  It is the usual unfinished ever-expanding project. Feel free to contribute to
11
11
  it in the form of pull requests if you are so inclined.
12
12
 
13
+ ## Requirements
14
+
15
+ Some `Function`s (notably: `Mext::Math::Polynomial`) require [`octave`](https://octave.org) to be
16
+ installed and running on the same computer.
17
+
13
18
  ## Installation
14
19
 
15
20
  Add this line to your application's Gemfile:
data/lib/mext/math.rb CHANGED
@@ -10,4 +10,5 @@ end
10
10
  expon
11
11
  log
12
12
  stepwise
13
+ polynomial
13
14
  ).each { |f| require File.join(Mext::MATH_PATH, f) }
@@ -0,0 +1,95 @@
1
+ module Math
2
+
3
+ class Polynomial < Function
4
+
5
+ #
6
+ # +:values+
7
+ #
8
+ # is an array of pairs (+x+, +y+)
9
+ #
10
+ attr_reader :values, :factors
11
+
12
+ #
13
+ # +Math::Polynomial.new(values = [])+
14
+ #
15
+ # stepwise function
16
+ #
17
+ # Arguments are:
18
+ #
19
+ # +values+: an array of (+x+, +y+) pairs
20
+ #
21
+ #:nodoc:
22
+ def initialize(vs = [])
23
+ setup(vs)
24
+ end
25
+
26
+ #:doc:
27
+ #
28
+ # +y(x)+:
29
+ #
30
+ # Returns a value for any given x
31
+ #
32
+ #:nodoc:
33
+ def y(x)
34
+ calculate(x)
35
+ end
36
+
37
+ def label
38
+ 'polynomial function'
39
+ end
40
+
41
+ class << self
42
+
43
+ #
44
+ # +from_yaml(yaml_hash)+:
45
+ #
46
+ # creates a Math::Polynomial class from a yaml file which must have the
47
+ # relevant fields:
48
+ #
49
+ # +values+: an array of duples [x, y]
50
+ #
51
+ def from_yaml(yh)
52
+ new(yh['values'])
53
+ end
54
+
55
+ end
56
+
57
+ private
58
+
59
+ def calculate(x)
60
+ result = 0
61
+ idx = self.factors.size-1
62
+ fidx = 0
63
+ idx.downto(0) do
64
+ |n|
65
+ result += (self.factors[fidx]*(x**(n)))
66
+ fidx += 1
67
+ end
68
+ result
69
+ end
70
+
71
+ #
72
+ # +setup+
73
+ #
74
+ # calls +octave+'s +polyfit+ function to load the factors of the
75
+ # polynomial
76
+ #
77
+ #:nodoc:
78
+ def setup(vs)
79
+ @values = []
80
+ sorted = vs.sort { |a, b| a[0] <=> b[0] }
81
+ @values.concat(sorted)
82
+ xs = self.values.map { |v| v[0] }
83
+ ys = self.values.map { |v| v[1] }
84
+ porder = vs.size
85
+ @factors = ::Octave::Io::polyfit(xs, ys, porder)
86
+ #
87
+ # keep in mind that vs might be empty
88
+ #
89
+ @x_start = self.values.first ? self.values.first.first : self.values.first
90
+ @x_end = self.values.last ? self.values.last.first : self.values.last
91
+ end
92
+
93
+ end
94
+
95
+ end
data/lib/mext/music.rb CHANGED
@@ -7,4 +7,6 @@ end
7
7
  %w(
8
8
  pitch_class
9
9
  meter
10
+ note_name
11
+ note_names
10
12
  ).each { |f| require File.join(Mext::MUSIC_PATH, f) }
@@ -0,0 +1,43 @@
1
+ module Mext
2
+ module Music
3
+ #
4
+ # +Mext::Music::NoteName+
5
+ #
6
+ # produces all the conversions from a named note to +pitch frequency+,
7
+ # +pitch class+, +LilyPond notation+, etc.
8
+ #
9
+ # In these conversions we assume that 8 is the central octave.
10
+ #
11
+ class NotALilypondName < StandardError; end
12
+ class NoteName
13
+
14
+ DEFAULT_NOTE_NAME_OFFSET = 9 # central 'A' 440 is 8.09, isn't it?
15
+ LILY_NOTE_NAMES = %w( a b c d e f )
16
+
17
+ attr_reader :name, :index
18
+
19
+ def initialize(name, idx)
20
+ @name = name
21
+ @index = idx
22
+ end
23
+
24
+ def to_lily(octave = 8, other = '') # 8 is the central octave
25
+ nn = self.name.sub(/\A\s*(.).*$/, '\1').downcase
26
+ raise NotALilypondName, "\"#{self.name}\" does not contain a note" unless LILY_NOTE_NAMES.include?(nn)
27
+ sharps = (self.name.match?(/\A\s*\w#/) && self.name.sub(/\A\s*\w(#+).*$/, '\1').gsub(/#/, 'is')) || nil
28
+ flats = (self.name.match?(/\A\s*\w[Ff]/) && self.name.sub(/\A\s*\w([Ff]+).*$/, '\1').gsub(/[Ff]/, 'es')) || nil
29
+ accs = sharps ? sharps : (flats ? flats : '')
30
+ "#{nn}#{accs}#{octave}#{other}"
31
+ end
32
+
33
+ def to_pitch_class(octave = 8) # 8 is the central octave
34
+ octave + (self.index/100.0)
35
+ end
36
+
37
+ def to_frequency(octave = 8)
38
+ self.to_pitch_class(octave).pchcps
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ module Mext
2
+ module Music
3
+
4
+ DEFAULT_ANGLO_NAMES = %w(A B C D E F G)
5
+ DEFAULT_ITA_NAMES = %w(Do Re Mi Fa Sol La Si)
6
+
7
+ end
8
+ end
data/lib/octave.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Mext
2
+ module Octave
3
+
4
+ PATH = File.expand_path(File.join('..', 'octave'), __FILE__)
5
+
6
+ end
7
+ end
8
+
9
+ %w(
10
+ io
11
+ polyfit
12
+ ).each { |f| require File.join(Mext::Octave::PATH, f) }
data/lib/octave/io.rb ADDED
@@ -0,0 +1,29 @@
1
+ module Octave
2
+
3
+ module Io
4
+
5
+ class << self
6
+
7
+ OCTAVE_EXE='/usr/bin/env octave -q'
8
+ #
9
+ # +Octave::Io.command(command_string)+
10
+ #
11
+ # send a command to Octave in a pipe and return the results.
12
+ # The results are returned as arrays of float values
13
+ #
14
+ def command(command_string)
15
+ res = []
16
+ decorated_command_string = "echo \"#{command_string}\" | #{OCTAVE_EXE}"
17
+ IO.popen(decorated_command_string) do
18
+ |octave_result|
19
+ out = octave_result.readlines.map { |l| l.chomp }
20
+ out.each { |line| res << line.to_f if line.match?(/\s*\d+/) }
21
+ end
22
+ res
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,22 @@
1
+ module Octave
2
+
3
+ module Io
4
+
5
+ class << self
6
+
7
+ #
8
+ # +Octave::Io.polyfit(x, y, n)+
9
+ #
10
+ # get the coefficients of an n-degree polynomial
11
+ # using +octave+'s +polyfit+ function
12
+ #
13
+ def polyfit(x, y, n)
14
+ command_string = "a = polyfit(#{x}, #{y}, #{n}); a'"
15
+ ::Octave::Io.command(command_string)
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
22
+ end
data/lib/ruby-mext.rb CHANGED
@@ -8,5 +8,6 @@ end
8
8
 
9
9
  %w(
10
10
  ruby
11
+ octave
11
12
  mext
12
13
  ).each { |f| require File.join(Mext::ROOT, f) }
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mext
2
- VERSION = '0.21.4'
2
+ VERSION = '0.21.5'
3
3
  end
data/ruby-mext.gemspec CHANGED
@@ -7,11 +7,11 @@ Gem::Specification.new do |spec|
7
7
  spec.name = 'ruby-mext'
8
8
  spec.version = Mext::VERSION
9
9
  spec.authors = ['Nicola Bernardini']
10
- spec.email = ['nicb@sme-ccppd.org']
10
+ spec.email = ['nicola.bernardini.rome@gmail.com']
11
11
 
12
12
  spec.summary = %q{ruby-mext: musical extensions for the Ruby language.}
13
13
  spec.description = %q{ruby-mext: musical extensions for the Ruby language.}
14
- spec.homepage = 'https://github.com/nicb/ruby-mext'
14
+ spec.homepage = 'https://git.smerm.org/nicb/ruby-mext'
15
15
 
16
16
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
17
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -36,6 +36,6 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency 'rspec'
37
37
  spec.add_development_dependency 'rdoc'
38
38
  spec.add_development_dependency 'byebug'
39
- spec.add_development_dependency 'gruff'
39
+ spec.add_development_dependency 'matplotlib'
40
40
 
41
41
  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.21.4
4
+ version: 0.21.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicola Bernardini
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-02 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wavefile
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: gruff
112
+ name: matplotlib
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -124,7 +124,7 @@ dependencies:
124
124
  version: '0'
125
125
  description: 'ruby-mext: musical extensions for the Ruby language.'
126
126
  email:
127
- - nicb@sme-ccppd.org
127
+ - nicola.bernardini.rome@gmail.com
128
128
  executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
@@ -157,9 +157,12 @@ files:
157
157
  - lib/mext/math/function.rb
158
158
  - lib/mext/math/line.rb
159
159
  - lib/mext/math/log.rb
160
+ - lib/mext/math/polynomial.rb
160
161
  - lib/mext/math/stepwise.rb
161
162
  - lib/mext/music.rb
162
163
  - lib/mext/music/meter.rb
164
+ - lib/mext/music/note_name.rb
165
+ - lib/mext/music/note_names.rb
163
166
  - lib/mext/music/pitch_class.rb
164
167
  - lib/mext/numeric.rb
165
168
  - lib/mext/numeric/addsemi.rb
@@ -185,6 +188,9 @@ files:
185
188
  - lib/mext/sound.rb
186
189
  - lib/mext/sound/info.rb
187
190
  - lib/mext/utilities.rb
191
+ - lib/octave.rb
192
+ - lib/octave/io.rb
193
+ - lib/octave/polyfit.rb
188
194
  - lib/ruby-mext.rb
189
195
  - lib/ruby.rb
190
196
  - lib/ruby/extensions.rb
@@ -197,7 +203,7 @@ files:
197
203
  - lib/tasks/gruff/stepwise.rake
198
204
  - lib/version.rb
199
205
  - ruby-mext.gemspec
200
- homepage: https://github.com/nicb/ruby-mext
206
+ homepage: https://git.smerm.org/nicb/ruby-mext
201
207
  licenses: []
202
208
  metadata:
203
209
  allowed_push_host: https://rubygems.org
@@ -216,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
222
  - !ruby/object:Gem::Version
217
223
  version: '0'
218
224
  requirements: []
219
- rubygems_version: 3.1.2
225
+ rubygems_version: 3.2.2
220
226
  signing_key:
221
227
  specification_version: 4
222
228
  summary: 'ruby-mext: musical extensions for the Ruby language.'