view yeti/scratch/generateTemplatesC.yeti @ 30:d697e78f81f1

Add templates in C header form (and conversion program)
author Chris Cannam
date Tue, 01 Apr 2014 17:40:21 +0100
parents
children 461d94ed3816
line wrap: on
line source

program generateTemplatesC;

vec = load may.vector;

instruments = [
    "bassoon",
    "cello",
    "clarinet",
    "flute",
    "guitar",
    "horn",
    "oboe",
    "tenorsax",
    "violin",
    "piano-maps-SptkBGCl",
    "piano1",
    "piano2",
    "piano3",
];

dataDir = "../../data";
includeDir = "\(dataDir)/include";

warning = "/* Do not edit this file: it was automatically generated */";

noteCount = 88;
templateHeight = 545;

convert instrument lines ostr =
   (notes = map do line:
                vec.fromList (map number (strSplit "," line))
            done lines;
    if (length notes) != noteCount then
        failWith "Wrong number of notes in instrument \(instrument): found (\(length notes), expected \(noteCount)";
    fi;
    for notes do n:
        if (vec.length n) != templateHeight then
            failWith "Wrong number of values in template while processing instrument \(instrument): found \(vec.length n), expected \(templateHeight)";
        fi
    done;
    levels = map vec.sum notes;
    first = length levels - length (find (>0) levels);
    last = length (find (>0) (reverse levels)) - 1;
    ostr.writeln warning;
    ostr.writeln "";
    ostr.writeln "{\n    \"\(instrument)\",\n    \(first),\n    \(last),";
    ostr.writeln "    {";
    for notes do note:
        ostr.write "        { ";
        for (vec.list note) do v:
            ostr.write "\(v), ";
        done;
        ostr.writeln "},";
    done;
    ostr.writeln "    }";
    ostr.writeln "},";
    ostr.close ()); 

writeMainHeader () =
   (ostr = openOutFile "\(includeDir)/templates.h" "UTF-8";
    for [
        warning,
        "",
        "#ifndef SILVET_DATA_TEMPLATES_H",
        "#define SILVET_DATA_TEMPLATES_H",
        "",
        "/* note: intended to parse as both C and C++ */",
        "",
        "#define SILVET_TEMPLATE_NOTE_COUNT \(noteCount)",
        "#define SILVET_TEMPLATE_HEIGHT     \(templateHeight)",
        "",
        "typedef struct {",
        "    const char *name;",
        "    int lowest;",
        "    int highest;",
        "    float data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_HEIGHT];",
        "} silvet_template_t;",
        "",
        "static silvet_template_t silvet_templates[\(length instruments)] = {",
    ] ostr.writeln;
    for instruments do instrument:
        ostr.writeln "#include \"\(instrument).h\"";
    done;
    for [
        "};",
        "",
        "#endif",
    ] ostr.writeln;
    ostr.close ()); 

convertAll () = 
   (writeMainHeader ();
    for instruments do instrument:
        i = openInFile "\(dataDir)/\(instrument).csv" "UTF-8";
        o = openOutFile "\(includeDir)/\(instrument).h" "UTF-8";
        eprintln "converting instrument \"\(instrument)\"...";
        convert instrument (i.lines ()) o;
    done);

convertAll ();