def test_s32p_inspection(self): fmt = AudioFormat('s32p') self.assertEqual(fmt.name, 's32p') self.assertTrue(fmt.is_planar) self.assertEqual(fmt.bits, 32) self.assertEqual(fmt.bytes, 4) self.assertRaises(ValueError, lambda: fmt.container_name)
def test_s16_inspection(self): fmt = AudioFormat('s16') self.assertEqual(fmt.name, 's16') self.assertFalse(fmt.is_planar) self.assertEqual(fmt.bits, 16) self.assertEqual(fmt.bytes, 2) self.assertEqual(fmt.container_name, 's16' + postfix) self.assertEqual(fmt.planar.name, 's16p') self.assertIs(fmt.packed, fmt)
def init_audio_sink(self): print("audit") self.pa = pyaudio.PyAudio() self.sink = self.pa.open(format=self.pa.get_format_from_width(2), channels=2, rate=44100, output=True) codec = None extradata = None if self.audio_format == Audio.AudioFormat.ALAC_44100_16_2.value: extradata = bytes([ # Offset 0x00000000 to 0x00000035 0x00, 0x00, 0x00, 0x24, 0x61, 0x6c, 0x61, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x60, 0x00, 0x10, 0x28, 0x0a, 0x0e, 0x02, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x44 ]) codec = av.codec.Codec('alac', 'r') elif self.audio_format == Audio.AudioFormat.AAC_LC_44100_2.value: codec = av.codec.Codec('aac', 'r') if codec is not None: self.codecContext = av.codec.CodecContext.create(codec) self.codecContext.sample_rate = 44100 self.codecContext.channels = 2 self.codecContext.format = AudioFormat('s16p') if extradata is not None: self.codecContext.extradata = extradata self.resampler = av.AudioResampler( format=av.AudioFormat('s16').packed, layout='stereo', rate=44100, )
def init_audio_sink(self): codecLatencySec = 0 self.pa = pyaudio.PyAudio() self.sink = self.pa.open(format=self.pa.get_format_from_width(2), channels=self.channel_count, rate=self.sample_rate, output=True) #nice Python3 crash if we don't check self.sink is null. Not harmful, but should check. if not self.sink: exit() # codec = None extradata = None if self.audio_format == Audio.AudioFormat.ALAC_44100_16_2.value: extradata = self.set_alac_extradata(self, 44100, 16, 2) elif self.audio_format == Audio.AudioFormat.ALAC_44100_24_2.value: extradata = self.set_alac_extradata(self, 44100, 24, 2) elif self.audio_format == Audio.AudioFormat.ALAC_48000_16_2.value: extradata = self.set_alac_extradata(self, 48000, 16, 2) elif self.audio_format == Audio.AudioFormat.ALAC_48000_24_2.value: extradata = self.set_alac_extradata(self, 48000, 24, 2) if 'ALAC' in self.af: self.codec = av.codec.Codec('alac', 'r') elif 'AAC' in self.af: self.codec = av.codec.Codec('aac', 'r') elif 'OPUS' in self.af: self.codec = av.codec.Codec('opus', 'r') #PCM - not sure which. Easy to fix. elif 'PCM' and '_16_' in self.af: self.codec = av.codec.Codec('pcm_s16be_planar', 'r') elif 'PCM' and '_24_' in self.af: self.codec = av.codec.Codec('pcm_s24be', 'r') """ #It seems that these are not required. if 'ELD' in self.af: codecLatencySec = (2017 / self.sample_rate) elif'AAC_LC'in self.af: codecLatencySec = (2624 / self.sample_rate) codecLatencySec = 0 print('codecLatencySec:',codecLatencySec) """ if self.codec is not None: self.codecContext = av.codec.CodecContext.create(self.codec) self.codecContext.sample_rate = self.sample_rate self.codecContext.channels = self.channel_count self.codecContext.format = AudioFormat('s' + str(self.sample_size) + 'p') if extradata is not None: self.codecContext.extradata = extradata self.resampler = av.AudioResampler( format=av.AudioFormat('s' + str(self.sample_size)).packed, layout='stereo', rate=self.sample_rate, ) audioDevicelatency = \ self.pa.get_default_output_device_info()['defaultHighOutputLatency'] #defaultLowOutputLatency is also available print(f"audioDevicelatency (sec): {audioDevicelatency}") pyAudioDelay = self.sink.get_output_latency() print(f"pyAudioDelay (sec): {pyAudioDelay}") ptpDelay = 0.002 self.sample_delay = pyAudioDelay + audioDevicelatency + codecLatencySec + ptpDelay print(f"sample_delay (sec): {self.sample_delay:0.5f}")
from av.audio.format import AudioFormat from av.audio.layout import AudioLayout from av.audio.resampler import AudioResampler import udp_ep import replier addr = '0.0.0.0' port = 8080 logger = logging.getLogger('http_ep') pcs = set() datagram_endpoint = udp_ep.Endpoint() target_audio_format = AudioFormat('s16') target_audio_layout = AudioLayout('mono') target_sample_rate = 44100 resampler = AudioResampler(target_audio_format, target_audio_layout, target_sample_rate) async def handler(request): uri = str(request.rel_url) logger.info('Request: {}'.format(uri)) if uri == '/start': udp_ep.status = "start" return web.Response(text=udp_ep.status) elif uri == '/stop': udp_ep.status = "stop" return web.Response(text=udp_ep.status)