-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
106 lines (90 loc) · 2.43 KB
/
main.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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <mutex>
#include "fileUtils.h"
#include "platform.h"
#include "posix_thread.h"
#include "wavReader.h"
#include "lameEncoder.h"
static std::vector<std::string> files;
std::string getNextFile()
{
std::string file;
{
static std::mutex filesMtx;
std::lock_guard<std::mutex> lock(filesMtx);
if (!files.empty())
{
file = std::move(files.back());
files.pop_back();
}
}
return file;
}
static void sync(std::function<void()> _func)
{
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
_func();
}
static void convertFile(const std::string& _inFile, const std::string& _outFile)
{
wavReader reader(_inFile);
if (!reader.isOk())
{
sync([&_inFile]() { std::cout << "skipping invalid or incompatible file " << _inFile << std::endl; });
return;
}
lameEncoder encoder(_outFile, reader.numberOfChannels(), reader.sampleRate(), reader.bitsPerSample(), reader.dataSize());
if (!encoder.isOk())
{
sync([&_inFile]() { std::cout << "LAME init error for " << _inFile << std::endl; });
return;
}
if (reader.numberOfChannels() == 1)
encoder.encodeMono(reader.samples());
else
encoder.encodeStereo(reader.samples());
}
static void processFiles()
{
const auto coreCount = std::min(int(files.size()), platform::coreCount());
std::cout << "converting using " << coreCount << " threads" << std::endl;
std::vector<posix_thread> threads;
threads.reserve(coreCount);
for (auto i = 0; i < coreCount; ++i)
{
threads.emplace_back([]()
{
while (true)
{
const auto file = getNextFile();
if (file.empty())
return;
sync([&file]() { std::cout << "processing " << file << std::endl; });
convertFile(file, replaceExtension(file));
}
});
}
for (const auto& t : threads)
t.join();
}
int main(int _argc, char** _argv)
{
if (_argc < 2)
{
std::cout << "no folder provided" << std::endl;
return 1;
}
files = getFiles(_argv[1]);
if (files.empty())
{
std::cout << "no WAV files found" << std::endl;
return 1;
}
processFiles();
std::cout << "done" << std::endl;
return 0;
}