Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
encoder is now using a single allocated buffer
  • Loading branch information
loscoala committed Jan 17, 2015
commit 583f66331f84635157dac12ce4632d6eb8360335
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.1.0
ruby-2.1.1
3 changes: 2 additions & 1 deletion lib/opus-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Constants
OPUS_SIGNAL_VOICE = 3001
OPUS_SIGNAL_MUSIC = 3002
OPUS_SET_BITRATE_REQUEST = 4002
OPUS_SET_SIGNAL_REQUEST = 4024
OPUS_SET_VBR_REQUEST = 4006
OPUS_RESET_STATE = 4028
end
Expand All @@ -42,5 +43,5 @@ module Constants
attach_function :opus_decode_float, [:pointer, :pointer, :int32, :pointer, :int, :int], :int
attach_function :opus_decoder_ctl, [:pointer, :int, :varargs], :int
attach_function :opus_decoder_destroy, [:pointer], :void
attach_function :opus_packet_get_samples_per_frame, [:pointer, :int32], :int
# attach_function :opus_packet_get_samples_per_frame, [:pointer, :int32], :int
end
98 changes: 50 additions & 48 deletions lib/opus-ruby/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,66 @@


module Opus
class Decoder
attr_reader :sample_rate, :frame_size, :channels
class Decoder
attr_reader :sample_rate, :frame_size, :channels, :lasterror

def initialize( sample_rate, frame_size, channels)
@sample_rate = sample_rate
@frame_size = frame_size
@channels = channels
@lasterror = 0
def initialize(sample_rate, frame_size, channels)
@sample_rate = sample_rate
@frame_size = frame_size
@channels = channels
@lasterror = 0

@decoder = Opus.opus_decoder_create sample_rate, channels, nil
end
@decoder = Opus.opus_decoder_create sample_rate, channels, nil
end

def destroy
Opus.opus_decoder_destroy @decoder
end
def destroy
Opus.opus_decoder_destroy @decoder
end

def reset
Opus.opus_decoder_ctl @decoder, Opus::Constants::OPUS_RESET_STATE, :pointer, nil
end
def reset
Opus.opus_decoder_ctl @decoder, Opus::Constants::OPUS_RESET_STATE, :pointer, nil
end

def decode( data )
len = data.size
def decode(data)
len = data.size

packet = FFI::MemoryPointer.new :char, len + 1
packet.put_string 0, data
packet = FFI::MemoryPointer.new :char, len + 1
packet.put_string 0, data

max_size = @frame_size * @channels * 2 # Was getting buffer_too_small errors without the 2
decoded = FFI::MemoryPointer.new :short, max_size + 1
max_size = @frame_size * @channels * 2 # Was getting buffer_too_small errors without the 2
decoded = FFI::MemoryPointer.new :short, max_size + 1

frame_size = Opus.opus_decode @decoder, packet, len, decoded, max_size, 0
if frame_size < 0 then
@lasterror = frame_size
frame_size = 0
end
return decoded.read_string_length frame_size * 2
end
frame_size = Opus.opus_decode @decoder, packet, len, decoded, max_size, 0
if frame_size < 0
@lasterror = frame_size
frame_size = 0
end
decoded.read_string frame_size * 2
end

def decode_missed
Opus.opus_decode @decoder, nil, 0, nil, 0, 0
end
def decode_missed
Opus.opus_decode @decoder, nil, 0, nil, 0, 0
end

def get_lasterror_code
return @lasterror
end
# <b>DEPRECATED:</b> Please use <tt>lasterror</tt> instead.
def get_lasterror_code
warn "[DEPRECATION] `get_lasterror_code` is deprecated. Please use `lasterror` instead."
@lasterror
end

def get_lasterror_string
errorstring = case @lasterror
when 0 then "OK"
when -1 then "BAD ARG"
when -2 then "BUFFER TOO SMALL"
when -3 then "INTERNAL ERROR"
when -4 then "INVALID PACKET"
when -5 then "UNIMPLEMENTED"
when -6 then "INVALID STATE"
when -7 then "ALLOC FAIL"
else "UNKNOWN ERROR / ERROR NOT DEFINED (should never happen)"
return errorstring
end
end
def get_lasterror_string
case @lasterror
when 0 then "OK"
when -1 then "BAD ARG"
when -2 then "BUFFER TOO SMALL"
when -3 then "INTERNAL ERROR"
when -4 then "INVALID PACKET"
when -5 then "UNIMPLEMENTED"
when -6 then "INVALID STATE"
when -7 then "ALLOC FAIL"
else "UNKNOWN ERROR / ERROR NOT DEFINED (should never happen)"
end
end
end
end

25 changes: 16 additions & 9 deletions lib/opus-ruby/encoder.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
module Opus
class Encoder
attr_reader :sample_rate, :frame_size, :channels,
:vbr_rate, :bitrate
:vbr_rate, :bitrate, :signal

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
@buf = FFI::MemoryPointer.new :char, @size + 1
@out = FFI::MemoryPointer.new :char, @size + 1
@encoder = Opus.opus_encoder_create sample_rate, channels, Constants::OPUS_APPLICATION_AUDIO, nil
end

def destroy
@buf.free
@out.free
Opus.opus_encoder_destroy @encoder
end

Expand All @@ -29,12 +33,15 @@ def bitrate=(value)
Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_BITRATE_REQUEST, :int32, value
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 = Opus.opus_encode @encoder, buf, @frame_size, out, size
out.read_string_length len
def signal=(value)
@signal = value
Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_SIGNAL_REQUEST, :int32, value
end

def encode(data)
@buf.put_string 0, data
len = Opus.opus_encode @encoder, @buf, @frame_size, @out, @size
@out.read_string len
end
end
end
2 changes: 1 addition & 1 deletion lib/opus-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Opus
VERSION = "0.0.1"
VERSION = "1.0.1"
end