4. sqlar_wavread

CLOSED: [2020-10-07 Wed 12:21] Reads from wav file via drwav, and loads it into an existing ftable (without allocating).

This was created for the sole purpose of loading/unloading wavetables.

4.1. sqlar-wavread in C

CLOSED: [2020-01-06 Mon 10:53] There are two functions here. sqlar_wavread is the low-level version, that only requires a DB handle and a ftable. The high-level (simple) version opens the DB handle and finds the ftable internally; all that is needed are the db filename, wav filename, and keyword.


<<funcdefs>>=
int sqlar_wavread(sqlite3 *db,
                  const char *name,
                  sp_ftbl *ft);
int sqlar_wavread_simple(monolith_d *m,
                         const char *db,
                         const char *wav,
                         const char *ftname);
<<functions>>=
int sqlar_wavread(sqlite3 *db,
                  const char *name,
                  sp_ftbl *ft)
{
    char *buf;
    int sz;
    drwav wav;
    int rc;

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

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

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

    drwav_read_f32(&wav, ft->size, ft->tbl);
    drwav_uninit(&wav);
    return MONOLITH_OK;
}
<<functions>>=
int sqlar_wavread_simple(monolith_d *m,
                         const char *sqlar,
                         const char *wav,
                         const char *ftname)
{
    int rc;
    sqlite3 *db;
    sp_ftbl *ft;

    /* Open database */

    if (access(sqlar, F_OK) == -1) {
        fprintf(stderr,
                "SQLar: could not find db %s\n",
                sqlar);
        return MONOLITH_NOTOK;
    }

    rc = sqlite3_open(sqlar, &db);

    if (rc) {
        fprintf(stderr, "SQLar: %s\n", sqlite3_errmsg(db));
        return MONOLITH_NOTOK;
    }

    /* find ftable */
    rc = monolith_ftbl_get(m, ftname, strlen(ftname), &ft);

    if (rc != MONOLITH_OK) {
        fprintf(stderr,
                "Could not find table %s\n",
                ftname);
        sqlite3_close(db);
        return MONOLITH_NOTOK;
    }

    rc = sqlar_wavread(db, wav, ft);

    sqlite3_close(db);
    return rc;
}

4.2. sqlar wavread in scheme

CLOSED: [2020-10-07 Wed 12:21] Known as monolith:sqlar-wavread.

Loaded as s9_load_sqlar_wavread.

<<funcdefs>>=
void s9_load_sqlar_wavread(void);
<<functions>>=
<<scheme_functions>>
void s9_load_sqlar_wavread(void)
{
    S9_PRIM primitives[] = {
<<scheme_entries>>
        {NULL}
    };
    monolith_scheme_add_primitives("monolith", primitives);
}
<<scheme_entries>>=
{
    "monolith:sqlar-wavread",
    pp_sqlar_wavread, 3, 3,
    {S9_T_STRING, S9_T_STRING, S9_T_STRING}
},
<<scheme_functions>>=
static s9_cell pp_sqlar_wavread(s9_cell x)
{
    const char *db;
    const char *wav;
    const char *ftname;
    monolith_d *m;

    m = monolith_data_get();

    db = monolith_scheme_string(s9_car(x));
    x = s9_cdr(x);
    wav = monolith_scheme_string(s9_car(x));
    x = s9_cdr(x);
    ftname = monolith_scheme_string(s9_car(x));

    sqlar_wavread_simple(m, db, wav, ftname);
    return S9_UNSPECIFIC;
}



prev | home | next