import threading
from pydub import AudioSegment
from pydub.playback import play

import io
chunk_size = 4096 * 10


# def gen():
#     with open("tests/hello.mp3", "rb") as f:
#         while True:
#             chunk = f.read(chunk_size)
#             if not chunk:
#                 return
#             logger.debug(f"yielding chunk, with size {len(chunk)}")
#             yield chunk


# byte_stream = list(gen())


# buffer_size = 4096*10
# audio_buffer = io.BytesIO()

# segments = [AudioSegment.from_file(io.BytesIO(chunk), format="mp3") for chunk in byte_stream]
# for seg in segments:
#     logger.debug(f"segment size: {len(seg.raw_data)}")
#     # play_obj = sa.play_buffer(seg.raw_data,
#     #                           num_channels=seg.channels,
#     #                           bytes_per_sample=seg.sample_width,
#     #                           sample_rate=seg.frame_rate)
#     play(seg)
#     # play_obj.wait_done()


with open("tests/dnll.mp3", "rb") as f:
    mp3_bytes = f.read()


mp3_file = io.BytesIO(mp3_bytes)

# Load the MP3 data into an AudioSegment
audio_segment = AudioSegment.from_file(mp3_file, format="mp3")

# Define the slice duration (in milliseconds)
slice_duration = 1000  # 1 second

# Slice and play audio in a loop with threading


def play_slice(start, end):
    slice_segment = audio_segment[start:end]
    play(slice_segment)


start_time = 0
while start_time < len(audio_segment):
    end_time = start_time + slice_duration
    # slice_segment = audio_segment[start_time:end_time]
    # play(slice_segment)

    thread = threading.Thread(target=play_slice, args=(start_time, end_time))
    thread.start()
    import time
    # time.sleep(slice_duration / 1000*0.995)
    thread.join()  # Wait for the slice to finish before starting the next one
    start_time = end_time

# for chunk in byte_stream:
#     # 将每个块写入缓冲区
#     logger.debug(f"get {len(chunk)} bytes, sample:{chunk[:4]} to buffer")

#     audio_buffer.write(chunk)
#     logger.debug(f"buffer size: {audio_buffer.tell()}")

#     # 检查缓冲区大小，如果达到一定大小，则进行播放
#     if audio_buffer.tell() >= buffer_size:  # 例如，每4KB播放一次
#         # 将缓冲区内容转换为 AudioSegment
#         st = time.time()
#         audio_buffer.seek(0)
#         audio_segment = AudioSegment.from_file(audio_buffer, format="mp3")
#         logger.info(f"playing audio segment, with origin size:{audio_buffer.tell()}, size {len(audio_segment.raw_data)}")
#         latency = time.time() - st
#         logger.info(f"convert to audio segment cost {latency} seconds")

#         # 播放音频段
#         play_obj = sa.play_buffer(audio_segment.raw_data,
#                                   num_channels=audio_segment.channels,
#                                   bytes_per_sample=audio_segment.sample_width,
#                                   sample_rate=audio_segment.frame_rate)
#         play_obj.wait_done()

#         # 清空缓冲区
#         audio_buffer = io.BytesIO()

# # 播放剩余部分
# if audio_buffer.tell() > 0:
#     audio_buffer.seek(0)
#     audio_segment = AudioSegment.from_file(audio_buffer, format="mp3")
#     play_obj = sa.play_buffer(audio_segment.raw_data,
#                               num_channels=audio_segment.channels,
#                               bytes_per_sample=audio_segment.sample_width,
#                               sample_rate=audio_segment.frame_rate)
#     play_obj.wait_done()
