Skip to content

Reading frames from a raw PCM stream #1185

Closed Answered by pencil
pencil asked this question in 1. Help
Discussion options

You must be logged in to vote

I ended up asking ChatGPT (GPT-4) and to my surprise it came up with code that actually works… What a time to be alive.

sample_rate = 16000  # 16kHz
sample_format = "s16"  # S16LE format
channels = 1  # Mono audio
bytes_per_sample = 2  # 16-bit audio
samples_per_frame = 1024  # Number of samples per frame
frame_size = samples_per_frame * channels * bytes_per_sample  # Total frame size in bytes

while True:
    data = result_stream.read(frame_size)
    if not data:
        break

    # Convert byte data to numpy array
    audio_array = np.frombuffer(data, dtype=np.int16).reshape(-1, channels)

    # Change the shape of the array to be (1, n_samples) for a mono packed format
    audio_array…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by pencil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant