-
Notifications
You must be signed in to change notification settings - Fork 8
/
HandlerMP3.cpp
190 lines (167 loc) · 5.29 KB
/
HandlerMP3.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
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
189
190
#include "HandlerMP3.h"
#include "EncoderMP3.h"
#include "resource.h"
#include "ShellMetadata.h"
#include "Utility.h"
HandlerMP3::HandlerMP3() :
Handler()
{
}
HandlerMP3::~HandlerMP3()
{
}
std::wstring HandlerMP3::GetDescription() const
{
std::wstring description = L"LAME MP3";
const char* version = get_lame_version();
if ( nullptr != version ) {
description += L" " + UTF8ToWideString( version );
}
return description;
}
std::set<std::wstring> HandlerMP3::GetSupportedFileExtensions() const
{
return { L"mp3" };
}
bool HandlerMP3::GetTags( const std::wstring& /*filename*/, Tags& /*tags*/ ) const
{
return false;
}
bool HandlerMP3::SetTags( const std::wstring& filename, const Tags& tags ) const
{
return ShellMetadata::Set( filename, tags );
}
Decoder::Ptr HandlerMP3::OpenDecoder( const std::wstring& /*filename*/, const Decoder::Context /*context*/ ) const
{
return nullptr;
}
Encoder::Ptr HandlerMP3::OpenEncoder() const
{
Encoder::Ptr encoder( new EncoderMP3() );
return encoder;
}
bool HandlerMP3::IsDecoder() const
{
return false;
}
bool HandlerMP3::IsEncoder() const
{
return true;
}
bool HandlerMP3::CanConfigureEncoder() const
{
return true;
}
INT_PTR CALLBACK HandlerMP3::DialogProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message ) {
case WM_INITDIALOG : {
ConfigurationInfo* config = reinterpret_cast<ConfigurationInfo*>( lParam );
if ( ( nullptr != config ) && ( nullptr != config->m_Handler ) ) {
SetWindowLongPtr( hwnd, DWLP_USER, reinterpret_cast<LPARAM>( config ) );
config->m_Handler->OnConfigureInit( hwnd, config->m_Settings );
}
break;
}
case WM_DESTROY : {
SetWindowLongPtr( hwnd, DWLP_USER, 0 );
break;
}
case WM_COMMAND : {
switch ( LOWORD( wParam ) ) {
case IDCANCEL :
case IDOK : {
ConfigurationInfo* config = reinterpret_cast<ConfigurationInfo*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( ( nullptr != config ) && ( nullptr != config->m_Handler ) ) {
config->m_Handler->OnConfigureClose( hwnd, config->m_Settings );
}
EndDialog( hwnd, IDOK == LOWORD( wParam ) );
return TRUE;
}
case IDC_ENCODER_MP3_DEFAULT : {
ConfigurationInfo* config = reinterpret_cast<ConfigurationInfo*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( ( nullptr != config ) && ( nullptr != config->m_Handler ) ) {
config->m_Handler->OnConfigureDefault( hwnd, config->m_Settings );
}
break;
}
default : {
break;
}
}
break;
}
case WM_NOTIFY : {
LPNMHDR nmhdr = reinterpret_cast<LPNMHDR>( lParam );
if ( ( nullptr != nmhdr ) && ( nmhdr->code == TTN_GETDISPINFO ) ) {
ConfigurationInfo* config = reinterpret_cast<ConfigurationInfo*>( GetWindowLongPtr( hwnd, DWLP_USER ) );
if ( nullptr != config ) {
const HWND sliderWnd = reinterpret_cast<HWND>( nmhdr->idFrom );
const std::wstring tooltip = config->m_Handler->GetTooltip( config->m_hInst, sliderWnd );
LPNMTTDISPINFO info = reinterpret_cast<LPNMTTDISPINFO>( lParam );
wcscpy_s( info->szText, tooltip.c_str() );
}
}
break;
}
default : {
break;
}
}
return FALSE;
}
bool HandlerMP3::ConfigureEncoder( const HINSTANCE instance, const HWND parent, std::string& settings ) const
{
ConfigurationInfo* config = new ConfigurationInfo( { settings, this, instance } );
const bool configured = DialogBoxParam( instance, MAKEINTRESOURCE( IDD_ENCODER_MP3 ), parent, DialogProc, reinterpret_cast<LPARAM>( config ) );
delete config;
return configured;
}
void HandlerMP3::OnConfigureInit( const HWND hwnd, const std::string& settings ) const
{
CentreDialog( hwnd );
const HWND sliderWnd = GetDlgItem( hwnd, IDC_ENCODER_MP3_VBR );
if ( nullptr != sliderWnd ) {
SendMessage( sliderWnd, TBM_SETRANGEMIN, TRUE /*redraw*/, 0 );
SendMessage( sliderWnd, TBM_SETRANGEMAX, TRUE /*redraw*/, 9 );
SendMessage( sliderWnd, TBM_SETTICFREQ, 1, 0 );
SetQuality( sliderWnd, EncoderMP3::GetVBRQuality( settings ) );
}
}
void HandlerMP3::OnConfigureDefault( const HWND hwnd, std::string& settings ) const
{
settings.clear();
const HWND sliderWnd = GetDlgItem( hwnd, IDC_ENCODER_MP3_VBR );
if ( nullptr != sliderWnd ) {
SetQuality( sliderWnd, EncoderMP3::GetVBRQuality( settings ) );
}
}
void HandlerMP3::OnConfigureClose( const HWND hwnd, std::string& settings ) const
{
const HWND sliderWnd = GetDlgItem( hwnd, IDC_ENCODER_MP3_VBR );
if ( nullptr != sliderWnd ) {
settings = std::to_string( GetQuality( sliderWnd ) );
}
}
int HandlerMP3::GetQuality( const HWND slider ) const
{
const int quality = static_cast<int>( SendMessage( slider, TBM_GETRANGEMAX, 0, 0 ) ) - static_cast<int>( SendMessage( slider, TBM_GETPOS, 0, 0 ) );
return quality;
}
void HandlerMP3::SetQuality( const HWND slider, const int quality ) const
{
const int position = static_cast<int>( SendMessage( slider, TBM_GETRANGEMAX, 0, 0 ) ) - quality;
SendMessage( slider, TBM_SETPOS, TRUE /*redraw*/, position );
}
std::wstring HandlerMP3::GetTooltip( const HINSTANCE instance, const HWND slider ) const
{
const int quality = GetQuality( slider );
const int bufSize = 32;
WCHAR buffer[ bufSize ] = {};
LoadString( instance, IDS_QUALITY_VBR, buffer, bufSize );
const std::wstring tooltip = std::wstring( buffer ) + L": " + std::to_wstring( quality );
return tooltip;
}
void HandlerMP3::SettingsChanged( Settings& /*settings*/ )
{
}