Project

General

Profile

cue.diff

cue.cc diff - Jim Turner, March 07, 2019 04:17

View differences:

cue.cc 2019-03-06 22:05:47.668582761 -0600
40 40
{
41 41
public:
42 42
    static constexpr PluginInfo info = {N_("Cue Sheet Plugin"), PACKAGE};
43
    constexpr CueLoader () : PlaylistPlugin (info, cue_exts, false) {}
43
    constexpr CueLoader () : PlaylistPlugin (info, cue_exts, true) {}
44 44

  
45 45
    bool load (const char * filename, VFSFile & file, String & title,
46 46
     Index<PlaylistAddItem> & items);
47
    bool save (const char * filename, VFSFile & file, const char * title,
48
     const Index<PlaylistAddItem> & items);
47 49
};
48 50

  
49 51
EXPORT CueLoader aud_plugin_instance;
......
150 152

  
151 153
        if (base_tuple.valid ())
152 154
        {
153
            StringBuf tfilename = str_printf ("%s?%d", (const char *) cue_filename, track);
155
            StringBuf tfilename = str_printf ("%s?%d", cue_filename, track);
154 156
            Tuple tuple = base_tuple.ref ();
155 157
            tuple.set_filename (tfilename);
156 158
            tuple.set_int (Tuple::Track, track);
......
205 207
        cur = next;
206 208
        cur_name = next_name;
207 209
    }
210
    if (cd)
211
        cd_delete (cd);  // free.
212

  
213
    return true;
214
}
215

  
216
bool CueLoader::save (const char * filename, VFSFile & file, const char * title,
217
 const Index<PlaylistAddItem> & items)
218
{
219
    bool haveAlbumInfo = false;
220
    bool from_cuesheet;
221
    String cueTitle = String ("");
222
    String cuePerformer = String ("");
223
    StringBuf linesBuf = str_copy ("");
224
    String actual_filename;
225
    for (auto & item : items)
226
    {
227
        if (strncmp ((const char *) item.filename, "file://", 7))  // ONLY SAVE "FILE" ENTRIES, NOT URLS, ETC.
228
        {
229
            AUDWARN ("Skipping adding non-file entry (%s) from playlist (%s).\n", 
230
             (const char *) item.filename, filename);
231
        }
232
        else
233
        {
234
            from_cuesheet = strstr ((const char *) item.filename, ".cue?") ? true : false;
235
            /* FOR CUESHEET ENTRIES, SAVE ACTUAL AUDIO-FILE (NO CUESHEETS EMBEDDED IN CUE FILES)! */
236
            actual_filename = from_cuesheet ? item.tuple.get_str (Tuple::AudioFile) : item.filename;
237
            if (! actual_filename || ! actual_filename[0])
238
                continue;
239

  
240
            StringBuf path = uri_deconstruct (actual_filename, filename);
241
            if (item.tuple.valid ())
242
            {
243
                String songTitle = item.tuple.get_str (Tuple::Title);
244
                if (! songTitle)
245
                    songTitle = String (filename_get_base (actual_filename));
246
                else
247
                {
248
                    StringBuf songTitleBuff = str_copy (songTitle);
249
                    str_replace_char (songTitleBuff, '"', '\'');  // SOME TITLES HAVE DOUBLE-QUOTES?
250
                    songTitle = String (songTitleBuff);
251
                }
252
                int start_time;
253
                int start_time_frames= 0;
254
                int start_time_sec = 0;
255
                int start_time_min = 0;
256
                String songArtist = item.tuple.get_str (Tuple::Artist);
257
                if (item.tuple.is_set (Tuple::StartTime)
258
                        && (start_time = item.tuple.get_int (Tuple::StartTime)) > 0)
259
                {
260
                    start_time_frames = ((start_time % 1000) * 75) / 1000;
261
                    start_time_sec = start_time / 1000;
262
                    start_time_min = start_time_sec / 60;
263
                    start_time_sec %= 60;
264
                }
265
                str_append_printf (linesBuf, "FILE \"%s\" MP3\n  TRACK 01 AUDIO\n    TITLE \"%s\"\n    PERFORMER \"%s\"\n    INDEX 01 %02d:%02d:%02d\n", 
266
                        (const char *) path, (const char *) songTitle, (const char *) songArtist,
267
                        start_time_min, start_time_sec, start_time_frames);
268

  
269
                if (! haveAlbumInfo)
270
                {
271
                    String songAlbum = item.tuple.get_str (Tuple::Album);
272
                    if (songAlbum && songAlbum[0])
273
                    {
274
                        cueTitle = songAlbum;
275
                        String songAlbumArtist = item.tuple.get_str (Tuple::AlbumArtist);
276
                        if (songAlbumArtist && songAlbumArtist[0])
277
                            cuePerformer = songAlbumArtist;
278

  
279
                        haveAlbumInfo = true;
280
                    }
281
                }
282
            }
283
        }
284
    }
285
    StringBuf toplineBuf = str_printf ("PERFORMER \"%s\"\nTITLE \"%s\"\n", (const char *) cuePerformer, (const char *) cueTitle);
286
    if (file.fwrite (toplineBuf, 1, toplineBuf.len ()) != toplineBuf.len ())
287
        return false;
288
    if (file.fwrite (linesBuf, 1, linesBuf.len ()) != linesBuf.len ())
289
        return false;
208 290

  
209 291
    return true;
210 292
}