From 7d567a206856d51a47af3105a410977779a7f3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=A9=20Cassiop=C3=A9e=20Gauthier?= Date: Fri, 8 Nov 2024 16:06:31 -0500 Subject: [PATCH] Initial commit, a microphone --- .gitignore | 2 + LICENSE.txt | 9 ++++ README.md | 21 ++++++++ pyproject.toml | 64 +++++++++++++++++++++++++ src/voice_lab/__about__.py | 7 +++ src/voice_lab/__init__.py | 9 ++++ src/voice_lab/main.py | 98 ++++++++++++++++++++++++++++++++++++++ tests/__init__.py | 3 ++ 8 files changed, 213 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 src/voice_lab/__about__.py create mode 100644 src/voice_lab/__init__.py create mode 100644 src/voice_lab/main.py create mode 100644 tests/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac8d7b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +dist diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..35dbc77 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024-present Zoé Cassiopée Gauthier + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a08dbc --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Voice Lab + +[![PyPI - Version](https://img.shields.io/pypi/v/voice-lab.svg)](https://pypi.org/project/voice-lab) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/voice-lab.svg)](https://pypi.org/project/voice-lab) + +----- + +## Table of Contents + +- [Installation](#installation) +- [License](#license) + +## Installation + +```console +pip install voice-lab +``` + +## License + +`voice-lab` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..729fa02 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,64 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "voice-lab" +dynamic = ["version"] +description = '' +readme = "README.md" +requires-python = ">=3.8" +license = "MIT" +keywords = [] +authors = [ + { name = "Zoé Cassiopée Gauthier", email = "zoe.gauthier@blorp.dev" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +dependencies = [ + "matplotlib", + "numpy", + "sounddevice", + "FreeSimpleGUI" +] + +[project.urls] +Source = "https://git.blorp.dev/zo/voice-lab" + +[tool.hatch.version] +path = "src/voice_lab/__about__.py" + +[tool.hatch.envs.types] +extra-dependencies = [ + "mypy>=1.0.0", +] +[tool.hatch.envs.types.scripts] +check = "mypy --install-types --non-interactive {args:src/voice_lab tests}" + +[tool.coverage.run] +source_pkgs = ["voice_lab", "tests"] +branch = true +parallel = true +omit = [ + "src/voice_lab/__about__.py", +] + +[tool.coverage.paths] +voice_lab = ["src/voice_lab", "*/voice-lab/src/voice_lab"] +tests = ["tests", "*/voice-lab/tests"] + +[tool.coverage.report] +exclude_lines = [ + "no cov", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] diff --git a/src/voice_lab/__about__.py b/src/voice_lab/__about__.py new file mode 100644 index 0000000..7284c6b --- /dev/null +++ b/src/voice_lab/__about__.py @@ -0,0 +1,7 @@ +# Copyright 2024 Zoé Cassiopée Gauthier. +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. + +__version__ = "0.0.1" diff --git a/src/voice_lab/__init__.py b/src/voice_lab/__init__.py new file mode 100644 index 0000000..20e1a78 --- /dev/null +++ b/src/voice_lab/__init__.py @@ -0,0 +1,9 @@ +# Copyright 2024 Zoé Cassiopée Gauthier. +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. + +from voice_lab.main import main + +__all__ = ["main"] diff --git a/src/voice_lab/main.py b/src/voice_lab/main.py new file mode 100644 index 0000000..c0f7b1e --- /dev/null +++ b/src/voice_lab/main.py @@ -0,0 +1,98 @@ +# Copyright 2024 Zoé Cassiopée Gauthier. +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. + +import argparse +from functools import partial +import queue +from typing import cast, Any + +from matplotlib.animation import FuncAnimation +import matplotlib.pyplot as plt +import numpy as np +import sounddevice as sd + + +q = queue.Queue() + +window = 200 +downsample = 10 + + +def audio_callback(indata, _frames, _time, status): + if status: + print(status) + q.put(indata[::downsample, 0].copy()) + + +def update_plot(_, lines, plotdata): + while True: + try: + data = q.get_nowait() + except queue.Empty: + break + shift = len(data) + plotdata[:] = np.roll(plotdata, -shift) + plotdata[-shift:] = data + lines[0].set_ydata(plotdata) + return lines + + +def int_or_str(arg): + try: + return int(arg) + except ValueError: + return arg + + +def main(): + parser = argparse.ArgumentParser(add_help=False) + parser.add_argument("-l", "--list-devices", action="store_true") + args, remaining = parser.parse_known_args() + if args.list_devices: + print(sd.query_devices()) + parser.exit(0) + parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, parents=[parser] + ) + parser.add_argument("-d", "--device", type=int_or_str) + args = parser.parse_args(remaining) + + device_info = cast(dict[str, Any], sd.query_devices(args.device, "input")) + samplerate = device_info["default_samplerate"] + + length = int(window * samplerate / (1000 * downsample)) + plotdata = np.zeros((length)) + + fig, ax = plt.subplots() + lines = ax.plot(plotdata) + ax.axis((0, len(plotdata), -1, 1)) + ax.set_yticks([0]) + ax.yaxis.grid(True) + ax.tick_params( + bottom=False, + top=True, + labelbottom=False, + right=False, + left=False, + labelleft=False, + ) + fig.tight_layout(pad=0) + + stream = sd.InputStream( + device=args.device, channels=1, samplerate=samplerate, callback=audio_callback + ) + _ani = FuncAnimation( + fig, + partial(update_plot, lines=lines, plotdata=plotdata), + interval=30, + blit=True, + ) + with stream: + plt.show() + + +if __name__ == "__main__": + main() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..688cc63 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2024-present Zoé Cassiopée Gauthier +# +# SPDX-License-Identifier: MIT