annotate yeti/scratch/generateTemplatesC.yeti @ 40:303c06efa8d2

Return a sketch of notes from pitch activation matrix
author Chris Cannam
date Sat, 05 Apr 2014 13:18:55 +0100
parents 461d94ed3816
children b49597c93132
rev   line source
Chris@30 1
Chris@30 2 program generateTemplatesC;
Chris@30 3
Chris@30 4 vec = load may.vector;
Chris@30 5
Chris@30 6 instruments = [
Chris@30 7 "bassoon",
Chris@30 8 "cello",
Chris@30 9 "clarinet",
Chris@30 10 "flute",
Chris@30 11 "guitar",
Chris@30 12 "horn",
Chris@30 13 "oboe",
Chris@30 14 "tenorsax",
Chris@30 15 "violin",
Chris@30 16 "piano-maps-SptkBGCl",
Chris@30 17 "piano1",
Chris@30 18 "piano2",
Chris@30 19 "piano3",
Chris@30 20 ];
Chris@30 21
Chris@30 22 dataDir = "../../data";
Chris@30 23 includeDir = "\(dataDir)/include";
Chris@30 24
Chris@30 25 warning = "/* Do not edit this file: it was automatically generated */";
Chris@30 26
Chris@30 27 noteCount = 88;
Chris@30 28 templateHeight = 545;
Chris@30 29
Chris@30 30 convert instrument lines ostr =
Chris@30 31 (notes = map do line:
Chris@30 32 vec.fromList (map number (strSplit "," line))
Chris@30 33 done lines;
Chris@30 34 if (length notes) != noteCount then
Chris@30 35 failWith "Wrong number of notes in instrument \(instrument): found (\(length notes), expected \(noteCount)";
Chris@30 36 fi;
Chris@30 37 for notes do n:
Chris@30 38 if (vec.length n) != templateHeight then
Chris@30 39 failWith "Wrong number of values in template while processing instrument \(instrument): found \(vec.length n), expected \(templateHeight)";
Chris@30 40 fi
Chris@30 41 done;
Chris@30 42 levels = map vec.sum notes;
Chris@30 43 first = length levels - length (find (>0) levels);
Chris@30 44 last = length (find (>0) (reverse levels)) - 1;
Chris@30 45 ostr.writeln warning;
Chris@30 46 ostr.writeln "";
Chris@30 47 ostr.writeln "{\n \"\(instrument)\",\n \(first),\n \(last),";
Chris@30 48 ostr.writeln " {";
Chris@30 49 for notes do note:
Chris@30 50 ostr.write " { ";
Chris@30 51 for (vec.list note) do v:
Chris@30 52 ostr.write "\(v), ";
Chris@30 53 done;
Chris@30 54 ostr.writeln "},";
Chris@30 55 done;
Chris@30 56 ostr.writeln " }";
Chris@30 57 ostr.writeln "},";
Chris@30 58 ostr.close ());
Chris@30 59
Chris@30 60 writeMainHeader () =
Chris@30 61 (ostr = openOutFile "\(includeDir)/templates.h" "UTF-8";
Chris@30 62 for [
Chris@30 63 warning,
Chris@30 64 "",
Chris@30 65 "#ifndef SILVET_DATA_TEMPLATES_H",
Chris@30 66 "#define SILVET_DATA_TEMPLATES_H",
Chris@30 67 "",
Chris@30 68 "/* note: intended to parse as both C and C++ */",
Chris@30 69 "",
Chris@30 70 "#define SILVET_TEMPLATE_NOTE_COUNT \(noteCount)",
Chris@30 71 "#define SILVET_TEMPLATE_HEIGHT \(templateHeight)",
Chris@35 72 "#define SILVET_TEMPLATE_COUNT \(length instruments)",
Chris@30 73 "",
Chris@30 74 "typedef struct {",
Chris@30 75 " const char *name;",
Chris@30 76 " int lowest;",
Chris@30 77 " int highest;",
Chris@30 78 " float data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_HEIGHT];",
Chris@30 79 "} silvet_template_t;",
Chris@30 80 "",
Chris@35 81 "static silvet_template_t silvet_templates[SILVET_TEMPLATE_COUNT] = {",
Chris@30 82 ] ostr.writeln;
Chris@30 83 for instruments do instrument:
Chris@30 84 ostr.writeln "#include \"\(instrument).h\"";
Chris@30 85 done;
Chris@30 86 for [
Chris@30 87 "};",
Chris@30 88 "",
Chris@30 89 "#endif",
Chris@30 90 ] ostr.writeln;
Chris@30 91 ostr.close ());
Chris@30 92
Chris@30 93 convertAll () =
Chris@30 94 (writeMainHeader ();
Chris@30 95 for instruments do instrument:
Chris@30 96 i = openInFile "\(dataDir)/\(instrument).csv" "UTF-8";
Chris@30 97 o = openOutFile "\(includeDir)/\(instrument).h" "UTF-8";
Chris@30 98 eprintln "converting instrument \"\(instrument)\"...";
Chris@30 99 convert instrument (i.lines ()) o;
Chris@30 100 done);
Chris@30 101
Chris@30 102 convertAll ();