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/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 diff --git a/lib/opus-ruby.rb b/lib/opus-ruby.rb index 3687b9b..c73a40a 100644 --- a/lib/opus-ruby.rb +++ b/lib/opus-ruby.rb @@ -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 @@ -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 diff --git a/lib/opus-ruby/decoder.rb b/lib/opus-ruby/decoder.rb index 8e9fa2f..47d22fa 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. # # # @@ -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 + # 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 + 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..edebd23 100644 --- a/lib/opus-ruby/encoder.rb +++ b/lib/opus-ruby/encoder.rb @@ -1,17 +1,48 @@ +################################################################################# +# 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, - :vbr_rate, :bitrate + :vbr_rate, :vbr_constraint, :bitrate, :signal - def initialize(sample_rate, frame_size, channels) + def initialize(sample_rate, frame_size, channels, size) + @vbr_constraint= 0 @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 @@ -23,18 +54,489 @@ 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_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_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_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_set_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) + 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) + 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, + # 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_channels(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 - 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 + # 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 diff --git a/lib/opus-ruby/version.rb b/lib/opus-ruby/version.rb index e4a1eae..dccdf7e 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.3" end