diff options
Diffstat (limited to 'generator/generator.cpp')
-rw-r--r-- | generator/generator.cpp | 109 |
1 files changed, 85 insertions, 24 deletions
diff --git a/generator/generator.cpp b/generator/generator.cpp index 19eba70..4244fd2 100644 --- a/generator/generator.cpp +++ b/generator/generator.cpp | |||
@@ -6,7 +6,6 @@ | |||
6 | #include <json.hpp> | 6 | #include <json.hpp> |
7 | #include <hkutil/progress.h> | 7 | #include <hkutil/progress.h> |
8 | #include <hkutil/string.h> | 8 | #include <hkutil/string.h> |
9 | #include "mood.h" | ||
10 | 9 | ||
11 | namespace cadence { | 10 | namespace cadence { |
12 | namespace generator { | 11 | namespace generator { |
@@ -93,7 +92,8 @@ namespace cadence { | |||
93 | DIR* subdir; | 92 | DIR* subdir; |
94 | if ((subdir = opendir(directory.c_str())) == nullptr) | 93 | if ((subdir = opendir(directory.c_str())) == nullptr) |
95 | { | 94 | { |
96 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); | 95 | throw std::invalid_argument( |
96 | "Invalid AcousticBrainz data directory"); | ||
97 | } | 97 | } |
98 | 98 | ||
99 | struct dirent* subent; | 99 | struct dirent* subent; |
@@ -106,7 +106,8 @@ namespace cadence { | |||
106 | DIR* subsubdir; | 106 | DIR* subsubdir; |
107 | if ((subsubdir = opendir(subdirectory.c_str())) == nullptr) | 107 | if ((subsubdir = opendir(subdirectory.c_str())) == nullptr) |
108 | { | 108 | { |
109 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); | 109 | throw std::invalid_argument( |
110 | "Invalid AcousticBrainz data directory"); | ||
110 | } | 111 | } |
111 | 112 | ||
112 | struct dirent* subsubent; | 113 | struct dirent* subsubent; |
@@ -149,27 +150,87 @@ namespace cadence { | |||
149 | 150 | ||
150 | try | 151 | try |
151 | { | 152 | { |
152 | std::vector<mood> moods; | 153 | auto& hl = jsonData["highlevel"]; |
153 | moods.emplace_back(mood::type::danceable, jsonData["highlevel"]["danceability"]["all"]["danceable"]); | 154 | |
154 | moods.emplace_back(mood::type::acoustic, jsonData["highlevel"]["mood_acoustic"]["all"]["acoustic"]); | 155 | double danceable = hl["danceability"]["all"]["danceable"]; |
155 | moods.emplace_back(mood::type::aggressive, jsonData["highlevel"]["mood_aggressive"]["all"]["aggressive"]); | 156 | double acoustic = hl["mood_acoustic"]["all"]["acoustic"]; |
156 | moods.emplace_back(mood::type::electronic, jsonData["highlevel"]["mood_electronic"]["all"]["electronic"]); | 157 | double aggressive = hl["mood_aggressive"]["all"]["aggressive"]; |
157 | moods.emplace_back(mood::type::happy, jsonData["highlevel"]["mood_happy"]["all"]["happy"]); | 158 | double electronic = hl["mood_electronic"]["all"]["electronic"]; |
158 | moods.emplace_back(mood::type::party, jsonData["highlevel"]["mood_party"]["all"]["party"]); | 159 | double happy = hl["mood_happy"]["all"]["happy"]; |
159 | moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]); | 160 | double party = hl["mood_party"]["all"]["party"]; |
160 | moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]); | 161 | double relaxed = hl["mood_relaxed"]["all"]["relaxed"]; |
161 | moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]); | 162 | double sad = hl["mood_sad"]["all"]["sad"]; |
162 | 163 | double instrumental = hl["voice_instrumental"]["all"]["instrumental"]; | |
163 | std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool { | 164 | |
164 | return left.getProbability() > right.getProbability(); | 165 | std::string title = jsonData["metadata"]["tags"]["title"][0]; |
165 | }); | 166 | std::string artist = jsonData["metadata"]["tags"]["artist"][0]; |
166 | 167 | ||
167 | std::list<hatkirby::column> columns; | 168 | uint64_t songId = db_.insertIntoTable( |
168 | columns.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get<std::string>()); | 169 | "songs", |
169 | columns.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get<std::string>()); | 170 | { |
170 | columns.emplace_back("category", moods.front().getCategory()); | 171 | { "title", title }, |
171 | 172 | { "artist", artist } | |
172 | db_.insertIntoTable("songs", std::move(columns)); | 173 | }); |
174 | |||
175 | std::list<std::string> moods; | ||
176 | |||
177 | // ~38% | ||
178 | if ((party > 0.5) || (danceable > 0.75)) | ||
179 | { | ||
180 | moods.push_back("party"); | ||
181 | } | ||
182 | |||
183 | // ~38% | ||
184 | if ((relaxed > 0.81) || (acoustic > 0.5)) | ||
185 | { | ||
186 | moods.push_back("chill"); | ||
187 | } | ||
188 | |||
189 | // ~42% | ||
190 | if ((aggressive > 0.5) || (electronic > 0.95)) | ||
191 | { | ||
192 | moods.push_back("crazy"); | ||
193 | } | ||
194 | |||
195 | // ~30% | ||
196 | if (happy > 0.5) | ||
197 | { | ||
198 | moods.push_back("happy"); | ||
199 | } | ||
200 | |||
201 | // ~30% | ||
202 | if (sad > 0.5) | ||
203 | { | ||
204 | moods.push_back("sad"); | ||
205 | } | ||
206 | |||
207 | // ~38% | ||
208 | if (instrumental > 0.9) | ||
209 | { | ||
210 | moods.push_back("instrumental"); | ||
211 | } | ||
212 | |||
213 | // ~34% | ||
214 | if (instrumental < 0.2) | ||
215 | { | ||
216 | moods.push_back("vocal"); | ||
217 | } | ||
218 | |||
219 | // ~1% | ||
220 | if (moods.empty()) | ||
221 | { | ||
222 | moods.push_back("unknown"); | ||
223 | } | ||
224 | |||
225 | for (const std::string& mood : moods) | ||
226 | { | ||
227 | db_.insertIntoTable( | ||
228 | "moods", | ||
229 | { | ||
230 | { "song_id", static_cast<int>(songId) }, | ||
231 | { "mood", mood } | ||
232 | }); | ||
233 | } | ||
173 | } catch (const std::domain_error& ex) | 234 | } catch (const std::domain_error& ex) |
174 | { | 235 | { |
175 | // Weird data. Ignore silently. | 236 | // Weird data. Ignore silently. |