Class: Ruck::UGen::Generators::WavOut

Inherits:
Object
  • Object
show all
Includes:
MultiChannelTarget, Source, UGenBase
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 UGenBase

#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 UGenBase

#to_s

Constructor Details

#initialize(attrs = {}) ⇒ WavOut

Returns a new instance of WavOut.



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

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.



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

def filename
  @filename
end

Instance Method Details

#attr_namesObject



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

def attr_names
  [:filename]
end

#next(now) ⇒ Object



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

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

#saveObject



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

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