-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAudio_Compiler.cpp
150 lines (145 loc) · 4.13 KB
/
Audio_Compiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <stdio.h>
#include <string>
#include <dirent.h>
#include <dirent.h>
#include <sstream>
#include <vector>
#include "nlohmann/json.hpp"
#include <time.h>
#include <iterator>
#include <algorithm>
#include <fstream>
#include <filesystem>
#ifdef _WIN32
#include <filesystem>
#include "process.h"
#include <windows.h>
#include <limits.h>
#include <direct.h>
#elif _WIN64
#include <filesystem>
#include "process.h"
#include <windows.h>
#include <limits.h>
#include <direct.h>
#elif __linux__
#include <unistd.h>
#endif
using namespace std;
using json = nlohmann::json;
const string MUSIC_DIRECTORY = "./Piano Samples/";
string CWD = "";
string platform()
{
#ifdef _WIN32
return "Windows";
#elif _WIN64
return "Windows";
#elif __APPLE__ || __MACH__
return "Mac";
#elif __linux__
return "Linux";
#elif __FreeBSD__
return "FreeBSD";
#elif __unix || __unix__
return "Unix";
#else
return "Other";
#endif
}
#ifdef _WIN32
string getCurrentDir()
{
char buff[MAX_PATH];
GetModuleFileName(NULL, buff, MAX_PATH);
string::size_type position = string(buff).find_last_of("\\/");
return string(buff).substr(0, position);
}
#elif _WIN64
string getCurrentDir()
{
char buff[MAX_PATH];
GetModuleFileName(NULL, buff, MAX_PATH);
string::size_type position = string(buff).find_last_of("\\/");
return string(buff).substr(0, position);
}
#endif
void combine_audio_files(vector<string> notes, vector<double> note_types, string output_filename)
{
system(("mkdir \"" + CWD + "/Process\"").c_str());
int index = 0;
ofstream list_file;
list_file.open("Process/list.txt");
for (string &file_name : notes)
{
string s_index = to_string(index);
string trim_ammount = to_string(note_types[index]);
list_file << "file '" << s_index + ".mp3'\n";
system(("ffmpeg -t " + trim_ammount + " -i \"" + file_name + "\" -acodec copy \"" + CWD + "/Process/" + s_index + ".mp3\"").c_str());
index++;
}
list_file.close();
string command_string = "ffmpeg -f concat -i \"" + CWD + "/Process/list.txt\" -c copy \"" + CWD + "/Music/" + output_filename + "\"";
system(command_string.c_str());
if (platform() == "Windows")
system(("rd /s /q \"" + CWD + "/Process/\"").c_str());
else if (platform() == "Linux")
system(("rm -rf \"" + CWD + "/Process/\"").c_str());
}
int main(int argc, const char *argv[])
{
vector<string> list_directory;
vector<string> files_to_compile;
vector<int> note_indexes;
vector<int> note_types_values;
vector<double> trim_note_audio_values;
vector<double> note_audio_values;
int index = 0;
#if _WIN64
CWD = getCurrentDir();
#elif _WIN32
CWD = getCurrentDir();
#elif __linux__
CWD = get_current_dir_name();
#endif
replace(CWD.begin(), CWD.end(), '\\', '/'); // replace all '\\' to '/'
// GET ALL KEYS
ifstream file1(CWD + "/keys.json");
json KEYS_JSON;
file1 >> KEYS_JSON;
for (json &key : KEYS_JSON[0]["keys"])
list_directory.push_back((CWD + "/Piano Samples/" + key.get<string>() + ".mp3").c_str());
// GET ALL KEY DURATIONS
ifstream file2(CWD + "/Key_Durations.json");
json KEYS_DURATION_JSON;
file2 >> KEYS_DURATION_JSON;
for (json &key : KEYS_DURATION_JSON[0]["Key_Durations"])
note_audio_values.push_back(key.get<double>());
// NOTES
stringstream ss_notes(argv[1]);
for (int i; ss_notes >> i;)
{
note_indexes.push_back(i);
if (ss_notes.peek() == ',')
ss_notes.ignore();
}
// NOTE_TYPES
stringstream ss_types_value(argv[2]);
for (int i; ss_types_value >> i;)
{
note_types_values.push_back(i);
if (ss_types_value.peek() == ',')
ss_types_value.ignore();
}
// GET APPROPRIATE AUDIO FILES FROM INDEX
for (int i = 0; i < note_indexes.size(); i++)
files_to_compile.push_back(list_directory[note_indexes[i]]);
for (double &duration : note_audio_values)
{
trim_note_audio_values.push_back(duration / note_types_values[index]);
index++;
}
combine_audio_files(files_to_compile, trim_note_audio_values, argv[3]);
return 0;
}