mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-07 01:11:22 +08:00
c, python : add option to query encode size
This commit is contained in:
@@ -37,7 +37,8 @@ cdef extern from "ggwave.h" nogil:
|
||||
int dataSize,
|
||||
ggwave_TxProtocolId txProtocolId,
|
||||
int volume,
|
||||
char * outputBuffer);
|
||||
char * outputBuffer,
|
||||
int query);
|
||||
|
||||
int ggwave_decode(
|
||||
ggwave_Instance instance,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
cimport cython
|
||||
|
||||
from cpython.mem cimport PyMem_Malloc, PyMem_Free
|
||||
|
||||
import re
|
||||
@@ -27,22 +28,21 @@ def encode(payload, txProtocolId = 1, volume = 10, instance = None):
|
||||
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(getDefaultParameters())
|
||||
|
||||
n = cggwave.ggwave_encode(instance, cdata, len(data_bytes), txProtocolId, volume, coutput)
|
||||
n = cggwave.ggwave_encode(instance, cdata, len(data_bytes), txProtocolId, volume, NULL, 1)
|
||||
|
||||
cdef bytes output_bytes = bytes(n)
|
||||
cdef char* coutput = output_bytes
|
||||
|
||||
n = cggwave.ggwave_encode(instance, cdata, len(data_bytes), txProtocolId, volume, coutput, 0)
|
||||
|
||||
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):
|
||||
|
||||
@@ -3,9 +3,9 @@ import ggwave
|
||||
|
||||
testFailed = False
|
||||
|
||||
n, samples = ggwave.encode("hello python")
|
||||
samples = ggwave.encode("hello python")
|
||||
|
||||
if not (samples and n > 1024):
|
||||
if not (samples):
|
||||
testFailed = True
|
||||
|
||||
if testFailed:
|
||||
|
||||
Reference in New Issue
Block a user