Class: Scissor::Chunk

Inherits:
Object
  • Object
show all
Defined in:
lib/scissor/chunk.rb

Defined Under Namespace

Classes: EmptyFragment, Error, OutOfDuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Chunk

Returns a new instance of Chunk.



12
13
14
15
16
17
18
19
20
21
# File 'lib/scissor/chunk.rb', line 12

def initialize(filename = nil)
  @fragments = []

  if filename
    @fragments << Fragment.new(
      filename,
      0,
      SoundFile.new(filename).length)
  end
end

Instance Attribute Details

#fragmentsObject (readonly)

Returns the value of attribute fragments.



10
11
12
# File 'lib/scissor/chunk.rb', line 10

def fragments
  @fragments
end

Instance Method Details

#+(other) ⇒ Object



74
75
76
77
78
# File 'lib/scissor/chunk.rb', line 74

def +(other)
  new_instance = Scissor()
  new_instance.add_fragments(@fragments + other.fragments)
  new_instance
end

#>>(filename) ⇒ Object



182
183
184
# File 'lib/scissor/chunk.rb', line 182

def >>(filename)
  to_file(filename, :overwrite => true)
end

#add_fragment(fragment) ⇒ Object



23
24
25
# File 'lib/scissor/chunk.rb', line 23

def add_fragment(fragment)
  @fragments << fragment
end

#add_fragments(fragments) ⇒ Object



27
28
29
30
31
# File 'lib/scissor/chunk.rb', line 27

def add_fragments(fragments)
  fragments.each do |fragment|
    add_fragment(fragment)
  end
end

#concat(other) ⇒ Object Also known as: <<



66
67
68
69
70
# File 'lib/scissor/chunk.rb', line 66

def concat(other)
  add_fragments(other.fragments)

  self
end

#durationObject



33
34
35
36
37
# File 'lib/scissor/chunk.rb', line 33

def duration
  @fragments.inject(0) do |memo, fragment|
    memo += fragment.duration
  end
end

#fill(filled_duration) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/scissor/chunk.rb', line 105

def fill(filled_duration)
  if @fragments.empty?
    raise EmptyFragment
  end

  remain = filled_duration
  new_instance = self.class.new

  while !remain.zero? && filled_duration > new_instance.duration
    if remain < duration
      added = slice(0, remain)
    else
      added = self
    end

    new_instance += added
    remain -= added.duration
  end


  new_instance
end

#loop(count) ⇒ Object Also known as: *



80
81
82
83
84
85
86
87
88
# File 'lib/scissor/chunk.rb', line 80

def loop(count)
  orig_fragments = @fragments.clone

  (count - 1).times do
    add_fragments(orig_fragments)
  end

  self
end

#pitch(pitch) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/scissor/chunk.rb', line 161

def pitch(pitch)
  new_instance = self.class.new

  @fragments.each do |fragment|
    new_instance.add_fragment(Fragment.new(
        fragment.filename,
        fragment.start,
        fragment.true_duration,
        fragment.reversed?,
        fragment.pitch * (pitch.to_f / 100)))
  end

  new_instance
end

#replace(start, length, replaced) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/scissor/chunk.rb', line 128

def replace(start, length, replaced)
  new_instance = self.class.new
  offset = start + length

  if offset > duration
    raise OutOfDuration
  end

  if start > 0
    new_instance += slice(0, start)
  end

  new_instance += replaced
  new_instance += slice(offset, duration - offset)

  new_instance
end

#reverseObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/scissor/chunk.rb', line 146

def reverse
  new_instance = self.class.new

  @fragments.reverse.each do |fragment|
    new_instance.add_fragment(Fragment.new(
        fragment.filename,
        fragment.start,
        fragment.true_duration,
        !fragment.reversed?,
        fragment.pitch))
  end

  new_instance
end

#slice(start, length) ⇒ Object Also known as: []



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/scissor/chunk.rb', line 39

def slice(start, length)
  if start + length > duration
    length = duration - start
  end

  new_instance = self.class.new
  remaining_start = start.to_f
  remaining_length = length.to_f

  @fragments.each do |fragment|
    new_fragment, remaining_start, remaining_length =
      fragment.create(remaining_start, remaining_length)

    if new_fragment
      new_instance.add_fragment(new_fragment)
    end

    if remaining_length == 0
      break
    end
  end

  new_instance
end

#split(count) ⇒ Object Also known as: /



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

def split(count)
  splitted_duration = duration / count.to_f
  results = []

  count.times do |i|
    results << slice(i * splitted_duration, splitted_duration)
  end

  results
end

#to_file(filename, options = {}) ⇒ Object Also known as: >



176
177
178
# File 'lib/scissor/chunk.rb', line 176

def to_file(filename, options = {})
  Scissor.mix([self], filename, options)
end