raiff 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/raiff.rb CHANGED
@@ -40,14 +40,6 @@ class Raiff
40
40
  @sample_size = common_chunk.sample_size
41
41
  @sample_rate = common_chunk.sample_rate
42
42
  end
43
-
44
- @samples = [ ]
45
-
46
- if (@chunks['SSND'])
47
- @chunks['SSND'].each do |sound_data|
48
- @samples += sound_data.samples
49
- end
50
- end
51
43
  end
52
44
  end
53
45
 
@@ -55,9 +47,27 @@ class Raiff
55
47
  "<Raiff\##{object_id} channels=#{channels} sample_frames=#{sample_frames} sample_size=#{sample_size} sample_rate=#{sample_rate}>"
56
48
  end
57
49
 
58
- def each_sample(&block)
50
+ def each_sample
59
51
  @chunks['SSND'].each do |sound_data|
60
- sound_data.samples.each(&:block)
52
+ @file.seek(sound_data.start_offset)
53
+
54
+ sound_data.samples_count.times do
55
+ sample = sound_data.read_sample
56
+
57
+ yield(sample)
58
+ end
59
+ end
60
+ end
61
+
62
+ def each_sample_block(size)
63
+ @chunks['SSND'].each do |sound_data|
64
+ @file.seek(sound_data.start_offset)
65
+
66
+ block_count = (@sample_frames - 1) / size + 1
67
+
68
+ block_count.times do
69
+ yield(sound_data.read_samples(size))
70
+ end
61
71
  end
62
72
  end
63
73
  end
data/lib/raiff/chunk.rb CHANGED
@@ -15,6 +15,7 @@ class Raiff::Chunk
15
15
  # == Instance Methods =====================================================
16
16
 
17
17
  def initialize(file)
18
+ @file = file
18
19
  @id = file.read(4)
19
20
  @size = file.unpack('N')[0]
20
21
  end
@@ -1,8 +1,7 @@
1
1
  class Raiff::Chunk::SoundData < Raiff::Chunk
2
2
  # == Properties ===========================================================
3
3
 
4
- attr_reader :offset, :block_size
5
- attr_reader :samples
4
+ attr_reader :offset, :block_size, :start_offset
6
5
 
7
6
  # == Class Methods ========================================================
8
7
 
@@ -11,33 +10,67 @@ class Raiff::Chunk::SoundData < Raiff::Chunk
11
10
  def initialize(file, common)
12
11
  super(file)
13
12
 
14
- end_offset = file.offset + @size
13
+ @common = common
14
+
15
+ @start_offset = file.offset
16
+ @end_offset = file.offset + @size
15
17
 
16
18
  @offset, @block_size = file.unpack('NN')
17
19
 
18
20
  file.read(@offset)
19
21
 
20
- sample_count = (end_offset - file.offset) / common.bytes_per_sample
21
- bit_offset = common.bytes_per_sample * 8 - common.sample_size
22
- value_offset = (1 << common.sample_size) - 1
22
+ @sample_count = (@end_offset - file.offset) / @common.bytes_per_sample
23
+ @bit_offset = @common.bytes_per_sample * 8 - @common.sample_size
23
24
 
24
- @samples = [ ]
25
+ file.advance(@sample_count * @common.bytes_per_sample)
26
+ end
27
+
28
+ def read_sample
29
+ self.decoder.call
30
+ end
31
+
32
+ def read_samples(count)
33
+ if (count + @file.offset > @end_offset)
34
+ count = (@end_offset - @file.offset) / @common.bytes_per_sample
35
+ end
25
36
 
26
- case (common.bytes_per_sample)
27
- when 3
28
- format = 'B24' * common.channels
29
-
30
- sample_count.times do
31
- if (sample = file.unpack(format))
32
- @samples << sample.collect { |s| (s.to_i(2) >> bit_offset) - value_offset }
33
- end
34
- end
35
- else
36
- format = %w[ C n x N ][common.bytes_per_sample] * common.channels
37
-
38
- sample_count.times do
39
- @samples << file.unpack(format).collect { |i| (i >> bit_offset) - value_offset }
40
- end
37
+ samples = [ ]
38
+ _decoder = self.decoder
39
+
40
+ count.times do
41
+ samples << _decoder.call
41
42
  end
43
+
44
+ samples
42
45
  end
46
+
47
+ def decoder
48
+ sample_size = @common.bytes_per_sample
49
+ channels = (1..@common.channels).to_a
50
+ negative = 1 << (@common.sample_size - 1)
51
+
52
+ @decoder =
53
+ case (@bit_offset)
54
+ when 0
55
+ lambda {
56
+ channels.collect {
57
+ v = @file.read(sample_size).bytes.inject(0) do |a, b|
58
+ a << 8 | b
59
+ end
60
+
61
+ (v & negative) ? ((v ^ negative) - negative) : v
62
+ }
63
+ }
64
+ else
65
+ lambda {
66
+ channels.collect {
67
+ v = @file.read(sample_size).bytes.inject(0) do |a, b|
68
+ a << 8 | b
69
+ end >> @bit_offset - @value_offset
70
+
71
+ (v & negative) ? ((v ^ negative) - negative) : v
72
+ }
73
+ }
74
+ end
75
+ end
43
76
  end
data/lib/raiff/file.rb CHANGED
@@ -55,11 +55,11 @@ class Raiff::File
55
55
  ((sign == '1') ? -1.0 : 1.0) * (fraction.to_f / ((1 << 63) - 1)) * (2 ** exponent)
56
56
  end
57
57
 
58
- def unpack(format)
58
+ def unpack(format, length = nil)
59
59
  return if (@offset >= @data.length)
60
60
 
61
61
  part = @data[@offset, @data.length].unpack(format)
62
- advance(part.pack(format).length)
62
+ advance(length || part.pack(format).length)
63
63
  part
64
64
 
65
65
  rescue TypeError
data/raiff.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{raiff}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott Tadman"]
12
- s.date = %q{2010-12-06}
12
+ s.date = %q{2011-02-14}
13
13
  s.description = %q{Reads in AIFF file data and audio contents}
14
14
  s.email = %q{github@tadman.ca}
15
15
  s.extra_rdoc_files = [
@@ -17,30 +17,28 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.files = [
19
19
  ".document",
20
- ".gitignore",
21
- "README.rdoc",
22
- "Rakefile",
23
- "VERSION",
24
- "lib/raiff.rb",
25
- "lib/raiff/chunk.rb",
26
- "lib/raiff/chunk/common.rb",
27
- "lib/raiff/chunk/data.rb",
28
- "lib/raiff/chunk/form.rb",
29
- "lib/raiff/chunk/sound_data.rb",
30
- "lib/raiff/file.rb",
31
- "raiff.gemspec",
32
- "sample/example-24bit.aiff",
33
- "test/helper.rb",
34
- "test/test_raiff.rb"
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/raiff.rb",
24
+ "lib/raiff/chunk.rb",
25
+ "lib/raiff/chunk/common.rb",
26
+ "lib/raiff/chunk/data.rb",
27
+ "lib/raiff/chunk/form.rb",
28
+ "lib/raiff/chunk/sound_data.rb",
29
+ "lib/raiff/file.rb",
30
+ "raiff.gemspec",
31
+ "sample/example-24bit.aiff",
32
+ "test/helper.rb",
33
+ "test/test_raiff.rb"
35
34
  ]
36
35
  s.homepage = %q{http://github.com/twg/raiff}
37
- s.rdoc_options = ["--charset=UTF-8"]
38
36
  s.require_paths = ["lib"]
39
37
  s.rubygems_version = %q{1.3.7}
40
38
  s.summary = %q{Ruby AIFF Reading Library}
41
39
  s.test_files = [
42
40
  "test/helper.rb",
43
- "test/test_raiff.rb"
41
+ "test/test_raiff.rb"
44
42
  ]
45
43
 
46
44
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott Tadman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-06 00:00:00 -05:00
17
+ date: 2011-02-14 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -28,7 +28,6 @@ extra_rdoc_files:
28
28
  - README.rdoc
29
29
  files:
30
30
  - .document
31
- - .gitignore
32
31
  - README.rdoc
33
32
  - Rakefile
34
33
  - VERSION
@@ -48,8 +47,8 @@ homepage: http://github.com/twg/raiff
48
47
  licenses: []
49
48
 
50
49
  post_install_message:
51
- rdoc_options:
52
- - --charset=UTF-8
50
+ rdoc_options: []
51
+
53
52
  require_paths:
54
53
  - lib
55
54
  required_ruby_version: !ruby/object:Gem::Requirement
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- *.gem
2
- .DS_Store