-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2925bec
commit feb9924
Showing
4 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,69 @@ | ||
""" | ||
Code which adds sounddevice lib support for interfaces, recording and playing. | ||
""" | ||
|
||
import logging | ||
import sounddevice as sd | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Interface: | ||
|
||
def __init__(self, config): | ||
self.config = config | ||
bits_per_sample = config.bits_per_sample | ||
assert bits_per_sample == 16 | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, *args): | ||
pass | ||
|
||
def recorder(self): | ||
return Recorder(self) | ||
|
||
def player(self): | ||
return Player(self) | ||
|
||
|
||
class Recorder: | ||
def __init__(self, lib): | ||
self.bufsize = 4096 | ||
|
||
self.audio_stream = sd.RawInputStream( | ||
samplerate=lib.config.Fs, | ||
blocksize=self.bufsize, | ||
channels=1, dtype='int16') | ||
self.audio_stream.start() | ||
|
||
def read(self, size): | ||
data, overflowed = self.audio_stream.read(size) | ||
if overflowed: | ||
raise OverflowError("Overflow reading from audio device") | ||
return data | ||
|
||
def close(self): | ||
self.audio_stream.stop() | ||
self.audio_stream.close() | ||
|
||
|
||
class Player: | ||
def __init__(self, lib): | ||
self.buffer_length_ms = 10 | ||
self.buffer_size = int(lib.config.Fs * (self.buffer_length_ms / 1000)) | ||
|
||
self.audio_stream = sd.RawOutputStream( | ||
samplerate=lib.config.Fs, | ||
blocksize=self.buffer_size, | ||
channels=1, dtype='int16') | ||
|
||
self.audio_stream.start() | ||
|
||
def write(self, data): | ||
self.audio_stream.write(data) | ||
|
||
def close(self): | ||
self.audio_stream.stop() | ||
self.audio_stream.close() |
This file contains 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