def _test_unsupported_sample_rates(self): """Unsupported sample rates TODO: make the same test with a opus_decoder_init() function""" for c in range(4): for i in range(-7, 96000): 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: should be a INT32_MAX elif i == -7: fs = -1 * (sys.maxint - 1) # Emulation of the INT32_MIN else: fs = i try: dec = decoder.create(fs, c) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) else: decoder.destroy(dec)
def test_create(self): try: dec = decoder.create(48000, 2) except OpusError: raise AssertionError() else: decoder.destroy(dec)
def test_unimplemented(self): dec = decoder.create(48000, 2) try: decoder.ctl(dec, ctl.unimplemented) except OpusError as e: self.assertEqual(e.code, constants.UNIMPLEMENTED) decoder.destroy(dec)
def _test_unsupported_sample_rates(self): """Unsupported sample rates TODO: make the same test with a opus_decoder_init() function""" for c in range(4): for i in range(-7, 96000): 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: should be a INT32_MAX elif i == -7: fs = -1*(sys.maxint-1) # Emulation of the INT32_MIN else: fs = i try: dec = decoder.create(fs, c) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) else: decoder.destroy(dec)
def test_decode(self): """opus_decode()""" packet = chr((63<<2)+3)+chr(49) for j in range(2, 51): packet += chr(0) dec = decoder.create(48000, 2) try: decoder.decode(dec, packet, 51, 960, 0) except OpusError as e: self.assertEqual(e.code, constants.INVALID_PACKET) packet = chr(63<<2)+chr(0)+chr(0) try: decoder.decode(dec, packet, -1, 960, 0) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) try: decoder.decode(dec, packet, 3, 60, 0) except OpusError as e: self.assertEqual(e.code, constants.BUFFER_TOO_SMALL) try: decoder.decode(dec, packet, 3, 480, 0) except OpusError as e: self.assertEqual(e.code, constants.BUFFER_TOO_SMALL) try: decoder.decode(dec, packet, 3, 960, 0) except OpusError: self.fail('Decode failed') decoder.destroy(dec)
def test_decode(self): """opus_decode()""" packet = chr((63 << 2) + 3) + chr(49) for j in range(2, 51): packet += chr(0) dec = decoder.create(48000, 2) try: decoder.decode(dec, packet, 51, 960, 0) except OpusError as e: self.assertEqual(e.code, constants.INVALID_PACKET) packet = chr(63 << 2) + chr(0) + chr(0) try: decoder.decode(dec, packet, -1, 960, 0) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) try: decoder.decode(dec, packet, 3, 60, 0) except OpusError as e: self.assertEqual(e.code, constants.BUFFER_TOO_SMALL) try: decoder.decode(dec, packet, 3, 480, 0) except OpusError as e: self.assertEqual(e.code, constants.BUFFER_TOO_SMALL) try: decoder.decode(dec, packet, 3, 960, 0) except OpusError: self.fail('Decode failed') decoder.destroy(dec)
def test_decode_float(self): dec = decoder.create(48000, 2) packet = chr(63 << 2) + chr(0) + chr(0) try: decoder.decode_float(dec, packet, 3, 960, 0) except OpusError: self.fail('Decode failed') decoder.destroy(dec)
def test_decode_float(self): dec = decoder.create(48000, 2) packet = chr(63<<2)+chr(0)+chr(0) try: decoder.decode_float(dec, packet, 3, 960, 0) except OpusError: self.fail('Decode failed') decoder.destroy(dec)
def test_get_nb_samples(self): """opus_decoder_get_nb_samples()""" dec = decoder.create(48000, 2) self.assertEqual(480, decoder.get_nb_samples(dec, '\x00', 1)) packet = ''.join([chr(x) for x in ((63<<2)|3, 63)]) # TODO: check for exception code self.assertRaises(OpusError, lambda: decoder.get_nb_samples(dec, packet, 2)) decoder.destroy(dec)
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 test_get_pitch(self): dec = decoder.create(48000, 2) i = decoder.ctl(dec, ctl.get_pitch) self.assertIn(i, (-1, 0)) packet = chr(63<<2)+chr(0)+chr(0) decoder.decode(dec, packet, 3, 960, False) i = decoder.ctl(dec, ctl.get_pitch) self.assertIn(i, (-1, 0)) packet = chr(1)+chr(0)+chr(0) decoder.decode(dec, packet, 3, 960, False) i = decoder.ctl(dec, ctl.get_pitch) self.assertIn(i, (-1, 0)) decoder.destroy(dec)
def test_get_pitch(self): dec = decoder.create(48000, 2) i = decoder.ctl(dec, ctl.get_pitch) self.assertIn(i, (-1, 0)) packet = chr(63 << 2) + chr(0) + chr(0) decoder.decode(dec, packet, 3, 960, False) i = decoder.ctl(dec, ctl.get_pitch) self.assertIn(i, (-1, 0)) packet = chr(1) + chr(0) + chr(0) decoder.decode(dec, packet, 3, 960, False) i = decoder.ctl(dec, ctl.get_pitch) self.assertIn(i, (-1, 0)) decoder.destroy(dec)
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_gain(self): dec = decoder.create(48000, 2) i = decoder.ctl(dec, ctl.get_gain) self.assertEqual(i, 0) try: decoder.ctl(dec, ctl.set_gain, -32769) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) try: decoder.ctl(dec, ctl.set_gain, 32768) except OpusError as e: self.assertEqual(e.code, constants.BAD_ARG) decoder.ctl(dec, ctl.set_gain, -15) i = decoder.ctl(dec, ctl.get_gain) self.assertEqual(i, -15) decoder.destroy(dec)
def test_get_bandwidth(self): dec = decoder.create(48000, 2) value = decoder.ctl(dec, ctl.get_bandwidth) self.assertEqual(value, 0) decoder.destroy(dec)
def test_get_final_range(self): dec = decoder.create(48000, 2) decoder.ctl(dec, ctl.get_final_range) decoder.destroy(dec)
RATE = 48000 INPUT_BLOCK_TIME = 0.02 INPUT_FRAMES_PER_BLOCK = int(RATE * INPUT_BLOCK_TIME) UDP_IP = "" UDP_PORT = 8001 MAX_BUFF_SIZE = 5 sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) sock.bind(("", UDP_PORT)) audio = pyaudio.PyAudio() streamout = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=INPUT_FRAMES_PER_BLOCK * 5) dec = decoder.create(RATE, CHANNELS) buf = [] # heapq.heapify(buf) # heapq._heapify_max(buf) a = recvAndDecode(sock, dec, buf) a.daemon = True b = playOut(streamout, buf) b.daemon = True a.start() b.start() while True: time.sleep(1)
def test_reset_state(self): dec = decoder.create(48000, 2) decoder.ctl(dec, ctl.reset_state) decoder.destroy(dec)