diff --git a/.rvmrc b/.rvmrc deleted file mode 100644 index c7d01ac..0000000 --- a/.rvmrc +++ /dev/null @@ -1 +0,0 @@ -rvm use --create ruby-1.9.3-p0@celt_ruby_gem diff --git a/README.rdoc b/README.rdoc index 5b7ad24..eef6c23 100644 --- a/README.rdoc +++ b/README.rdoc @@ -28,7 +28,7 @@ What this library can do: == USAGE: # Create new encoder with a sample rate of 48 kHz, a frame size of 480 bytes and 1 channel - encoder = Celt::Encoder.new 48000, 480, 1 + encoder = Celt::Encoder.new 48000, 480, 1, [60000 / 800, 127].min # Set the prediction request to 0 encoder.prediction_request = 0 # Set the VBR rate to 60,000 @@ -38,8 +38,7 @@ What this library can do: encoder.bitstream_version # Encode some raw audio - compressed_size = [@encoder.vbr_rate / 800, 127].min - encoded = encoder.encode(raw_audio, compressed_size) + encoded = encoder.encode raw_audio # Safely destroy encoder encoder.destroy diff --git a/lib/celt-ruby.rb b/lib/celt-ruby.rb index c93039a..14bb86c 100644 --- a/lib/celt-ruby.rb +++ b/lib/celt-ruby.rb @@ -1,30 +1,31 @@ require 'ffi' require 'celt-ruby/version' require 'celt-ruby/encoder' +require 'celt-ruby/decoder' module Celt extend FFI::Library - ffi_lib 'celt0' + ffi_lib 'libcelt0.so' module Constants - CELT_OK = 0 - CELT_BAD_ARG = -1 - CELT_INVALID_MODE = -2 - CELT_INTERNAL_ERROR = -3 - CELT_CORRUPTED_DATA = -4 - CELT_UNIMPLEMENTED = -5 - CELT_INVALID_STATE = -6 - CELT_ALLOC_FAIL = -7 - CELT_GET_MODE_REQUEST = 1 - CELT_SET_COMPLEXITY_REQUEST = 2 - CELT_SET_PREDICTION_REQUEST = 4 - CELT_SET_VBR_RATE_REQUEST = 6 - CELT_RESET_STATE_REQUEST = 8 - CELT_GET_FRAME_SIZE = 1000 - CELT_GET_LOOKAHEAD = 1001 - CELT_GET_SAMPLE_RATE = 1003 - CELT_GET_BITSTREAM_VERSION = 2000 + CELT_OK = 0 + CELT_BAD_ARG = -1 + CELT_INVALID_MODE = -2 + CELT_INTERNAL_ERROR = -3 + CELT_CORRUPTED_DATA = -4 + CELT_UNIMPLEMENTED = -5 + CELT_INVALID_STATE = -6 + CELT_ALLOC_FAIL = -7 + CELT_GET_MODE_REQUEST = 1 + CELT_SET_COMPLEXITY_REQUEST = 2 + CELT_SET_PREDICTION_REQUEST = 4 + CELT_SET_VBR_RATE_REQUEST = 6 + CELT_RESET_STATE_REQUEST = 8 + CELT_GET_FRAME_SIZE = 1000 + CELT_GET_LOOKAHEAD = 1001 + CELT_GET_SAMPLE_RATE = 1003 + CELT_GET_BITSTREAM_VERSION = 2000 end attach_function :celt_mode_create, [:int32, :int, :pointer], :pointer diff --git a/lib/celt-ruby/decoder.rb b/lib/celt-ruby/decoder.rb new file mode 100644 index 0000000..6968afa --- /dev/null +++ b/lib/celt-ruby/decoder.rb @@ -0,0 +1,59 @@ +################################################################################# +# The MIT License (MIT) # +# # +# Copyright (c) 2014, Reinhard Bramel 'dafoxia' # +# # +# Permission is hereby granted, free of charge, to any person obtaining a copy # +# of this software and associated documentation files (the "Software"), to deal # +# in the Software without restriction, including without limitation the rights # +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # +# copies of the Software, and to permit persons to whom the Software is # +# furnished to do so, subject to the following conditions: # +# # +# The above copyright notice and this permission notice shall be included in # +# all copies or substantial portions of the Software. # +# # +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # +# THE SOFTWARE. # +################################################################################# + +module Celt + class Decoder + attr_reader :sample_rate, :frame_size, :channels, + :prediction_request, :vbr_rate + + def initialize(sample_rate, frame_size, channels) + @sample_rate = sample_rate + @frame_size = frame_size + @channels = channels + @mode = Celt.celt_mode_create sample_rate, frame_size, nil + @decoder = Celt.celt_decoder_create @mode, channels, nil + puts "decoder initialized" + end + + def destroy + Celt.celt_decoder_destroy @decoder + Celt.celt_mode_destroy @mode + end + + def bitstream_version + bv = FFI::MemoryPointer.new :int32 + Celt.celt_mode_info @mode, Celt::Constants::CELT_GET_BITSTREAM_VERSION, bv + bv.read_int32 + end + + def decode( data ) + len = data.size + packet = FFI::MemoryPointer.new :char, len + 1 + packet.put_string 0,data + decoded = FFI::MemoryPointer.new :short, @frame_size + error = Celt.celt_decode @decoder, packet, len, decoded + return decoded.read_string_length frame_size * 2 if error == 0 + end + end +end diff --git a/lib/celt-ruby/encoder.rb b/lib/celt-ruby/encoder.rb index 30ed11d..195ba83 100644 --- a/lib/celt-ruby/encoder.rb +++ b/lib/celt-ruby/encoder.rb @@ -3,16 +3,20 @@ class Encoder attr_reader :sample_rate, :frame_size, :channels, :prediction_request, :vbr_rate - def initialize(sample_rate, frame_size, channels) + def initialize(sample_rate, frame_size, channels, size) @sample_rate = sample_rate @frame_size = frame_size @channels = channels - + @size = size + @out = FFI::MemoryPointer.new :char, @size + 1 + @buf = FFI::MemoryPointer.new :char, @frame_size * 2 + 1 @mode = Celt.celt_mode_create sample_rate, frame_size, nil @encoder = Celt.celt_encoder_create @mode, channels, nil end def destroy + @out.free + @buf.free Celt.celt_encoder_destroy @encoder Celt.celt_mode_destroy @mode end @@ -37,12 +41,10 @@ def vbr_rate=(value) Celt.celt_encoder_ctl @encoder, Celt::Constants::CELT_SET_VBR_RATE_REQUEST, :pointer, v_ptr end - def encode(data, size) - out = FFI::MemoryPointer.new :char, data.size + 1 - buf = FFI::MemoryPointer.new :char, data.size + 1 - buf.put_string 0, data - len = Celt.celt_encode @encoder, buf, nil, out, size - out.read_string_length len + def encode(data) + @buf.put_string 0, data + len = Celt.celt_encode @encoder, @buf, nil, @out, @size + @out.read_string len end end end