Skip to content

Commit

Permalink
feat(cli): play audio(windows only)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyapx committed May 20, 2022
1 parent ac409b1 commit 3b037bf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**支持功能**
- 非阻塞异步处理
- 完整的silk/pcm转换
- 部分支持wav to pcm
- 支持wav to pcm
- 跨平台

# 环境准备
Expand Down Expand Up @@ -55,4 +55,8 @@ python3 -m pysilk --sample-rate 24000 input.wav output.pcm
# pcm/wav转silk时,比特率默认设置为24000
# 你可以添加-r选项来修改它
python3 -m pysilk -r 24000 input.wav output.silk
```

# 额外内容
# pcm/wav/silk播放器
python3 -m pysilk -p input.silk
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from setuptools import Extension
from distutils.command.build_ext import build_ext

__version__ = "1.5.0"
__version__ = "1.6.0"
basic_dependency = ["pybind11", "setuptools"]

if version_info.major != 3 or version_info.minor < 6:
Expand Down
34 changes: 22 additions & 12 deletions src/pysilk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
import time

from . import encode_file, decode_file
from .utils import get_file
from .utils import get_file, play_audio
from .wav import Wave

parser = argparse.ArgumentParser("pysilk", description="encode/decode your silk file")
parser.add_argument("-r", "--rate", default=24000, type=int, help="set silk output datarate")
parser.add_argument("-q", "--quiet", action="store_const", const=bool, default=False, help="reduce console output")
parser.add_argument("-p", "--play", action="store_true", help="play input file")
parser.add_argument("-q", "--quiet", action="store_true", help="reduce console output")
parser.add_argument("--sample-rate", default=24000, type=int, help="set pcm output samplerate")
parser.add_argument("input", action="store", type=str, help="input file path")
parser.add_argument("output", action="store", type=str, help="output file path")
parser.add_argument("output", help="output file path", nargs="?", default="")


def get_suffix(path: str):
sp = path.rsplit(".", 1)
if len(sp) == 1:
raise ValueError("cannot parse suffix")
elif sp[1] not in ("wav", "pcm", "silk"):
raise TypeError("%s format not supported" % sp[1])
else:
return sp[1]
def get_suffix(path: str) -> str:
if path:
sp = path.rsplit(".", 1)
if len(sp) == 1:
raise ValueError("cannot parse suffix")
elif sp[1] not in ("wav", "pcm", "silk"):
raise TypeError("%s format not supported" % sp[1])
else:
return sp[1]


def log(*content_args):
Expand All @@ -32,7 +34,15 @@ def log(*content_args):
st = time.time()
args = parser.parse_args()
i_suffix, o_suffix = get_suffix(args.input), get_suffix(args.output)
if i_suffix == o_suffix:
if args.play:
log("playing:", args.input)
if i_suffix == "wav":
play_audio(args.input)
elif i_suffix == "pcm":
play_audio(Wave.pcm2wav(get_file(args.input), args.sample_rate))
elif i_suffix == "silk":
play_audio(decode_file(args.input, to_wav=True))
elif i_suffix == o_suffix or not o_suffix:
log("nothing can do.")
elif i_suffix == "pcm" and not args.sample_rate:
raise ValueError("--sample-rate must be set")
Expand Down
32 changes: 32 additions & 0 deletions src/pysilk/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from pathlib import Path
from typing import BinaryIO, Union

Expand All @@ -19,3 +20,34 @@ def get_file(file: Union[str, BinaryIO]) -> BinaryIO:
return file
else:
raise TypeError(file)


def force_quit():
import os
import multiprocessing
os.kill(multiprocessing.current_process().pid, 15) # sigterm


def _play_sound(source: Union[str, bytes]):
import winsound
from threading import Thread

t = Thread(
target=winsound.PlaySound,
name="PlayerThread",
args=(source, winsound.SND_FILENAME if isinstance(source, str) else winsound.SND_MEMORY),
)
t.start()
try:
while True:
t.join(0.5)
except KeyboardInterrupt:
print("Interrupt received")
force_quit()


def play_audio(source: Union[str, bytes]):
if sys.platform != "win32":
raise RuntimeError("PlaySound only support windows")

_play_sound(source)

0 comments on commit 3b037bf

Please sign in to comment.