Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions README-tmpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
3 changes: 3 additions & 0 deletions bindings/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "Cython"]
build-backend = "setuptools.build_meta"
36 changes: 33 additions & 3 deletions bindings/python/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from setuptools import setup, Extension
from codecs import open
import os
import sys
import shutil

cmdclass = {}
long_description = ""
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
)