/src/plugins/FileDialogs/QmmpFileDialog/

cae8'>blamecommitdiff
path: root/src/plugins/Input/cdaudio/decoder_cdaudio.cpp
blob: afc6b2995f5f9b534cba821b0028a150176d32da (plain) (tree)
1
2
3
                                                                            
                                                                            
                                                                            













                                                                            
                                                                            





                                                                             

                    
                      
                             
                      


                               
                       

                          
                      

                        
                              
 

                                                             


                            

                                               
                                                                     





                                                                                  
               







                                                                                    






                                                                                  
               







                                                                                    

                
                                                              
 
                  
                    



                           
                     

                                          

 

                                 



                             
                         
     
                       

 
                                                                                                



                                                                 
                                                                       

                                                               
                           

                                                    
                                                                  
                                                    
     
                                                                                                                   

                                            
         







                                                                                      

                                                                




                                                                       

                          


        
                                                                





                                                                                               
     
 






                                                                 






                                                                                    
                                                                                                             







                                                                                                
                       

                      
                         





                                                                         

                                                                            
                                                                        









                                                       




                                                                                       
                           


                          
                             
                                                                                            

                                                  


                                                                                                   
                             
         
     
                                                                                         

                   


                                                                                                                


                             
            
                                                                                            


                                                                   
 
                                                                              


                                                  
                                               









                                                                                                         
                                                                                                                    

                                                                                        
                                                            

                                             
                                                                                                            
                                                                                                                                    


                                                                   
                                                                                                     


                                                                              

                                                                                                          

                     














                                                                                       
                                                       

                                                        
 

                                                                 

                

                                                                
                 
                                                                                           

                                                                      




                                                                  




                                                                                          







                                                                                                      


                                             




                                                                                 








                                          
                       
                   
                           


                  

                                                                        
                                


                                
                                                                           















                                                                          
                                     

                                                         
                     


                                                   
                     

                                  



                                                                                                        
     
                

 




                                                                         




                                      

                                 
                  
                    
                            
                                                      



                                          
                                         
                                                                                                   


                                                      
                     



                                           
                                                                      






                                                         
                     
     
 
                                                                                   
     
                                                                     

                                                                           

     
                                                    
     
                                                                                                                   

                                            
         




                                                               
                             

                                                                                      
                                                                 



                                                                       
                         
         


        
                                                                  


                                                           
                         
         
                                                                                               
     
                                         
                     
                                                   


                                                     
                                                                  

                                           
                                                
                


 
                                        
 


                       
                                   
 
                     

 
                                                                 
 
                    
     






                                                                                           
                                                                                            



                            

            
                                                                  
                                                
         
     









                                                       
 
 
                                     

                                                        
                    
 
/***************************************************************************
 *   Copyright (C) 2009-2016 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 <QObject>
#include <QRegExp>
#include <QSettings>
#include <QFileInfo>
#include <QDir>
#include <cdio/cdio.h>
#if LIBCDIO_VERSION_NUM <= 83
#include <cdio/cdda.h>
#else
#include <cdio/paranoia/cdda.h>
#endif
#include <cdio/audio.h>
#include <cdio/cd_types.h>
#include <cdio/logging.h>
#include <cddb/cddb.h>
#include <qmmp/buffer.h>
#include <qmmp/output.h>
#include <qmmp/qmmpsettings.h>

#define CDDA_SECTORS 4
#define CDDA_BUFFER_SIZE (CDDA_SECTORS*CDIO_CD_FRAMESIZE_RAW)

#include "decoder_cdaudio.h"

QList <CDATrack> DecoderCDAudio::m_track_cache;

static void log_handler (cdio_log_level_t level, const char *message)
{
    QString str = QString::fromLocal8Bit(message).trimmed();
    switch (level)
    {
    case CDIO_LOG_DEBUG:
        qDebug("DecoderCDAudio: cdio message: %s (level=debug)", qPrintable(str));
        return;
    case CDIO_LOG_INFO:
        qDebug("DecoderCDAudio: cdio message: %s (level=info)", qPrintable(str));
        return;
    default:
        qWarning("DecoderCDAudio: cdio message: %s (level=error)", qPrintable(str));
    }
}

static void cddb_log_handler(cddb_log_level_t level, const char *message)
{
    QString str = QString::fromLocal8Bit(message).trimmed();
    switch (level)
    {
    case CDDB_LOG_DEBUG:
        qDebug("DecoderCDAudio: cddb message: %s (level=debug)", qPrintable(str));
        return;
    case CDDB_LOG_INFO:
        qDebug("DecoderCDAudio: cddb message: %s (level=info)", qPrintable(str));
        return;
    default:
        qWarning("DecoderCDAudio: cddb message: %s (level=error)", qPrintable(str));
    }
}

// Decoder class

DecoderCDAudio::DecoderCDAudio(const QString &url) : Decoder()
{
    m_bitrate = 0;
    m_totalTime = 0;
    m_first_sector = -1;
    m_last_sector  = -1;
    m_current_sector  = -1;
    m_url = url;
    m_cdio = nullptr;
    m_buffer_at = 0;
    m_buffer = new char[CDDA_BUFFER_SIZE];
}

DecoderCDAudio::~DecoderCDAudio()
{
    m_bitrate = 0;
    if (m_cdio)
    {
        cdio_destroy(m_cdio);
        m_cdio = nullptr;
    }
    delete [] m_buffer;
}

QList<CDATrack> DecoderCDAudio::generateTrackList(const QString &device, TrackInfo::Parts parts)
{
    //read settings
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    int cd_speed = settings.value("cdaudio/speed", 0).toInt();
    bool use_cd_text = settings.value("cdaudio/cdtext", true).toBool();
    QList <CDATrack> tracks;
    cdio_log_set_handler(log_handler); //setup cdio log handler
    CdIo_t *cdio = nullptr;
    QString device_path = device;
    if (device_path.isEmpty() || device_path == "/")
        device_path = settings.value("cdaudio/device").toString();
    if (device_path.isEmpty() || device_path == "/")
    {
        char **cd_drives = cdio_get_devices_with_cap(nullptr, CDIO_FS_AUDIO, true); //get drive list with CDA disks
        // open first audio capable cd drive
        if (cd_drives && *cd_drives)
        {
            cdio = cdio_open_cd(*cd_drives);
            if (!cdio)
            {
                qWarning("DecoderCDAudio: failed to open CD.");
                cdio_free_device_list(cd_drives);
                return tracks;
            }
            qDebug("DecoderCDAudio: found cd audio capable drive \"%s\"", *cd_drives);
            device_path = QString(*cd_drives);
            cdio_free_device_list(cd_drives); //free device list
        }
        else
        {
            qWarning("DecoderCDAudio: unable to find cd audio drive.");
            cdio_free_device_list(cd_drives);
            return tracks;
        }
    }
    else
    {
        cdio = cdio_open_cd(device_path.toLatin1().constData());
        if (!cdio)
        {
            qWarning("DecoderCDAudio: failed to open CD.");
            return tracks;
        }
        qDebug("DecoderCDAudio: using cd audio capable drive \"%s\"", qPrintable(device_path));
    }

    if(!m_track_cache.isEmpty() && !cdio_get_media_changed(cdio))
    {
        qDebug("DecoderCDAudio: using track cache...");
        cdio_destroy(cdio);
        return m_track_cache;
    }

    if (cd_speed)
    {
        qDebug("DecoderCDAudio: setting drive speed to %dX.", cd_speed);
        if (cdio_set_speed(cdio, 1) != DRIVER_OP_SUCCESS)
            qWarning("DecoderCDAudio: unable to set drive speed to %dX.", cd_speed);
    }

    cdrom_drive_t *pcdrom_drive = cdio_cddap_identify_cdio(cdio, 1, nullptr); //create paranoya CD-ROM object
    //get first and last track numbers
    int first_track_number = cdio_get_first_track_num(pcdrom_drive->p_cdio);
    int last_track_number = cdio_get_last_track_num(pcdrom_drive->p_cdio);

    if ((first_track_number == CDIO_INVALID_TRACK) || (last_track_number == CDIO_INVALID_TRACK))
    {
        qWarning("DecoderCDAudio: invalid first (last) track number.");
        cdio_destroy(cdio);
        cdio = nullptr;
        return tracks;
    }
    bool use_cddb = true;
    //fill track list
    for (int i = first_track_number; i <= last_track_number; ++i)
    {
        CDATrack t;
        t.first_sector = cdio_get_track_lsn(pcdrom_drive->p_cdio, i);
        t.last_sector = cdio_get_track_last_lsn(pcdrom_drive->p_cdio, i);
        t.info.setDuration((t.last_sector - t.first_sector +1) * 1000 / 75);
        t.info.setValue(Qmmp::TRACK, i);
        t.info.setPath(QString("cdda://%1#%2").arg(device_path).arg(i));

        if(parts & TrackInfo::Properties)
        {
            t.info.setValue(Qmmp::BITRATE, 1411);
            t.info.setValue(Qmmp::SAMPLERATE, 44100);
            t.info.setValue(Qmmp::CHANNELS, 2);
            t.info.setValue(Qmmp::BITS_PER_SAMPLE, 16);
            t.info.setValue(Qmmp::FORMAT_NAME, "CDDA");
        }

        if ((t.first_sector == CDIO_INVALID_LSN) || (t.last_sector== CDIO_INVALID_LSN))
        {
            qWarning("DecoderCDAudio: invalid stard(end) lsn for the track %d.", i);
            tracks.clear();
            cdio_destroy(cdio);
            cdio = nullptr;
            return tracks;
        }
        //cd text