Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Update 0.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
romanin-rf committed Dec 16, 2023
1 parent 0da284f commit 25aa5e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
10 changes: 8 additions & 2 deletions playsoundsimple/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(
dtype=self.__dtype,
device=self.kwargs.get("device")
)
self.__thread = None
self.__volume = volume
self.__cur_frame: int = 0
self.__playing = False
Expand Down Expand Up @@ -237,8 +238,10 @@ def __streaming__(self, mode: int) -> None:
self.sf.seek(0)
self.__cur_frame, mode = 0, mode-1
self.sf.seek(0)
self.__cur_frame, self.__playing, self.__paused = 0, False, False
self.__cur_frame = 0
self.__streamer.stop()
self.__playing = False
self.__paused = False

# ! Control Functions
def get_volume(self) -> float:
Expand Down Expand Up @@ -266,11 +269,14 @@ def unpause(self) -> None:
def play(self, mode: int=1) -> None:
if not self.__playing:
self.__playing = True
Thread(target=self.__streaming__, args=(mode,), daemon=True).start()
self.__thread = Thread(target=self.__streaming__, args=(mode,))
self.__thread.start()

def stop(self) -> None:
if self.__playing:
self.__playing, self.__paused = False, False
if self.__thread is not None:
self.__thread.join()

def wait(self) -> None:
while self.__playing:
Expand Down
17 changes: 5 additions & 12 deletions playsoundsimple/streamers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@ def __init__(
self.samplerate = samplerate
self.channels = channels
self.kwargs = kwargs
self.queue: Queue[T] = Queue(1)
self.running = False

# ! Main Private Method
def __stream__(self) -> None:
pass
# ! Magic Methods

# ! Control Methods
# ! Main Methods
def start(self) -> None:
if not self.running:
self.running = True
Thread(target=self.__stream__).start()
pass

def stop(self) -> None:
if self.running:
self.running = False
pass

def send(self, data: T) -> None:
self.queue.put(data)
pass
37 changes: 28 additions & 9 deletions playsoundsimple/streamers/sds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

# ! Main Class
class SoundDeviceStreamer(StreamerBase[ndarray]):
def __stream__(self) -> None:
with sd.OutputStream(
samplerate=self.samplerate,
channels=self.channels,
**self.kwargs
) as stream:
while self.running:
stream.write(self.queue.get())
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.stream = None
self.running = False

@staticmethod
def get_hostapis() -> List[Dict[str, Any]]:
Expand All @@ -28,4 +24,27 @@ def search_device(hostapi: str) -> int:
for i in SoundDeviceStreamer.get_hostapis():
if i["name"] == hostapi:
return i["default_output_device"]
raise RuntimeError()
raise RuntimeError()

# ! Main Methods
def start(self) -> None:
if not self.running:
self.running = True
self.stream = sd.OutputStream(
samplerate=self.samplerate,
channels=self.channels,
dtype=self.kwargs.get('dtype'),
device=self.kwargs.get('device')
)
self.stream.start()

def stop(self) -> None:
if self.running:
self.running = False
self.stream.stop()
self.stream.close()
self.stream = None

def send(self, data: ndarray) -> None:
if self.running:
self.stream.write(data)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "playsoundsimple-py"
version = "0.8.3"
version = "0.8.4"
description = "A simple library for playing sound files."
authors = ["Romanin <semina054@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 25aa5e0

Please sign in to comment.