summaryrefslogtreecommitdiff
path: root/tools/audio/samplebank.c
blob: 30593e1d1e00e19f87a9535ea1ebbdb6a1420cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * SPDX-FileCopyrightText: Copyright (C) 2024 ZeldaRET
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include <assert.h>
#include <stdlib.h>

#include "xml.h"
#include "samplebank.h"
#include "util.h"

const char *
samplebank_path_forname(samplebank *sb, const char *name)
{
    assert(name != NULL);

    for (size_t i = 0; i < sb->num_samples; i++) {
        if (strequ(sb->sample_names[i], name))
            return sb->sample_paths[i];
    }
    return NULL;
}

typedef struct {
    const char *name;
    const char *path;
} samplebank_xml_entry;

void
read_samplebank_xml(samplebank *sb, xmlDocPtr doc)
{
    // XML Example:
    // <SampleBank Name="" Index="" Medium="" CachePolicy="" BufferBug="">
    //     <Pointer Index=""/>
    //     <Sample Name="" Path=""/>
    //     <Blob Name="" Path=""/>

    static const xml_attr_spec header_spec = {
        {"Name",         false, xml_parse_c_identifier, offsetof(samplebank, name)        },
        { "Index",       false, xml_parse_int,          offsetof(samplebank, index)       },
        { "Medium",      false, xml_parse_c_identifier, offsetof(samplebank, medium)      },
        { "CachePolicy", false, xml_parse_c_identifier, offsetof(samplebank, cache_policy)},
        { "BufferBug",   true,  xml_parse_bool,         offsetof(samplebank, buffer_bug)  },
    };
    static const xml_attr_spec entry_spec = {
        {"Name",  false, xml_parse_c_identifier, offsetof(samplebank_xml_entry, name)},
        { "Path", false, xml_parse_string,       offsetof(samplebank_xml_entry, path)},
    };

    xmlNodePtr root = xmlDocGetRootElement(doc);

    if (!strequ(XMLSTR_TO_STR(root->name), "SampleBank"))
        error("Root node must be <SampleBank>");

    sb->buffer_bug = false;
    xml_parse_node_by_spec(sb, root, header_spec, ARRAY_COUNT(header_spec));

    if (root->children == NULL)
        error("Missing samples list");

    size_t entries_cap = 8;
    size_t entries_len = 0;
    sb->sample_names = malloc(entries_cap * sizeof(const char *));
    sb->sample_paths = malloc(entries_cap * sizeof(const char *));
    sb->is_sample = malloc(entries_cap * sizeof(bool));

    size_t pointers_cap = 4;
    size_t pointers_len = 0;
    sb->pointer_indices = malloc(pointers_cap * sizeof(int));

    LL_FOREACH(xmlNodePtr, node, root->children) {
        if (node->type != XML_ELEMENT_NODE)
            continue;

        if (node->children != NULL) {
            xmlNodePtr first_child = node->children;
            const char *child_name = XMLSTR_TO_STR(first_child->name);
            error("Unexpected child node(s) (first is %s) in samples list (line %d)", child_name, first_child->line);
        }

        const char *node_name = XMLSTR_TO_STR(node->name);
        bool is_sample;

        if (strequ(node_name, "Sample")) {
            is_sample = true;
        } else if (strequ(node_name, "Blob")) {
            is_sample = false;
        } else if (strequ(node_name, "Pointer")) {
            // pointer entry
            int ptr_index;
            xml_get_single_property(&ptr_index, node, "Index", xml_parse_int);

            if (pointers_len == pointers_cap) {
                pointers_cap *= 2;
                sb->pointer_indices = realloc(sb->pointer_indices, pointers_cap * sizeof(int));
            }
            sb->pointer_indices[pointers_len++] = ptr_index;
            continue;
        } else {
            error("Unexpected element node %s in samples list (line %d)", node_name, node->line);
        }

        samplebank_xml_entry ent;
        xml_parse_node_by_spec(&ent, node, entry_spec, ARRAY_COUNT(entry_spec));

        if (entries_len == entries_cap) {
            entries_cap *= 2;
            sb->sample_names = realloc(sb->sample_names, entries_cap * sizeof(const char *));
            sb->sample_paths = realloc(sb->sample_paths, entries_cap * sizeof(const char *));
            sb->is_sample = realloc(sb->is_sample, entries_cap * sizeof(bool));
        }

        sb->sample_names[entries_len] = ent.name;
        sb->sample_paths[entries_len] = ent.path;
        sb->is_sample[entries_len] = is_sample;
        entries_len++;
    }

    sb->num_samples = entries_len;
    sb->num_pointers = pointers_len;
}