mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-07 01:11:22 +08:00
Add python bindings + package (#10)
* wip : python package * wip : minor fixes * wip : upload package to main pypi * wip : initial text encoding * wip : extending C api * wip : use map of global instances * wip : added decode functionality * update main README
This commit is contained in:
65
bindings/python/ggwave.pyx
Normal file
65
bindings/python/ggwave.pyx
Normal file
@@ -0,0 +1,65 @@
|
||||
cimport cython
|
||||
from cpython.mem cimport PyMem_Malloc, PyMem_Free
|
||||
|
||||
import re
|
||||
import struct
|
||||
|
||||
cimport cggwave
|
||||
|
||||
def defaultParameters():
|
||||
return cggwave.ggwave_defaultParameters()
|
||||
|
||||
def init(parameters = None):
|
||||
if (parameters is None):
|
||||
parameters = defaultParameters()
|
||||
|
||||
return cggwave.ggwave_init(parameters)
|
||||
|
||||
def free(instance):
|
||||
return cggwave.ggwave_free(instance)
|
||||
|
||||
def encode(payload, txProtocol = 1, volume = 10, instance = None):
|
||||
""" Encode payload into an audio waveform.
|
||||
@param {string} payload, the data to be encoded
|
||||
@return Generated audio waveform bytes representing 16-bit signed integer samples.
|
||||
"""
|
||||
|
||||
cdef bytes data_bytes = payload.encode()
|
||||
cdef char* cdata = data_bytes
|
||||
|
||||
cdef bytes output_bytes = bytes(1024*1024)
|
||||
cdef char* coutput = output_bytes
|
||||
|
||||
own = False
|
||||
if (instance is None):
|
||||
own = True
|
||||
instance = init(defaultParameters())
|
||||
|
||||
n = cggwave.ggwave_encode(instance, cdata, len(data_bytes), txProtocol, volume, coutput)
|
||||
|
||||
if (own):
|
||||
free(instance)
|
||||
|
||||
# add short silence at the end
|
||||
n += 16*1024
|
||||
|
||||
return struct.unpack("h"*n, output_bytes[0:2*n])
|
||||
|
||||
def decode(instance, waveform):
|
||||
""" Analyze and decode audio waveform to obtain original payload
|
||||
@param {bytes} waveform, the audio waveform to decode
|
||||
@return The decoded payload if successful.
|
||||
"""
|
||||
|
||||
cdef bytes data_bytes = waveform
|
||||
cdef char* cdata = data_bytes
|
||||
|
||||
cdef bytes output_bytes = bytes(256)
|
||||
cdef char* coutput = output_bytes
|
||||
|
||||
rxDataLength = cggwave.ggwave_decode(instance, cdata, len(data_bytes), coutput)
|
||||
|
||||
if (rxDataLength > 0):
|
||||
return coutput[0:rxDataLength]
|
||||
|
||||
return None
|
||||
Reference in New Issue
Block a user