Skip to content

Commit f516426

Browse files
committed
Load sound libraries on class init
1 parent 05d1f13 commit f516426

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

mediadecoder/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.3"
1+
__version__ = "0.1.4"
22
__author__ = "Daniel Schreij"
33
__license__ = "MIT"
44

mediadecoder/soundrenderers/pyaudiorenderer.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import time
2-
import pyaudio
3-
41
try:
52
# Python 3
63
from queue import Queue, Empty
@@ -26,8 +23,9 @@ def __init__(self, audioformat, queue=None):
2623
A queue object which serves as a buffer on which the individual
2724
audio frames are placed by the decoder.
2825
"""
29-
if pyaudio is None:
30-
raise RuntimeError("Pyaudio sound renderer is not available")
26+
27+
global pyaudio
28+
import pyaudio
3129

3230
if not queue is None:
3331
self.queue = queue

mediadecoder/soundrenderers/pyaudiorenderer2.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import time
2-
import pyaudio
32
import threading
43

54
try:
@@ -27,9 +26,10 @@ def __init__(self, audioformat, queue=None):
2726
A queue object which serves as a buffer on which the individual
2827
audio frames are placed by the decoder.
2928
"""
30-
if pyaudio is None:
31-
raise RuntimeError("Pyaudio sound renderer is not available")
29+
global pyaudio
30+
import pyaudio
3231

32+
# Init thread
3333
super(SoundrendererPyAudio, self).__init__()
3434

3535
if not queue is None:
@@ -39,7 +39,7 @@ def __init__(self, audioformat, queue=None):
3939
self.stream = self.pa.open(
4040
channels = audioformat["nchannels"],
4141
rate = audioformat["fps"],
42-
# frames_per_buffer = audioformat['buffersize'],
42+
frames_per_buffer = audioformat['buffersize'],
4343
format = pyaudio.get_format_from_width(audioformat["nbytes"]),
4444
output = True,
4545
)

mediadecoder/soundrenderers/pygamerenderer.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import time
22
import threading
3-
import pygame
4-
53
import warnings
6-
warnings.warn("Pygame sound renderer is not working correctly yet. Using the "
7-
"pyaudio renderer is recommended for now.")
84

95
try:
106
# Python 3
@@ -31,10 +27,14 @@ def __init__(self, audioformat, queue=None):
3127
A queue object which serves as a buffer on which the individual
3228
audio frames are placed by the decoder.
3329
"""
30+
global pygame
31+
import pygame
32+
33+
# Init thread
3434
super(SoundrendererPygame, self).__init__()
3535

36-
if pygame is None:
37-
raise RuntimeError("Pygame sound renderer is not available")
36+
warnings.warn("Pygame sound renderer is not working correctly yet. Using the "
37+
"pyaudio renderer is recommended for now.")
3838

3939
if not queue is None:
4040
self.queue = queue
@@ -71,7 +71,7 @@ def run(self):
7171
chunk = None
7272
time.sleep(0.005)
7373

74-
if not channel is None:
74+
if not channel is None and pygame.mixer.get_init():
7575
channel.stop()
7676
pygame.mixer.quit()
7777

mediadecoder/soundrenderers/sounddevicerenderer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import time
2-
import sounddevice as sd
3-
41
try:
52
# Python 3
63
from queue import Queue, Empty
@@ -26,6 +23,9 @@ def __init__(self, audioformat, queue=None):
2623
A queue object which serves as a buffer on which the individual
2724
audio frames are placed by the decoder.
2825
"""
26+
global sd
27+
import sounddevice as sd
28+
2929
if not queue is None:
3030
self.queue = queue
3131

mediadecoder/soundrenderers/sounddevicerenderer2.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import threading
2-
import sounddevice as sd
32
import logging
43
logger = logging.getLogger(__name__)
54

@@ -34,6 +33,9 @@ def __init__(self, audioformat, queue=None):
3433
A queue object which serves as a buffer on which the individual
3534
audio frames are placed by the decoder.
3635
"""
36+
global sd
37+
import sounddevice as sd
38+
3739
super(SoundrendererSounddevice, self).__init__()
3840

3941
if not queue is None:

0 commit comments

Comments
 (0)