Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python AudioDataStream.read_data should not modify immutable bytes object #2337

Open
msehnout opened this issue Apr 11, 2024 · 3 comments
Open
Assignees
Labels
enhancement New feature or request text-to-speech Text-to-Speech update needed For items that are in progress but have not been updated

Comments

@msehnout
Copy link

Hello

This sample can lead to subtle bugs:

filled_size = audio_data_stream.read_data(audio_buffer)

            audio_buffer = bytes(16000)
            total_size = 0
            filled_size = audio_data_stream.read_data(audio_buffer)
            while filled_size > 0:
                print("{} bytes received.".format(filled_size))
                total_size += filled_size
                filled_size = audio_data_stream.read_data(audio_buffer)

The problem is that bytes type is immutable in Python, but Speech SDK uses native C library and it modifies the immutable type: https://docs.python.org/3/library/stdtypes.html#bytes-objects

I stumbled upon the bug when I tried to accumulate the buffer in a separate function like this:

buffer = b""
for chunk in stream_from_azure:
   buffer += chunk

where the iterator was implemented like this:

            audio_buffer = bytes(16000)
            while filled_size > 0:
                filled_size = audio_data_stream.read_data(audio_buffer)
                yield audio_buffer[:filled_size]

And the result was corrupted.

The SDK should probably take bytearray instead: https://docs.python.org/3/library/stdtypes.html#bytearray-objects
Because it is a mutable counterpart to bytes objects.

I could not find a better place to report this issue. Please let me know if I can submit it somewhere else.

@yulin-li
Copy link
Contributor

Thanks for this issue. I agree with you that we should not modify this immutable object. We will discuss internally to update this as we don't want to introducing breaking changes at this time

@yulin-li yulin-li self-assigned this Apr 14, 2024
@yulin-li yulin-li added enhancement New feature or request text-to-speech Text-to-Speech labels Apr 14, 2024
@yulin-li
Copy link
Contributor

And this is the right place to report this issue, this is the official Speech SDK repo

Copy link

github-actions bot commented May 6, 2024

This item has been open without activity for 19 days. Provide a comment on status and remove "update needed" label.

@github-actions github-actions bot added the update needed For items that are in progress but have not been updated label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request text-to-speech Text-to-Speech update needed For items that are in progress but have not been updated
Projects
None yet
Development

No branches or pull requests

2 participants