annotate yeti/scratch/generateTemplatesC.yeti @ 120:ab1d8efbb7b5 bqvec-openmp

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