-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
6 changed files
with
112 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
from mediadecoder.soundrenderers.pyaudiorenderer import SoundrendererPyAudio | ||
from mediadecoder.soundrenderers.pygamerenderer import SoundrendererPygame | ||
from mediadecoder.soundrenderers.sounddevicerenderer import SoundrendererSounddevice | ||
import warnings | ||
|
||
try: | ||
from mediadecoder.soundrenderers.pyaudiorenderer import SoundrendererPyAudio | ||
except Exception as e: | ||
warnings.warn("Could not import pyaudio sound renderer: {}".format(e)) | ||
|
||
try: | ||
from mediadecoder.soundrenderers.pygamerenderer import SoundrendererPygame | ||
except Exception as e: | ||
warnings.warn("Could not import pygame sound renderer: {}".format(e)) | ||
|
||
try: | ||
from mediadecoder.soundrenderers.sounddevicerenderer import SoundrendererSounddevice | ||
except Exception as e: | ||
warnings.warn("Could not import sounddevice sound renderer: {}".format(e)) | ||
|
||
__all__ = ['SoundrendererPygame', 'SoundrendererPyAudio','SoundrendererSounddevice'] | ||
|
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,73 @@ | ||
|
||
""" This is an alternative implementation of sounddevicerenderer, that doesn't use | ||
the callback functionality of sounddevice's OutputStream. The threading is done by | ||
python, instead of C (under the hood) by sounddevice. I haven't determined yet | ||
which method is better, so I am leaving them both in for now. """ | ||
|
||
|
||
import threading | ||
import sounddevice as sd | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
# Python 3 | ||
from queue import Queue, Empty | ||
except: | ||
# Python 2 | ||
from Queue import Queue, Empty | ||
|
||
from mediadecoder.soundrenderers._base import SoundRenderer | ||
|
||
queue_timeout=0.01 | ||
|
||
class SoundrendererSounddevice(threading.Thread, SoundRenderer): | ||
""" Uses pyaudio to play sound """ | ||
def __init__(self, audioformat, queue=None): | ||
"""Constructor. | ||
Creates a pyaudio sound renderer. | ||
Parameters | ||
---------- | ||
audioformat : dict | ||
A dictionary containing the properties of the audiostream | ||
queue : Queue.queue | ||
A queue object which serves as a buffer on which the individual | ||
audio frames are placed by the decoder. | ||
""" | ||
super(SoundrendererSounddevice, self).__init__() | ||
|
||
if not queue is None: | ||
self.queue = queue | ||
|
||
self.stream = sd.OutputStream( | ||
channels = audioformat["nchannels"], | ||
samplerate = audioformat["fps"], | ||
dtype = 'int{}'.format(audioformat['nbytes']*8), | ||
blocksize = audioformat["buffersize"], | ||
) | ||
|
||
|
||
def run(self): | ||
""" Initializes the stream. """ | ||
if not hasattr(self, 'queue'): | ||
raise RuntimeError("Audio queue is not intialized.") | ||
self.keep_listening = True | ||
self.stream.start() | ||
|
||
while self.keep_listening: | ||
try: | ||
chunk = self.queue.get(timeout=queue_timeout) | ||
underflowed = self.stream.write(chunk) | ||
if underflowed: | ||
logger.debug("Buffer underrun") | ||
except Empty: | ||
pass | ||
|
||
self.stream.stop() | ||
self.stream.close() | ||
|
||
|
||
def close_stream(self): | ||
""" Closes the stream. Performs cleanup. """ | ||
self.keep_listening = False |
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