From 4a3c655eead40bcf406964b23b270620a14d8c41 Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sat, 24 May 2014 07:46:40 +0200 Subject: [PATCH 01/14] add packet_get_samples_per_frame, some error query, prevent of read out of bounds --- lib/opus-ruby.rb | 3 ++- lib/opus-ruby/decoder.rb | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/opus-ruby.rb b/lib/opus-ruby.rb index 3687b9b..1ecc41f 100644 --- a/lib/opus-ruby.rb +++ b/lib/opus-ruby.rb @@ -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 @@ -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 diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index 8e9fa2f..00a391b 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -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 @@ -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 From 96021e42953bc4eedacd1c23fca9e05614834861 Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sat, 24 May 2014 07:55:02 +0200 Subject: [PATCH 02/14] add packet_get_samples_per_frame, some error query, prevent of read out of bounds --- lib/opus-ruby/decoder.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index 00a391b..df16ff8 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -62,6 +62,7 @@ def decode( data ) 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 From c2d4f0e4d40a5807de53eb3715bb9b52d2bdf990 Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sat, 14 Jun 2014 23:54:06 +0200 Subject: [PATCH 03/14] Support for decoding missing packets --- lib/opus-ruby/decoder.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index df16ff8..f2fdad4 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -57,7 +57,7 @@ def decode( data ) #@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 + max_size = 5760 #2880 decoded = FFI::MemoryPointer.new :short, max_size + 1 frame_size = Opus.opus_decode @decoder, packet, len, decoded, max_size, 0 @@ -67,6 +67,10 @@ def decode( data ) end return decoded.read_string_length frame_size * 2 end + + def decode_missed + Opus.opus_decode @decoder, nil, 0, nil, 0, 0 + end def get_lasterror_code return @lasterror From 1c2a06736e418c883743886c734c85e8c766d1c5 Mon Sep 17 00:00:00 2001 From: Dafoxia Date: Sun, 3 Aug 2014 23:16:53 +0200 Subject: [PATCH 04/14] buffersize corrections --- lib/opus-ruby/decoder.rb | 128 ++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 68 deletions(-) diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index f2fdad4..2718814 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -27,72 +27,64 @@ module Opus - class Decoder - attr_reader :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 - - def destroy - Opus.opus_decoder_destroy @decoder - end - - def reset - Opus.opus_decoder_ctl @decoder, Opus::Constants::OPUS_RESET_STATE, :pointer, nil - end - - def decode( data ) - len = data.size - - packet = FFI::MemoryPointer.new :char, len + 1 - packet.put_string 0, data - - # 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 = 5760 #2880 - 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 - - def decode_missed - Opus.opus_decode @decoder, nil, 0, nil, 0, 0 - 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 + class Decoder + attr_reader :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 + + def destroy + Opus.opus_decoder_destroy @decoder + end + + def reset + Opus.opus_decoder_ctl @decoder, Opus::Constants::OPUS_RESET_STATE, :pointer, nil + end + + def decode( data ) + len = data.size + + 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 + + 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 + + def decode_missed + Opus.opus_decode @decoder, nil, 0, nil, 0, 0 + 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 - - From 158ed28fc8b736ef946341b8c47795bd0b30a6bb Mon Sep 17 00:00:00 2001 From: Dafoxia Date: Sun, 3 Aug 2014 23:38:05 +0200 Subject: [PATCH 05/14] change spaces --- lib/opus-ruby/decoder.rb | 96 ++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index 2718814..a522cff 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -27,64 +27,64 @@ module Opus - class Decoder - attr_reader :sample_rate, :frame_size, :channels + class Decoder + attr_reader :sample_rate, :frame_size, :channels - 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 then + @lasterror = frame_size + frame_size = 0 + end + return decoded.read_string_length 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 + 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 + 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 From 583f66331f84635157dac12ce4632d6eb8360335 Mon Sep 17 00:00:00 2001 From: loscoala Date: Sat, 17 Jan 2015 20:21:18 +0100 Subject: [PATCH 06/14] encoder is now using a single allocated buffer --- .ruby-version | 2 +- lib/opus-ruby.rb | 3 +- lib/opus-ruby/decoder.rb | 98 ++++++++++++++++++++-------------------- lib/opus-ruby/encoder.rb | 25 ++++++---- lib/opus-ruby/version.rb | 2 +- 5 files changed, 70 insertions(+), 60 deletions(-) diff --git a/.ruby-version b/.ruby-version index 9304515..314a6ed 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-2.1.0 +ruby-2.1.1 diff --git a/lib/opus-ruby.rb b/lib/opus-ruby.rb index 1ecc41f..bfabd33 100644 --- a/lib/opus-ruby.rb +++ b/lib/opus-ruby.rb @@ -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 @@ -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 diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index 2718814..4209b21 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -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 + # DEPRECATED: Please use lasterror 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 + diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index ddd1ac9..b8ae3ea 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -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 @@ -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 diff --git a/lib/opus-ruby/version.rb b/lib/opus-ruby/version.rb index e4a1eae..469f847 100644 --- a/lib/opus-ruby/version.rb +++ b/lib/opus-ruby/version.rb @@ -1,3 +1,3 @@ module Opus - VERSION = "0.0.1" + VERSION = "1.0.1" end From e55d95609c5a4fdd6ccd2ad1661cfa4a29e5e5f2 Mon Sep 17 00:00:00 2001 From: loscoala Date: Sun, 18 Jan 2015 01:02:20 +0100 Subject: [PATCH 07/14] modified readme --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1ea0e63..72ce539 100644 --- a/README.md +++ b/README.md @@ -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 From e2edac777bc5a01a8ee40007d17ed07ece58f5ad Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sun, 29 Nov 2015 01:03:42 +0100 Subject: [PATCH 08/14] add frame size change method --- lib/opus-ruby/encoder.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index b8ae3ea..c1ec6f9 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -37,6 +37,10 @@ def signal=(value) @signal = value Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_SIGNAL_REQUEST, :int32, value end + + def set_frame_size frame_size + @frame_size = frame_size + end def encode(data) @buf.put_string 0, data From 37d7026828627095c49b1b845fdaaa19f6b78177 Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sun, 29 Nov 2015 14:47:10 +0100 Subject: [PATCH 09/14] add more OPUS constants --- lib/opus-ruby.rb | 70 ++++++++++++++++++++++++++++++++++++++-- lib/opus-ruby/encoder.rb | 12 ++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/lib/opus-ruby.rb b/lib/opus-ruby.rb index bfabd33..44a282e 100644 --- a/lib/opus-ruby.rb +++ b/lib/opus-ruby.rb @@ -17,15 +17,81 @@ 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 THESE (Begin) ------------------------------------------------------------------------ + OPUS_SET_APPLICATION_REQUEST = 4000 + OPUS_GET_APPLICATION_REQUEST = 4001 OPUS_SET_BITRATE_REQUEST = 4002 - OPUS_SET_SIGNAL_REQUEST = 4024 + 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 + 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 # OLD! should not used anymore + 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 + # WE SHOULD NOT USE THESE (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 + end attach_function :opus_encoder_get_size, [:int], :int diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index c1ec6f9..e48ff1f 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -1,9 +1,10 @@ module Opus class Encoder attr_reader :sample_rate, :frame_size, :channels, - :vbr_rate, :bitrate, :signal + :vbr_rate, :vbr_constraint, :bitrate, :signal def initialize(sample_rate, frame_size, channels, size) + @vbr_constraint= 0 @sample_rate = sample_rate @frame_size = frame_size @channels = channels @@ -27,6 +28,15 @@ def vbr_rate=(value) @vbr_rate = value Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_VBR_REQUEST, :int32, value end + + def vbr_contstraint=(value) + @vbr_constraint = value + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_VBR_CONSTRAINT_REQUEST, :int32, value + end + + def packet_loss_perc=(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_PACKET_LOSS_PERC, :int32, value + end def bitrate=(value) @bitrate = value From 8e4a101e6e8c31b0133b31b82e6d976471a14fca Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sun, 29 Nov 2015 16:27:24 +0100 Subject: [PATCH 10/14] implement more opus functions --- lib/opus-ruby.rb | 6 +- lib/opus-ruby/decoder.rb | 3 + lib/opus-ruby/encoder.rb | 457 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 459 insertions(+), 7 deletions(-) diff --git a/lib/opus-ruby.rb b/lib/opus-ruby.rb index 44a282e..b88a5c2 100644 --- a/lib/opus-ruby.rb +++ b/lib/opus-ruby.rb @@ -36,7 +36,7 @@ module Constants OPUS_SIGNAL_VOICE = 3001 OPUS_SIGNAL_MUSIC = 3002 - # WE SHOULD NOT USE THESE (Begin) ------------------------------------------------------------------------ + # WE SHOULD NOT USE NUMBERS (could change) (Begin) ------------------------------------------------------------------------ OPUS_SET_APPLICATION_REQUEST = 4000 OPUS_GET_APPLICATION_REQUEST = 4001 OPUS_SET_BITRATE_REQUEST = 4002 @@ -65,7 +65,7 @@ module Constants OPUS_GET_SIGNAL_REQUEST = 4025 OPUS_GET_LOOKAHEAD_REQUEST = 4027 - OPUS_RESET_STATE = 4028 # OLD! should not used anymore + OPUS_RESET_STATE = 4028 OPUS_GET_SAMPLE_RATE_REQUEST = 4029 OPUS_GET_FINAL_RANGE_REQUEST = 4031 @@ -81,7 +81,7 @@ module Constants OPUS_GET_EXPERT_FRAME_DURATION_REQUEST= 4041 OPUS_SET_PREDICTION_DISABLED_REQUEST = 4042 OPUS_GET_PREDICTION_DISABLED_REQUEST = 4043 - # WE SHOULD NOT USE THESE (End) -------------------------------------------------------------------------- + # WE SHOULD NOT USE NUMBERS (could change) (End) -------------------------------------------------------------------------- OPUS_FRAMESIZE_ARG = 5000 # Select frame size from the argument (default) diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index 4209b21..db5a4c0 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -2,6 +2,7 @@ # The MIT License (MIT) # # # # Copyright (c) 2014, Aaron Herting 'qwertos' # +# Copyright (c) 2015, dafoxia # # Heavily based on code (c) GitHub User 'perrym5' and code found in the # # libopus public documentation. # # # @@ -87,6 +88,8 @@ def get_lasterror_string else "UNKNOWN ERROR / ERROR NOT DEFINED (should never happen)" end end + + end end diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index e48ff1f..b925ddf 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -31,21 +31,27 @@ def vbr_rate=(value) def vbr_contstraint=(value) @vbr_constraint = value - Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_VBR_CONSTRAINT_REQUEST, :int32, value + opus_set_vbr_constraint value + #Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_VBR_CONSTRAINT_REQUEST, :int32, value end def packet_loss_perc=(value) - Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_PACKET_LOSS_PERC, :int32, value + opus_set_packet_loss_perc value + #Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_PACKET_LOSS_PERC, :int32, value end + #Deprecated, use opus_set_bitrate=(value) instead for new projects def bitrate=(value) @bitrate = value - Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_BITRATE_REQUEST, :int32, value + opus_set_bitrate value + #Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_BITRATE_REQUEST, :int32, value end + #Deprecated, use opus_set_signal instead for new projects def signal=(value) @signal = value - Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_SIGNAL_REQUEST, :int32, value + opus_set_signal value + #Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_SIGNAL_REQUEST, :int32, value end def set_frame_size frame_size @@ -57,5 +63,448 @@ def encode(data) len = Opus.opus_encode @encoder, @buf, @frame_size, @out, @size @out.read_string len end + + # Gets the encoder's complexity configuration. + # @see OPUS_SET_COMPLEXITY + # @param[out] x opus_int32 #: Returns a value in the range 0-10, + # inclusive. + def opus_get_complexity + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_COMPLEXITY_REQUEST + end + + # Gets the encoder's bitrate configuration. + # @see OPUS_SET_BITRATE + # @param[out] x opus_int32 #: Returns the bitrate in bits per second. + # The default is determined based on the + # number of channels and the input + # sampling rate. + def opus_get_bitrate + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_BITRATE_REQUEST + end + + # Configures the encoder's computational complexity. + # The supported range is 0-10 inclusive with 10 representing the highest complexity. + # @see OPUS_GET_COMPLEXITY + # @param[in] x opus_int32: Allowed values: 0-10, inclusive. + def opus_set_complexity(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_COMPLEXITY_REQUEST, :int32, value + end + + # Configures the bitrate in the encoder. + # Rates from 500 to 512000 bits per second are meaningful, as well as the + # special values #OPUS_AUTO and #OPUS_BITRATE_MAX. + # The value #OPUS_BITRATE_MAX can be used to cause the codec to use as much + # rate as it can, which is useful for controlling the rate by adjusting the + # output buffer size. + # @see OPUS_GET_BITRATE + # @param[in] x opus_int32: Bitrate in bits per second. The default + # is determined based on the number of + # channels and the input sampling rate. + def opus_set_bitrate(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_BITRATE_REQUEST, :int32, value + end + + # Enables or disables variable bitrate (VBR) in the encoder. + # The configured bitrate may not be met exactly because frames must + # be an integer number of bytes in length. + # @warning Only the MDCT mode of Opus can provide hard CBR behavior. + # @see OPUS_GET_VBR + # @see OPUS_SET_VBR_CONSTRAINT + #
+ #
0
Hard CBR. For LPC/hybrid modes at very low bit-rate, this can + # cause noticeable quality degradation.
+ #
1
VBR (default). The exact type of VBR is controlled by + # #OPUS_SET_VBR_CONSTRAINT.
+ #
+ def opus_set_vbr(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_VBR_REQUEST, :int32, value + end + + # Determine if variable bitrate (VBR) is enabled in the encoder. + # @see OPUS_SET_VBR + # @see OPUS_GET_VBR_CONSTRAINT + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
0
Hard CBR.
+ #
1
VBR (default). The exact type of VBR may be retrieved via + # #OPUS_GET_VBR_CONSTRAINT.
+ #
+ def opus_get_vbr + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_VBR_REQUEST + end + + # Enables or disables constrained VBR in the encoder. + # This setting is ignored when the encoder is in CBR mode. + # @warning Only the MDCT mode of Opus currently heeds the constraint. + # Speech mode ignores it completely, hybrid mode may fail to obey it + # if the LPC layer uses more bitrate than the constraint would have + # permitted. + # @see OPUS_GET_VBR_CONSTRAINT + # @see OPUS_SET_VBR + # @param[in] x opus_int32: Allowed values: + #
+ #
0
Unconstrained VBR.
+ #
1
Constrained VBR (default). This creates a maximum of one + # frame of buffering delay assuming a transport with a + # serialization speed of the nominal bitrate.
+ #
+ def opus_set_vbr_constraint(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_VBR_CONSTRAINT_REQUEST, :int32, value + end + + # Determine if constrained VBR is enabled in the encoder. + # @see OPUS_SET_VBR_CONSTRAINT + # @see OPUS_GET_VBR + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
0
Unconstrained VBR.
+ #
1
Constrained VBR (default).
+ #
+ def opus_get_vbr_constraint + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_VBR_CONSTRAINT_REQUEST + end + + # Configures mono/stereo forcing in the encoder. + # This can force the encoder to produce packets encoded as either mono or + # stereo, regardless of the format of the input audio. This is useful when + # the caller knows that the input signal is currently a mono source embedded + # in a stereo stream. + # @see OPUS_GET_FORCE_CHANNELS + # @param[in] x opus_int32: Allowed values: + #
+ #
#OPUS_AUTO
Not forced (default)
+ #
1
Forced mono
+ #
2
Forced stereo
+ #*
+ def opus_set_force_cannels(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_FORCE_CHANNELS_REQUEST, :int32, value + end + + # Gets the encoder's forced channel configuration. + # @see OPUS_SET_FORCE_CHANNELS + # @param[out] x opus_int32 #: + #
+ #
#OPUS_AUTO
Not forced (default)
+ #
1
Forced mono
+ #
2
Forced stereo
+ #
+ #define OPUS_GET_FORCE_CHANNELS(x) OPUS_GET_FORCE_CHANNELS_REQUEST, __opus_check_int_ptr(x) + def opus_get_force_channels + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_FORCE_CHANNELS_REQUEST + end + + # Configures the maximum bandpass that the encoder will select automatically. + # Applications should normally use this instead of #OPUS_SET_BANDWIDTH + # (leaving that set to the default, #OPUS_AUTO). This allows the + # application to set an upper bound based on the type of input it is + # providing, but still gives the encoder the freedom to reduce the bandpass + # when the bitrate becomes too low, for better overall quality. + # @see OPUS_GET_MAX_BANDWIDTH + # @param[in] x opus_int32: Allowed values: + #
+ #
OPUS_BANDWIDTH_NARROWBAND
4 kHz passband
+ #
OPUS_BANDWIDTH_MEDIUMBAND
6 kHz passband
+ #
OPUS_BANDWIDTH_WIDEBAND
8 kHz passband
+ #
OPUS_BANDWIDTH_SUPERWIDEBAND
12 kHz passband
+ #
OPUS_BANDWIDTH_FULLBAND
20 kHz passband (default)
+ #
+ #define OPUS_SET_MAX_BANDWIDTH(x) OPUS_SET_MAX_BANDWIDTH_REQUEST, __opus_check_int(x) + def opus_set_max_bandwidth(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_MAX_BANDWIDTH_REQUEST, :int32, value + end + + # Gets the encoder's configured maximum allowed bandpass. + # @see OPUS_SET_MAX_BANDWIDTH + # @param[out] x opus_int32 #: Allowed values: + #
+ #
#OPUS_BANDWIDTH_NARROWBAND
4 kHz passband
+ #
#OPUS_BANDWIDTH_MEDIUMBAND
6 kHz passband
+ #
#OPUS_BANDWIDTH_WIDEBAND
8 kHz passband
+ #
#OPUS_BANDWIDTH_SUPERWIDEBAND
12 kHz passband
+ #
#OPUS_BANDWIDTH_FULLBAND
20 kHz passband (default)
+ #
+ #define OPUS_GET_MAX_BANDWIDTH(x) OPUS_GET_MAX_BANDWIDTH_REQUEST, __opus_check_int_ptr(x) + def opus_get_max_bandwidth + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_MAX_BANDWIDTH_REQUEST + end + + # Sets the encoder's bandpass to a specific value. + # This prevents the encoder from automatically selecting the bandpass based + # on the available bitrate. If an application knows the bandpass of the input + # audio it is providing, it should normally use #OPUS_SET_MAX_BANDWIDTH + # instead, which still gives the encoder the freedom to reduce the bandpass + # when the bitrate becomes too low, for better overall quality. + # @see OPUS_GET_BANDWIDTH + # @param[in] x opus_int32: Allowed values: + #
+ #
#OPUS_AUTO
(default)
+ #
#OPUS_BANDWIDTH_NARROWBAND
4 kHz passband
+ #
#OPUS_BANDWIDTH_MEDIUMBAND
6 kHz passband
+ #
#OPUS_BANDWIDTH_WIDEBAND
8 kHz passband
+ #
#OPUS_BANDWIDTH_SUPERWIDEBAND
12 kHz passband
+ #
#OPUS_BANDWIDTH_FULLBAND
20 kHz passband
+ #
+ #define OPUS_SET_BANDWIDTH(x) OPUS_SET_BANDWIDTH_REQUEST, __opus_check_int(x) + def opus_set_bandwidth(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_BANDWIDTH_REQUEST, :int32, value + end + + # Configures the type of signal being encoded. + # This is a hint which helps the encoder's mode selection. + # @see OPUS_GET_SIGNAL + # @param[in] x opus_int32: Allowed values: + #
+ #
#OPUS_AUTO
(default)
+ #
#OPUS_SIGNAL_VOICE
Bias thresholds towards choosing LPC or Hybrid modes.
+ #
#OPUS_SIGNAL_MUSIC
Bias thresholds towards choosing MDCT modes.
+ #
+ #define OPUS_SET_SIGNAL(x) OPUS_SET_SIGNAL_REQUEST, __opus_check_int(x) + def opus_set_signal(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_SIGNAL_REQUEST, :int32, value + end + + # Gets the encoder's configured signal type. + # @see OPUS_SET_SIGNAL + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
#OPUS_AUTO
(default)
+ #
#OPUS_SIGNAL_VOICE
Bias thresholds towards choosing LPC or Hybrid modes.
+ #
#OPUS_SIGNAL_MUSIC
Bias thresholds towards choosing MDCT modes.
+ #
+ #define OPUS_GET_SIGNAL(x) OPUS_GET_SIGNAL_REQUEST, __opus_check_int_ptr(x) + def opus_get_signal + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_SIGNAL_REQUEST + end + + # Configures the encoder's intended application. + # The initial value is a mandatory argument to the encoder_create function. + # @see OPUS_GET_APPLICATION + # @param[in] x opus_int32: Returns one of the following values: + #
+ #
#OPUS_APPLICATION_VOIP
+ #
Process signal for improved speech intelligibility.
+ #
#OPUS_APPLICATION_AUDIO
+ #
Favor faithfulness to the original input.
+ #
#OPUS_APPLICATION_RESTRICTED_LOWDELAY
+ #
Configure the minimum possible coding delay by disabling certain modes + # of operation.
+ #
+ #define OPUS_SET_APPLICATION(x) OPUS_SET_APPLICATION_REQUEST, __opus_check_int(x) + def opus_set_application(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_APPLICATION_REQUEST, :int32, value + end + + # Gets the encoder's configured application. + # @see OPUS_SET_APPLICATION + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
#OPUS_APPLICATION_VOIP
+ #
Process signal for improved speech intelligibility.
+ #
#OPUS_APPLICATION_AUDIO
+ #
Favor faithfulness to the original input.
+ #
#OPUS_APPLICATION_RESTRICTED_LOWDELAY
+ #
Configure the minimum possible coding delay by disabling certain modes + # of operation.
+ #
+ #define OPUS_GET_APPLICATION(x) OPUS_GET_APPLICATION_REQUEST, __opus_check_int_ptr(x) + def opus_get_application + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_APPLICATION_REQUEST, :int32, value + end + + # Gets the sampling rate the encoder or decoder was initialized with. + # This simply returns the Fs value passed to opus_encoder_init() + # or opus_decoder_init(). + # @param[out] x opus_int32 #: Sampling rate of encoder or decoder. + # + # + #define OPUS_GET_SAMPLE_RATE(x) OPUS_GET_SAMPLE_RATE_REQUEST, __opus_check_int_ptr(x) + def opus_encoder_get_sample_rate + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_SAMPLE_RATE_REQUEST + end + + # Gets the total samples of delay added by the entire codec. + # This can be queried by the encoder and then the provided number of samples can be + # skipped on from the start of the decoder's output to provide time aligned input + # and output. From the perspective of a decoding application the real data begins this many + # samples late. + # + # The decoder contribution to this delay is identical for all decoders, but the + # encoder portion of the delay may vary from implementation to implementation, + # version to version, or even depend on the encoder's initial configuration. + # Applications needing delay compensation should call this CTL rather than + # hard-coding a value. + # @param[out] x opus_int32 #: Number of lookahead samples + #define OPUS_GET_LOOKAHEAD(x) OPUS_GET_LOOKAHEAD_REQUEST, __opus_check_int_ptr(x) + def opus_get_lookahead + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_LOOKAHEAD_REQUEST + end + + # Configures the encoder's use of inband forward error correction (FEC). + # @note This is only applicable to the LPC layer + # @see OPUS_GET_INBAND_FEC + # @param[in] x opus_int32: Allowed values: + #
+ #
0
Disable inband FEC (default).
+ #
1
Enable inband FEC.
+ #
+ #define OPUS_SET_INBAND_FEC(x) OPUS_SET_INBAND_FEC_REQUEST, __opus_check_int(x) + def opus_set_inband_fec(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_INBAND_FEC_REQUEST, :int32, value + end + + # Gets encoder's configured use of inband forward error correction. + # @see OPUS_SET_INBAND_FEC + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
0
Inband FEC disabled (default).
+ #
1
Inband FEC enabled.
+ #
+ #define OPUS_GET_INBAND_FEC(x) OPUS_GET_INBAND_FEC_REQUEST, __opus_check_int_ptr(x) + def opus_get_inband_fec + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_INBAND_FEC_REQUEST + end + + # Configures the encoder's expected packet loss percentage. + # Higher values with trigger progressively more loss resistant behavior in the encoder + # at the expense of quality at a given bitrate in the lossless case, but greater quality + # under loss. + # @see OPUS_GET_PACKET_LOSS_PERC + # @param[in] x opus_int32: Loss percentage in the range 0-100, inclusive (default: 0). + #define OPUS_SET_PACKET_LOSS_PERC(x) OPUS_SET_PACKET_LOSS_PERC_REQUEST, __opus_check_int(x) + def opus_set_packet_loss_perc(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_PACKET_LOSS_PERC_REQUEST, :int32, value + end + + # Gets the encoder's configured packet loss percentage. + # @see OPUS_SET_PACKET_LOSS_PERC + # @param[out] x opus_int32 #: Returns the configured loss percentage + # in the range 0-100, inclusive (default: 0). + #define OPUS_GET_PACKET_LOSS_PERC(x) OPUS_GET_PACKET_LOSS_PERC_REQUEST, __opus_check_int_ptr(x) + def opus_get_packet_loss_perc + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_PACKET_LOSS_PERC_REQUEST + end + + # Configures the encoder's use of discontinuous transmission (DTX). + # @note This is only applicable to the LPC layer + # @see OPUS_GET_DTX + # @param[in] x opus_int32: Allowed values: + #
+ #
0
Disable DTX (default).
+ #
1
Enabled DTX.
+ #
+ #define OPUS_SET_DTX(x) OPUS_SET_DTX_REQUEST, __opus_check_int(x) + def opus_set_dtx(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_DTX_REQUEST, :int32, value + end + + # Gets encoder's configured use of discontinuous transmission. + # @see OPUS_SET_DTX + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
0
DTX disabled (default).
+ #
1
DTX enabled.
+ #
+ #define OPUS_GET_DTX(x) OPUS_GET_DTX_REQUEST, __opus_check_int_ptr(x) + def opus_get_dtx + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_DTX_REQUEST + end + + # Configures the depth of signal being encoded. + # This is a hint which helps the encoder identify silence and near-silence. + # @see OPUS_GET_LSB_DEPTH + # @param[in] x opus_int32: Input precision in bits, between 8 and 24 + # (default: 24). + #define OPUS_SET_LSB_DEPTH(x) OPUS_SET_LSB_DEPTH_REQUEST, __opus_check_int(x) + def opus_set_lsb_depth(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_LSB_DEPTH_REQUEST, :int32, value + end + + # Gets the encoder's configured signal depth. + # @see OPUS_SET_LSB_DEPTH + # @param[out] x opus_int32 #: Input precision in bits, between 8 and + # 24 (default: 24). + #define OPUS_GET_LSB_DEPTH(x) OPUS_GET_LSB_DEPTH_REQUEST, __opus_check_int_ptr(x) + def opus_get_lsb_depth + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_LSB_DEPTH_REQUEST + end + + # Gets the duration (in samples) of the last packet successfully decoded or concealed. + # @param[out] x opus_int32 #: Number of samples (at current sampling rate). + #define OPUS_GET_LAST_PACKET_DURATION(x) OPUS_GET_LAST_PACKET_DURATION_REQUEST, __opus_check_int_ptr(x) + def opus_get_last_packet_duration + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_LAST_PACKET_DURATION_REQUEST + end + + # Configures the encoder's use of variable duration frames. + # When variable duration is enabled, the encoder is free to use a shorter frame + # size than the one requested in the opus_encode#() call. + # It is then the user's responsibility + # to verify how much audio was encoded by checking the ToC byte of the encoded + # packet. The part of the audio that was not encoded needs to be resent to the + # encoder for the next call. Do not use this option unless you really + # know what you are doing. + # @see OPUS_GET_EXPERT_VARIABLE_DURATION + # @param[in] x opus_int32: Allowed values: + #
+ #
OPUS_FRAMESIZE_ARG
Select frame size from the argument (default).
+ #
OPUS_FRAMESIZE_2_5_MS
Use 2.5 ms frames.
+ #
OPUS_FRAMESIZE_5_MS
Use 2.5 ms frames.
+ #
OPUS_FRAMESIZE_10_MS
Use 10 ms frames.
+ #
OPUS_FRAMESIZE_20_MS
Use 20 ms frames.
+ #
OPUS_FRAMESIZE_40_MS
Use 40 ms frames.
+ #
OPUS_FRAMESIZE_60_MS
Use 60 ms frames.
+ #
OPUS_FRAMESIZE_VARIABLE
Optimize the frame size dynamically.
+ #
+ #define OPUS_SET_EXPERT_FRAME_DURATION(x) OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, __opus_check_int(x) + def opus_set_expert_frame_duration(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, :int32, value + end + + # Gets the encoder's configured use of variable duration frames. + # @see OPUS_SET_EXPERT_VARIABLE_DURATION + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
OPUS_FRAMESIZE_ARG
Select frame size from the argument (default).
+ #
OPUS_FRAMESIZE_2_5_MS
Use 2.5 ms frames.
+ #
OPUS_FRAMESIZE_5_MS
Use 2.5 ms frames.
+ #
OPUS_FRAMESIZE_10_MS
Use 10 ms frames.
+ #
OPUS_FRAMESIZE_20_MS
Use 20 ms frames.
+ #
OPUS_FRAMESIZE_40_MS
Use 40 ms frames.
+ #
OPUS_FRAMESIZE_60_MS
Use 60 ms frames.
+ #
OPUS_FRAMESIZE_VARIABLE
Optimize the frame size dynamically.
+ #
+ #define OPUS_GET_EXPERT_FRAME_DURATION(x) OPUS_GET_EXPERT_FRAME_DURATION_REQUEST, __opus_check_int_ptr(x) + def opus_get_expert_frame_duration + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_EXPERT_FRAME_DURATION_REQUEST + end + + # If set to 1, disables almost all use of prediction, making frames almost completely independent. This reduces quality. (default : 0) + #define OPUS_SET_PREDICTION_DISABLED(x) OPUS_SET_PREDICTION_DISABLED_REQUEST, __opus_check_int(x) + def opus_set_prediction_disabled(value) + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_PREDICTION_DISABLED_REQUEST, :int32, value + end + + # Gets the encoder's configured prediction status. + #define OPUS_GET_PREDICTION_DISABLED(x) OPUS_GET_PREDICTION_DISABLED_REQUEST, __opus_check_int_ptr(x) + def opus_get_prediction_disabled + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_PREDICTION_DISABLED_REQUEST + end + + # Gets the encoder's configured bandpass. + # @see OPUS_SET_BANDWIDTH + # @param[out] x opus_int32 #: Returns one of the following values: + #
+ #
#OPUS_AUTO
(default)
+ #
#OPUS_BANDWIDTH_NARROWBAND
4 kHz passband
+ #
#OPUS_BANDWIDTH_MEDIUMBAND
6 kHz passband
+ #
#OPUS_BANDWIDTH_WIDEBAND
8 kHz passband
+ #
#OPUS_BANDWIDTH_SUPERWIDEBAND
12 kHz passband
+ #
#OPUS_BANDWIDTH_FULLBAND
20 kHz passband
+ #
+ #define OPUS_GET_BANDWIDTH(x) OPUS_GET_BANDWIDTH_REQUEST, __opus_check_int_ptr(x) + def opus_encoder_get_bandwidth + Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_GET_BANDWIDTH_REQUEST + end + end end From d651a0298e37da2b95f68a05c60f789bb0ebf5af Mon Sep 17 00:00:00 2001 From: dafoxia Date: Sun, 29 Nov 2015 16:33:26 +0100 Subject: [PATCH 11/14] add license information --- lib/opus-ruby/encoder.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index b925ddf..ebd3a70 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -1,3 +1,29 @@ +################################################################################# +# The MIT License (MIT) # +# # +# Copyright (c) 2015, dafoxia # +# Based on code (c) GitHub User 'mattvperry' and code found in the # +# libopus public documentation. # +# # +# 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 Opus class Encoder attr_reader :sample_rate, :frame_size, :channels, From d1ff44c87b74d2155506abab68b722fd1e4013f7 Mon Sep 17 00:00:00 2001 From: dafoxia Date: Wed, 6 Jan 2016 17:16:44 +0100 Subject: [PATCH 12/14] encoding with memorypointer --- lib/opus-ruby/encoder.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index ebd3a70..23ef059 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -90,6 +90,10 @@ def encode(data) @out.read_string len end + def encode_ptr(memorypointer) + len = Opus.opus_encode @encoder, memorypointer, @frame_size, @out, @size + @out.read_string len + end # Gets the encoder's complexity configuration. # @see OPUS_SET_COMPLEXITY # @param[out] x opus_int32 #: Returns a value in the range 0-10, From f71ccce41641775a40ee088470ea07782ed8a58d Mon Sep 17 00:00:00 2001 From: dafoxia Date: Thu, 15 Jun 2017 19:55:10 +0200 Subject: [PATCH 13/14] fix typo --- lib/opus-ruby/encoder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index 23ef059..1fe5d46 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -206,7 +206,7 @@ def opus_get_vbr_constraint #
1
Forced mono
#
2
Forced stereo
#* - def opus_set_force_cannels(value) + def opus_set_force_channels(value) Opus.opus_encoder_ctl @encoder, Opus::Constants::OPUS_SET_FORCE_CHANNELS_REQUEST, :int32, value end From f133402230b34faba269a3d7646cde32fcd3dead Mon Sep 17 00:00:00 2001 From: dafoxia Date: Wed, 12 Jun 2024 18:20:07 +0200 Subject: [PATCH 14/14] Update contact information an add new Opus functionality. --- lib/opus-ruby.rb | 12 ++++++++++++ lib/opus-ruby/decoder.rb | 2 +- lib/opus-ruby/encoder.rb | 10 ++++++---- lib/opus-ruby/version.rb | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/opus-ruby.rb b/lib/opus-ruby.rb index b88a5c2..c73a40a 100644 --- a/lib/opus-ruby.rb +++ b/lib/opus-ruby.rb @@ -81,6 +81,15 @@ module Constants 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) -------------------------------------------------------------------------- @@ -91,6 +100,9 @@ module Constants 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 diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index db5a4c0..47d22fa 100644 --- a/lib/opus-ruby/decoder.rb +++ b/lib/opus-ruby/decoder.rb @@ -2,7 +2,7 @@ # The MIT License (MIT) # # # # Copyright (c) 2014, Aaron Herting 'qwertos' # -# Copyright (c) 2015, dafoxia # +# Copyright (c) 2015, dafoxia # # Heavily based on code (c) GitHub User 'perrym5' and code found in the # # libopus public documentation. # # # diff --git a/lib/opus-ruby/encoder.rb b/lib/opus-ruby/encoder.rb index 1fe5d46..edebd23 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -1,7 +1,7 @@ ################################################################################# # The MIT License (MIT) # # # -# Copyright (c) 2015, dafoxia # +# Copyright (c) 2015, dafoxia # # Based on code (c) GitHub User 'mattvperry' and code found in the # # libopus public documentation. # # # @@ -85,9 +85,11 @@ def set_frame_size frame_size end def encode(data) - @buf.put_string 0, data - len = Opus.opus_encode @encoder, @buf, @frame_size, @out, @size - @out.read_string len + if data != nil # if no data given do not encode anything + @buf.put_string 0, data + len = Opus.opus_encode @encoder, @buf, @frame_size, @out, @size + @out.read_string len + end end def encode_ptr(memorypointer) diff --git a/lib/opus-ruby/version.rb b/lib/opus-ruby/version.rb index 469f847..dccdf7e 100644 --- a/lib/opus-ruby/version.rb +++ b/lib/opus-ruby/version.rb @@ -1,3 +1,3 @@ module Opus - VERSION = "1.0.1" + VERSION = "1.0.3" end