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
Next Next commit
add packet_get_samples_per_frame, some error query, prevent of read o…
…ut of bounds
  • Loading branch information
dafoxia committed May 24, 2014
commit 4a3c655eead40bcf406964b23b270620a14d8c41
3 changes: 2 additions & 1 deletion lib/opus-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Constants
OPUS_SET_BITRATE_REQUEST = 4002
OPUS_SET_VBR_REQUEST = 4006
OPUS_RESET_STATE = 4028
end
end

attach_function :opus_encoder_get_size, [:int], :int
attach_function :opus_encoder_create, [:int32, :int, :int, :pointer], :pointer
Expand All @@ -42,4 +42,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
end
38 changes: 31 additions & 7 deletions lib/opus-ruby/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ module Opus
class Decoder
attr_reader :sample_rate, :frame_size, :channels

def initialize( sample_rate, frame_size, channels )
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
Expand All @@ -52,17 +53,40 @@ def decode( data )
packet = FFI::MemoryPointer.new :char, len + 1
packet.put_string 0, data

max_size = @frame_size * @channels

# Always get correct frame_size, @sample_rate has to be the right value, else we get wrong size!
#@frame_size = Opus.opus_packet_get_samples_per_frame packet, @sample_rate
#max_size = @frame_size * @channels
# since calculation runs wrong, set it to max. framesize that should occur.
max_size = 2880
decoded = FFI::MemoryPointer.new :short, max_size + 1

frame_size = Opus.opus_decode @decoder, packet, len, decoded, max_size, 0

# The times 2 is very important and caused much grief prior to an IRC
# chat with the Opus devs. Just remember a short is 2 bytes... Seems so
# simple now...
if frame_size < 0 then
frame_size = 0
end
return decoded.read_string_length frame_size * 2
end

def get_lasterror_code
return @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


end
end

Expand Down