forked from ghDaYuYu/foo_flex_dsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaygainScanner.cpp
More file actions
190 lines (170 loc) · 7.21 KB
/
ReplaygainScanner.cpp
File metadata and controls
190 lines (170 loc) · 7.21 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "stdafx.h"
#include "helpers/input_helpers.h"
#include "guids.h"
#include "ReplayGainResultPopup.h"
#include "ReplayGainScanner.h"
pfc::list_t<metadb_handle_list> ReplayGainScanProcess::createInputList() {
if (m_scanMode == ScanMode::ALBUM_BY_TAGS) {
metadb_handle_list input_files = m_items;
pfc::string8_fast sort_format = m_settings.sortFormat();
sort_format += "|%path_sort%";
service_ptr_t<titleformat_object> script;
if (!static_api_ptr_t<titleformat_compiler>()->compile(script, sort_format))
script.release();
input_files.sort_by_format(script, NULL);
sort_format.truncate(sort_format.length() - strlen("|%path_sort%"));
if (!static_api_ptr_t<titleformat_compiler>()->compile(script, sort_format))
script.release();
pfc::string8_fast current_album, temp_album;
pfc::list_t<metadb_handle_list> nested_list;
metadb_handle_list list;
for (unsigned i = 0; i < input_files.get_count(); i++) {
metadb_handle_ptr ptr = input_files[i];
if (!ptr->format_title(NULL, temp_album, script, NULL)) temp_album.reset();
if (stricmp_utf8(current_album, temp_album))
{
if (list.get_count()) nested_list.add_item(list);
list.remove_all();
current_album = temp_album;
}
list.add_item(ptr);
}
if (list.get_count()) nested_list.add_item(list);
return nested_list;
} else if (m_scanMode == ScanMode::PER_FILE) {
pfc::list_t<metadb_handle_list> nested_list;
for (unsigned i = 0; i < m_items.get_count(); i++) {
metadb_handle_list list;
list.add_item(m_items[i]);
nested_list.add_item(list);
}
return nested_list;
} else if (m_scanMode == ScanMode::SINGLE_ALBUM) {
pfc::list_t<metadb_handle_list> nested_list;
metadb_handle_list list;
for (unsigned i = 0; i < m_items.get_count(); i++) {
list.add_item(m_items[i]);
}
nested_list.add_item(list);
return nested_list;
}
}
void ReplayGainScanProcess::run(threaded_process_status & p_status, abort_callback & p_abort) {
try {
SetThreadPriority(GetCurrentThread(), thread_priority_levels[m_settings.priority() - 1]);
GetSystemTimeAsFileTime(&m_start_time);
// tell the decoders that we won't seek and that we don't want looping on formats that support looping.
const t_uint32 decode_flags = input_flag_no_seeking | input_flag_no_looping;
input_helper input;
pfc::list_t<metadb_handle_list> input_files = createInputList();
ReplayGainResultList tmpResultList;
unsigned count = 0;
for (size_t group_i = 0; group_i < input_files.get_count(); ++group_i) {
const metadb_handle_list& curGroup = input_files[group_i];
replaygain_result::ptr merged;
for (size_t track_i = 0; track_i < curGroup.get_size(); ++track_i) {
ReplayGainResult r;
r.metadb = curGroup[track_i];
r.album = NULL;
r.track = NULL;
try {
p_abort.check();
p_status.set_progress(count++, m_items.get_count());
p_status.set_progress_secondary(0);
p_status.set_item_path(curGroup[track_i]->get_path());
input.open(NULL, curGroup[track_i], decode_flags, p_abort);
double length;
{ // fetch the track length for proper dual progress display;
file_info_impl info;
if (curGroup[track_i]->get_info_async(info)) length = info.get_length();
else length = 0;
}
m_output_duration += length;
audio_chunk_impl_temporary l_chunk;
replaygain_scanner::ptr scanner = static_api_ptr_t<replaygain_scanner_entry>()->instantiate();
dsp_manager tmp_manager;
tmp_manager.set_config(m_chain);
dsp_chunk_list_impl chunk_list;
double decoded = 0;
// MAIN DECODE LOOP
while (true) {
bool valid = input.run(l_chunk, p_abort); // get next chunk
if (valid) chunk_list.add_chunk(&l_chunk);
if (chunk_list.get_count() >= DSP_PROCESS_AHEAD || !valid) {
tmp_manager.run(&chunk_list, curGroup[track_i], 0, p_abort);
for (size_t chunk_i = 0; chunk_i < chunk_list.get_count(); chunk_i++) {
scanner->process_chunk(*(chunk_list.get_item(chunk_i)));
p_abort.check();
}
chunk_list.remove_all();
}
if (!valid) break;
if (length > 0) { // don't bother for unknown length tracks
decoded += l_chunk.get_duration();
if (decoded > length) decoded = length;
p_status.set_progress_secondary_float(decoded / length);
}
p_abort.check();
}
replaygain_result::ptr result = scanner->finalize();
r.track = result;
// For album repayGain
if (track_i == 0) merged = result;
else merged = merged->merge(result);
r.success = true;
} catch (std::exception const & e) {
r.errorMessage = e.what();
r.success = false;
}
tmpResultList.add_item(r);
}
// Set album repayGain if requested
if (m_scanMode != ScanMode::PER_FILE) {
for (size_t track_i = 0; track_i < tmpResultList.get_count(); ++track_i) {
tmpResultList[track_i].album = merged;
}
}
m_resultList.add_items(tmpResultList);
tmpResultList.remove_all();
}
GetSystemTimeAsFileTime(&m_end_time);
} catch (std::exception const & e) {
m_failMsg = e.what();
}
}
void ReplayGainScanProcess::on_done(HWND p_wnd, bool p_was_aborted) {
if (!p_was_aborted) {
if (!m_failMsg.is_empty()) {
popup_message::g_complain("Flex DSP: ReplayGain Scanner - failure", m_failMsg);
} else {
DWORD high = m_end_time.dwHighDateTime - m_start_time.dwHighDateTime;
DWORD low = m_end_time.dwLowDateTime - m_start_time.dwLowDateTime;
if (m_end_time.dwLowDateTime < m_start_time.dwLowDateTime) high--;
unsigned __int64 timestamp = ((unsigned __int64)(high) << 32) + low;
CReplayGainResultPopup::RunResultPopup(m_resultList, m_output_duration, timestamp, core_api::get_main_window());
}
}
}
void ReplayGainScanProcess::RunReplaygainScanner(metadb_handle_list_cref data, ScanMode scanMode) {
try {
if (data.get_count() == 0) throw pfc::exception_invalid_params();
// Query DSP chain
dsp_chain_config_impl chain;
bool ok = static_api_ptr_t<dsp_config_manager>().get_ptr()->configure_popup(chain, core_api::get_main_window(), "Dynamic DSP: ReplayGain Scanner");
if (!ok) return;
// Get setting
pfc::string8 sortFormat;
cfg_album_pattern.get(sortFormat);
Settings settings(cfg_thread_priority.get(), sortFormat);
// Create worker thread
service_ptr_t<threaded_process_callback> cb = new service_impl_t<ReplayGainScanProcess>(data, chain, scanMode, settings);
// Launch worker thread
static_api_ptr_t<threaded_process>()->run_modeless(
cb,
threaded_process::flag_show_progress_dual | threaded_process::flag_show_item | threaded_process::flag_show_abort,
core_api::get_main_window(),
"Flex DSP: ReplayGain Scanner");
} catch (std::exception const & e) {
popup_message::g_complain("Could not start Flex DSP: ReplayGain Scanner process", e);
}
}