Class: NamedSample

Inherits:
Sample show all
Includes:
Comparable
Defined in:
lib/NamedSample.rb

Overview

A Sample with a note value (such as A1, C#4, F5, etc) in the filename.

Instance Attribute Summary

Attributes inherited from Sample

#path, #sample

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sample

#note, suffix_glob, suffix_regexp, suffixes, #to_s

Constructor Details

#initialize(sample, path = false) ⇒ NamedSample

Instantiate the NamedSample.



23
24
25
26
# File 'lib/NamedSample.rb', line 23

def initialize( sample, path=false )
  super( sample, path )
  @value = NamedSample.value( sample )
end

Class Method Details

.regexpObject

returns the RegExp used to test for a NamedSample.



37
38
39
# File 'lib/NamedSample.rb', line 37

def NamedSample.regexp
  /([A-Ga-g])(#)?(\d)/
end

.sample?(name) ⇒ Boolean

returns true if the String name is a NamedSample.

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/NamedSample.rb', line 30

def NamedSample.sample?( name )
  name =~ /^.*(#{NamedSample.regexp})/ &&
   name =~ /.*\.#{Sample.suffix_regexp}$/i
end

.value(string) ⇒ Object

returns the numeric value of a NamedSample



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/NamedSample.rb', line 43

def NamedSample.value( string )
  result = false

  if( string =~ regexp )
    notes = { 'C'=>0, 'D'=>2, 'E'=>4, 'F'=>5, 'G'=>7, 'A'=>9, 'B'=>11,
              'c'=>0, 'd'=>2, 'e'=>4, 'f'=>5, 'g'=>7, 'a'=>9, 'b'=>11}
    note = $1
    sharp = 0 
    if $2 == "#" then 
      sharp = 1
    end
    octave = $3.to_i
    result = ((octave * 12) + 12) + notes[note] + sharp
  end

  result 
end

Instance Method Details

#<=>(anOther) ⇒ Object

required by the Comparable mixin; allows sorting



69
70
71
# File 'lib/NamedSample.rb', line 69

def <=>(anOther)  
    value <=> anOther.value  
end

#valueObject

returns the numeric value of this instance of NamedSample



63
64
65
# File 'lib/NamedSample.rb', line 63

def value
  @value
end