def test_max_bandwidth(self): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) i = -2 self.assertRaises(OpusError, lambda: encoder.ctl(enc, ctl.set_max_bandwidth, i)) i = constants.BANDWIDTH_FULLBAND + 1 self.assertRaises(OpusError, lambda: encoder.ctl(enc, ctl.set_max_bandwidth, i)) i = constants.BANDWIDTH_NARROWBAND encoder.ctl(enc, ctl.set_max_bandwidth, i) i = constants.BANDWIDTH_FULLBAND encoder.ctl(enc, ctl.set_max_bandwidth, i) i = constants.BANDWIDTH_WIDEBAND encoder.ctl(enc, ctl.set_max_bandwidth, i) i = constants.BANDWIDTH_MEDIUMBAND encoder.ctl(enc, ctl.set_max_bandwidth, i) i = -12345 value = encoder.ctl(enc, ctl.get_max_bandwidth) self.assertIn( value, ( constants.BANDWIDTH_FULLBAND, constants.BANDWIDTH_MEDIUMBAND, constants.BANDWIDTH_WIDEBAND, constants.BANDWIDTH_NARROWBAND, constants.AUTO, ), ) encoder.destroy(enc)
def test_bandwidth(self): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) # Set bandwidth i = -2 self.assertRaises(OpusError, lambda: encoder.ctl(enc, ctl.set_bandwidth, i)) i = constants.BANDWIDTH_FULLBAND+1 self.assertRaises(OpusError, lambda: encoder.ctl(enc, ctl.set_bandwidth, i)) i = constants.BANDWIDTH_NARROWBAND encoder.ctl(enc, ctl.set_bandwidth, i) i = constants.BANDWIDTH_FULLBAND encoder.ctl(enc, ctl.set_bandwidth, i) i = constants.BANDWIDTH_WIDEBAND encoder.ctl(enc, ctl.set_bandwidth, i) i = constants.BANDWIDTH_MEDIUMBAND encoder.ctl(enc, ctl.set_bandwidth, i) # Get bandwidth i = -12345 value = encoder.ctl(enc, ctl.get_bandwidth) self.assertIn(value, (constants.BANDWIDTH_FULLBAND, constants.BANDWIDTH_MEDIUMBAND, constants.BANDWIDTH_WIDEBAND, constants.BANDWIDTH_NARROWBAND, constants.AUTO)) encoder.ctl(enc, ctl.set_bandwidth, constants.AUTO) encoder.destroy(enc)
def test_unimplemented(self): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) try: encoder.ctl(enc, ctl.unimplemented) except OpusError as e: self.assertEqual(e.code, constants.UNIMPLEMENTED) encoder.destroy(enc)
def _test_unsupported_sample_rates(self): for c in range(0, 4): for i in range(-7, 96000+1): if i in (8000, 12000, 16000, 24000, 48000) and c in (1, 2): continue if i == -5: fs = -8000 elif i == -6: fs = sys.maxint # TODO: Must be an INT32_MAX elif i == -7: fs = -1*(sys.maxint-1) # TODO: Must be an INT32_MIN else: fs = i try: encoder.create(fs, c, constants.APPLICATION_VOIP) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG)
def _test_unsupported_sample_rates(self): for c in range(0, 4): for i in range(-7, 96000 + 1): if i in (8000, 12000, 16000, 24000, 48000) and c in (1, 2): continue if i == -5: fs = -8000 elif i == -6: fs = sys.maxint # TODO: Must be an INT32_MAX elif i == -7: fs = -1 * (sys.maxint - 1) # TODO: Must be an INT32_MIN else: fs = i try: encoder.create(fs, c, constants.APPLICATION_VOIP) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG)
def test_bitrate(self): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) encoder.ctl(enc, ctl.set_bitrate, 1073741832) value = encoder.ctl(enc, ctl.get_bitrate) self.assertLess(value, 700000) self.assertGreater(value, 256000) encoder.destroy(enc) self.check_setget(ctl.set_bitrate, ctl.get_bitrate, (-12345, 0), (500, 256000))
def test_create(self): try: encoder.create(48000, 2, constants.AUTO) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) enc = encoder.create(48000, 2, constants.APPLICATION_VOIP) encoder.destroy(enc) enc = encoder.create(48000, 2, constants.APPLICATION_RESTRICTED_LOWDELAY) i = encoder.ctl(enc, ctl.get_lookahead) # TODO: rewrite that code # if(err!=OPUS_OK || i<0 || i>32766)test_failed(); encoder.destroy(enc) enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) i = encoder.ctl(enc, ctl.get_lookahead) # TODO: rewrite that code # err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i)); # if(err!=OPUS_OK || i<0 || i>32766)test_failed(); encoder.destroy(enc)
def check_setget(self, set, get, bad, good): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) for value in bad: self.assertRaises(OpusError, lambda: encoder.ctl(enc, set, value)) for value in good: encoder.ctl(enc, set, value) result = encoder.ctl(enc, get) self.assertEqual(value, result) encoder.destroy(enc)
def __init__(self, fs, channels, application): """ Parameters: fs : sampling rate channels : number of channels """ if application in APPLICATION_TYPES_MAP.keys(): application = APPLICATION_TYPES_MAP[application] elif application in APPLICATION_TYPES_MAP.values(): pass # Nothing to do here else: raise ValueError("`application` value must be in 'voip', 'audio' or 'restricted_lowdelay'") self._fs = fs self._channels = channels self._application = application self._state = encoder.create(fs, channels, application)
def test_encode_float(self): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) data = chr(0)*ctypes.sizeof(ctypes.c_float)*2*960 encoder.encode_float(enc, data, 960, len(data)) encoder.destroy(enc)
def test_encode_float(self): enc = encoder.create(48000, 2, constants.APPLICATION_AUDIO) data = chr(0) * ctypes.sizeof(ctypes.c_float) * 2 * 960 encoder.encode_float(enc, data, 960, len(data)) encoder.destroy(enc)