Class: Note

Inherits:
Object show all
Defined in:
lib/api/note.rb

Overview

A musical note. E.g. A sharp in octave 3.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(note = 0, octave = 4) ⇒ Note

Returns a new instance of Note.



8
9
10
11
12
13
14
15
# File 'lib/api/note.rb', line 8

def initialize(note = 0, octave = 4)
  if note.class == String
    @note = Composer.note_m(note)
  else
    @note = note
  end
  @octave = octave
end

Instance Attribute Details

#noteObject

note without octave. from 0 to 11 starting at A ending in G#



5
6
7
# File 'lib/api/note.rb', line 5

def note
  @note
end

#octaveObject

octave, starts at 0, an ‘A’ in ‘5’ is 440 hz



7
8
9
# File 'lib/api/note.rb', line 7

def octave
  @octave
end

Instance Method Details

#+(n = 1) ⇒ Object

returns a value up some semitones. (changes octave where necessary)

n

number of semitones, can be pos or neg Integer.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/api/note.rb', line 49

def +(n = 1)
  return self-(n*-1) if n < 0
  i=0
  out = deep_copy
  while i < n do
    out = Note.new(out.note+1, out.octave)
    out = Note.new(0, out.octave+1) if out.note==12
    i+=1
  end
  out
end

#-(n = 1) ⇒ Object

returns a value down n semitones. (changes octave where necessary)



61
62
63
64
65
66
67
68
69
70
# File 'lib/api/note.rb', line 61

def -(n = 1)
  i=0
  out = deep_copy
  while i < n do
    out = Note.new(out.note-1, out.octave)
    out = Note.new(11, out.octave-1) if out.note==-1
    i+=1
  end
  out
end

#freqObject

return the frequency (HZ) of a note.



73
74
75
76
77
78
79
# File 'lib/api/note.rb', line 73

def freq
  raise "no note or oct given" if (!note || !octave)
  a=2 ** (octave.to_f-1)
  b=1.059463 ** note.to_f
  out = 27.5*a*b
  return out
end

#inc(n) ⇒ Object

increment by n notes in the scale set in Composer#scale



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/api/note.rb', line 18

def inc(n)
  notes = scale_notes
  ind = note_index(self.note)
  semis = 0
  n.times do
    ind_old = ind
    ind += 1
    if ind >= notes.count
      diff = 12 - notes[ind_old]
      ind = 0
    else
      diff = notes[ind] - notes[ind_old]
    end
    semis += diff
  end
  (-1*n).times do
    ind_old = ind
    ind -= 1
    if ind < 0
      ind=notes.count-1 
      diff = notes[ind] - 12
    else
      diff = notes[ind] - notes[ind_old]
    end
    semis += diff
  end
  self+semis
end

#is_eql(other) ⇒ Object

return true if this note has the same values as another note.



82
83
84
# File 'lib/api/note.rb', line 82

def is_eql(other)
  vars_eql? other
end

#SndObject

return a new Snd with its frequency set to this note.



87
88
89
90
# File 'lib/api/note.rb', line 87

def Snd
  s=freq.Snd
  s
end