aboutsummaryrefslogtreecommitdiff
path: root/src/qmmpui/pluginitem_p.h
blob: 8c2d6306c0da3562a362319bb207b3e8fff4334a (plain) (blame)
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
/***************************************************************************
 *   Copyright (C) 2011 by Ilya Kotov                                      *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#ifndef PLUGINITEM_P_H
#define PLUGINITEM_P_H

#include <QTreeWidgetItem>

/**
   @author Ilya Kotov <forkotov02@hotmail.ru>
*/

class QWidget;
class InputSourceFactory;
class DecoderFactory;
class EngineFactory;
class OutputFactory;
class VisualFactory;
class EffectFactory;
class GeneralFactory;
class OutputFactory;
class FileDialogFactory;
class UiFactory;

class PluginItem : public QTreeWidgetItem
{
public:

    PluginItem(QTreeWidgetItem *parent, InputSourceFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, DecoderFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, EngineFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, EffectFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, VisualFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, GeneralFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, OutputFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, FileDialogFactory *factory, const QString &path);
    PluginItem(QTreeWidgetItem *parent, UiFactory *factory, const QString &path);
    ~PluginItem();

    enum PluginType
    {
        TRANSPORT = QTreeWidgetItem::UserType,
        DECODER,
        ENGINE,
        EFFECT,
        VISUAL,
        GENERAL,
        OUTPUT,
        FILE_DIALOG,
        USER_INTERFACE
    };

    bool hasAbout() const;
    bool hasSettings() const;
    void showAbout(QWidget *parent);
    void showSettings(QWidget *parent);
    void setEnabled(bool enabled);


private:
    bool m_has_about;
    bool m_has_config;
    void *m_factory;
};

#endif
src/plugins/General/rgscan/rgscandialog.cpp?id=5bcb2dcc6048e4df6898640a0f7a053dc75d8ec9'>5bcb2dcc6
1e31fe896
85cae67a5
5bcb2dcc6
c8134e1cf
85cae67a5
5bcb2dcc6
c8134e1cf
32dc2e325
b434bc9dc
c8134e1cf
b434bc9dc
32dc2e325
c8134e1cf
b434bc9dc

26d4175f4

3f249105c
845322411
519c571ee

5bcb2dcc6
c8134e1cf
264f3ddbe
5bcb2dcc6
c8134e1cf

5bcb2dcc6





85cae67a5


86de1c2d7
38295c0a5
86de1c2d7
38295c0a5


4163cabef
85cae67a5


970555264

38295c0a5

970555264
85cae67a5


37370fab0
85cae67a5


8878f195f
e574a099b
32dc2e325
8878f195f
32dc2e325

8878f195f
32dc2e325

4163cabef



e574a099b
4163cabef








8878f195f




85cae67a5

970555264






188288cea
32dc2e325




970555264



1e31fe896
970555264
0b4c28342
970555264




4163cabef
970555264
6b48eaf59
37370fab0
6b48eaf59
37370fab0
1e31fe896
6b48eaf59
6d005aae5

37370fab0






6b48eaf59
37370fab0
1e31fe896
37370fab0










1e31fe896
37370fab0







38295c0a5


37370fab0
38295c0a5
6b48eaf59

37370fab0
6d005aae5
1e31fe896
37370fab0


6d005aae5
37370fab0





e574a099b

6b48eaf59

37370fab0
37370fab0
86de1c2d7

970555264


86de1c2d7


38295c0a5


4163cabef
86de1c2d7


970555264

32dc2e325
970555264
1e31fe896
bc573bf07
970555264
bc573bf07
970555264
32dc2e325



188288cea
32dc2e325
1e31fe896
32dc2e325



b917024aa
970555264
37370fab0


c8134e1cf
37370fab0

c8134e1cf
37370fab0


38295c0a5


845322411
38295c0a5



845322411
38295c0a5

b434bc9dc









5bcb2dcc6













a615b3fe4




































5bcb2dcc6













b434bc9dc

















38295c0a5






1e31fe896
38295c0a5
5bcb2dcc6
38295c0a5
5bcb2dcc6



a615b3fe4

5bcb2dcc6

38295c0a5
5bcb2dcc6


38295c0a5
3f249105c
38295c0a5
5bcb2dcc6


38295c0a5
3f249105c
26d4175f4




3f249105c





3a08cb8fb
b434bc9dc


3a08cb8fb
b434bc9dc
519c571ee





38295c0a5

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                                                                            
                                                                            
                                                                            



















                                                                             
                    


                                     
                                 

                            


                               
                              
                               

                                           
                           
                            
                      
                          

                         
                                       
 






                                           


                                                                                              
                                                                                          
                                                                                 
 

                                                              
                                                      
     
                      
                                                                   
                     
                         
                                         
                     
                                
                                                                 
                     
 
                                                               

                                             

                                              
                                             
                                        

                                                     
         
                                        
                                                   
                                                                

                                                          





                                                                                              


                                                
                                        
                   
                                                                 


                                                                                          
                                                                                                 


                             

           

                                     
 


                                               
                                        


                                                                                  
                                             
                                                                   
 
                                  

                                                                               
                           

                     



                                                                                            
                                                                      








                                                                                                                          




                                                                                                         

     






                                                                              
                                                   




                                                                                                



                        
                                                        
     
                                                        




                            
                                                         
                                                     
 
                                                                                       
 
                             
                                                      
         

                                     






                                                                      
         
                                       
                                                       










                                                                                              
                                                           







                                                                     


                                               
                      
                                                     

                                                             
                                                                                      
                               
                                                                                


                                    
                                 





                                                                                                       

                                                                                    

         
                     
                             

                                           


     


                                                                 


                                                                                
                                                                                       


                      

                         
                            
               
                                                 
     
                       
     
                                                 



                           
                                                             
 
                                                  



                                 
                   
 


                                                      
                                                                                   

                          
                                                         


                         


                                                       
                                                                    



                                                       
                                                                 

 









                                                               













                                                                                                      




































                                                                                                             













                                                                                                      

















                                                                                






                                                         
                                                                  
     
                                                           
 



                                                           

                                                                                  

                                     
         


                                                             
         
                                        
         


                                                                
         
                                          




                                                                  





                                                              
                                              


                                                          
                        
         





                                                                

     
/***************************************************************************
 *   Copyright (C) 2013-2020 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include <QSettings>
#include <QProgressBar>
#include <QThreadPool>
#include <QSettings>
#include <qmmpui/playlisttrack.h>
#include <qmmpui/metadataformatter.h>
#include <qmmpui/filedialog.h>
#include <qmmp/metadatamanager.h>
#include <taglib/mpegfile.h>
#include <taglib/apetag.h>
#include <taglib/flacfile.h>
#include <taglib/xiphcomment.h>
#include <taglib/oggflacfile.h>
#include <taglib/vorbisfile.h>
#include <taglib/wavpackfile.h>
#include <taglib/id3v2tag.h>
#include <taglib/textidentificationframe.h>
#include <taglib/mp4file.h>
#include <taglib/opusfile.h>
#include "rgscanner.h"
#include "gain_analysis.h"
#include "rgscandialog.h"

#define FILE_SKIPPED (Qt::UserRole + 1)

struct ReplayGainInfoItem
{
    QMap<Qmmp::ReplayGainKey, double> info;
    QString url;
    GainHandle_t *handle;
};

RGScanDialog::RGScanDialog(QList <PlayListTrack *> tracks,  QWidget *parent) : QDialog(parent)
{
    m_ui.setupUi(this);
    m_ui.tableWidget->verticalHeader()->setDefaultSectionSize(fontMetrics().height() + 3);
    m_ui.tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    QStringList paths;
    MetaDataFormatter formatter("%if(%p&%t,%p - %t,%f) - %l");
    for(const PlayListTrack *track : qAsConst(tracks))
    {
        //skip streams
        if(track->duration() == 0 || track->path().contains("://"))
            continue;
        //skip duplicates
        if(paths.contains(track->path()))
            continue;
        //skip unsupported files
        if(!MetaDataManager::instance()->supports(track->path()))
            continue;

        QString ext = track->path().section(".", -1).toLower();

        if((ext == "mp3") || //mpeg 1 layer 3
                ext == "flac" || //native flac
                ext == "oga" || //ogg flac
                ext == "ogg" ||  //ogg vorbis
                ext == "wv" || //wavpack
                ext == "m4a" || //aac (mp4 container)
                ext == "opus")
        {
            paths.append(track->path());
            QString name = formatter.format(track);
            QTableWidgetItem *item = new QTableWidgetItem(name);
            item->setData(Qt::UserRole, track->path());
            item->setData(Qt::ToolTipRole, track->path());
            m_ui.tableWidget->insertRow(m_ui.tableWidget->rowCount());
            m_ui.tableWidget->setItem(m_ui.tableWidget->rowCount() - 1, 0, item);
            QProgressBar *progressBar = new QProgressBar(this);
            progressBar->setRange(0, 100);
            m_ui.tableWidget->setCellWidget(m_ui.tableWidget->rowCount() - 1, 1, progressBar);
        }
    }

    m_ui.tableWidget->resizeColumnsToContents();
    m_ui.writeButton->setEnabled(false);
    //read settings
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    restoreGeometry(settings.value("RGScanner/geometry").toByteArray());
    m_ui.trackCheckBox->setChecked(settings.value("RGScanner/write_track",true).toBool());
    m_ui.albumCheckBox->setChecked(settings.value("RGScanner/write_album",true).toBool());
    m_ui.skipScannedCheckBox->setChecked(settings.value("RGScanner/skip_scanned",true).toBool());
}

RGScanDialog::~RGScanDialog()
{
    stop();
    qDeleteAll(m_replayGainItemList);
    m_replayGainItemList.clear();
}

void RGScanDialog::on_calculateButton_clicked()
{
    m_ui.writeButton->setEnabled(false);
    for(int i = 0; i < m_ui.tableWidget->rowCount(); ++i)
    {
        QString url = m_ui.tableWidget->item(i, 0)->data(Qt::UserRole).toString();
        RGScanner *scanner = new RGScanner();
        m_ui.tableWidget->item(i, 0)->setData(FILE_SKIPPED, false);

        if(!scanner->prepare(url))
        {
            m_ui.tableWidget->setItem(i, 2, new QTableWidgetItem(tr("Error")));
            delete scanner;
            continue;
        }

        if(m_ui.skipScannedCheckBox->isChecked() && !scanner->oldReplayGainInfo().isEmpty())
        {
            qDebug("RGScanDialog: skipping scanned file..");
            m_ui.tableWidget->item(i, 0)->setData(FILE_SKIPPED, true);
            QMap<Qmmp::ReplayGainKey, double> rg = scanner->oldReplayGainInfo();
            m_ui.tableWidget->setItem(i, 2, new QTableWidgetItem(tr("%1 dB").arg(rg.value(Qmmp::REPLAYGAIN_TRACK_GAIN))));
            m_ui.tableWidget->setItem(i, 3, new QTableWidgetItem(tr("%1 dB").arg(rg.value(Qmmp::REPLAYGAIN_ALBUM_GAIN))));
            m_ui.tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(rg.value(Qmmp::REPLAYGAIN_TRACK_PEAK))));
            m_ui.tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(rg.value(Qmmp::REPLAYGAIN_ALBUM_PEAK))));
            delete scanner;
            continue;
        }

        scanner->setAutoDelete(false);
        m_scanners.append(scanner);
        connect(scanner, SIGNAL(progress(int)), m_ui.tableWidget->cellWidget(i, 1), SLOT(setValue(int)));
        connect(scanner, SIGNAL(finished(QString)), SLOT(onScanFinished(QString)));
        QThreadPool::globalInstance()->start(scanner);
    }
}

void RGScanDialog::onScanFinished(QString url)
{
    for(int i = 0; i < m_ui.tableWidget->rowCount(); ++i)
    {
        if(url != m_ui.tableWidget->item(i, 0)->data(Qt::UserRole).toString())
            continue;
        RGScanner *scanner = findScannerByUrl(url);
        if(!scanner)
            qFatal("RGScanDialog: unable to find scanner by URL!");
        m_ui.tableWidget->setItem(i, 2, new QTableWidgetItem(tr("%1 dB").arg(scanner->gain())));
        m_ui.tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(scanner->peak())));
        break;
    }

    bool stopped = true;

    for(const RGScanner *scanner : qAsConst(m_scanners))
    {
        if(scanner->isRunning() || scanner->isPending())
            stopped = false;
    }

    if(stopped)
    {
        qDebug("RGScanDialog: all threads are finished");
        QThreadPool::globalInstance()->waitForDone();

        QMultiMap<QString, ReplayGainInfoItem*> itemGroupMap; //items grouped  by album

        //group by album name
        for(RGScanner *scanner : qAsConst(m_scanners))
        {
            if(!scanner->hasValues())
                continue;
            ReplayGainInfoItem *item = new ReplayGainInfoItem;
            item->info[Qmmp::REPLAYGAIN_TRACK_GAIN] = scanner->gain();
            item->info[Qmmp::REPLAYGAIN_TRACK_PEAK] = scanner->peak();
            item->url = scanner->url();
            item->handle = scanner->handle();
            QString album = getAlbumName(item->url);
            itemGroupMap.insert(album, item);
        }
        //calculate album peak and gain
        for(const QString &album : itemGroupMap.keys())
        {
            QList<ReplayGainInfoItem*> items = itemGroupMap.values(album);
            GainHandle_t **a = (GainHandle_t **) malloc(items.count()*sizeof(GainHandle_t *));
            double album_peak = 0;
            for(int i = 0; i < items.count(); ++i)
            {
                a[i] = items[i]->handle;
                album_peak = qMax(items[i]->info[Qmmp::REPLAYGAIN_TRACK_PEAK], album_peak);
            }
            double album_gain = GetAlbumGain(a, items.count());
            free(a);
            for(ReplayGainInfoItem *item : qAsConst(items))
            {
                item->info[Qmmp::REPLAYGAIN_ALBUM_PEAK] = album_peak;
                item->info[Qmmp::REPLAYGAIN_ALBUM_GAIN] = album_gain;
            }
        }
        //clear scanners
        qDeleteAll(m_scanners);
        m_scanners.clear();
        //clear previous replaygain information
        qDeleteAll(m_replayGainItemList);
        m_replayGainItemList.clear();
        //update table
        m_replayGainItemList = itemGroupMap.values();
        for(int i = 0; i < m_ui.tableWidget->rowCount(); ++i)
        {
            QString url = m_ui.tableWidget->item(i, 0)->data(Qt::UserRole).toString();
            bool found = false;
            for(const ReplayGainInfoItem *item : qAsConst(m_replayGainItemList))
            {
                if(item->url == url)
                {
                    found = true;
                    double album_gain = item->info[Qmmp::REPLAYGAIN_ALBUM_GAIN];
                    double album_peak = item->info[Qmmp::REPLAYGAIN_ALBUM_PEAK];
                    m_ui.tableWidget->setItem(i, 3, new QTableWidgetItem(tr("%1 dB").arg(album_gain)));
                    m_ui.tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(album_peak)));
                }
            }
            if(!found && !m_ui.tableWidget->item(i, 0)->data(FILE_SKIPPED).toBool())
                m_ui.tableWidget->setItem(i, 3, new QTableWidgetItem(tr("Error")));
        }

        //clear items
        itemGroupMap.clear();

        m_ui.writeButton->setEnabled(true);
    }
}

void RGScanDialog::reject()
{