static void playqueue_send_trackinfochanged (playItem_t *track) { ddb_event_track_t *ev = (ddb_event_track_t *)messagepump_event_alloc (DB_EV_TRACKINFOCHANGED); ev->track = DB_PLAYITEM (track); if (track) { pl_item_ref (track); } messagepump_push_event ((ddb_event_t*)ev, DDB_PLAYLIST_CHANGE_PLAYQUEUE, 0); }
static gboolean set_dnd_cursor_idle (gpointer data) { DdbListview *listview = DDB_LISTVIEW (lookup_widget (mainwin, "playlist")); if (!data) { ddb_listview_set_cursor (listview, -1); return FALSE; } int cursor = deadbeef->pl_get_idx_of (DB_PLAYITEM (data)); ddb_listview_set_cursor (listview, cursor); return FALSE; }
static gboolean set_dnd_cursor_idle (gpointer data) { if (!data) { deadbeef->pl_set_cursor (PL_MAIN, -1); deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, 0, 0); return FALSE; } int cursor = deadbeef->pl_get_idx_of (DB_PLAYITEM (data)); deadbeef->pl_set_cursor (PL_MAIN, cursor); deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, 0, 0); return FALSE; }
int convert (DB_playItem_t *it, const char *out, int output_bps, int output_is_float, ddb_encoder_preset_t *encoder_preset, ddb_dsp_preset_t *dsp_preset, int *abort) { char *buffer = NULL; char *dspbuffer = NULL; if (deadbeef->pl_get_item_duration (it) <= 0) { deadbeef->pl_lock (); fprintf (stderr, "converter: stream %s doesn't have finite length, skipped\n", deadbeef->pl_find_meta (it, ":URI")); deadbeef->pl_unlock (); return -1; } int err = -1; FILE *enc_pipe = NULL; int temp_file = -1; DB_decoder_t *dec = NULL; DB_fileinfo_t *fileinfo = NULL; char input_file_name[PATH_MAX] = ""; deadbeef->pl_lock (); dec = (DB_decoder_t *)deadbeef->plug_get_for_id (deadbeef->pl_find_meta (it, ":DECODER")); deadbeef->pl_unlock (); if (dec) { fileinfo = dec->open (0); if (fileinfo && dec->init (fileinfo, DB_PLAYITEM (it)) != 0) { deadbeef->pl_lock (); fprintf (stderr, "converter: failed to decode file %s\n", deadbeef->pl_find_meta (it, ":URI")); deadbeef->pl_unlock (); goto error; } if (fileinfo) { if (output_bps == -1) { output_bps = fileinfo->fmt.bps; output_is_float = fileinfo->fmt.is_float; } char *final_path = strdupa (out); char *sep = strrchr (final_path, '/'); if (sep) { *sep = 0; if (!check_dir (final_path, 0755)) { fprintf (stderr, "converter: failed to create output folder: %s\n", final_path); goto error; } } if (encoder_preset->method == DDB_ENCODER_METHOD_FILE) { const char *tmp = getenv ("TMPDIR"); if (!tmp) { tmp = "/tmp"; } snprintf (input_file_name, sizeof (input_file_name), "%s/ddbconvXXXXXX", tmp); char *res = mktemp (input_file_name); strcat (input_file_name, ".wav"); } else { strcpy (input_file_name, "-"); } char enc[2000]; memset (enc, 0, sizeof (enc)); char escaped_out[PATH_MAX]; escape_filepath (out, escaped_out, sizeof (escaped_out)); // formatting: %o = outfile, %i = infile char *e = encoder_preset->encoder; char *o = enc; *o = 0; int len = sizeof (enc); while (e && *e) { if (len <= 0) { fprintf (stderr, "converter: failed to assemble encoder command line - buffer is not big enough, try to shorten your parameters. max allowed length is %u characters\n", (unsigned)sizeof (enc)); goto error; } if (e[0] == '%' && e[1]) { if (e[1] == 'o') { int l = snprintf (o, len, "\"%s\"", escaped_out); o += l; len -= l; } else if (e[1] == 'i') { int l = snprintf (o, len, "\"%s\"", input_file_name); o += l; len -= l; } else { strncpy (o, e, 2); o += 2; len -= 2; } e += 2; } else { *o++ = *e++; *o = 0; len--; } } fprintf (stderr, "converter: will encode using: %s\n", enc[0] ? enc : "internal RIFF WAVE writer"); mode_t wrmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; if (!encoder_preset->encoder[0]) { // write to wave file trace ("opening %s\n", out); temp_file = open (out, O_LARGEFILE | O_WRONLY | O_CREAT | O_TRUNC, wrmode); if (temp_file == -1) { fprintf (stderr, "converter: failed to open output wave file %s\n", out); goto error; } } else if (encoder_preset->method == DDB_ENCODER_METHOD_FILE) { temp_file = open (input_file_name, O_LARGEFILE | O_WRONLY | O_CREAT | O_TRUNC, wrmode); if (temp_file == -1) { fprintf (stderr, "converter: failed to open temp file %s\n", input_file_name); goto error; } } else { enc_pipe = popen (enc, "w"); if (!enc_pipe) { fprintf (stderr, "converter: failed to open encoder\n"); goto error; } } if (temp_file == -1 && enc_pipe) { temp_file = fileno (enc_pipe); } // write wave header char wavehdr_int[] = { 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61 }; char wavehdr_float[] = { 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x28, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x02, 0x00, 0x40, 0x1f, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x08, 0x00, 0x20, 0x00, 0x16, 0x00, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71, 0x66, 0x61, 0x63, 0x74, 0x04, 0x00, 0x00, 0x00, 0xc5, 0x5b, 0x00, 0x00, 0x64, 0x61, 0x74, 0x61 }; char *wavehdr = output_is_float ? wavehdr_float : wavehdr_int; int wavehdr_size = output_is_float ? sizeof (wavehdr_float) : sizeof (wavehdr_int); int header_written = 0; uint32_t outsize = 0; uint32_t outsr = fileinfo->fmt.samplerate; uint16_t outch = fileinfo->fmt.channels; int samplesize = fileinfo->fmt.channels * fileinfo->fmt.bps / 8; // block size int bs = 2000 * samplesize; // expected buffer size after worst-case dsp int dspsize = bs/samplesize*sizeof(float)*8*48; buffer = malloc (dspsize); // account for up to float32 7.1 resampled to 48x ratio dspbuffer = malloc (dspsize); int eof = 0; for (;;) { if (eof) { break; } if (abort && *abort) { break; } int sz = dec->read (fileinfo, buffer, bs); if (sz != bs) { eof = 1; } if (dsp_preset) { ddb_waveformat_t fmt; ddb_waveformat_t outfmt; memcpy (&fmt, &fileinfo->fmt, sizeof (fmt)); memcpy (&outfmt, &fileinfo->fmt, sizeof (fmt)); fmt.bps = 32; fmt.is_float = 1; deadbeef->pcm_convert (&fileinfo->fmt, buffer, &fmt, dspbuffer, sz); ddb_dsp_context_t *dsp = dsp_preset->chain; int frames = sz / samplesize; while (dsp) { frames = dsp->plugin->process (dsp, (float *)dspbuffer, frames, dspsize / (fmt.channels * 4), &fmt, NULL); if (frames <= 0) { break; } dsp = dsp->next; } if (frames <= 0) { fprintf (stderr, "converter: dsp error, please check you dsp preset\n"); goto error; } outsr = fmt.samplerate; outch = fmt.channels; outfmt.bps = output_bps; outfmt.is_float = output_is_float; outfmt.channels = outch; outfmt.samplerate = outsr; int n = deadbeef->pcm_convert (&fmt, dspbuffer, &outfmt, buffer, frames * sizeof (float) * fmt.channels); sz = n; } else if (fileinfo->fmt.bps != output_bps || fileinfo->fmt.is_float != output_is_float) { ddb_waveformat_t outfmt; memcpy (&outfmt, &fileinfo->fmt, sizeof (outfmt)); outfmt.bps = output_bps; outfmt.is_float = output_is_float; outfmt.channels = outch; outfmt.samplerate = outsr; int frames = sz / samplesize; int n = deadbeef->pcm_convert (&fileinfo->fmt, buffer, &outfmt, dspbuffer, frames * samplesize); memcpy (buffer, dspbuffer, n); sz = n; } outsize += sz; if (!header_written) { uint64_t size = (int64_t)(it->endsample-it->startsample) * outch * output_bps / 8; if (!size) { size = (double)deadbeef->pl_get_item_duration (it) * fileinfo->fmt.samplerate * outch * output_bps / 8; } if (outsr != fileinfo->fmt.samplerate) { uint64_t temp = size; temp *= outsr; temp /= fileinfo->fmt.samplerate; size = temp; } uint64_t chunksize; chunksize = size + 40; // for float, add 36 more if (output_is_float) { chunksize += 36; } uint32_t size32 = 0xffffffff; if (chunksize <= 0xffffffff) { size32 = chunksize; } memcpy (&wavehdr[4], &size32, 4); memcpy (&wavehdr[22], &outch, 2); memcpy (&wavehdr[24], &outsr, 4); uint16_t blockalign = outch * output_bps / 8; memcpy (&wavehdr[32], &blockalign, 2); memcpy (&wavehdr[34], &output_bps, 2); size32 = 0xffffffff; if (size <= 0xffffffff) { size32 = size; } if (wavehdr_size != write (temp_file, wavehdr, wavehdr_size)) { fprintf (stderr, "converter: wave header write error\n"); goto error; } if (encoder_preset->method == DDB_ENCODER_METHOD_PIPE) { size32 = 0; } if (write (temp_file, &size32, sizeof (size32)) != sizeof (size32)) { fprintf (stderr, "converter: wave header size write error\n"); goto error; } header_written = 1; } int64_t res = write (temp_file, buffer, sz); if (sz != res) { fprintf (stderr, "converter: write error (%"PRId64" bytes written out of %d)\n", res, sz); goto error; } } if (abort && *abort) { goto error; } if (temp_file != -1 && (!enc_pipe || temp_file != fileno (enc_pipe))) { lseek (temp_file, wavehdr_size, SEEK_SET); if (4 != write (temp_file, &outsize, 4)) { fprintf (stderr, "converter: data size write error\n"); goto error; } if (temp_file != -1 && (!enc_pipe || temp_file != fileno (enc_pipe))) { close (temp_file); temp_file = -1; } } if (encoder_preset->encoder[0] && encoder_preset->method == DDB_ENCODER_METHOD_FILE) { enc_pipe = popen (enc, "w"); } } } err = 0; error: if (buffer) { free (buffer); buffer = NULL; } if (dspbuffer) { free (dspbuffer); dspbuffer = NULL; } if (temp_file != -1 && (!enc_pipe || temp_file != fileno (enc_pipe))) { close (temp_file); temp_file = -1; } if (enc_pipe) { pclose (enc_pipe); enc_pipe = NULL; } if (dec && fileinfo) { dec->free (fileinfo); fileinfo = NULL; } if (abort && *abort && out[0]) { unlink (out); } if (input_file_name[0] && strcmp (input_file_name, "-")) { unlink (input_file_name); } if (err != 0) { return err; } // write junklib tags DB_playItem_t *out_it = NULL; if (encoder_preset->tag_id3v2 || encoder_preset->tag_id3v1 || encoder_preset->tag_apev2 || encoder_preset->tag_flac || encoder_preset->tag_oggvorbis) { out_it = deadbeef->pl_item_alloc (); deadbeef->pl_item_copy (out_it, it); deadbeef->pl_set_item_flags (out_it, 0); DB_metaInfo_t *m = deadbeef->pl_get_metadata_head (out_it); while (m) { DB_metaInfo_t *next = m->next; if (m->key[0] == ':' || m->key[0] == '!' || !strcasecmp (m->key, "cuesheet")) { deadbeef->pl_delete_metadata (out_it, m); } m = next; } deadbeef->pl_replace_meta (out_it, ":URI", out); } uint32_t tagflags = 0; if (encoder_preset->tag_id3v2) { tagflags |= JUNK_WRITE_ID3V2; } if (encoder_preset->tag_id3v1) { tagflags |= JUNK_WRITE_ID3V1; } if (encoder_preset->tag_apev2) { tagflags |= JUNK_WRITE_APEV2; } if (tagflags) { tagflags |= JUNK_STRIP_ID3V2 | JUNK_STRIP_APEV2 | JUNK_STRIP_ID3V1; deadbeef->junk_rewrite_tags (out_it, tagflags, encoder_preset->id3v2_version + 3, "iso8859-1"); } // write flac tags if (encoder_preset->tag_flac) { // find flac decoder plugin DB_decoder_t **plugs = deadbeef->plug_get_decoder_list (); DB_decoder_t *flac = NULL; for (int i = 0; plugs[i]; i++) { if (!strcmp (plugs[i]->plugin.id, "stdflac")) { flac = plugs[i]; break; } } if (!flac) { fprintf (stderr, "converter: flac plugin not found, cannot write flac metadata\n"); } else { if (0 != flac->write_metadata (out_it)) { fprintf (stderr, "converter: failed to write flac metadata, not a flac file?\n"); } } } // write vorbis tags if (encoder_preset->tag_oggvorbis) { // find flac decoder plugin DB_decoder_t **plugs = deadbeef->plug_get_decoder_list (); int res = -1; for (int i = 0; plugs[i]; i++) { if (!strcmp (plugs[i]->plugin.id, "stdogg") || !strcmp (plugs[i]->plugin.id, "stdopus")) { res = plugs[i]->write_metadata (out_it); if (!res) { break; } } } if (res) { fprintf (stderr, "converter: failed to write ogg metadata, not an ogg file?\n"); } } if (out_it) { deadbeef->pl_item_unref (out_it); } return err; }
int convert (DB_playItem_t *it, const char *outfolder, const char *outfile, int output_bps, int output_is_float, int preserve_folder_structure, const char *root_folder, ddb_encoder_preset_t *encoder_preset, ddb_dsp_preset_t *dsp_preset, int *abort) { if (deadbeef->pl_get_item_duration (it) <= 0) { deadbeef->pl_lock (); const char *fname = deadbeef->pl_find_meta (it, ":URI"); fprintf (stderr, "converter: stream %s doesn't have finite length, skipped\n", fname); deadbeef->pl_unlock (); return -1; } char *path = outfolder[0] ? strdupa (outfolder) : strdupa (getenv("HOME")); if (!check_dir (path, 0755)) { fprintf (stderr, "converter: failed to create output folder: %s\n", outfolder); return -1; } int err = -1; FILE *enc_pipe = NULL; FILE *temp_file = NULL; DB_decoder_t *dec = NULL; DB_fileinfo_t *fileinfo = NULL; char out[PATH_MAX] = ""; // full path to output file char input_file_name[PATH_MAX] = ""; dec = (DB_decoder_t *)deadbeef->plug_get_for_id (deadbeef->pl_find_meta (it, ":DECODER")); if (dec) { fileinfo = dec->open (0); if (fileinfo && dec->init (fileinfo, DB_PLAYITEM (it)) != 0) { deadbeef->pl_lock (); fprintf (stderr, "converter: failed to decode file %s\n", deadbeef->pl_find_meta (it, ":URI")); deadbeef->pl_unlock (); goto error; } if (fileinfo) { if (output_bps == -1) { output_bps = fileinfo->fmt.bps; output_is_float = fileinfo->fmt.is_float; } get_output_path (it, outfolder, outfile, encoder_preset, out, sizeof (out)); if (encoder_preset->method == DDB_ENCODER_METHOD_FILE) { const char *tmp = getenv ("TMPDIR"); if (!tmp) { tmp = "/tmp"; } snprintf (input_file_name, sizeof (input_file_name), "%s/ddbconvXXXXXX", tmp); mktemp (input_file_name); strcat (input_file_name, ".wav"); } else { strcpy (input_file_name, "-"); } char enc[2000]; memset (enc, 0, sizeof (enc)); // formatting: %o = outfile, %i = infile char *e = encoder_preset->encoder; char *o = enc; *o = 0; int len = sizeof (enc); while (e && *e) { if (len <= 0) { fprintf (stderr, "converter: failed to assemble encoder command line - buffer is not big enough, try to shorten your parameters. max allowed length is %u characters\n", (unsigned)sizeof (enc)); goto error; } if (e[0] == '%' && e[1]) { if (e[1] == 'o') { int l = snprintf (o, len, "\"%s\"", out); o += l; len -= l; } else if (e[1] == 'i') { int l = snprintf (o, len, "\"%s\"", input_file_name); o += l; len -= l; } else { strncpy (o, e, 2); o += 2; len -= 2; } e += 2; } else { *o++ = *e++; *o = 0; len--; } } fprintf (stderr, "converter: will encode using: %s\n", enc[0] ? enc : "internal RIFF WAVE writer"); if (!encoder_preset->encoder[0]) { // write to wave file temp_file = fopen (out, "w+b"); if (!temp_file) { fprintf (stderr, "converter: failed to open output wave file %s\n", out); goto error; } } else if (encoder_preset->method == DDB_ENCODER_METHOD_FILE) { temp_file = fopen (input_file_name, "w+b"); if (!temp_file) { fprintf (stderr, "converter: failed to open temp file %s\n", input_file_name); goto error; } } else { enc_pipe = popen (enc, "w"); if (!enc_pipe) { fprintf (stderr, "converter: failed to open encoder\n"); goto error; } } if (!temp_file) { temp_file = enc_pipe; } // write wave header char wavehdr_int[] = { 0x52, 0x49, 0x46, 0x46, 0x24, 0x70, 0x0d, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61 }; char wavehdr_float[] = { 0x52, 0x49, 0x46, 0x46, 0x2a, 0xdf, 0x02, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x28, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x02, 0x00, 0x40, 0x1f, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x08, 0x00, 0x20, 0x00, 0x16, 0x00, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71, 0x66, 0x61, 0x63, 0x74, 0x04, 0x00, 0x00, 0x00, 0xc5, 0x5b, 0x00, 0x00, 0x64, 0x61, 0x74, 0x61 }; char *wavehdr = output_is_float ? wavehdr_float : wavehdr_int; int wavehdr_size = output_is_float ? sizeof (wavehdr_float) : sizeof (wavehdr_int); int header_written = 0; uint32_t outsize = 0; uint32_t outsr = fileinfo->fmt.samplerate; uint16_t outch = fileinfo->fmt.channels; int samplesize = fileinfo->fmt.channels * fileinfo->fmt.bps / 8; int bs = 10250 * samplesize; char buffer[bs * 4]; int dspsize = bs / samplesize * sizeof (float) * fileinfo->fmt.channels; char dspbuffer[dspsize * 4]; int eof = 0; for (;;) { if (eof) { break; } if (abort && *abort) { break; } int sz = dec->read (fileinfo, buffer, bs); if (sz != bs) { eof = 1; } if (dsp_preset) { ddb_waveformat_t fmt; ddb_waveformat_t outfmt; memcpy (&fmt, &fileinfo->fmt, sizeof (fmt)); memcpy (&outfmt, &fileinfo->fmt, sizeof (fmt)); fmt.bps = 32; fmt.is_float = 1; deadbeef->pcm_convert (&fileinfo->fmt, buffer, &fmt, dspbuffer, sz); ddb_dsp_context_t *dsp = dsp_preset->chain; int frames = sz / samplesize; while (dsp) { frames = dsp->plugin->process (dsp, (float *)dspbuffer, frames, sizeof (dspbuffer) / (fmt.channels * 4), &fmt, NULL); dsp = dsp->next; } outsr = fmt.samplerate; outch = fmt.channels; outfmt.bps = output_bps; outfmt.is_float = output_is_float; outfmt.channels = outch; outfmt.samplerate = outsr; int n = deadbeef->pcm_convert (&fmt, dspbuffer, &outfmt, buffer, frames * sizeof (float) * fmt.channels); sz = n; } else if (fileinfo->fmt.bps != output_bps || fileinfo->fmt.is_float != output_is_float) { ddb_waveformat_t outfmt; memcpy (&outfmt, &fileinfo->fmt, sizeof (outfmt)); outfmt.bps = output_bps; outfmt.is_float = output_is_float; outfmt.channels = outch; outfmt.samplerate = outsr; int frames = sz / samplesize; int n = deadbeef->pcm_convert (&fileinfo->fmt, buffer, &outfmt, dspbuffer, frames * samplesize); memcpy (buffer, dspbuffer, n); sz = n; } outsize += sz; if (!header_written) { uint32_t size = (it->endsample-it->startsample) * outch * output_bps / 8; if (!size) { size = deadbeef->pl_get_item_duration (it) * fileinfo->fmt.samplerate * outch * output_bps / 8; } if (outsr != fileinfo->fmt.samplerate) { uint64_t temp = size; temp *= outsr; temp /= fileinfo->fmt.samplerate; size = temp; } memcpy (&wavehdr[22], &outch, 2); memcpy (&wavehdr[24], &outsr, 4); uint16_t blockalign = outch * output_bps / 8; memcpy (&wavehdr[32], &blockalign, 2); memcpy (&wavehdr[34], &output_bps, 2); fwrite (wavehdr, 1, wavehdr_size, temp_file); if (encoder_preset->method == DDB_ENCODER_METHOD_PIPE) { size = 0; } fwrite (&size, 1, sizeof (size), temp_file); header_written = 1; } int64_t res = fwrite (buffer, 1, sz, temp_file); if (sz != res) { fprintf (stderr, "converter: write error (%lld bytes written out of %d)\n", res, sz); goto error; } } if (abort && *abort) { goto error; } if (temp_file && temp_file != enc_pipe) { fseek (temp_file, wavehdr_size, SEEK_SET); fwrite (&outsize, 1, 4, temp_file); fclose (temp_file); temp_file = NULL; } if (encoder_preset->encoder[0] && encoder_preset->method == DDB_ENCODER_METHOD_FILE) { enc_pipe = popen (enc, "w"); } } } err = 0; error: if (temp_file && temp_file != enc_pipe) { fclose (temp_file); temp_file = NULL; } if (enc_pipe) { pclose (enc_pipe); enc_pipe = NULL; } if (dec && fileinfo) { dec->free (fileinfo); fileinfo = NULL; } if (abort && *abort && out[0]) { unlink (out); } if (input_file_name[0] && strcmp (input_file_name, "-")) { unlink (input_file_name); } // write junklib tags uint32_t tagflags = JUNK_STRIP_ID3V2 | JUNK_STRIP_APEV2 | JUNK_STRIP_ID3V1; if (encoder_preset->tag_id3v2) { tagflags |= JUNK_WRITE_ID3V2; } if (encoder_preset->tag_id3v1) { tagflags |= JUNK_WRITE_ID3V1; } if (encoder_preset->tag_apev2) { tagflags |= JUNK_WRITE_APEV2; } DB_playItem_t *out_it = deadbeef->pl_item_alloc (); deadbeef->pl_item_copy (out_it, it); deadbeef->pl_replace_meta (out_it, ":URI", out); deadbeef->pl_delete_meta (out_it, "cuesheet"); deadbeef->junk_rewrite_tags (out_it, tagflags, encoder_preset->id3v2_version + 3, "iso8859-1"); // write flac tags if (encoder_preset->tag_flac) { // find flac decoder plugin DB_decoder_t **plugs = deadbeef->plug_get_decoder_list (); DB_decoder_t *flac = NULL; for (int i = 0; plugs[i]; i++) { if (!strcmp (plugs[i]->plugin.id, "stdflac")) { flac = plugs[i]; break; } } if (!flac) { fprintf (stderr, "converter: flac plugin not found, cannot write flac metadata\n"); } else { if (0 != flac->write_metadata (out_it)) { fprintf (stderr, "converter: failed to write flac metadata, not a flac file?\n"); } } } // write vorbis tags if (encoder_preset->tag_oggvorbis) { // find flac decoder plugin DB_decoder_t **plugs = deadbeef->plug_get_decoder_list (); DB_decoder_t *ogg = NULL; for (int i = 0; plugs[i]; i++) { if (!strcmp (plugs[i]->plugin.id, "stdogg")) { ogg = plugs[i]; break; } } if (!ogg) { fprintf (stderr, "converter: ogg plugin not found, cannot write ogg metadata\n"); } else { if (0 != ogg->write_metadata (out_it)) { fprintf (stderr, "converter: failed to write ogg metadata, not an ogg file?\n"); } } } deadbeef->pl_item_unref (out_it); return err; }
void rg_calc_thread(void *ctx) { DB_decoder_t *dec = NULL; DB_fileinfo_t *fileinfo = NULL; char *buffer = NULL; char *bufferf = NULL; track_state_t *st = (track_state_t *)ctx; if (st->settings->pabort && *(st->settings->pabort)) { return; } if (deadbeef->pl_get_item_duration (st->settings->tracks[st->track_index]) <= 0) { st->settings->results[st->track_index].scan_result = DDB_RG_SCAN_RESULT_INVALID_FILE; return; } deadbeef->pl_lock (); dec = (DB_decoder_t *)deadbeef->plug_get_for_id (deadbeef->pl_find_meta (st->settings->tracks[st->track_index], ":DECODER")); deadbeef->pl_unlock (); if (dec) { fileinfo = dec->open (DDB_DECODER_HINT_RAW_SIGNAL); if (fileinfo && dec->init (fileinfo, DB_PLAYITEM (st->settings->tracks[st->track_index])) != 0) { st->settings->results[st->track_index].scan_result = DDB_RG_SCAN_RESULT_FILE_NOT_FOUND; goto error; } if (fileinfo) { st->gain_state[st->track_index] = ebur128_init(fileinfo->fmt.channels, fileinfo->fmt.samplerate, EBUR128_MODE_I); st->peak_state[st->track_index] = ebur128_init(fileinfo->fmt.channels, fileinfo->fmt.samplerate, EBUR128_MODE_SAMPLE_PEAK); // speaker mask mapping from WAV to EBUR128 static const int chmap[18] = { EBUR128_LEFT, EBUR128_RIGHT, EBUR128_CENTER, EBUR128_UNUSED, EBUR128_LEFT_SURROUND, EBUR128_RIGHT_SURROUND, EBUR128_LEFT_SURROUND, EBUR128_RIGHT_SURROUND, EBUR128_CENTER, EBUR128_LEFT_SURROUND, EBUR128_RIGHT_SURROUND, EBUR128_CENTER, EBUR128_LEFT_SURROUND, EBUR128_CENTER, EBUR128_RIGHT_SURROUND, EBUR128_LEFT_SURROUND, EBUR128_CENTER, EBUR128_RIGHT_SURROUND, }; uint32_t channelmask = fileinfo->fmt.channelmask; // first 18 speaker positions are known, the rest will be marked as UNUSED int ch = 0; for (int i = 0; i < 32 && ch < fileinfo->fmt.channels; i++) { if (i < 18) { if (channelmask & (1<<i)) { ebur128_set_channel (st->gain_state[st->track_index], ch, chmap[i]); ebur128_set_channel (st->peak_state[st->track_index], ch, chmap[i]); ch++; } } else { ebur128_set_channel (st->gain_state[st->track_index], ch, EBUR128_UNUSED); ebur128_set_channel (st->peak_state[st->track_index], ch, EBUR128_UNUSED); ch++; } } int samplesize = fileinfo->fmt.channels * fileinfo->fmt.bps / 8; int bs = 2000 * samplesize; ddb_waveformat_t fmt; buffer = malloc (bs); if (!fileinfo->fmt.is_float) { bufferf = malloc (2000 * sizeof (float) * fileinfo->fmt.channels); memcpy (&fmt, &fileinfo->fmt, sizeof (fmt)); fmt.bps = 32; fmt.is_float = 1; } else { bufferf = buffer; } int eof = 0; for (;;) { if (eof) { break; } if (st->settings->pabort && *(st->settings->pabort)) { break; } int sz = dec->read (fileinfo, buffer, bs); // read one block deadbeef->mutex_lock (st->settings->sync_mutex); int samplesize = fileinfo->fmt.channels * (fileinfo->fmt.bps >> 3); int numsamples = sz / samplesize; st->settings->cd_samples_processed += numsamples * 44100 / fileinfo->fmt.samplerate; deadbeef->mutex_unlock (st->settings->sync_mutex); if (sz != bs) { eof = 1; } // convert from native output to float, // only if the input is not float already if (!fileinfo->fmt.is_float) { deadbeef->pcm_convert (&fileinfo->fmt, buffer, &fmt, bufferf, sz); } int frames = sz / samplesize; ebur128_add_frames_float (st->gain_state[st->track_index], (float*) bufferf, frames); // collect data ebur128_add_frames_float (st->peak_state[st->track_index], (float*) bufferf, frames); // collect data } } if (!st->settings->pabort || !(*(st->settings->pabort))) { // calculating track peak // libEBUR128 calculates peak per channel, so we have to pick the highest value double tr_peak = 0; double ch_peak = 0; int res; for (int ch = 0; ch < fileinfo->fmt.channels; ++ch) { res = ebur128_sample_peak (st->peak_state[st->track_index], ch, &ch_peak); //trace ("rg_scanner: peak for ch %d: %f\n", ch, ch_peak); if (ch_peak > tr_peak) { //trace ("rg_scanner: %f > %f\n", ch_peak, tr_peak); tr_peak = ch_peak; } } st->settings->results[st->track_index].track_peak = (float) tr_peak; // calculate track loudness double loudness = st->settings->ref_loudness; ebur128_loudness_global (st->gain_state[st->track_index], &loudness); /* * EBUR128 sets the target level to -23 LUFS = 84dB * -> -23 - loudness = track gain to get to 84dB * * The old implementation of RG used 89dB, most people still use that * -> the above + (loudness - 84) = track gain to get to 89dB (or user specified) */ st->settings->results[st->track_index].track_gain = -23 - loudness + st->settings->ref_loudness - 84; } } error: // clean up if (fileinfo) { dec->free (fileinfo); } if (buffer && buffer != bufferf) { free (buffer); buffer = NULL; } if (bufferf) { free (bufferf); bufferf = NULL; } }