diff --git a/README-tmpl.md b/README-tmpl.md index 973e89a..e67605f 100644 --- a/README-tmpl.md +++ b/README-tmpl.md @@ -193,6 +193,61 @@ pip install ggwave More info: https://pypi.org/project/ggwave/ +#### Usage + +* Encode and transmit data with sound: + +```python +import ggwave +import pyaudio + +p = pyaudio.PyAudio() + +# generate audio waveform for string "hello python" +waveform = ggwave.encode("hello python", protocolId = 1, volume = 20) + +print("Transmitting text 'hello python' ...") +stream = p.open(format=pyaudio.paFloat32, channels=1, rate=48000, output=True, frames_per_buffer=4096) +stream.write(waveform, len(waveform)//4) +stream.stop_stream() +stream.close() + +p.terminate() +``` + +* Capture and decode audio data: + +```python +import ggwave +import pyaudio + +p = pyaudio.PyAudio() + +stream = p.open(format=pyaudio.paFloat32, channels=1, rate=48000, input=True, frames_per_buffer=1024) + +print('Listening ... Press Ctrl+C to stop') +instance = ggwave.init() + +try: + while True: + data = stream.read(1024, exception_on_overflow=False) + res = ggwave.decode(instance, data) + if (not res is None): + try: + print('Received text: ' + res.decode("utf-8")) + except: + pass +except KeyboardInterrupt: + pass + +ggwave.free(instance) + +stream.stop_stream() +stream.close() + +p.terminate() +``` + ### Node.js ```bash diff --git a/README.md b/README.md index 118def3..102f3d4 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,20 @@ [![pypi](https://img.shields.io/pypi/v/ggwave.svg)](https://pypi.org/project/ggwave/) [![npm](https://img.shields.io/npm/v/ggwave.svg)](https://www.npmjs.com/package/ggwave/) +## Installation + +You can install the package directly from GitHub: + +```bash +pip install git+https://github.com/not-lain/ggwave.git@python#subdirectory=bindings/python +``` +or you can run it using uv +``` +uv add "ggwave @ git+https://github.com/not-lain/ggwave.git@python#subdirectory=bindings/python" +``` +to use this package with python, check out the [Usage](#usage-1) section. + + Tiny data-over-sound library. Click on the images below to hear what it sounds like: @@ -195,6 +209,61 @@ pip install ggwave More info: https://pypi.org/project/ggwave/ +#### Usage + +* Encode and transmit data with sound: + +```python +import ggwave +import pyaudio + +p = pyaudio.PyAudio() + +# generate audio waveform for string "hello python" +waveform = ggwave.encode("hello python", protocolId = 1, volume = 20) + +print("Transmitting text 'hello python' ...") +stream = p.open(format=pyaudio.paFloat32, channels=1, rate=48000, output=True, frames_per_buffer=4096) +stream.write(waveform, len(waveform)//4) +stream.stop_stream() +stream.close() + +p.terminate() +``` + +* Capture and decode audio data: + +```python +import ggwave +import pyaudio + +p = pyaudio.PyAudio() + +stream = p.open(format=pyaudio.paFloat32, channels=1, rate=48000, input=True, frames_per_buffer=1024) + +print('Listening ... Press Ctrl+C to stop') +instance = ggwave.init() + +try: + while True: + data = stream.read(1024, exception_on_overflow=False) + res = ggwave.decode(instance, data) + if (not res is None): + try: + print('Received text: ' + res.decode("utf-8")) + except: + pass +except KeyboardInterrupt: + pass + +ggwave.free(instance) + +stream.stop_stream() +stream.close() + +p.terminate() +``` + ### Node.js ```bash diff --git a/bindings/python/.gitignore b/bindings/python/.gitignore index 9044a01..f55db96 100644 --- a/bindings/python/.gitignore +++ b/bindings/python/.gitignore @@ -1,6 +1,8 @@ ggwave.so README.rst ggwave.bycython.cpp +ggwave.cpp +*.pyd ggwave.cpython-*-x86_64-linux-gnu.so ggwave/ ggwave.egg-info/ diff --git a/bindings/python/pyproject.toml b/bindings/python/pyproject.toml new file mode 100644 index 0000000..c07b1d3 --- /dev/null +++ b/bindings/python/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools", "Cython"] +build-backend = "setuptools.build_meta" diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 0101603..56d003a 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -1,6 +1,8 @@ from setuptools import setup, Extension from codecs import open import os +import sys +import shutil cmdclass = {} long_description = "" @@ -10,6 +12,9 @@ # User has to set environment variable GGWAVE_USE_CYTHON. # e.g.: GGWAVE_USE_CYTHON=1 python setup.py install USE_CYTHON = os.getenv('GGWAVE_USE_CYTHON', False) +if not os.path.exists("ggwave.bycython.cpp"): + USE_CYTHON = True + if USE_CYTHON: from Cython.Build import build_ext ggwave_module_src = "ggwave.pyx" @@ -22,8 +27,33 @@ OMIT_README_RST = os.getenv('GGWAVE_OMIT_README_RST', False) if not OMIT_README_RST: here = os.path.abspath(os.path.dirname(__file__)) - with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f: - long_description = f.read() + readme_rst_path = os.path.join(here, 'README.rst') + if os.path.exists(readme_rst_path): + with open(readme_rst_path, encoding='utf-8') as f: + long_description = f.read() + else: + print("WARNING: README.rst undefined") + +# Prepare sources +if not os.path.exists("ggwave"): + os.makedirs("ggwave") + +if not os.path.exists("ggwave/src"): + if os.path.exists("../../src"): + shutil.copytree("../../src", "ggwave/src") + else: + print("WARNING: ../../src undefined") + +if not os.path.exists("ggwave/include"): + if os.path.exists("../../include"): + shutil.copytree("../../include", "ggwave/include") + else: + print("WARNING: ../../include undefined") + +# Compile args +compile_args = ["-O3", "-std=c++11"] +if sys.platform == 'win32': + compile_args = ["/O2"] setup( # Information @@ -42,6 +72,6 @@ include_dirs=["ggwave/include", "ggwave/include/ggwave"], depends=["ggwave/include/ggwave/ggwave.h"], language="c++", - extra_compile_args=["-O3", "-std=c++11"])], + extra_compile_args=compile_args)], cmdclass = cmdclass )