mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-03-20 18:19:25 +08:00
update default parameters + python updates
- by default ggwave will now output 32-bit float samples - python samples no longer use numpy - python test now decodes payload - fix buffer overflow in receive.py example
This commit is contained in:
@@ -134,7 +134,6 @@ Usage
|
|||||||
|
|
||||||
import ggwave
|
import ggwave
|
||||||
import pyaudio
|
import pyaudio
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
p = pyaudio.PyAudio()
|
p = pyaudio.PyAudio()
|
||||||
|
|
||||||
@@ -142,8 +141,8 @@ Usage
|
|||||||
waveform = ggwave.encode("hello python", txProtocol = 1, volume = 20)
|
waveform = ggwave.encode("hello python", txProtocol = 1, volume = 20)
|
||||||
|
|
||||||
print("Transmitting text 'hello python' ...")
|
print("Transmitting text 'hello python' ...")
|
||||||
stream = p.open(format=pyaudio.paInt16, channels=1, rate=48000, output=True, frames_per_buffer=4096)
|
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=48000, output=True, frames_per_buffer=4096)
|
||||||
stream.write(np.array(waveform).astype(np.int16), len(waveform))
|
stream.write(waveform, len(waveform)//4)
|
||||||
stream.stop_stream()
|
stream.stop_stream()
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
@@ -165,7 +164,7 @@ Usage
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = stream.read(1024)
|
data = stream.read(1024, exception_on_overflow=False)
|
||||||
res = ggwave.decode(instance, data)
|
res = ggwave.decode(instance, data)
|
||||||
if (not res is None):
|
if (not res is None):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ README.rst is not commited to git because it is generated from [README-tmpl.rst]
|
|||||||
## Building
|
## Building
|
||||||
|
|
||||||
Run `make build` to generate an extension module as .so file.
|
Run `make build` to generate an extension module as .so file.
|
||||||
You can test it then by importing it from python interpreter `import ggwave` and running `ggwave.testC(...)` (you have to be positioned in the directory where .so was built).
|
You can test it then by importing it from python interpreter `import ggwave` and running `ggwave.encode('test')` (you have to be positioned in the directory where .so was built).
|
||||||
This is useful for testing while developing.
|
This is useful for testing while developing.
|
||||||
|
|
||||||
Run `make sdist` to create a source distribution, but not publish it - it is a tarball in dist/ that will be uploaded to pip on `publish`.
|
Run `make sdist` to create a source distribution, but not publish it - it is a tarball in dist/ that will be uploaded to pip on `publish`.
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ cimport cython
|
|||||||
from cpython.mem cimport PyMem_Malloc, PyMem_Free
|
from cpython.mem cimport PyMem_Malloc, PyMem_Free
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import struct
|
|
||||||
|
|
||||||
cimport cggwave
|
cimport cggwave
|
||||||
|
|
||||||
@@ -43,7 +42,7 @@ def encode(payload, txProtocolId = 1, volume = 10, instance = None):
|
|||||||
if (own):
|
if (own):
|
||||||
free(instance)
|
free(instance)
|
||||||
|
|
||||||
return struct.unpack("h"*n, output_bytes[0:2*n])
|
return output_bytes
|
||||||
|
|
||||||
def decode(instance, waveform):
|
def decode(instance, waveform):
|
||||||
""" Analyze and decode audio waveform to obtain original payload
|
""" Analyze and decode audio waveform to obtain original payload
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ instance = ggwave.init()
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = stream.read(1024)
|
data = stream.read(1024, exception_on_overflow=False)
|
||||||
res = ggwave.decode(instance, data)
|
res = ggwave.decode(instance, data)
|
||||||
if (not res is None):
|
if (not res is None):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import ggwave
|
import ggwave
|
||||||
import pyaudio
|
import pyaudio
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
p = pyaudio.PyAudio()
|
p = pyaudio.PyAudio()
|
||||||
|
|
||||||
@@ -8,9 +7,8 @@ p = pyaudio.PyAudio()
|
|||||||
waveform = ggwave.encode("hello python", txProtocolId = 1, volume = 20)
|
waveform = ggwave.encode("hello python", txProtocolId = 1, volume = 20)
|
||||||
|
|
||||||
print("Transmitting text 'hello python' ...")
|
print("Transmitting text 'hello python' ...")
|
||||||
stream = p.open(format=pyaudio.paInt16, channels=1, rate=48000, output=True, frames_per_buffer=4096)
|
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=48000, output=True, frames_per_buffer=4096)
|
||||||
stream.write(np.array(waveform).astype(np.int16), len(waveform))
|
stream.write(waveform, len(waveform)//4)
|
||||||
stream.write(np.zeros(16*1024), 16*1024) # short silence at the end
|
|
||||||
stream.stop_stream()
|
stream.stop_stream()
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ const GGWave::Parameters & GGWave::getDefaultParameters() {
|
|||||||
GGWave::kBaseSampleRate,
|
GGWave::kBaseSampleRate,
|
||||||
GGWave::kDefaultSamplesPerFrame,
|
GGWave::kDefaultSamplesPerFrame,
|
||||||
GGWAVE_SAMPLE_FORMAT_F32,
|
GGWAVE_SAMPLE_FORMAT_F32,
|
||||||
GGWAVE_SAMPLE_FORMAT_I16
|
GGWAVE_SAMPLE_FORMAT_F32,
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
|
import sys
|
||||||
import ggwave
|
import ggwave
|
||||||
|
|
||||||
instance = ggwave.init()
|
instance = ggwave.init()
|
||||||
|
|
||||||
|
payload = 'hello python'
|
||||||
|
|
||||||
# generate audio waveform for string "hello python"
|
# generate audio waveform for string "hello python"
|
||||||
waveform = ggwave.encode("hello python", txProtocolId = 1, volume = 20, instance = instance)
|
waveform = ggwave.encode(payload, txProtocolId = 1, volume = 20, instance = instance)
|
||||||
|
|
||||||
|
# decode the audio waveform back to text
|
||||||
|
res = ggwave.decode(instance, waveform)
|
||||||
|
|
||||||
|
if res != payload.encode():
|
||||||
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user