8. Line16 State Management
This needs to be reworked to save multiple lines. For now, it only saves one line.
8.0.1. Line16 Schema
8.0.1.1. Line16 Schema Generation
A new sliders schema is created and initialized with
the function line16_schema
.
The columns for line data are as follows:
Column 1: Line Data Column 2: Song Map Column 3: minimum duration Column 4: maximum duration Column 5: selected point Column 6: selected line Column 7: global playback rate
static int line16_schema(page_line16_d *line16,
monolith_state_schema **schema);
static int line16_schema(page_line16_d *line16,
monolith_state_schema **schema)
{
monolith_state_schema_init(schema, 7);
monolith_param_mkblob(*schema, 0,
"lines", 5,
NULL, 0, NULL);
monolith_param_mkblob(*schema, 1,
"map", 3,
NULL, 0, NULL);
monolith_param_mkfloat(*schema, 2,
"durmin", 6,
line16->min);
monolith_param_mkfloat(*schema, 3,
"durmax", 6,
line16->max);
monolith_param_mkint(*schema, 4,
"selpt", 5,
line16->selected_point);
monolith_param_mkint(*schema, 5,
"selln", 5,
line16->selected_line);
monolith_param_mkfloat(*schema, 6,
"rate", 6,
line16->rate);
return 1;
}
8.0.1.2. msgpack blobs
This section outlines the various binary blobs that the line16 state saves.
8.0.1.2.1. Lines
The lines blob stores an array of lines. It will only hold one line for now.
static int line16_lines_write(page_line16_d *l,
monolith_state_schema *s);
static int line16_lines_write(page_line16_d *l,
monolith_state_schema *s)
{
cmp_ctx_t cmp;
moncmp_d m;
size_t sz;
uint8_t *buf;
sz = 0;
moncmp_init_getsize(&cmp, &sz);
line16_lines_array_write(l, &cmp);
buf = calloc(1, sz);
moncmp_init_write(&m, &cmp, buf);
line16_lines_array_write(l, &cmp);
monolith_param_setblob_default(s, 0, buf, sz);
return 1;
}
static int line16_lines_read(page_line16_d *l,
monolith_state_schema *s);
static int line16_lines_read(page_line16_d *l,
monolith_state_schema *s)
{
size_t sz;
cmp_ctx_t cmp;
uint8_t *buf;
moncmp_d m;
int rc;
sz = 0;
monolith_param_blob(s, 0, (void **)&buf, (unsigned int *)&sz);
moncmp_init_read(&m, &cmp, buf, sz);
rc = line16_lines_array_read(l, &cmp);
return rc;
}
static int line16_lines_array_write(page_line16_d *l,
cmp_ctx_t *c);
static int line16_lines_array_write(page_line16_d *l,
cmp_ctx_t *c)
{
int rc;
int p;
int nlines;
nlines = 0;
for (p = 0; p < 16; p++) {
if (l->lines[p].nactive > 0) {
nlines++;
}
}
if (!cmp_write_array(c, nlines)) return 0;
for (p = 0; p < 16; p++) {
if (l->lines[p].nactive > 0) {
rc = line16_lines_line_write(c,
p,
l->lines[p].points);
if (!rc) return rc;
}
}
return 1;
}
static int line16_lines_array_read(page_line16_d *l,
cmp_ctx_t *c);
static int line16_lines_array_read(page_line16_d *l,
cmp_ctx_t *c)
{
int rc;
uint32_t sz;
int id;
uint32_t i;
if (!cmp_read_array(c, &sz)) return 0;
for (i = 0; i < sz; i++) {
rc = line16_lines_line_read(c, &id, l->lines);
if (!rc) {
printf("Error reading line\n");
return rc;
}
}
return rc;
}
A line contains an id value (integer) and an array of 16 points.
static int line16_lines_line_write(cmp_ctx_t *c,
int id,
line16_point *pts);
static int line16_lines_line_write(cmp_ctx_t *c,
int id,
line16_point *pts)
{
int p;
int rc;
if (!cmp_write_array(c, 2)) return 0;
if (!cmp_write_u16(c, (uint16_t)id)) return 0;
if (!cmp_write_array(c, 16)) return 0;
for (p = 0; p < 16; p++) {
rc = line16_lines_point_write(c, &pts[p]);
if (!rc) return 0;
}
return 1;
}
static int line16_lines_line_read(cmp_ctx_t *c,
int *id,
line16_line *lines);
static int line16_lines_line_read(cmp_ctx_t *c,
int *id,
line16_line *lines)
{
int p;
int rc;
uint32_t sz;
uint16_t tmp;
line16_point *pts;
if(!cmp_read_array(c, &sz)) return 0;
if(sz != 2) return 0;
*id = -1;
if(!cmp_read_u16(c, &tmp)) return 0;
if (tmp < 0 || tmp >= 16) {
printf("Invalid line id: %d\n", tmp);
return 0;
}
*id = tmp;
pts = lines[tmp].points;
if(!cmp_read_array(c, &sz)) return 0;
if(sz != 16) return 0;
for(p = 0; p < 16; p++) {
rc = line16_lines_point_read(c, &pts[p]);
if(!rc) return 0;
}
return 1;
}
A point has the following values:
- state (int) - value (float) - dur (float) - type (int) - aux1 (float) - aux2 (float)
CLOSED: [2019-08-25 Sun 10:56]
static int line16_lines_point_write(cmp_ctx_t *c,
line16_point *pt);
static int line16_lines_point_write(cmp_ctx_t *c,
line16_point *pt)
{
if (!cmp_write_array(c, 6)) return 0;
if (!cmp_write_int(c, pt->active)) return 0;
if (!cmp_write_float(c, pt->val)) return 0;
if (!cmp_write_float(c, pt->dur)) return 0;
if (!cmp_write_int(c, pt->type)) return 0;
if (!cmp_write_float(c, pt->aux[0])) return 0;
if (!cmp_write_float(c, pt->aux[1])) return 0;
return 1;
}
CLOSED: [2019-08-25 Sun 13:20]
static int line16_lines_point_read(cmp_ctx_t *c,
line16_point *pt);
static int line16_lines_point_read(cmp_ctx_t *c,
line16_point *pt)
{
uint32_t sz;
sz = 0;
if (!cmp_read_array(c, &sz)) return 0;
if (sz != 6) return 0;
if (!cmp_read_int(c, &pt->active)) return 0;
if (!cmp_read_float(c, &pt->val)) return 0;
if (!cmp_read_float(c, &pt->dur)) return 0;
if (!cmp_read_int(c, &pt->type)) return 0;
/* if (pt->type < 0 || pt->type >= 4) { */
/* fprintf(stderr, "Point: invalid type %d\n", */
/* pt->type); */
/* fprintf(stderr, "Setting to default value of 0\n"); */
/* pt->type = 0; */
/* } */
if (!cmp_read_float(c, &pt->aux[0])) return 0;
if (!cmp_read_float(c, &pt->aux[1])) return 0;
return 1;
}
8.0.1.2.2. Songmap
The songmap is an array of ints. For now, it is an array of size one.
CLOSED: [2019-08-25 Sun 11:13]
static int line16_songmap_write(page_line16_d *l,
monolith_state_schema *s);
static int line16_songmap_write(page_line16_d *l,
monolith_state_schema *s)
{
cmp_ctx_t cmp;
moncmp_d m;
size_t sz;
uint8_t *buf;
sz = 0;
moncmp_init_getsize(&cmp, &sz);
line16_songmap_arrayw(l, &cmp);
buf = calloc(1, sz);
moncmp_init_write(&m, &cmp, buf);
line16_songmap_arrayw(l, &cmp);
return 1;
}
static int line16_songmap_arrayw(page_line16_d *l,
cmp_ctx_t *c);
static int line16_songmap_arrayw(page_line16_d *l,
cmp_ctx_t *c)
{
if (!cmp_write_array(c, 1)) return 0;
if (!cmp_write_int(c, 0)) return 0;
return 1;
}
CLOSED: [2019-08-25 Sun 13:20]
static void line16_songmap_read(page_line16_d *l,
monolith_state_schema *s);
There is nothing useful to do here, so it is a stub.
static void line16_songmap_read(page_line16_d *l,
monolith_state_schema *s)
{
/* not necessary rigt now */
}
8.0.2. Line16 State Save
static int line16_save(monolith_page *pg,
monolith_state *s,
const char *key,
unsigned int len);
static int line16_save(monolith_page *pg,
monolith_state *s,
const char *key,
unsigned int len)
{
page_line16_d *line16;
monolith_state_schema *schema;
line16 = monolith_page_data_get(pg);
line16_schema(line16, &schema);
line16_lines_write(line16, schema);
line16_songmap_write(line16, schema);
monolith_state_write_schema(s, schema,
"line16", 6,
key, len);
monolith_state_schema_cleanup(&schema);
return 1;
}
monolith_page_save_set(pg, line16_save);
8.0.3. Line16 State Load
static int line16_load(monolith_page *pg,
monolith_state *s,
const char *key,
unsigned int len);
static int line16_load(monolith_page *pg,
monolith_state *s,
const char *key,
unsigned int len)
{
page_line16_d *line16;
monolith_state_schema *schema;
int rc;
monolith_state_schema_init(&schema, 9);
line16 = monolith_page_data_get(pg);
line16_schema(line16, &schema);
rc = monolith_state_read_schema(s, schema, "line16", 6, key, len);
if(rc) {
line16_lines_read(line16, schema);
line16_songmap_read(line16, schema);
monolith_param_float(schema, 2, &line16->min);
monolith_param_float(schema, 3, &line16->max);
monolith_param_int(schema,
4,
&line16->selected_point);
monolith_param_int(schema,
5,
&line16->selected_line);
monolith_param_float(schema,
6,
&line16->rate);
line16_state_copy(line16);
line16_redraw_points(line16);
line16_select_point(line16, line16->selected_point);
line16_draw_selected_line(line16->selected_line,
line16->mstate);
}
monolith_state_schema_cleanup(&schema);
return rc;
}
monolith_page_load_set(pg, line16_load);
prev | home | next