forked from exaile/moodbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodbar_exe.cpp
114 lines (95 loc) · 3.31 KB
/
moodbar_exe.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
/*
Moodbar audio timeline visualization
Copyright (C) 2015, 2017 Johannes Sasongko <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "moodbar-config.h"
#include "moodbar.h"
#include <gio/gio.h>
#include <condition_variable>
#include <iostream>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
using namespace std::string_literals;
// RAII helper types
using GCharH = std::unique_ptr<char, decltype(&g_free)>;
using GFileH = std::unique_ptr<GFile, decltype(&g_object_unref)>;
using GFileOutputStreamH =
std::unique_ptr<GFileOutputStream, decltype(&g_object_unref)>;
namespace {
void printUsage(std::ostream& stream, const char* exe) {
stream << "Usage: " << exe << " -o OUTPUT INPUT" << std::endl;
}
} // namespace
int main(int argc, char* argv[]) {
bool isCorrectUsage = false;
if (argc == 2) {
std::string argv1 = argv[1];
if (argv1 == "--version") {
std::cout << "moodbar " MOODBAR_VERSION_STR << std::endl;
return 0;
} else if (argv1 == "--help") {
printUsage(std::cout, argv[0]);
return 0;
}
} else if (argc == 4 and argv[1] == "-o"s) {
isCorrectUsage = true;
}
if (not isCorrectUsage) {
printUsage(std::cerr, (argc > 0 ? argv[0] : "moodbar"));
return 1;
}
const char* argOut = argv[2];
const char* argIn = argv[3];
// Process input file
GFileH fileIn{g_file_new_for_commandline_arg(argIn), &g_object_unref};
GCharH uriIn{g_file_get_uri(fileIn.get()), &g_free};
std::mutex mutex;
std::condition_variable cond;
int status = -1; // 0 = success, >0 = error
moodbar_init();
MoodbarPipeline mood{uriIn.get()};
mood.Finished = [&mutex, &status, &cond](bool success) {
{
std::lock_guard<std::mutex> lock(mutex);
status = success ? 0 : 1;
}
cond.notify_one();
};
mood.Start();
{ // Wait until the pipeline finishes and we get a status
std::unique_lock<std::mutex> lock(mutex);
while (status == -1) cond.wait(lock);
}
if (status != 0) return status; // There should already be an error message.
// Write output file
GFileH fileOut{g_file_new_for_commandline_arg(argOut), &g_object_unref};
GFileOutputStreamH streamOut{
g_file_replace(fileOut.get(), nullptr, FALSE,
G_FILE_CREATE_REPLACE_DESTINATION, nullptr, nullptr),
&g_object_unref};
if (not streamOut) {
std::cerr << "ERROR: Failed writing " << argOut << std::endl;
return 1;
}
const auto& data = mood.data();
bool ok =
g_output_stream_write_all(G_OUTPUT_STREAM(streamOut.get()), data.data(),
data.size(), nullptr, nullptr, nullptr);
if (not ok) {
std::cerr << "ERROR: Failed writing " << argOut << std::endl;
return 1;
}
return 0;
}