about summary refs log tree commit diff stats
path: root/generator
diff options
context:
space:
mode:
Diffstat (limited to 'generator')
-rw-r--r--generator/database.cpp14
-rw-r--r--generator/database.h2
-rw-r--r--generator/field.h12
-rw-r--r--generator/generator.cpp56
-rw-r--r--generator/generator.h36
-rw-r--r--generator/main.cpp2
-rw-r--r--generator/mood.cpp42
-rw-r--r--generator/mood.h22
-rw-r--r--generator/progress.h16
9 files changed, 101 insertions, 101 deletions
diff --git a/generator/database.cpp b/generator/database.cpp index f326f46..b46a0d1 100644 --- a/generator/database.cpp +++ b/generator/database.cpp
@@ -73,19 +73,19 @@ namespace cadence {
73 { 73 {
74 sqlite3_close_v2(ppdb_); 74 sqlite3_close_v2(ppdb_);
75 } 75 }
76 76
77 void database::runQuery(std::string query) 77 void database::runQuery(std::string query)
78 { 78 {
79 // This can only happen when doing bad things with move semantics. 79 // This can only happen when doing bad things with move semantics.
80 assert(ppdb_ != nullptr); 80 assert(ppdb_ != nullptr);
81 81
82 sqlite3_stmt* ppstmt; 82 sqlite3_stmt* ppstmt;
83 83
84 if (sqlite3_prepare_v2(ppdb_, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) 84 if (sqlite3_prepare_v2(ppdb_, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
85 { 85 {
86 throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); 86 throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_));
87 } 87 }
88 88
89 int result = sqlite3_step(ppstmt); 89 int result = sqlite3_step(ppstmt);
90 sqlite3_finalize(ppstmt); 90 sqlite3_finalize(ppstmt);
91 91
@@ -99,10 +99,10 @@ namespace cadence {
99 { 99 {
100 // This can only happen when doing bad things with move semantics. 100 // This can only happen when doing bad things with move semantics.
101 assert(ppdb_ != nullptr); 101 assert(ppdb_ != nullptr);
102 102
103 // This shouldn't happen. 103 // This shouldn't happen.
104 assert(!fields.empty()); 104 assert(!fields.empty());
105 105
106 std::list<std::string> fieldNames; 106 std::list<std::string> fieldNames;
107 std::list<std::string> qs; 107 std::list<std::string> qs;
108 for (field& f : fields) 108 for (field& f : fields)
@@ -110,7 +110,7 @@ namespace cadence {
110 fieldNames.push_back(f.getName()); 110 fieldNames.push_back(f.getName());
111 qs.push_back("?"); 111 qs.push_back("?");
112 } 112 }
113 113
114 std::ostringstream query; 114 std::ostringstream query;
115 query << "INSERT INTO "; 115 query << "INSERT INTO ";
116 query << table; 116 query << table;
@@ -119,7 +119,7 @@ namespace cadence {
119 query << ") VALUES ("; 119 query << ") VALUES (";
120 query << implode(std::begin(qs), std::end(qs), ", "); 120 query << implode(std::begin(qs), std::end(qs), ", ");
121 query << ")"; 121 query << ")";
122 122
123 std::string query_str = query.str(); 123 std::string query_str = query.str();
124 124
125 sqlite3_stmt* ppstmt; 125 sqlite3_stmt* ppstmt;
diff --git a/generator/database.h b/generator/database.h index 7aa8dfa..e4f3ba2 100644 --- a/generator/database.h +++ b/generator/database.h
@@ -52,7 +52,7 @@ namespace cadence {
52 ~database(); 52 ~database();
53 53
54 // Actions 54 // Actions
55 55
56 void runQuery(std::string query); 56 void runQuery(std::string query);
57 57
58 void insertIntoTable(std::string table, std::list<field> fields); 58 void insertIntoTable(std::string table, std::list<field> fields);
diff --git a/generator/field.h b/generator/field.h index 9d6ed9a..836c079 100644 --- a/generator/field.h +++ b/generator/field.h
@@ -32,27 +32,27 @@ namespace cadence {
32 ~field(); 32 ~field();
33 33
34 // Generic accessors 34 // Generic accessors
35 35
36 type getType() const 36 type getType() const
37 { 37 {
38 return type_; 38 return type_;
39 } 39 }
40 40
41 std::string getName() const 41 std::string getName() const
42 { 42 {
43 return name_; 43 return name_;
44 } 44 }
45 45
46 // Integer 46 // Integer
47 47
48 field(std::string name, int arg); 48 field(std::string name, int arg);
49 49
50 int getInteger() const; 50 int getInteger() const;
51 51
52 // String 52 // String
53 53
54 field(std::string name, std::string arg); 54 field(std::string name, std::string arg);
55 55
56 std::string getString() const; 56 std::string getString() const;
57 57
58 private: 58 private:
diff --git a/generator/generator.cpp b/generator/generator.cpp index eb2750c..54f5d69 100644 --- a/generator/generator.cpp +++ b/generator/generator.cpp
@@ -11,7 +11,7 @@
11 11
12namespace cadence { 12namespace cadence {
13 namespace generator { 13 namespace generator {
14 14
15 generator::generator( 15 generator::generator(
16 std::string inputpath, 16 std::string inputpath,
17 std::string outputpath) : 17 std::string outputpath) :
@@ -23,23 +23,23 @@ namespace cadence {
23 { 23 {
24 inputpath_ += '/'; 24 inputpath_ += '/';
25 } 25 }
26 26
27 inputpath_ += "highlevel/"; 27 inputpath_ += "highlevel/";
28 } 28 }
29 29
30 void generator::run() 30 void generator::run()
31 { 31 {
32 // Creates the datafile. 32 // Creates the datafile.
33 writeSchema(); 33 writeSchema();
34 34
35 // Scans the AcousticBrainz data dump and generates a list of all of the 35 // Scans the AcousticBrainz data dump and generates a list of all of the
36 // files in the dump. 36 // files in the dump.
37 scanDirectories(); 37 scanDirectories();
38 38
39 // Parses each data file and enters it into the database. 39 // Parses each data file and enters it into the database.
40 parseData(); 40 parseData();
41 } 41 }
42 42
43 void generator::writeSchema() 43 void generator::writeSchema()
44 { 44 {
45 std::ifstream file("schema.sql"); 45 std::ifstream file("schema.sql");
@@ -47,7 +47,7 @@ namespace cadence {
47 { 47 {
48 throw std::invalid_argument("Could not find database schema"); 48 throw std::invalid_argument("Could not find database schema");
49 } 49 }
50 50
51 std::ostringstream schemaBuilder; 51 std::ostringstream schemaBuilder;
52 std::string line; 52 std::string line;
53 while (std::getline(file, line)) 53 while (std::getline(file, line))
@@ -56,10 +56,10 @@ namespace cadence {
56 { 56 {
57 line.pop_back(); 57 line.pop_back();
58 } 58 }
59 59
60 schemaBuilder << line; 60 schemaBuilder << line;
61 } 61 }
62 62
63 std::string schema = schemaBuilder.str(); 63 std::string schema = schemaBuilder.str();
64 auto queries = split<std::list<std::string>>(schema, ";"); 64 auto queries = split<std::list<std::string>>(schema, ";");
65 progress ppgs("Writing database schema...", queries.size()); 65 progress ppgs("Writing database schema...", queries.size());
@@ -69,73 +69,73 @@ namespace cadence {
69 { 69 {
70 db_.runQuery(query); 70 db_.runQuery(query);
71 } 71 }
72 72
73 ppgs.update(); 73 ppgs.update();
74 } 74 }
75 } 75 }
76 76
77 void generator::scanDirectories() 77 void generator::scanDirectories()
78 { 78 {
79 std::cout << "Scanning AcousticBrainz dump..." << std::endl; 79 std::cout << "Scanning AcousticBrainz dump..." << std::endl;
80 80
81 DIR* topdir; 81 DIR* topdir;
82 if ((topdir = opendir(inputpath_.c_str())) == nullptr) 82 if ((topdir = opendir(inputpath_.c_str())) == nullptr)
83 { 83 {
84 throw std::invalid_argument("Invalid AcousticBrainz data directory"); 84 throw std::invalid_argument("Invalid AcousticBrainz data directory");
85 } 85 }
86 86
87 struct dirent* topent; 87 struct dirent* topent;
88 while ((topent = readdir(topdir)) != nullptr) 88 while ((topent = readdir(topdir)) != nullptr)
89 { 89 {
90 if (topent->d_name[0] != '.') 90 if (topent->d_name[0] != '.')
91 { 91 {
92 std::string directory = inputpath_ + topent->d_name + "/"; 92 std::string directory = inputpath_ + topent->d_name + "/";
93 93
94 DIR* subdir; 94 DIR* subdir;
95 if ((subdir = opendir(directory.c_str())) == nullptr) 95 if ((subdir = opendir(directory.c_str())) == nullptr)
96 { 96 {
97 throw std::invalid_argument("Invalid AcousticBrainz data directory"); 97 throw std::invalid_argument("Invalid AcousticBrainz data directory");
98 } 98 }
99 99
100 struct dirent* subent; 100 struct dirent* subent;
101 while ((subent = readdir(subdir)) != nullptr) 101 while ((subent = readdir(subdir)) != nullptr)
102 { 102 {
103 if (subent->d_name[0] != '.') 103 if (subent->d_name[0] != '.')
104 { 104 {
105 std::string subdirectory = directory + subent->d_name + "/"; 105 std::string subdirectory = directory + subent->d_name + "/";
106 106
107 DIR* subsubdir; 107 DIR* subsubdir;
108 if ((subsubdir = opendir(subdirectory.c_str())) == nullptr) 108 if ((subsubdir = opendir(subdirectory.c_str())) == nullptr)
109 { 109 {
110 throw std::invalid_argument("Invalid AcousticBrainz data directory"); 110 throw std::invalid_argument("Invalid AcousticBrainz data directory");
111 } 111 }
112 112
113 struct dirent* subsubent; 113 struct dirent* subsubent;
114 while ((subsubent = readdir(subsubdir)) != nullptr) 114 while ((subsubent = readdir(subsubdir)) != nullptr)
115 { 115 {
116 if (subsubent->d_name[0] != '.') 116 if (subsubent->d_name[0] != '.')
117 { 117 {
118 std::string datafile = subdirectory + subsubent->d_name; 118 std::string datafile = subdirectory + subsubent->d_name;
119 119
120 datafiles_.push_back(datafile); 120 datafiles_.push_back(datafile);
121 } 121 }
122 } 122 }
123 123
124 closedir(subsubdir); 124 closedir(subsubdir);
125 } 125 }
126 } 126 }
127 127
128 closedir(subdir); 128 closedir(subdir);
129 } 129 }
130 } 130 }
131 131
132 closedir(topdir); 132 closedir(topdir);
133 } 133 }
134 134
135 void generator::parseData() 135 void generator::parseData()
136 { 136 {
137 progress ppgs("Parsing AcousticBrainz data files...", datafiles_.size()); 137 progress ppgs("Parsing AcousticBrainz data files...", datafiles_.size());
138 138
139 for (std::string datafile : datafiles_) 139 for (std::string datafile : datafiles_)
140 { 140 {
141 ppgs.update(); 141 ppgs.update();
@@ -145,7 +145,7 @@ namespace cadence {
145 std::ifstream dataStream(datafile); 145 std::ifstream dataStream(datafile);
146 dataStream >> jsonData; 146 dataStream >> jsonData;
147 } 147 }
148 148
149 try 149 try
150 { 150 {
151 std::vector<mood> moods; 151 std::vector<mood> moods;
@@ -158,16 +158,16 @@ namespace cadence {
158 moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]); 158 moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]);
159 moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]); 159 moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]);
160 moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]); 160 moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]);
161 161
162 std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool { 162 std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool {
163 return left.getProbability() > right.getProbability(); 163 return left.getProbability() > right.getProbability();
164 }); 164 });
165 165
166 std::list<field> fields; 166 std::list<field> fields;
167 fields.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get<std::string>()); 167 fields.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get<std::string>());
168 fields.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get<std::string>()); 168 fields.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get<std::string>());
169 fields.emplace_back("category", moods.front().getCategory()); 169 fields.emplace_back("category", moods.front().getCategory());
170 170
171 db_.insertIntoTable("songs", std::move(fields)); 171 db_.insertIntoTable("songs", std::move(fields));
172 } catch (const std::domain_error& ex) 172 } catch (const std::domain_error& ex)
173 { 173 {
@@ -175,6 +175,6 @@ namespace cadence {
175 } 175 }
176 } 176 }
177 } 177 }
178 178
179 }; 179 };
180}; 180};
diff --git a/generator/generator.h b/generator/generator.h index d911c61..dff8eeb 100644 --- a/generator/generator.h +++ b/generator/generator.h
@@ -7,44 +7,44 @@
7 7
8namespace cadence { 8namespace cadence {
9 namespace generator { 9 namespace generator {
10 10
11 class generator { 11 class generator {
12 public: 12 public:
13 13
14 // Constructor 14 // Constructor
15 15
16 generator( 16 generator(
17 std::string inputpath, 17 std::string inputpath,
18 std::string outputpath); 18 std::string outputpath);
19 19
20 // Action 20 // Action
21 21
22 void run(); 22 void run();
23 23
24 // Subroutines 24 // Subroutines
25 25
26 void writeSchema(); 26 void writeSchema();
27 27
28 void scanDirectories(); 28 void scanDirectories();
29 29
30 void parseData(); 30 void parseData();
31 31
32 private: 32 private:
33 33
34 // Input 34 // Input
35 35
36 std::string inputpath_; 36 std::string inputpath_;
37 37
38 // Output 38 // Output
39 39
40 database db_; 40 database db_;
41 41
42 // Cache 42 // Cache
43 43
44 std::list<std::string> datafiles_; 44 std::list<std::string> datafiles_;
45 45
46 }; 46 };
47 47
48 }; 48 };
49}; 49};
50 50
diff --git a/generator/main.cpp b/generator/main.cpp index 5a3d2dd..318770e 100644 --- a/generator/main.cpp +++ b/generator/main.cpp
@@ -16,7 +16,7 @@ int main(int argc, char** argv)
16 try 16 try
17 { 17 {
18 cadence::generator::generator app(argv[1], argv[2]); 18 cadence::generator::generator app(argv[1], argv[2]);
19 19
20 try 20 try
21 { 21 {
22 app.run(); 22 app.run();
diff --git a/generator/mood.cpp b/generator/mood.cpp index 9f53dce..b74211c 100644 --- a/generator/mood.cpp +++ b/generator/mood.cpp
@@ -2,7 +2,7 @@
2 2
3namespace cadence { 3namespace cadence {
4 namespace generator { 4 namespace generator {
5 5
6 // The categories are: 6 // The categories are:
7 // - party (+danceable, -acoustic, +electronic, +party) = ~.21 7 // - party (+danceable, -acoustic, +electronic, +party) = ~.21
8 // - chill (-danceable, +acoustic, -aggressive, -electronic, -party, +relaxed) = ~.49 8 // - chill (-danceable, +acoustic, -aggressive, -electronic, -party, +relaxed) = ~.49
@@ -11,7 +11,7 @@ namespace cadence {
11 // - sad (-happy, +sad) = ~.02 11 // - sad (-happy, +sad) = ~.02
12 // - instrumental (+instrumental) = ~.12 12 // - instrumental (+instrumental) = ~.12
13 // - vocal (-instrumental) = ~.10 13 // - vocal (-instrumental) = ~.10
14 14
15 mood::mood(type t, double prob) : type_(t) 15 mood::mood(type t, double prob) : type_(t)
16 { 16 {
17 if (prob >= 0.5) 17 if (prob >= 0.5)
@@ -22,73 +22,73 @@ namespace cadence {
22 probability_ = 1.0 - prob; 22 probability_ = 1.0 - prob;
23 positive_ = false; 23 positive_ = false;
24 } 24 }
25 25
26 switch (t) 26 switch (t)
27 { 27 {
28 case type::danceable: 28 case type::danceable:
29 { 29 {
30 category_ = (positive_ ? "party" : "chill"); 30 category_ = (positive_ ? "party" : "chill");
31 31
32 break; 32 break;
33 } 33 }
34 34
35 case type::acoustic: 35 case type::acoustic:
36 { 36 {
37 category_ = (positive_ ? "chill" : "party"); 37 category_ = (positive_ ? "chill" : "party");
38 38
39 break; 39 break;
40 } 40 }
41 41
42 case type::aggressive: 42 case type::aggressive:
43 { 43 {
44 category_ = (positive_ ? "crazy" : "chill"); 44 category_ = (positive_ ? "crazy" : "chill");
45 45
46 break; 46 break;
47 } 47 }
48 48
49 case type::electronic: 49 case type::electronic:
50 { 50 {
51 category_ = (positive_ ? "party" : "chill"); 51 category_ = (positive_ ? "party" : "chill");
52 52
53 break; 53 break;
54 } 54 }
55 55
56 case type::happy: 56 case type::happy:
57 { 57 {
58 category_ = (positive_ ? "happy" : "sad"); 58 category_ = (positive_ ? "happy" : "sad");
59 59
60 break; 60 break;
61 } 61 }
62 62
63 case type::party: 63 case type::party:
64 { 64 {
65 category_ = (positive_ ? "party" : "chill"); 65 category_ = (positive_ ? "party" : "chill");
66 66
67 break; 67 break;
68 } 68 }
69 69
70 case type::relaxed: 70 case type::relaxed:
71 { 71 {
72 category_ = (positive_ ? "chill" : "crazy"); 72 category_ = (positive_ ? "chill" : "crazy");
73 73
74 break; 74 break;
75 } 75 }
76 76
77 case type::sad: 77 case type::sad:
78 { 78 {
79 category_ = (positive_ ? "sad" : "happy"); 79 category_ = (positive_ ? "sad" : "happy");
80 80
81 break; 81 break;
82 } 82 }
83 83
84 case type::instrumental: 84 case type::instrumental:
85 { 85 {
86 category_ = (positive_ ? "instrumental" : "vocal"); 86 category_ = (positive_ ? "instrumental" : "vocal");
87 87
88 break; 88 break;
89 } 89 }
90 } 90 }
91 } 91 }
92 92
93 }; 93 };
94}; 94};
diff --git a/generator/mood.h b/generator/mood.h index df5d28f..c36e5ee 100644 --- a/generator/mood.h +++ b/generator/mood.h
@@ -5,7 +5,7 @@
5 5
6namespace cadence { 6namespace cadence {
7 namespace generator { 7 namespace generator {
8 8
9 class mood { 9 class mood {
10 public: 10 public:
11 enum class type { 11 enum class type {
@@ -19,41 +19,41 @@ namespace cadence {
19 sad, 19 sad,
20 instrumental 20 instrumental
21 }; 21 };
22 22
23 // Constructor 23 // Constructor
24 24
25 mood(type t, double prob); 25 mood(type t, double prob);
26 26
27 // Accessors 27 // Accessors
28 28
29 type getType() const 29 type getType() const
30 { 30 {
31 return type_; 31 return type_;
32 } 32 }
33 33
34 double getProbability() const 34 double getProbability() const
35 { 35 {
36 return probability_; 36 return probability_;
37 } 37 }
38 38
39 bool getPositive() const 39 bool getPositive() const
40 { 40 {
41 return positive_; 41 return positive_;
42 } 42 }
43 43
44 std::string getCategory() const 44 std::string getCategory() const
45 { 45 {
46 return category_; 46 return category_;
47 } 47 }
48 48
49 private: 49 private:
50 type type_; 50 type type_;
51 double probability_; 51 double probability_;
52 bool positive_; 52 bool positive_;
53 std::string category_; 53 std::string category_;
54 54
55 }; 55 };
56 56
57 }; 57 };
58}; 58};
59 59
diff --git a/generator/progress.h b/generator/progress.h index f42f701..e5cc13d 100644 --- a/generator/progress.h +++ b/generator/progress.h
@@ -5,20 +5,20 @@
5 5
6namespace cadence { 6namespace cadence {
7 namespace generator { 7 namespace generator {
8 8
9 class progress { 9 class progress {
10 private: 10 private:
11 std::string message; 11 std::string message;
12 int total; 12 int total;
13 int cur = 0; 13 int cur = 0;
14 int lprint = 0; 14 int lprint = 0;
15 15
16 public: 16 public:
17 progress(std::string message, int total) : message(message), total(total) 17 progress(std::string message, int total) : message(message), total(total)
18 { 18 {
19 std::cout << message << " 0%" << std::flush; 19 std::cout << message << " 0%" << std::flush;
20 } 20 }
21 21
22 void update(int val) 22 void update(int val)
23 { 23 {
24 if (val <= total) 24 if (val <= total)
@@ -27,29 +27,29 @@ namespace cadence {
27 } else { 27 } else {
28 cur = total; 28 cur = total;
29 } 29 }
30 30
31 int pp = cur * 100 / total; 31 int pp = cur * 100 / total;
32 if (pp != lprint) 32 if (pp != lprint)
33 { 33 {
34 lprint = pp; 34 lprint = pp;
35 35
36 std::cout << "\b\b\b\b" << std::right; 36 std::cout << "\b\b\b\b" << std::right;
37 std::cout.width(3); 37 std::cout.width(3);
38 std::cout << pp << "%" << std::flush; 38 std::cout << pp << "%" << std::flush;
39 } 39 }
40 } 40 }
41 41
42 void update() 42 void update()
43 { 43 {
44 update(cur+1); 44 update(cur+1);
45 } 45 }
46 46
47 ~progress() 47 ~progress()
48 { 48 {
49 std::cout << "\b\b\b\b100%" << std::endl; 49 std::cout << "\b\b\b\b100%" << std::endl;
50 } 50 }
51 }; 51 };
52 52
53 }; 53 };
54}; 54};
55 55