Phase 7: Add MIDI clips, instrument and piano roll
This commit is contained in:
parent
8a37090b81
commit
cacc086805
38 changed files with 1373 additions and 74 deletions
|
|
@ -19,7 +19,7 @@ static xtc_result line(reader *input)
|
|||
char *cursor;
|
||||
input->count = 0U;
|
||||
while ((byte = fgetc(input->file)) != EOF) {
|
||||
if (++input->bytes > 2097152U) return XTC_ERR_FORMAT;
|
||||
if (++input->bytes > 4194304U) return XTC_ERR_FORMAT;
|
||||
if (byte == '\n') break;
|
||||
if (byte < 32 || byte > 126 || used == sizeof(input->line) - 1U) return XTC_ERR_FORMAT;
|
||||
input->line[used++] = (char)byte;
|
||||
|
|
@ -89,7 +89,7 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
size_t i, j;
|
||||
xtc_graph *graph = project->graph;
|
||||
READ("XTC_PROJECT", 2U); NUM(1U, version);
|
||||
if (version != 1U && version != 2U) return XTC_ERR_UNSUPPORTED;
|
||||
if (version < 1U || version > 3U) return XTC_ERR_UNSUPPORTED;
|
||||
format_version = version;
|
||||
READ("APP", 4U);
|
||||
for (i = 1U; i < 4U; ++i) { NUM(i, value); if (value > UINT_MAX) return XTC_ERR_FORMAT; }
|
||||
|
|
@ -104,11 +104,11 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
for (i = 0U; i < (size_t)node_count; ++i) {
|
||||
xtc_node node;
|
||||
READ("NODE", 4U); NUM(1U, value); NUM(2U, version);
|
||||
if (value >= next_id || value >= SIZE_MAX || version < XTC_NODE_GAIN || version > (format_version == 1U ? XTC_NODE_OUTPUT : XTC_NODE_AUDIO) ||
|
||||
if (value >= next_id || value >= SIZE_MAX || version < XTC_NODE_GAIN || version > (format_version == 1U ? XTC_NODE_OUTPUT : format_version == 2U ? XTC_NODE_AUDIO : XTC_NODE_INSTRUMENT) ||
|
||||
!decode_float(input->tokens[3], &node.value)) return XTC_ERR_FORMAT;
|
||||
node.id = (size_t)value; node.type = (xtc_node_type)version;
|
||||
if (node.type == XTC_NODE_OSCILLATOR && node.value < 0.0f) return XTC_ERR_FORMAT;
|
||||
if ((node.type == XTC_NODE_METER || node.type == XTC_NODE_OUTPUT || node.type == XTC_NODE_AUDIO) && node.value != 0.0f) return XTC_ERR_FORMAT;
|
||||
if ((node.type == XTC_NODE_METER || node.type == XTC_NODE_OUTPUT || node.type == XTC_NODE_AUDIO || node.type == XTC_NODE_INSTRUMENT) && node.value != 0.0f) return XTC_ERR_FORMAT;
|
||||
for (j = 0U; j < i; ++j) if (graph->nodes[j].id == node.id) return XTC_ERR_FORMAT;
|
||||
graph->nodes[graph->node_count++] = node;
|
||||
}
|
||||
|
|
@ -134,18 +134,37 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
NUM(1U, clip->id); NUM(2U, clip->track_id); NUM(3U, clip->start_tick);
|
||||
NUM(4U, clip->length_tick); NUM(5U, clip->offset_tick); NUM(6U, clip->source_length_tick);
|
||||
if (!decode_name(input->tokens[7], clip->name)) return XTC_ERR_FORMAT;
|
||||
if (format_version == 2U) {
|
||||
if (format_version >= 2U) {
|
||||
size_t k, length;
|
||||
READ("ASSET", 2U);
|
||||
if (!strcmp(input->tokens[1], "-")) continue;
|
||||
length = strlen(input->tokens[1]);
|
||||
if (!length || length % 2 || length >= XTC_ASSET_PATH_BYTES * 2U) return XTC_ERR_FORMAT;
|
||||
for (k = 0; k < length / 2; ++k) {
|
||||
int high = hex_digit(input->tokens[1][k * 2]), low = hex_digit(input->tokens[1][k * 2 + 1]);
|
||||
if (high < 0 || low < 0 || (high == 0 && low == 0)) return XTC_ERR_FORMAT;
|
||||
clip->asset_path[k] = (char)(high * 16 + low);
|
||||
if (strcmp(input->tokens[1], "-")) {
|
||||
length = strlen(input->tokens[1]);
|
||||
if (!length || length % 2 || length >= XTC_ASSET_PATH_BYTES * 2U) return XTC_ERR_FORMAT;
|
||||
for (k = 0; k < length / 2; ++k) {
|
||||
int high = hex_digit(input->tokens[1][k * 2]), low = hex_digit(input->tokens[1][k * 2 + 1]);
|
||||
if (high < 0 || low < 0 || (high == 0 && low == 0)) return XTC_ERR_FORMAT;
|
||||
clip->asset_path[k] = (char)(high * 16 + low);
|
||||
}
|
||||
clip->asset_path[length / 2] = 0;
|
||||
}
|
||||
clip->asset_path[length / 2] = 0;
|
||||
}
|
||||
if (format_version >= 3U) {
|
||||
READ("MIDI", 2U); NUM(1U, value);
|
||||
if (value > 1) return XTC_ERR_FORMAT;
|
||||
clip->midi = (int)value;
|
||||
}
|
||||
}
|
||||
if (format_version >= 3U) {
|
||||
READ("NOTES", 3U); NUM(1U, project->next_note_id); NUM(2U, value);
|
||||
if (value > XTC_PROJECT_MAX_NOTES) return XTC_ERR_FORMAT;
|
||||
project->note_count = (size_t)value;
|
||||
for (i = 0; i < project->note_count; ++i) {
|
||||
xtc_note_info *n = project->notes + i;
|
||||
READ("NOTE", 8U);
|
||||
NUM(1U, n->id); NUM(2U, n->clip_id); NUM(3U, n->start_tick); NUM(4U, n->length_tick);
|
||||
NUM(5U, value); if (value > 127) return XTC_ERR_FORMAT; n->pitch = (unsigned int)value;
|
||||
NUM(6U, value); if (!value || value > 127) return XTC_ERR_FORMAT; n->velocity = (unsigned int)value;
|
||||
NUM(7U, value); if (value > 15) return XTC_ERR_FORMAT; n->channel = (unsigned int)value;
|
||||
}
|
||||
}
|
||||
READ("END", 1U);
|
||||
|
|
@ -164,7 +183,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
|
|||
const xtc_graph *graph = project->graph;
|
||||
size_t i;
|
||||
if (sizeof(float) != sizeof(uint32_t) || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) return XTC_ERR_UNSUPPORTED;
|
||||
if (fprintf(file, "XTC_PROJECT 2\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
|
||||
if (fprintf(file, "XTC_PROJECT 3\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
|
||||
XTC_VERSION_MAJOR, XTC_VERSION_MINOR, XTC_VERSION_PATCH, project->tempo,
|
||||
project->next_track_id, project->next_clip_id, graph->require_output_nodes,
|
||||
graph->next_id, graph->node_count, graph->edge_count) < 0) return XTC_ERR_IO;
|
||||
|
|
@ -190,6 +209,13 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
|
|||
if (clip->asset_path[0]) {
|
||||
if (!write_name(file, clip->asset_path)) return XTC_ERR_IO;
|
||||
} else if (fputs("-\n", file) == EOF) return XTC_ERR_IO;
|
||||
if (fprintf(file, "MIDI %d\n", clip->midi) < 0) return XTC_ERR_IO;
|
||||
}
|
||||
if (fprintf(file, "NOTES %" PRIu64 " %zu\n", project->next_note_id, project->note_count) < 0) return XTC_ERR_IO;
|
||||
for (i = 0; i < project->note_count; ++i) {
|
||||
const xtc_note_info *n = project->notes + i;
|
||||
if (fprintf(file, "NOTE %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %u %u %u\n",
|
||||
n->id, n->clip_id, n->start_tick, n->length_tick, n->pitch, n->velocity, n->channel) < 0) return XTC_ERR_IO;
|
||||
}
|
||||
return fputs("END\n", file) == EOF ? XTC_ERR_IO : XTC_OK;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue