From fb3333ee490df3d6113661f5b78f507c381bbcbe Mon Sep 17 00:00:00 2001 From: dafoxia Date: Thu, 24 Jul 2014 22:19:19 +0200 Subject: [PATCH 1/6] add decoder --- .rvmrc | 1 - lib/celt-ruby/decoder.rb | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) delete mode 100644 .rvmrc create mode 100644 lib/celt-ruby/decoder.rb 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/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 From 5ff68197a542ee90c253413bf259ac53a52fc830 Mon Sep 17 00:00:00 2001 From: Dafoxia Date: Thu, 31 Jul 2014 01:36:25 +0200 Subject: [PATCH 2/6] Fix missing --- lib/celt-ruby.rb | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) 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 From f6d1de55768f8e78a3e4fcb6f6fdba72fac85c9d Mon Sep 17 00:00:00 2001 From: loscoala Date: Sat, 17 Jan 2015 20:19:07 +0100 Subject: [PATCH 3/6] encoder is now using a single allocated buffer --- lib/celt-ruby/encoder.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/celt-ruby/encoder.rb b/lib/celt-ruby/encoder.rb index 30ed11d..13ee554 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, @size + 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 From 976b41f41842ad73f4e8b5a83d7c67c416826c59 Mon Sep 17 00:00:00 2001 From: loscoala Date: Sun, 18 Jan 2015 12:38:32 +0100 Subject: [PATCH 4/6] fix: wrong buffer size --- lib/celt-ruby/encoder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/celt-ruby/encoder.rb b/lib/celt-ruby/encoder.rb index 13ee554..195ba83 100644 --- a/lib/celt-ruby/encoder.rb +++ b/lib/celt-ruby/encoder.rb @@ -9,7 +9,7 @@ def initialize(sample_rate, frame_size, channels, size) @channels = channels @size = size @out = FFI::MemoryPointer.new :char, @size + 1 - @buf = 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 From bebe6e7af20f87612e6b60d437410ffa1f3dc28f Mon Sep 17 00:00:00 2001 From: loscoala Date: Sun, 18 Jan 2015 12:51:13 +0100 Subject: [PATCH 5/6] readme fixed --- README.rdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rdoc b/README.rdoc index 5b7ad24..6029813 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, compressed_size # Set the prediction request to 0 encoder.prediction_request = 0 # Set the VBR rate to 60,000 @@ -39,7 +39,7 @@ What this library can do: # 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 From de19028d2630a50c995633420ff13a90f0e65ab7 Mon Sep 17 00:00:00 2001 From: loscoala Date: Sun, 18 Jan 2015 12:55:19 +0100 Subject: [PATCH 6/6] readme fixed --- README.rdoc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.rdoc b/README.rdoc index 6029813..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, compressed_size + 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,7 +38,6 @@ 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 # Safely destroy encoder