annotate yeti/scratch/generateTemplatesC.yeti @ 167:416b555df3b2 finetune

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