From 270ab6799b4b37d64e92fcfbc46f64aa930e5857 Mon Sep 17 00:00:00 2001 From: 0x01fe <0x01fe@0x01fe.net> Date: Thu, 16 Jan 2025 22:44:04 -0600 Subject: [PATCH] still can't get audio to play properly --- SubSonic.py | 18 +++++++++++- main.py | 67 +++++++++++++++++++++++++++++++++---------- responses/echidna.xml | 15 ++++++++++ 3 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 responses/echidna.xml diff --git a/SubSonic.py b/SubSonic.py index cbbef87..5a876c7 100644 --- a/SubSonic.py +++ b/SubSonic.py @@ -232,7 +232,23 @@ class SubpyConn: payload['id'] = song_id - return requests.get(url + self._encodePayload(payload)) + return requests.get(url + self._encodePayload(payload), stream=True) + + def getSong(self, song_id: str) -> Song: + url = self.base_url + '/rest/getSong?' + + payload = self._getBasePayload() + payload['id'] = song_id + + r = requests.get(url + self._encodePayload(payload)) + + xml_data = r.content.decode('utf-8') + + root = xml.etree.ElementTree.fromstring(xml_data) + + for item in root.iter(): + if item.tag == '{http://subsonic.org/restapi}song': + return Song(dict(item.items())) conn = SubpyConn(u, p, "https://music.0x01fe.net") diff --git a/main.py b/main.py index 79561e8..52261be 100644 --- a/main.py +++ b/main.py @@ -5,10 +5,13 @@ # loop = urwid.MainLoop(fill) # loop.run() +import subprocess import wave +import io import SubSonic import pyaudio +import pydub u = 'pseudo' p = 'telegram9dock-MICHEL' @@ -19,28 +22,62 @@ echidna_id = "937004317800a879f0cddd471fb82387" def play_song(song_id: str): r = conn.stream(song_id) - # print(len(r.content)) + p = pyaudio.PyAudio() - stream_data: bytes = r.content + # ogg_data = io.BytesIO(r.content) + # audio = pydub.AudioSegment.from_ogg(ogg_data) + # # audio.set_sample_width(2) + # print("channels", audio.channels) + # print("rate", audio.frame_rate) - def callback(in_data, frame_count, time_info, status): + stream = p.open(format=pyaudio.paInt16, + channels=2, + rate=44100, + output=True) - return (stream_data, pyaudio.paContinue) + ffmpeg_process = subprocess.Popen( + ['ffmpeg', '-i', 'pipe:0', '-f', 'wav', '-ac', '2', '-ar', '44100', '-'], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + + for chunk in r.iter_content(chunk_size=1024): + if chunk: + # print('b') + ffmpeg_process.stdin.write(chunk) + + ffmpeg_process.stdin.close() + + while True: + pcm_data = ffmpeg_process.stdout.read(1024) + print('a') + if not pcm_data: + break + + stream.write(pcm_data) + # for chunk in r.iter_content(chunk_size=1024): + # if chunk: + # ogg_data = io.BytesIO(chunk) + # audio = pydub.AudioSegment.from_ogg(ogg_data) + # audio = audio.set_sample_width(2) + # stream.write(audio.raw_data) - audio = pyaudio.PyAudio() - - stream = audio.open(format=audio.get_format_from_width(2), - channels=2, - rate=44100, - output=True, - stream_callback=callback) - - # r.content. - - # def stream_callback(in_data) + # stream.write(audio.raw_data) + stream.stop_stream() + stream.close() + p.terminate() + ffmpeg_process.wait() play_song(echidna_id) + +# with open('echidna.xml', 'wb') as file: +# file.write(conn.getSong(echidna_id).content) + +# echidna = conn.getSong(echidna_id) + +# print(echidna.title) + +# conn.stream(echidna_id) \ No newline at end of file diff --git a/responses/echidna.xml b/responses/echidna.xml new file mode 100644 index 0000000..b05c648 --- /dev/null +++ b/responses/echidna.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file