1 /* This file is part of Clementine.
2  Copyright 2014, Andre Siviero <altsiviero@gmail.com>
3 
4  Clementine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Clementine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SRC_RIPPER_RIPPER_H_
19 #define SRC_RIPPER_RIPPER_H_
20 
21 #include <QMutex>
22 #include <QObject>
23 #include <cdio/cdio.h>
24 
25 #include "core/song.h"
26 #include "core/tagreaderclient.h"
27 #include "transcoder/transcoder.h"
28 
29 class QFile;
30 
31 // Rips selected tracks from an audio CD, transcodes them to a chosen
32 // format, and finally tags the files with the supplied metadata.
33 //
34 // Usage: Add tracks with AddTrack() and album metadata with
35 // SetAlbumInformation(). Then start the ripper with Start(). The ripper
36 // emits the Finished() signal when it's done or the Cancelled()
37 // signal if the ripping has been cancelled.
38 class Ripper : public QObject {
39   Q_OBJECT
40 
41  public:
42   explicit Ripper(QObject* parent = nullptr);
43   ~Ripper();
44 
45   // Adds a track to the rip list if the track number corresponds to a
46   // track on the audio cd. The track will transcoded according to the
47   // chosen TranscoderPreset.
48   void AddTrack(int track_number, const QString& title,
49                 const QString& transcoded_filename,
50                 const TranscoderPreset& preset);
51   // Sets album metadata. This information is used when tagging the
52   // final files.
53   void SetAlbumInformation(const QString& album, const QString& artist,
54                            const QString& genre, int year, int disc,
55                            Song::FileType type);
56   // Returns the number of audio tracks on the disc.
57   int TracksOnDisc() const;
58   // Returns the number of tracks added to the rip list.
59   int AddedTracks() const;
60   // Clears the rip list.
61   void ClearTracks();
62   // Returns true if a cd device was successfully opened.
63   bool CheckCDIOIsValid();
64   // Returns true if the cd media has changed.
65   bool MediaChanged() const;
66 
67 signals:
68   void Finished();
69   void Cancelled();
70   void ProgressInterval(int min, int max);
71   void Progress(int progress);
72   void RippingComplete();
73 
74  public slots:
75   void Start();
76   void Cancel();
77 
78  private slots:
79   void TranscodingJobComplete(const QString& input, const QString& output,
80                               bool success);
81   void AllTranscodingJobsComplete();
82   void LogLine(const QString& message);
83   void FileTagged(TagReaderReply* reply);
84 
85  private:
86   struct TrackInformation {
TrackInformationTrackInformation87     TrackInformation(int track_number, const QString& title,
88                      const QString& transcoded_filename,
89                      const TranscoderPreset& preset)
90         : track_number(track_number),
91           title(title),
92           transcoded_filename(transcoded_filename),
93           preset(preset) {}
94 
95     int track_number;
96     QString title;
97     QString transcoded_filename;
98     TranscoderPreset preset;
99     QString temporary_filename;
100   };
101 
102   struct AlbumInformation {
AlbumInformationAlbumInformation103     AlbumInformation() : year(0), disc(0), type(Song::Type_Unknown) {}
104 
105     QString album;
106     QString artist;
107     QString genre;
108     int year;
109     int disc;
110     Song::FileType type;
111   };
112 
113   void WriteWAVHeader(QFile* stream, int32_t i_bytecount);
114   void Rip();
115   void SetupProgressInterval();
116   void UpdateProgress();
117   void RemoveTemporaryDirectory();
118   void TagFiles();
119 
120   CdIo_t* cdio_;
121   Transcoder* transcoder_;
122   QString temporary_directory_;
123   bool cancel_requested_;
124   QMutex mutex_;
125   int finished_success_;
126   int finished_failed_;
127   int files_tagged_;
128   QList<TrackInformation> tracks_;
129   AlbumInformation album_;
130 };
131 
132 #endif  // SRC_RIPPER_RIPPER_H_
133