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
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ Or install it yourself as:
## Usage

# Create new encoder with a sample rate of 48 kHz, a frame size of 480 bytes and 1 channel
encoder = Opus::Encoder.new 48000, 480, 1
encoder = Opus::Encoder.new 48000, 480, 1, 960
# Set the bitrate to 32 kbit/s
encoder.bitrate = 32000
# Set the VBR rate to 0 (CBR)
encoder.vbr_rate = 0
encoder.signal = Opus::OPUS_SIGNAL_MUSIC

# Encode some raw audio
encoded = encoder.encode(raw_audio, 960)
encoded = encoder.encode raw_audio

# Safely destroy encoder
encoder.destroy
Expand Down
84 changes: 82 additions & 2 deletions lib/opus-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,94 @@ module Constants
OPUS_UNIMPLEMENTED = -5
OPUS_INVALID_STATE = -6
OPUS_ALLOC_FAIL = -7


OPUS_AUTO = -1000 # Auto/default setting @hideinitializer
OPUS_BITRATE_MAX = -1 # Maximum bitrate @hideinitializer


OPUS_BANDWIDTH_NARROWBAND = 1101 # < 4 kHz bandpass @hideinitializer
OPUS_BANDWIDTH_MEDIUMBAND = 1102 # < 6 kHz bandpass @hideinitializer
OPUS_BANDWIDTH_WIDEBAND = 1103 # < 8 kHz bandpass @hideinitializer
OPUS_BANDWIDTH_SUPERWIDEBAND = 1104 # <12 kHz bandpass @hideinitializer
OPUS_BANDWIDTH_FULLBAND = 1105 # <20 kHz bandpass @hideinitializer

OPUS_APPLICATION_VOIP = 2048
OPUS_APPLICATION_AUDIO = 2049
OPUS_APPLICATION_RESTRICTED_LOWDELAY = 2051

OPUS_SIGNAL_VOICE = 3001
OPUS_SIGNAL_MUSIC = 3002

# WE SHOULD NOT USE NUMBERS (could change) (Begin) ------------------------------------------------------------------------
OPUS_SET_APPLICATION_REQUEST = 4000
OPUS_GET_APPLICATION_REQUEST = 4001
OPUS_SET_BITRATE_REQUEST = 4002
OPUS_GET_BITRATE_REQUEST = 4003
OPUS_SET_MAX_BANDWIDTH_REQUEST = 4004
OPUS_GET_MAX_BANDWIDTH_REQUEST = 4005
OPUS_SET_VBR_REQUEST = 4006
OPUS_RESET_STATE = 4028
end
OPUS_GET_VBR_REQUEST = 4007
OPUS_SET_BANDWIDTH_REQUEST = 4008
OPUS_GET_BANDWIDTH_REQUEST = 4009
OPUS_SET_COMPLEXITY_REQUEST = 4010
OPUS_GET_COMPLEXITY_REQUEST = 4011
OPUS_SET_INBAND_FEC_REQUEST = 4012
OPUS_GET_INBAND_FEC_REQUEST = 4013
OPUS_SET_PACKET_LOSS_PERC_REQUEST = 4014
OPUS_GET_PACKET_LOSS_PERC_REQUEST = 4015
OPUS_SET_DTX_REQUEST = 4016
OPUS_GET_DTX_REQUEST = 4017


OPUS_SET_VBR_CONSTRAINT_REQUEST = 4020
OPUS_GET_VBR_CONSTRAINT_REQUEST = 4021
OPUS_SET_FORCE_CHANNELS_REQUEST = 4022
OPUS_GET_FORCE_CHANNELS_REQUEST = 4023
OPUS_SET_SIGNAL_REQUEST = 4024
OPUS_GET_SIGNAL_REQUEST = 4025

OPUS_GET_LOOKAHEAD_REQUEST = 4027
OPUS_RESET_STATE = 4028
OPUS_GET_SAMPLE_RATE_REQUEST = 4029

OPUS_GET_FINAL_RANGE_REQUEST = 4031

OPUS_GET_PITCH_REQUEST = 4033
OPUS_SET_GAIN_REQUEST = 4034
OPUS_GET_GAIN_REQUEST = 4045 # Should have been 4035
OPUS_SET_LSB_DEPTH_REQUEST = 4036
OPUS_GET_LSB_DEPTH_REQUEST = 4037

OPUS_GET_LAST_PACKET_DURATION_REQUEST = 4039
OPUS_SET_EXPERT_FRAME_DURATION_REQUEST= 4040
OPUS_GET_EXPERT_FRAME_DURATION_REQUEST= 4041
OPUS_SET_PREDICTION_DISABLED_REQUEST = 4042
OPUS_GET_PREDICTION_DISABLED_REQUEST = 4043

OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST = 4046
OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST = 4047

OPUS_GET_IN_DTX_REQUEST = 4049
OPUS_SET_DRED_DURATION_REQUEST = 4050
OPUS_GET_DRED_DURATION_REQUEST = 4051
OPUS_SET_DNN_BLOB_REQUEST = 4052

# WE SHOULD NOT USE NUMBERS (could change) (End) --------------------------------------------------------------------------


OPUS_FRAMESIZE_ARG = 5000 # Select frame size from the argument (default)
OPUS_FRAMESIZE_2_5_MS = 5001 # Use 2.5 ms frames
OPUS_FRAMESIZE_5_MS = 5002 # Use 5 ms frames
OPUS_FRAMESIZE_10_MS = 5003 # Use 10 ms frames
OPUS_FRAMESIZE_20_MS = 5004 # Use 20 ms frames
OPUS_FRAMESIZE_40_MS = 5005 # Use 40 ms frames
OPUS_FRAMESIZE_60_MS = 5006 # Use 60 ms frames
OPUS_FRAMESIZE_80_MS = 5007 # Use 80 ms frames
OPUS_FRAMESIZE_100_MS = 5008 # Use 100 ms frames
OPUS_FRAMESIZE_120_MS = 5009 # Use 120 ms frames

end

attach_function :opus_encoder_get_size, [:int], :int
attach_function :opus_encoder_create, [:int32, :int, :int, :pointer], :pointer
Expand All @@ -42,4 +121,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
82 changes: 54 additions & 28 deletions lib/opus-ruby/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# The MIT License (MIT) #
# #
# Copyright (c) 2014, Aaron Herting 'qwertos' <[email protected]> #
# Copyright (c) 2015, dafoxia <[email protected]@gmail.com> #
# Heavily based on code (c) GitHub User 'perrym5' and code found in the #
# libopus public documentation. #
# #
Expand All @@ -27,43 +28,68 @@


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
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
max_size = @frame_size * @channels * 2 # Was getting buffer_too_small errors without the 2
decoded = FFI::MemoryPointer.new :short, max_size + 1

decoded = FFI::MemoryPointer.new :short, max_size + 1
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

frame_size = Opus.opus_decode @decoder, packet, len, decoded, max_size, 0
def decode_missed
Opus.opus_decode @decoder, nil, 0, nil, 0, 0
end

# 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...
return decoded.read_string_length frame_size * 2
end
end
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
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

Loading