annotate yeti/scratch/generateTemplatesC.yeti @ 325:4cf4313d7e30 livemode

Always use q=0.8 and accept the hit on speed -- the templates are made for that configuration and it does work better. Also some adjustments to thresholding and peak picking for live mode in particular.
author Chris Cannam
date Mon, 18 May 2015 13:58:27 +0100
parents a144217a9cfb
children
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@218 16 "viola",
Chris@161 17 // "piano-maps-SptkBGCl",
Chris@161 18 "piano1",
Chris@161 19 "piano2",
Chris@161 20 "piano3",
Chris@218 21 "pianorwc",
Chris@30 22 ];
Chris@30 23
Chris@41 24 hardcodedRanges = [
Chris@41 25 "bassoon": { lowest = 15, highest = 51 },
Chris@41 26 "cello": { lowest = 15, highest = 60 },
Chris@41 27 "clarinet": { lowest = 29, highest = 68 },
Chris@41 28 "flute": { lowest = 39, highest = 72 },
Chris@41 29 "guitar": { lowest = 19, highest = 55 },
Chris@41 30 "horn": { lowest = 20, highest = 56 },
Chris@41 31 "oboe": { lowest = 37, highest = 70 },
Chris@41 32 "tenorsax": { lowest = 23, highest = 54 },
Chris@41 33 "violin": { lowest = 34, highest = 72 },
Chris@161 34 // "piano-maps-SptkBGCl": { lowest = 16, highest = 72 },
Chris@41 35 "piano1": { lowest = 16, highest = 72 },
Chris@41 36 "piano2": { lowest = 16, highest = 72 },
Chris@41 37 "piano3": { lowest = 16, highest = 72 },
Chris@219 38 "pianorwc": { lowest = 16, highest = 72 },
Chris@41 39 ];
Chris@41 40
Chris@41 41 overallLowest = 15;
Chris@41 42 overallHighest = 72;
Chris@41 43
Chris@30 44 dataDir = "../../data";
Chris@30 45 includeDir = "\(dataDir)/include";
Chris@30 46
Chris@136 47 warning = "/* Do not edit: this file was automatically generated by generateTemplatesC */";
Chris@30 48
Chris@30 49 noteCount = 88;
Chris@30 50 templateHeight = 545;
Chris@44 51 shiftRange = 2;
Chris@30 52
Chris@30 53 convert instrument lines ostr =
Chris@30 54 (notes = map do line:
Chris@30 55 vec.fromList (map number (strSplit "," line))
Chris@30 56 done lines;
Chris@30 57 if (length notes) != noteCount then
Chris@30 58 failWith "Wrong number of notes in instrument \(instrument): found (\(length notes), expected \(noteCount)";
Chris@30 59 fi;
Chris@30 60 for notes do n:
Chris@30 61 if (vec.length n) != templateHeight then
Chris@30 62 failWith "Wrong number of values in template while processing instrument \(instrument): found \(vec.length n), expected \(templateHeight)";
Chris@30 63 fi
Chris@30 64 done;
Chris@30 65 levels = map vec.sum notes;
Chris@218 66 first =
Chris@218 67 if instrument in hardcodedRanges then
Chris@218 68 hardcodedRanges[instrument].lowest;
Chris@218 69 else
Chris@218 70 length levels - length (find (>0) levels);
Chris@218 71 fi;
Chris@218 72 last =
Chris@218 73 if instrument in hardcodedRanges then
Chris@218 74 hardcodedRanges[instrument].highest;
Chris@218 75 else
Chris@218 76 length (find (>0) (reverse levels)) - 1;
Chris@218 77 fi;
Chris@30 78 ostr.writeln warning;
Chris@30 79 ostr.writeln "";
Chris@30 80 ostr.writeln "{\n \"\(instrument)\",\n \(first),\n \(last),";
Chris@30 81 ostr.writeln " {";
Chris@30 82 for notes do note:
Chris@30 83 ostr.write " { ";
Chris@44 84 for [0..shiftRange-1] do s:
Chris@44 85 ostr.write "0.0, ";
Chris@44 86 done;
Chris@30 87 for (vec.list note) do v:
Chris@30 88 ostr.write "\(v), ";
Chris@30 89 done;
Chris@44 90 for [0..shiftRange-1] do s:
Chris@44 91 ostr.write "0.0, ";
Chris@44 92 done;
Chris@30 93 ostr.writeln "},";
Chris@30 94 done;
Chris@30 95 ostr.writeln " }";
Chris@30 96 ostr.writeln "},";
Chris@30 97 ostr.close ());
Chris@30 98
Chris@30 99 writeMainHeader () =
Chris@30 100 (ostr = openOutFile "\(includeDir)/templates.h" "UTF-8";
Chris@30 101 for [
Chris@30 102 warning,
Chris@30 103 "",
Chris@30 104 "#ifndef SILVET_DATA_TEMPLATES_H",
Chris@30 105 "#define SILVET_DATA_TEMPLATES_H",
Chris@30 106 "",
Chris@30 107 "/* note: intended to parse as both C and C++ */",
Chris@30 108 "",
Chris@44 109 "#define SILVET_TEMPLATE_COUNT \(length instruments) /* Number of instruments */",
Chris@44 110 "#define SILVET_TEMPLATE_NOTE_COUNT \(noteCount) /* Number of notes per instrument */ ",
Chris@44 111 "#define SILVET_TEMPLATE_HEIGHT \(templateHeight) /* Frequency bins per template */",
Chris@44 112 "#define SILVET_TEMPLATE_MAX_SHIFT \(shiftRange) /* Zeros at either end of template */ ",
Chris@44 113 "#define SILVET_TEMPLATE_SIZE \(templateHeight + 2 * shiftRange) /* Height + 2 * max shift space */ ",
Chris@30 114 "",
Chris@30 115 "typedef struct {",
Chris@30 116 " const char *name;",
Chris@30 117 " int lowest;",
Chris@30 118 " int highest;",
Chris@151 119 " float data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_SIZE];",
Chris@30 120 "} silvet_template_t;",
Chris@30 121 "",
Chris@41 122 "static int silvet_templates_lowest_note = \(overallLowest);",
Chris@41 123 "static int silvet_templates_highest_note = \(overallHighest);",
Chris@41 124 "",
Chris@35 125 "static silvet_template_t silvet_templates[SILVET_TEMPLATE_COUNT] = {",
Chris@30 126 ] ostr.writeln;
Chris@30 127 for instruments do instrument:
Chris@30 128 ostr.writeln "#include \"\(instrument).h\"";
Chris@30 129 done;
Chris@30 130 for [
Chris@30 131 "};",
Chris@30 132 "",
Chris@30 133 "#endif",
Chris@30 134 ] ostr.writeln;
Chris@30 135 ostr.close ());
Chris@30 136
Chris@30 137 convertAll () =
Chris@30 138 (writeMainHeader ();
Chris@30 139 for instruments do instrument:
Chris@30 140 i = openInFile "\(dataDir)/\(instrument).csv" "UTF-8";
Chris@30 141 o = openOutFile "\(includeDir)/\(instrument).h" "UTF-8";
Chris@30 142 eprintln "converting instrument \"\(instrument)\"...";
Chris@30 143 convert instrument (i.lines ()) o;
Chris@30 144 done);
Chris@30 145
Chris@30 146 convertAll ();