Mercurial > hg > silvet
comparison src/Instruments.cpp @ 161:6003a9af43af
Introduce InstrumentPack class
author | Chris Cannam |
---|---|
date | Tue, 20 May 2014 15:31:05 +0100 |
parents | |
children | 8af9b6cd7451 |
comparison
equal
deleted
inserted
replaced
160:58547262e735 | 161:6003a9af43af |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Silvet | |
5 | |
6 A Vamp plugin for note transcription. | |
7 Centre for Digital Music, Queen Mary University of London. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "Instruments.h" | |
17 | |
18 #include "data/include/templates.h" | |
19 | |
20 #include <iostream> | |
21 | |
22 const int InstrumentPack::templateNoteCount = SILVET_TEMPLATE_NOTE_COUNT; | |
23 const int InstrumentPack::templateHeight = SILVET_TEMPLATE_HEIGHT; | |
24 const int InstrumentPack::templateMaxShift = SILVET_TEMPLATE_MAX_SHIFT; | |
25 const int InstrumentPack::templateSize = SILVET_TEMPLATE_SIZE; | |
26 | |
27 using std::string; | |
28 using std::vector; | |
29 using std::cerr; | |
30 using std::endl; | |
31 | |
32 const char *simpleInstruments[] = { | |
33 "Guitar", "guitar", | |
34 "Violin", "violin", | |
35 "Cello", "cello", | |
36 "Horn", "horn", | |
37 "Flute", "flute", | |
38 "Oboe", "oboe", | |
39 "Clarinet", "clarinet", | |
40 "Tenor Sax", "tenorsax", | |
41 "Bassoon", "bassoon", | |
42 }; | |
43 | |
44 static InstrumentPack::Templates | |
45 templatesFor(string name) | |
46 { | |
47 for (int i = 0; i < SILVET_TEMPLATE_COUNT; ++i) { | |
48 | |
49 if (string(silvet_templates[i].name) == name) { | |
50 | |
51 silvet_template_t *t = &silvet_templates[i]; | |
52 | |
53 InstrumentPack::Templates rt; | |
54 rt.lowestNote = t->lowest; | |
55 rt.highestNote = t->highest; | |
56 rt.data = vector<vector<float> > | |
57 (SILVET_TEMPLATE_NOTE_COUNT, | |
58 vector<float>(SILVET_TEMPLATE_SIZE, 0.f)); | |
59 | |
60 for (int j = 0; j < SILVET_TEMPLATE_NOTE_COUNT; ++j) { | |
61 for (int k = 0; k < SILVET_TEMPLATE_SIZE; ++k) { | |
62 rt.data[j][k] = t->data[j][k]; | |
63 } | |
64 } | |
65 | |
66 return rt; | |
67 } | |
68 } | |
69 | |
70 return InstrumentPack::Templates(); | |
71 } | |
72 | |
73 static bool | |
74 isOK(InstrumentPack &d) | |
75 { | |
76 if (d.name == "") { | |
77 cerr << "ERROR: Silvet::InstrumentPack: Empty name in instrument definition" << endl; | |
78 return false; | |
79 } | |
80 if (d.templates.empty()) { | |
81 cerr << "ERROR: Silvet::InstrumentPack: Instrument definition \"" << d.name << "\" contains no templates!" << endl; | |
82 return false; | |
83 } | |
84 for (int i = 0; i < int(d.templates.size()); ++i) { | |
85 if (d.templates[i].data.empty()) { | |
86 cerr << "ERROR: Silvet::InstrumentPack: Instrument definition \"" << d.name << "\" contains one or more empty templates!" << endl; | |
87 return false; | |
88 } | |
89 } | |
90 return true; | |
91 } | |
92 | |
93 vector<InstrumentPack> | |
94 InstrumentPack::listInstrumentPacks() | |
95 { | |
96 vector<InstrumentPack> ii; | |
97 | |
98 vector<Templates> allTemplates; | |
99 //!!! is piano-maps-SptkBGCl the same as one of piano1, piano2, piano3? | |
100 allTemplates.push_back(templatesFor("piano1")); | |
101 | |
102 vector<Templates> pianoTemplates; | |
103 pianoTemplates.push_back(templatesFor("piano1")); | |
104 pianoTemplates.push_back(templatesFor("piano2")); | |
105 pianoTemplates.push_back(templatesFor("piano3")); | |
106 InstrumentPack piano(silvet_templates_lowest_note, | |
107 silvet_templates_highest_note, | |
108 "Piano", | |
109 pianoTemplates); | |
110 if (isOK(piano)) { | |
111 ii.push_back(piano); | |
112 } | |
113 | |
114 for (int i = 0; | |
115 i < int(sizeof(simpleInstruments)/sizeof(simpleInstruments[0])); | |
116 i += 2) { | |
117 vector<Templates> tt; | |
118 Templates t = templatesFor(simpleInstruments[i+1]); | |
119 tt.push_back(t); | |
120 allTemplates.push_back(t); | |
121 InstrumentPack instr(t.lowestNote, | |
122 t.highestNote, | |
123 simpleInstruments[i], | |
124 tt); | |
125 if (isOK(instr)) { | |
126 ii.push_back(instr); | |
127 } | |
128 } | |
129 | |
130 InstrumentPack all(silvet_templates_lowest_note, | |
131 silvet_templates_highest_note, | |
132 "Multiple or unknown instruments", | |
133 allTemplates); | |
134 if (isOK(all)) { | |
135 ii.insert(ii.begin(), all); | |
136 } | |
137 | |
138 return ii; | |
139 } | |
140 |