Class: Deftones::IO::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/deftones/io/recorder.rb

Constant Summary collapse

MIME_TYPES =
{
  wav: "audio/wav",
  mp3: "audio/mpeg",
  ogg: "audio/ogg"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, node: nil, mime_type: "audio/wav") ⇒ Recorder

Returns a new instance of Recorder.



14
15
16
17
18
19
20
21
22
# File 'lib/deftones/io/recorder.rb', line 14

def initialize(context:, node: nil, mime_type: "audio/wav")
  @context = context
  @node = node || context.output
  @captured_buffer = nil
  @mime_type = mime_type
  @state = :stopped
  @started_at = nil
  @recorded_duration = 0.0
end

Instance Attribute Details

#captured_bufferObject (readonly)

Returns the value of attribute captured_buffer.



6
7
8
# File 'lib/deftones/io/recorder.rb', line 6

def captured_buffer
  @captured_buffer
end

#mime_typeObject (readonly) Also known as: mimeType

Returns the value of attribute mime_type.



6
7
8
# File 'lib/deftones/io/recorder.rb', line 6

def mime_type
  @mime_type
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/deftones/io/recorder.rb', line 6

def state
  @state
end

Instance Method Details

#capture_buffer(duration:) ⇒ Object (private)



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/deftones/io/recorder.rb', line 64

def capture_buffer(duration:)
  if @context.is_a?(Deftones::OfflineContext)
    @context.render
  else
    seconds = [duration.to_f, 1.0 / @context.sample_rate].max
    frames = (seconds * @context.sample_rate).ceil
    start_frame = ((@started_at || 0.0) * @context.sample_rate).floor
    block = @node.send(:render_block, frames, start_frame, {}).fit_channels(@context.channels)
    Buffer.new(block.interleaved, channels: @context.channels, sample_rate: @context.sample_rate)
  end
end

#disposeObject



52
53
54
55
56
57
58
# File 'lib/deftones/io/recorder.rb', line 52

def dispose
  @captured_buffer = nil
  @state = :stopped
  @started_at = nil
  @recorded_duration = 0.0
  self
end

#elapsed_durationObject (private)



76
77
78
79
80
# File 'lib/deftones/io/recorder.rb', line 76

def elapsed_duration
  return 0.0 unless @started_at

  [@context.current_time - @started_at, 0.0].max
end

#record(duration: nil, format: :wav, path: nil) ⇒ Object



41
42
43
44
# File 'lib/deftones/io/recorder.rb', line 41

def record(duration: nil, format: :wav, path: nil)
  start
  stop(path: path, format: format, duration: duration)
end

#resolve_format(format, path) ⇒ Object (private)



82
83
84
85
86
87
# File 'lib/deftones/io/recorder.rb', line 82

def resolve_format(format, path)
  return format if format
  return File.extname(path).delete_prefix(".").downcase.to_sym if path

  MIME_TYPES.key(@mime_type) || :wav
end

#save(path, format: :wav) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
# File 'lib/deftones/io/recorder.rb', line 46

def save(path, format: :wav)
  raise ArgumentError, "Nothing recorded yet" unless @captured_buffer

  @captured_buffer.save(path, format: format)
end

#startObject



24
25
26
27
28
29
# File 'lib/deftones/io/recorder.rb', line 24

def start
  @captured_buffer = nil
  @started_at = @context.current_time
  @state = :started
  self
end

#stop(path: nil, format: nil, duration: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/deftones/io/recorder.rb', line 31

def stop(path: nil, format: nil, duration: nil)
  return @captured_buffer if @state == :stopped && @captured_buffer

  @recorded_duration = duration || elapsed_duration
  @captured_buffer = capture_buffer(duration: @recorded_duration)
  @state = :stopped
  save(path, format: resolve_format(format, path)) if path
  @captured_buffer
end