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 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_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)
CHANNELS = 1 RATE = 48000 INPUT_BLOCK_TIME = 0.02 INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME) UDP_IP = "2620:0:e50:1400:3c2e:e003:46c4:d73" UDP_PORT = 8000 sock = socket.socket(socket.AF_INET6,socket.SOCK_DGRAM) # sock = socket.socket(socket.AF_INET6,socket.SOCK_STREAM) # sock.connect((UDP_IP,UDP_PORT)) audio = pyaudio.PyAudio() stream = audio.open(format = FORMAT, channels = CHANNELS, rate=RATE, input = True, frames_per_buffer = 5*INPUT_FRAMES_PER_BLOCK) #streamout = audio.open(format = FORMAT, channels = CHANNELS, rate= RATE,output=True, frames_per_buffer = INPUT_FRAMES_PER_BLOCK) enc = encoder.create(RATE,CHANNELS,constants.APPLICATION_VOIP) # disable variable bitrate (VBR) encoder.ctl(enc,opus_ctl.set_vbr,0) # configure expected jitter loss encoder.ctl(enc,opus_ctl.set_packet_loss_perc,2) # configure forward error correction (FEC) encoder.ctl(enc, opus_ctl.set_inband_fec,False) errorCount = 0 i = 0 while(1): try: raw_data = stream.read(INPUT_FRAMES_PER_BLOCK) i+=1 print i
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)