c, python : add option to query encode size

This commit is contained in:
Georgi Gerganov
2021-01-23 17:13:13 +02:00
parent 0c746c1b15
commit ba8e0cd2b1
8 changed files with 58 additions and 21 deletions

View File

@@ -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,

View File

@@ -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):

View File

@@ -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: