Class: Ruck::Generators::WavOut

Inherits:
Object
  • Object
show all
Includes:
MultiChannelTarget, Source, UGen
Defined in:
lib/ruck/ugen/wav.rb

Overview

saves all incoming samples in memory to export to disk later outputs 0.0 samples

Instance Attribute Summary collapse

Attributes included from UGen

#name

Instance Method Summary collapse

Methods included from MultiChannelTarget

#add_source, #in, #in_channels, #remove_source

Methods included from Source

#<<, #>>, #last, #out, #out_channels

Methods included from UGen

#to_s

Constructor Details

#initialize(attrs = {}) ⇒ WavOut

Returns a new instance of WavOut.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruck/ugen/wav.rb', line 16

def initialize(attrs = {})
  require_attrs attrs, [:filename]
  @filename = attrs.delete(:filename)
  @num_channels = attrs.delete(:num_channels) || 1
  @bits_per_sample = attrs.delete(:bits_per_sample) || 16
  parse_attrs attrs

  @in_channels = (1..@num_channels).map { InChannel.new }

  @sample_rate = SAMPLE_RATE
  @samples = (1..@num_channels).map { [] }
  @ins = []
  @last = 0.0

  # TODO: this is necessary, but if UGen graph were explicitly
  # destructed, that would be nice.
  at_exit { save }
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



14
15
16
# File 'lib/ruck/ugen/wav.rb', line 14

def filename
  @filename
end

Instance Method Details

#attr_namesObject



47
48
49
# File 'lib/ruck/ugen/wav.rb', line 47

def attr_names
  [:filename]
end

#next(now) ⇒ Object



35
36
37
38
39
40
# File 'lib/ruck/ugen/wav.rb', line 35

def next(now)
  return @last if @now == now
  @now = now
  @samples << @in_channels.map { |chan| chan.next now }
  @last
end

#saveObject



42
43
44
45
# File 'lib/ruck/ugen/wav.rb', line 42

def save
  LOG.info "Saving WAV to #{@filename}..."
  File.open(@filename, "wb") { |f| f.write encode }
end