2. sqlar_loadwav

2.1. C function

<<funcdefs>>=
int sqlar_loadwav(sp_data *sp,
                  sp_ftbl **ft,
                  const char *db,
                  const char *filename);
<<functions>>=
int sqlar_loadwav(sp_data *sp,
                  sp_ftbl **ft,
                  const char *db,
                  const char *filename)
{
    char *buf;
    int sz;
    drwav wav;
    size_t size;
    SPFLOAT *tbl;
    sp_ftbl *ftp;
    int rc;

    sz = 0;
    buf = NULL;
    rc = sqlar_extract_to_buffer(db, filename, &buf, &sz);

    if (rc != SQLITE_OK) {
        if (buf != NULL) free(buf);
        fprintf(stderr, "SQLite problems\n");
        return SP_NOT_OK;
    }

    if (!drwav_init_memory(&wav, buf, sz)) {
        fprintf(stderr, "drwav could not initialize\n");
        free(buf);
        return SP_NOT_OK;
    }

    size = wav.totalSampleCount;
    sp_ftbl_create(sp, ft, size);
    ftp = *ft;
    tbl = ftp->tbl;
    drwav_read_f32(&wav, size, tbl);
    drwav_uninit(&wav);
    free(buf);
    return SP_OK;
}
<<funcdefs>>=
int sqlar_loadwav_db(sqlite3 *db,
                     const char *filename,
                     sp_ftbl **ft);

sqlar_loadwav_db is just like sqlar_loadwav, except that it takes in a sqlite3 handle rather than a filename. At some point, these functions should be refactored to share the same components.

Also, the soundpipe sp_data component is completely unused when creating these ftables, so I'm axing that from this version and supplying NULL until I get around to cleaning that up.

<<functions>>=
int sqlar_loadwav_db(sqlite3 *db,
                     const char *filename,
                     sp_ftbl **ft)
{
    char *buf;
    int sz;
    drwav wav;
    size_t size;
    SPFLOAT *tbl;
    sp_ftbl *ftp;
    int rc;

    sz = 0;
    buf = NULL;
    rc = sqlar_extract_to_buffer_db(db, filename, &buf, &sz);

    if (rc != SQLITE_OK) {
        if (buf != NULL) free(buf);
        fprintf(stderr, "SQLite problems\n");
        return SP_NOT_OK;
    }

    if (!drwav_init_memory(&wav, buf, sz)) {
        fprintf(stderr, "drwav could not initialize\n");
        free(buf);
        return SP_NOT_OK;
    }

    size = wav.totalSampleCount;
    sp_ftbl_create(NULL, ft, size);
    ftp = *ft;
    tbl = ftp->tbl;
    drwav_read_f32(&wav, size, tbl);
    drwav_uninit(&wav);
    free(buf);
    return SP_OK;
}

2.2. runt word (sqlar_loadwav)

2.2.1. Loader

<<functions>>=
<<proc>>
int load_sqlar_loadwav(runt_vm *vm, runt_ptr pw)
{
    runt_cell *c;
    runt_keyword_define(vm,
                        "sqlar_loadwav",
                        13,
                        rproc_sqlar_loadwav,
                        &c);
    runt_cell_data(vm, c, pw);
    return runt_is_alive(vm);
}

2.2.2. Procedure

<<proc>>=
static runt_int rproc_sqlar_loadwav(runt_vm *vm, runt_ptr p)
{
    sp_ftbl *ft;
    runt_int rc;
    runt_stacklet *s;
    gf_patch *patch;
    const char *filename;
    sqlite3 *db;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    filename = runt_to_string(s->p);
/*
    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    db = runt_to_string(s->p);
*/
    rc = rgf_get_sqlite(vm, &db);
    RUNT_ERROR_CHECK(rc);

    patch = rgf_get_patch(p);

    rc = sqlar_loadwav_db(db, filename, &ft);

    if(rc != SP_OK) {
        runt_print(vm,
                   "Could open not the file %s\n",
                   filename);
        return RUNT_NOT_OK;
    }

    rgf_append_ftable(patch, ft);

    rc = runt_ppush(vm, &s);
    RUNT_ERROR_CHECK(rc);
    rgf_stacklet_ftable(vm, s, ft);

    return RUNT_OK;
}



prev | home | next