Class: WaveData

Inherits:
Object show all
Defined in:
lib/wave_data.rb

Overview

basically an array of data of a fully rendered down sound.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dps = Array.new, file = nil) ⇒ WaveData

Returns a new instance of WaveData.



8
9
10
11
12
# File 'lib/wave_data.rb', line 8

def initialize(dps=Array.new,file=nil)
    @dps = dps.dup
    @blanks = [0,0]
    @out_file=file
end

Instance Attribute Details

#blanksObject

Array containing blanks frames at the start as Integer at [0], end at [1]



6
7
8
# File 'lib/wave_data.rb', line 6

def blanks
  @blanks
end

#dpsObject

the data points in an Array of Float. range -1 to 1 unless you like distortion.



4
5
6
# File 'lib/wave_data.rb', line 4

def dps
  @dps
end

#out_fileObject

Returns the value of attribute out_file.



7
8
9
# File 'lib/wave_data.rb', line 7

def out_file
  @out_file
end

Instance Method Details

#+(data) ⇒ Object

appends Array data to dps



74
75
76
77
# File 'lib/wave_data.rb', line 74

def +(data)
  self.dps+= data.dps
  self
end

#add(array) ⇒ Object



68
69
70
71
# File 'lib/wave_data.rb', line 68

def add(array)
  self.dps+= array
  self
end

#countObject

return num of #dps



64
65
66
# File 'lib/wave_data.rb', line 64

def count
  dps.count
end

#dup(n = 1) ⇒ Object

duplicate the current data n times.



43
44
45
46
# File 'lib/wave_data.rb', line 43

def dup(n=1)
  n.times{dps.concat(dps)}
  self
end

#fit_to(len, fade_frames = 250) ⇒ Object

fit to the size of #dps to len by removing or adding tailing points

fade_frames

stop the annoying popping at end of sound by fading out this many frames



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wave_data.rb', line 27

def fit_to(len, fade_frames=250)
  meant_to_be = len
  self.dps.pop(dps.count- meant_to_be) if meant_to_be < dps.count
  while meant_to_be > dps.count # too short
    self.dps.push 0
  end
  # stop the annoying popping
  if dps.count > fade_frames
    fade_frames.times do |i|
      dps[dps.count-1-i] *= i.to_f / fade_frames
    end
  end
  self
end

#get(start, final = nil) ⇒ Object

return the datapoints within a range of indexs.



20
21
22
23
# File 'lib/wave_data.rb', line 20

def get(start, final=nil)
  final ||= dps.count
  WaveData.new dps.slice(start..final)
end

#included_blanksObject

TODO



15
16
# File 'lib/wave_data.rb', line 15

def included_blanks
end

#interpolate(progress) ⇒ Object

return the value of a wave at the specifed progress 0 to 1



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wave_data.rb', line 49

def interpolate(progress)
  progress *= dps.count # range from 0 to wavedata length.
  val = dps[progress.to_i] # truncate progress.
  #puts progress.to_i
  # now add the interpolation to the next point
  if (progress < dps.count-1) # avoid error on last point
    dif_x = progress.to_i+1 - progress # how far to next datapoint? 0 to 1
    dif_y = dps[progress.to_i+1] - dps[progress.to_i]
    interpolation = dif_x * dif_y
    val += interpolation
  end
  return val
end

#mix(wave) ⇒ Object

combine my dps with WaveData wave. adding length if necessary.



92
93
94
95
96
97
98
99
100
101
# File 'lib/wave_data.rb', line 92

def mix(wave)
  other = wave.dps
  longest = dps.count > other.count ? dps : other
  longest.each_with_index do |a,i|
    my_v = (dps[i].nil? ? 0 : dps[i])
    oth_v = (other[i].nil? ? 0 : other[i])
    dps[i] = my_v + oth_v 
  end
  self
end

#silence(frames, val = 0) ⇒ Object

append silence to #dps.

frames

duration of the silence in seconds

default: one beat.

val

constant middle value in buffer.

default: 0 Range: -1 to 1



84
85
86
87
88
# File 'lib/wave_data.rb', line 84

def silence(frames, val = 0)
  d=WaveData.new(Array.new(frames, val))
  self+d if frames > 0
  self
end