Class: LogicalRhythms

Inherits:
Object
  • Object
show all
Defined in:
lib/rhythmruby/LogicalRhythms.rb

Constant Summary collapse

@@eventMarker =

symbol which denotes event or hit

'#'
@@silenceMarker =

symbol that denotes silence

'-'

Class Method Summary collapse

Class Method Details

.AND(rhythmA, rhythmB) ⇒ String

logical AND of two rhythms, returns only events present in both rhythms

Parameters:

  • rhythmA, (String)

    one of the rhythms to and

  • rhythmB, (String)

    one of the rhythms to and

Returns:

  • (String)

    resultRhythm, result of logical and



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rhythmruby/LogicalRhythms.rb', line 63

def self.AND(rhythmA, rhythmB)
  resultRhythm = ""
  rhythmA.split("").zip(rhythmB.split("")).each do
    |symA, symB|
    if symA == @@eventMarker and symB == @@eventMarker
      resultRhythm += @@eventMarker
     else
       resultRhythm += @@silenceMarker       
    end
  end
  return resultRhythm
end

.Exclusion(masterRhythm, slaveRhythm) ⇒ String

logical exlusion of two rhythms, removes events from slave simultaneous with an event in the master rhythm

Parameters:

  • masterRhythm (String)

    rhythm to intersect with

  • slaveRhythm (String)

    rhythm to intersect

Returns:

  • (String)

    intersected slaveRhythm



10
11
12
13
14
15
16
17
18
19
# File 'lib/rhythmruby/LogicalRhythms.rb', line 10

def self.Exclusion(masterRhythm, slaveRhythm)
  masterRhythm.split("").zip(slaveRhythm.split("")).each_with_index do
    |symbols, index|
    master,slave = symbols
    if master == @@eventMarker && slave == @@eventMarker # if both have an event
      slaveRhythm[index] = @@silenceMarker # replace event with silence
    end
  end
  return slaveRhythm
end

.OR(rhythmA, rhythmB) ⇒ String

logical OR of two rhythms, thus event in result rhythm, when at least one of two original rhythms has an event (functionally the same as addition)

Parameters:

  • rhythmA, (String)

    one of the rhythms to or

  • rhythmB, (String)

    one of the rhythms to or

Returns:

  • (String)

    resultRhyhtm, result of logical or



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rhythmruby/LogicalRhythms.rb', line 45

def self.OR(rhythmA, rhythmB)
  resultRhythm = ""
      
  rhythmA.split("").zip(rhythmB.split("")).each do
    |symA, symB|
    if symA == @@eventMarker or symB == @@eventMarker
      resultRhythm += @@eventMarker
    else
      resultRhythm += @@silenceMarker
    end
  end
  return resultRhythm    
end

.XOR(rhythmA, rhythmB) ⇒ String

logical XOR of two rhythms, true except both true or both false

Parameters:

  • masterRhythm (String)

    rhythm to intersect with

  • slaveRhythm (String)

    rhythm to intersect

Returns:

  • (String)

    intersected slaveRhythm



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rhythmruby/LogicalRhythms.rb', line 25

def self.XOR(rhythmA, rhythmB)
  resultRhythm = ""
  rhythmA.split("").zip(rhythmB.split("")).each do
    |symA, symB|
    if symA == @@eventMarker && symB == @@eventMarker # if both have an event
      resultRhythm += @@silenceMarker # replace event with silence
    elsif symA == @@eventMarker or symB == @@eventMarker
      resultRhythm += @@eventMarker
    else
      resultRhythm += @@silenceMarker
    end
  end
  return resultRhythm
end