Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

5 changes: 2 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
37 changes: 19 additions & 18 deletions lib/celt-ruby.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
59 changes: 59 additions & 0 deletions lib/celt-ruby/decoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#################################################################################
# The MIT License (MIT) #
# #
# Copyright (c) 2014, Reinhard Bramel 'dafoxia' <[email protected]> #
# #
# 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
18 changes: 10 additions & 8 deletions lib/celt-ruby/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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