shithub: soundpipe

Download patch

ref: 3a3b446c59acabc34e39d73af846ecb37a8dff77
parent: 7939860c62433745bc57f9077c1266edd2174e2a
author: Paul Batchelor <thisispaulbatchelor@gmail.com>
date: Sun Feb 7 03:03:08 EST 2021

re-introduced sp_ftbl_bind, with fixes to go along with it

--- a/h/ftbl.h
+++ b/h/ftbl.h
@@ -4,6 +4,7 @@
 typedef struct sp_ftbl{
     size_t size;
     SPFLOAT *tbl;
+    unsigned char del;
 } sp_ftbl;
 
 int sp_ftbl_create(sp_data *sp, sp_ftbl **ft, size_t size);
--- a/lib/spa/spa.c
+++ b/lib/spa/spa.c
@@ -77,15 +77,12 @@
     sp_audio spa;
     size_t size;
 
-    *ft = malloc(sizeof(sp_ftbl));
-    ftp = *ft;
 
     spa_open(sp, &spa, filename, SPA_READ);
 
     size = spa.header.len;
-
-    ftp->tbl = malloc(sizeof(SPFLOAT) * (size + 1));
-    ftp->size = size;
+    sp_ftbl_create(sp, ft, size);
+    ftp = *ft;
 
     spa_read_buf(sp, &spa, ftp->tbl, ftp->size);
     spa_close(&spa);
--- a/modules/ftbl.c
+++ b/modules/ftbl.c
@@ -21,13 +21,27 @@
     ftp = *ft;
     ftp->tbl = calloc(1, sizeof(SPFLOAT) * (size + 1));
     ftp->size = size;
+    ftp->del = 1;
     return SP_OK;
 }
 
+/* like create, but use externally managed memory */
+
+int sp_ftbl_bind(sp_data *sp, sp_ftbl **ft, SPFLOAT *tbl, size_t size)
+{
+    sp_ftbl *ftp;
+    *ft = malloc(sizeof(sp_ftbl));
+    ftp = *ft;
+    ftp->tbl = tbl;
+    ftp->size = size;
+    ftp->del = 0;
+    return SP_OK;
+}
+
 int sp_ftbl_destroy(sp_ftbl **ft)
 {
     sp_ftbl *ftp = *ft;
-    free(ftp->tbl);
+    if (ftp->del) free(ftp->tbl);
     free(*ft);
     return SP_OK;
 }
--- a/modules/loadwav.c
+++ b/modules/loadwav.c
@@ -15,11 +15,13 @@
     if (!sp_drwav_init_file(wav, filename)) return SP_NOT_OK;
 
     size = sp_drwav_sampcount(wav);
-    *ft = malloc(sizeof(sp_ftbl));
+
+    sp_ftbl_create(sp, ft, size);
+
     ftp = *ft;
-    ftp->tbl = calloc(1, sizeof(SPFLOAT) * size);
-    ftp->size = size;
+
     tbl = ftp->tbl;
+
     sp_drwav_read_f32(wav, size, tbl);
     sp_drwav_uninit(wav);
     free(wav);