Skip to content

Use pyaudio's built-in async api.#645

Merged
theacodes merged 1 commit intomasterfrom
pyaudio
Nov 11, 2016
Merged

Use pyaudio's built-in async api.#645
theacodes merged 1 commit intomasterfrom
pyaudio

Conversation

@jerjou
Copy link
Copy Markdown
Contributor

@jerjou jerjou commented Nov 9, 2016

I discovered, while reviewing #635, that pyaudio has an async api.
This lets us avoid spinning up our own thread, and saves some LOC.
Also - since the callback thread will automatically be closed by pyaudio when the stream is closed, we don't have to check stopaudio in it any longer, which saves us a control object being passed around.

Tested in OSX and linux.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 9, 2016
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add the audio_stream.stop_stream() back.

@puneith
Copy link
Copy Markdown
Contributor

puneith commented Nov 9, 2016

LGTM other than the single comment I had.

if None in data:
stop = True
data.remove(None)
yield b''.join(data)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: blank line above yield.



def _fill_buffer(audio_stream, buff, chunk, stoprequest):
def _fill_buffer(buff):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a closure, just use functools.partial:

def audio_stream_callback(buffer, in_data, frame_count, time_info, status_flags):
    ...

stream_callback=functools.partial(audio_stream_callback, buffer)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh neat.

def record_audio(rate, chunk):
"""Opens a recording stream in a context manager."""
# Create a thread-safe buffer of audio data
buff = queue.Queue()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/buff/buffer? (throughout)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I avoided this since buffer is a keyword

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL.

(It's a built-in function which means it can be shadowed, like file, but I'm fine if you leave it as buff).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, me too. I just avoided it because vim bolded it after I spelled it out, which made me wary :-)

# the `audio_stream` once it's set.
stoprequest = threading.Event()

# For streaming audio from the microphone, there are three threads.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these comments about the number of threads still accurate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! It's just that before we were spinning up the buffer-the-audio thread manually, and now pyaudio is doing it for us.

@jerjou
Copy link
Copy Markdown
Contributor Author

jerjou commented Nov 9, 2016

@dpebot would you please merge when travis becomes green (presumably with envy)?

@dpebot
Copy link
Copy Markdown
Collaborator

dpebot commented Nov 9, 2016

Okay! I'll merge when all statuses are green.

@dpebot dpebot added the automerge Merge the pull request once unit tests and other checks pass. label Nov 9, 2016
@theacodes
Copy link
Copy Markdown
Contributor

@jerjou travis failed here.

@jerjou
Copy link
Copy Markdown
Contributor Author

jerjou commented Nov 11, 2016

Oh right. Tests.
Should be fixed now.

This lets us avoid spinning up our own thread, and saves some LOC.

Tested in OSX and linux.
@jerjou
Copy link
Copy Markdown
Contributor Author

jerjou commented Nov 11, 2016

...i mean, now.
go, dpebot go!

@theacodes theacodes merged commit 5a8e12f into master Nov 11, 2016
@theacodes theacodes deleted the pyaudio branch November 11, 2016 19:10
chalmerlowe pushed a commit that referenced this pull request Apr 7, 2026
* Add GCS file-like io samples

* now with pandas!

* change pandas version

* add to readme

* requests from andrew

* lint

* canonical command line args

* Update storage_fileio_pandas.py

* Update storage_fileio_write_read.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Merge the pull request once unit tests and other checks pass. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants