Class: Radio::Signal::File::WAV

Inherits:
Object
  • Object
show all
Defined in:
lib/radio/signals/wav.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WAV

Returns a new instance of WAV.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/radio/signals/wav.rb', line 24

def initialize options
  @file = ::File.new options[:id]
  #TODO validate header instead?
  @file.read 12 # discard header
  @rate = options[:rate].to_i
  if input = options[:input]
    @channel_i = input[0]
    @channel_q = input[1]
  end
  @data = [next_data]
  @time = Time.now
end

Instance Attribute Details

#rateObject (readonly)

Returns the value of attribute rate.



22
23
24
# File 'lib/radio/signals/wav.rb', line 22

def rate
  @rate
end

Instance Method Details

#in(samples) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/radio/signals/wav.rb', line 37

def in samples
  sample_size = @channels * (@bit_sample/8)
  @time += 1.0/(rate.to_f/samples)
  sleep [0,@time-Time.now].max
  while @data.reduce(0){|a,b|a+b.size} < samples * sample_size
    @data.push next_data
  end
  if input_channels > 1
    out = NArray.scomplex samples
  else
    out = NArray.sfloat samples
  end
  i = 0
  while i < samples
    if @data.first.size/sample_size > samples-i
      out[i..-1] = convert @data.first[0...(samples-i)*sample_size]
      @data[0] = @data.first[(samples-i)*sample_size..-1]
      i = samples
    else
      converted_data = convert @data.shift
      out[i...(i+converted_data.size)] = converted_data
      i += converted_data.size
    end
  end
  out
end

#input_channelsObject



64
65
66
67
# File 'lib/radio/signals/wav.rb', line 64

def input_channels
  return 2 if @channel_q and @channels > 1
  1
end

#output_channelsObject



69
70
71
# File 'lib/radio/signals/wav.rb', line 69

def output_channels
  0
end

#stopObject



73
74
75
# File 'lib/radio/signals/wav.rb', line 73

def stop
  @file.close
end