Class: Webmidi::Middleware::Recorder

Inherits:
Base
  • Object
show all
Defined in:
lib/webmidi/middleware/recorder.rb

Defined Under Namespace

Classes: Tape

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, **options) ⇒ Recorder

Returns a new instance of Recorder.



8
9
10
11
12
13
# File 'lib/webmidi/middleware/recorder.rb', line 8

def initialize(app = nil, **options)
  super(app || ->(msg) { msg }, **options)
  @tape = Tape.new
  @recording = false
  @mutex = Mutex.new
end

Instance Attribute Details

#tapeObject (readonly)

Returns the value of attribute tape.



6
7
8
# File 'lib/webmidi/middleware/recorder.rb', line 6

def tape
  @tape
end

Instance Method Details

#call(message) ⇒ Object



15
16
17
18
19
# File 'lib/webmidi/middleware/recorder.rb', line 15

def call(message)
  tape = @mutex.synchronize { @recording ? @tape : nil }
  tape&.add(message)
  @app.call(message)
end

#recordObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/webmidi/middleware/recorder.rb', line 21

def record
  @mutex.synchronize do
    @tape = Tape.new
    @recording = true
  end
  if block_given?
    begin
      yield
    ensure
      @mutex.synchronize { @recording = false }
    end
  end
  @mutex.synchronize { @tape }
end

#recording?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/webmidi/middleware/recorder.rb', line 43

def recording?
  @mutex.synchronize { @recording }
end

#stopObject



36
37
38
39
40
41
# File 'lib/webmidi/middleware/recorder.rb', line 36

def stop
  @mutex.synchronize do
    @recording = false
    @tape
  end
end