This repository was archived by the owner on May 6, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Added pyaudio as an input source #2
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,3 +24,5 @@ pytest-cov | |
| pytest-watch | ||
| pytest-mock | ||
| coveralls | ||
| # library | ||
| pyaudio | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| This module uses pyaudio to receive input from a | ||
| device microphone | ||
| """ | ||
| import pyaudio # type: ignore | ||
|
|
||
|
|
||
| class PyAudioMicrophoneInput: | ||
| """ This class retrieves audio from an input device | ||
|
|
||
| :param sample_rate: desired sample rate for input | ||
| :type sample_rate: int | ||
| :param frame_width: desired frame width for input | ||
| :type frame_width: int | ||
| :param exception_on_overflow: produce exception for input overflow | ||
| :type exception_on_overflow: bool | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, sample_rate: int, frame_width: int, exception_on_overflow: bool = True | ||
| ) -> None: | ||
| self._sample_rate = sample_rate | ||
| self._frame_size = int(sample_rate / 1000 * frame_width) | ||
| self._exeception_on_overflow = exception_on_overflow | ||
| self._audio = pyaudio.PyAudio() | ||
| device = self._audio.get_default_input_device_info() | ||
| self._stream = self._audio.open( | ||
| input=True, | ||
| input_device_index=device["index"], | ||
| format=pyaudio.paInt16, | ||
| frames_per_buffer=self._frame_size, | ||
| channels=1, | ||
| rate=self._sample_rate, | ||
| start=False, | ||
| ) | ||
|
|
||
| def read(self) -> bytes: | ||
| """ Reads a single frame of audio | ||
|
|
||
| :return: single frame of audio | ||
| :rtype: bytes | ||
| """ | ||
| return self._stream.read( | ||
| self._frame_size, exception_on_overflow=self._exeception_on_overflow | ||
| ) | ||
|
|
||
| def start(self) -> None: | ||
| """ Starts the audio stream """ | ||
| self._stream.start_stream() | ||
|
|
||
| def stop(self) -> None: | ||
| """ Stops the audio stream """ | ||
| self._stream.stop_stream() | ||
|
|
||
| def close(self) -> None: | ||
| """ Closes the audio stream """ | ||
| self._stream.close() | ||
|
|
||
| @property | ||
| def is_active(self) -> bool: | ||
will-rice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ Stream active property | ||
|
|
||
| :return: 'True' if stream is active, 'False' otherwise | ||
| :rtype: bool | ||
| """ | ||
| return self._stream.is_active() | ||
|
|
||
| @property | ||
| def is_stopped(self) -> bool: | ||
| """ Stream stopped property | ||
|
|
||
| :return: 'True' if stream is stopped, 'False' otherwise | ||
| :rtype: bool | ||
| """ | ||
| return self._stream.is_stopped() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """ | ||
| Mock tests for microphone input | ||
| """ | ||
| from unittest.mock import patch | ||
|
|
||
| from spokestack.mic.pyaudio import PyAudioMicrophoneInput | ||
|
|
||
|
|
||
| @patch("spokestack.mic.pyaudio.pyaudio") | ||
| def test_audio_input(mock_class): | ||
| mic = PyAudioMicrophoneInput(sample_rate=16000, frame_width=10) | ||
| mic._audio.open.assert_called() | ||
| # start audio stream | ||
| mic.start() | ||
| assert mic.is_active | ||
| # read a single frame | ||
| _ = mic.read() | ||
| mic._stream.read.assert_called() | ||
| # stop audio stream | ||
| mic.stop() | ||
| assert mic.is_stopped | ||
| mic.close() |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.