Initial commit, a microphone

This commit is contained in:
Zoé Cassiopée Gauthier 2024-11-08 16:06:31 -05:00
commit 7d567a2068
8 changed files with 213 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__
dist

9
LICENSE.txt Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024-present Zoé Cassiopée Gauthier <hello@blorp.dev>
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.

21
README.md Normal file
View File

@ -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.

64
pyproject.toml Normal file
View File

@ -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:",
]

View File

@ -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"

View File

@ -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"]

98
src/voice_lab/main.py Normal file
View File

@ -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()

3
tests/__init__.py Normal file
View File

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2024-present Zoé Cassiopée Gauthier <hello@blorp.dev>
#
# SPDX-License-Identifier: MIT