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_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_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.maxsize # TODO: Must be an INT32_MAX elif i == -7: fs = -1 * (sys.maxsize - 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 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 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 __init__(self, untalk): self.untalk = untalk self.encoder = opus_encoder.create(SAMPLE_RATE, CHANNELS, opus_constants.APPLICATION_VOIP) self.decoder = opus_decoder.create(SAMPLE_RATE, CHANNELS) # disable variable bitrate (VBR) opus_encoder.ctl(self.encoder, opus_ctl.set_vbr, 0) # configure expected jitter loss opus_encoder.ctl(self.encoder, opus_ctl.set_packet_loss_perc, self.untalk.loss_percentage) # configure forward error correction (FEC) opus_encoder.ctl(self.encoder, opus_ctl.set_inband_fec, self.untalk.decode_fec)
def __init__(self, fec=0): self.chunk = 960 #2880#960#2880#960 # 20 ms at 48000 self.channels = 1 # mono self.rate = 48000 # max rate (should this be reduced?) self.fec = fec # fec self.encoder = opus_encoder.create(self.rate, self.channels, opus_constants.APPLICATION_VOIP) opus_encoder.ctl(self.encoder, opus_ctl.set_vbr, 0) # disable vbr opus_encoder.ctl(self.encoder, opus_ctl.set_packet_loss_perc, 2) # configure expected jitter loss if fec: print('FEC enabled: this may increase latency slightly.') print( ' It will also (hopefully) compensate for any lost/delayed packets.' ) print(' It also seems to result in slightly mushier audio.') opus_encoder.ctl(self.encoder, opus_ctl.set_inband_fec, 1) # enable fec self.decoder = opus_decoder.create(self.rate, self.channels)
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)
from opuslib.exceptions import OpusError from websocket import create_connection import pyaudio p = pyaudio.PyAudio() # 16 bits per sample ? FORMAT = pyaudio.paInt16 # 44.1k sampling rate ? RATE = 24000 # number of channels CHANNELS = 1 FRAME_SIZE = 60 # in milliseconds # frames per buffer ? CHUNK_SIZE = int(RATE * FRAME_SIZE / 1000) STREAM = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE) print "initialized stream" enc = encoder.create(RATE, CHANNELS, constants.APPLICATION_AUDIO) URL = "ws://localhost:8000/stream/ws/opus/publish/DVF_W31C-" ws = create_connection(URL) while True: chunk = STREAM.read(CHUNK_SIZE) encoded = encoder.encode(enc, chunk, CHUNK_SIZE, CHUNK_SIZE) print len(encoded) ws.send_binary(encoded)
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)