Permalink
Browse files

making encode and decode alac work with 1.8 and 1.9. also faster

  • Loading branch information...
1 parent eb91dcd commit 294b9250e3a1eafdb1d03763eb8d02277b006d47 @tenderlove committed Apr 22, 2008
Showing with 33 additions and 72 deletions.
  1. +0 −5 lib/raop.rb
  2. +0 −41 lib/raop/1_8/client.rb
  3. +0 −25 lib/raop/1_9/client.rb
  4. +32 −1 lib/raop/client.rb
  5. +1 −0 test/test_alac.rb
View
@@ -5,8 +5,3 @@ class RAOP
require 'raop/client'
require 'raop/rtsp'
-if RUBY_VERSION >= '1.9.0'
- require 'raop/1_9/client'
-else
- require 'raop/1_8/client'
-end
View
@@ -1,41 +0,0 @@
-module Net
- class RAOP
- class Client
- class << self
- @@decode_cache = "\0" * 16384
-
- def decode_alac(bits)
- len = bits.length
- new_bits = len == 16387 ? @@decode_cache.dup : "\0" * bits.length - 3
-
- i = 0
- while i < len - 3
- middle = bits[i + 3]
- new_bits[i + 1] |= ((bits[i + 2] & 0x01) << 7) | ((middle & 0xFE) >> 1)
- new_bits[i] |= ((middle & 0x01) << 7) | ((bits[i + 4] & 0xFE) >> 1)
- i += 2
- end
- new_bits
- end
-
- def encode_alac(bits)
- cb = HEADER + bits
- i, j = 3, 4
- len = cb.length
- while i < len
- l = cb[i]
- r = cb[j]
-
- cb[i - 1] |= r >> 7
- cb[i] = (r << 1) | (l >> 7)
- cb[j] = l << 1
-
- i += 2
- j += 2
- end
- cb
- end
- end
- end
- end
-end
View
@@ -1,25 +0,0 @@
-module Net
- class RAOP
- class Client
- class << self
- def encode_alac(bits)
- cb = [32, 0, 2] + bits.unpack("C*")
- i, j = 3, 4
- len = cb.length
- while i < len
- l = cb[i]
- r = cb[j]
-
- cb[i - 1] |= r >> 7
- cb[i] = (r << 1) | (l >> 7)
- cb[j] = l << 1
-
- i += 2
- j += 2
- end
- cb.pack("C#{len}")
- end
- end
- end
- end
-end
View
@@ -169,5 +169,36 @@ def aes_cipher
aes_crypt
end
- HEADER = [32, 0, 2].pack('C3')
+ HEADER = [2, 0].pack('C2')
+ class << self
+ def encode_alac(bits)
+ cb = HEADER + bits
+ cb = cb.unpack('v*')
+ i = 1
+ len = cb.length
+ while i < len
+ cb[i - 1] |= cb[i] >> 15
+ cb[i] <<= 1
+
+ i += 1
+ end
+ 32.chr + cb.pack('n*')
+ end
+
+ def decode_alac(bits)
+ cb = bits.unpack('C*')
+ cb.shift
+ cb = cb.pack('C*').unpack('n*').reverse
+
+ len = cb.length
+ i = 0
+ while i < len - 1
+ cb[i] >>= 1
+ cb[i] |= cb[i + 1] << 15
+ i += 1
+ end
+ cb.pop
+ cb.reverse.pack('v*')
+ end
+ end
end
View
@@ -19,6 +19,7 @@ def test_decode_alac
def test_encode_alac
output_frame = Net::RAOP::Client.encode_alac(@input_frame)
+ assert_equal(output_frame.length, @output_frame.length)
assert_equal(@output_frame, output_frame)
end
end

0 comments on commit 294b925

Please sign in to comment.