void grabbag__replaygain_get_album(float *gain, float *peak) { *gain = (float)GetAlbumGain(); *peak = (float)album_peak_; album_peak_ = 0.0; }
/** * \brief Processs the file in file_list. * * Process the files in file_list. If settings->album is set, the files are * considered to belong to one album. * * \param file_list list of files to process. * \param settings settings and global variables. * \return 0 if successful and -1 if an error occured (in which case a * message has been printed). */ int process_files(FILE_LIST* file_list, SETTINGS* settings, const char* dir) { FILE_LIST* file; double factor_clip, audiophile_gain = 0., Gain, scale, dB, album_dc_offset[2] = {0., 0.}; settings->album_peak = NO_PEAK; if (file_list == NULL) return 0; settings->first_file = 1; /* Undo previously applied gain */ if (settings->undo) { for (file = file_list; file; file = file->next_file) { if (file->filename == '\0') continue; if (!write_gains(file->filename, 0, 0, 0, 0, 0, settings)) { fprintf(stderr, " Error processing GAIN for file - %s\n", file->filename); continue; } } } else { /* Analyze the files */ for (file = file_list; file; file = file->next_file) { int dc_l; int dc_r; if (file->filename == '\0') continue; if (!get_gain(file->filename, &file->track_peak, &file->track_gain, file->dc_offset, file->offset, settings)) { file->filename = '\0'; continue; } dc_l = (int)(file->dc_offset[0] * 32768 * -1); dc_r = (int)(file->dc_offset[1] * 32768 * -1); if (dc_l < -1 || dc_l > 1 || dc_r < -1 || dc_r > 1) settings->need_to_process = 1; album_dc_offset[0] += file->offset[0]; album_dc_offset[1] += file->offset[1]; } album_dc_offset[0] /= total_samples; album_dc_offset[1] /= total_samples; if (!settings->no_offset && settings->adc) { int dc_l = (int)(album_dc_offset[0] * 32768 * -1); int dc_r = (int)(album_dc_offset[1] * 32768 * -1); fprintf(stderr, " ********************* Album DC Offset | %4d | %4d | ***************\n", dc_l, dc_r); if(write_to_log) { write_log(" ********************* Album DC Offset | %4d | %4d | ***************\n", dc_l, dc_r); } } fprintf(stderr, "\n"); if(write_to_log) write_log("\n"); if (settings->audiophile) { Gain = GetAlbumGain() + settings->man_gain; scale = pow(10., Gain * 0.05); settings->set_album_gain = 0; if(settings->clip_prev) { factor_clip = (32767./( settings->album_peak + 1)); if(scale < factor_clip) factor_clip = 1.0; else factor_clip /= scale; scale *= factor_clip; } if (settings->scale) { fprintf(stdout, "%8.6lf", scale); } if (scale > 0.0) dB = 20. * log10(scale); else dB = 0.0; audiophile_gain = dB; if (audiophile_gain < 0.1 && audiophile_gain > -0.1 && !settings->need_to_process) { fprintf(stderr, " No Album Gain adjustment or DC Offset correction required, exiting.\n"); if(write_to_log) write_log(" No Album Gain adjustment or DC Offset correction required, exiting.\n"); settings->set_album_gain = 1; } else { fprintf(stderr, " Recommended Album Gain: %+6.2f dB\tScale: %6.4f\n\n", audiophile_gain, scale); if(write_to_log) write_log(" Recommended Album Gain: %+6.2f dB\tScale: %6.4f\n\n", audiophile_gain, scale); } } if(settings->apply_gain) { /* Write radio and audiophile gains. */ total_files = 0.0; for (file = file_list; file; file = file->next_file) { if (file->filename == '\0') continue; if (settings->audiophile && settings->set_album_gain == 1) break; if (settings->radio && (file->track_gain < 0.1 && file->track_gain > -0.1) && !settings->need_to_process) { fprintf(stderr, " No Title Gain adjustment or DC Offset correction required for file: %s, skipping.\n", file->filename); if(write_to_log) write_log(" No Title Gain adjustment or DC Offset correction required for file: %s, skipping.\n", file->filename); } else if (!write_gains(file->filename, file->track_gain, audiophile_gain, file->track_peak, file->dc_offset, album_dc_offset, settings)) { fprintf(stderr, " Error processing GAIN for file - %s\n", file->filename); continue; } } } } fprintf(stderr, "\n WaveGain Processing completed normally\n"); if(write_to_log) write_log("\n WaveGain Processing completed normally\n\n"); #ifdef _WIN32 if (settings->cmd) { /* execute user command */ FILE_LIST* file; char buff[MAX_PATH]; char *b, *p, *q; double track_scale = 1.0; SetEnvironmentVariable("ALBUM_GAIN", ftos(audiophile_gain, "%.2lf")); SetEnvironmentVariable("ALBUM_SCALE", ftos(scale, "%.5lf")); SetEnvironmentVariable("ALBUM_NEW_PEAK", ftos(settings->album_peak * scale, "%.0lf")); SetEnvironmentVariable("ALBUM_PEAK", ftos(settings->album_peak, "%.0lf")); for (file = file_list; file; file = file->next_file) { if (file->filename == '\0') continue; if (dir[0] == '.' && dir[1] == '\\') dir += 2; strcpy(buff, file->filename); b = (buff[0] == '.' && buff[1] == '\\') ? buff+2 : buff; SetEnvironmentVariable("INPUT_FILE", b); p = strrchr(b, '\\'); if (p) { p[0] = '\0'; ++p; SetEnvironmentVariable("INPUT_FDIR", b); SetEnvironmentVariable("INPUT_RDIR", b); // assume dir = "." } else { p = b; SetEnvironmentVariable("INPUT_FDIR", "."); SetEnvironmentVariable("INPUT_RDIR", dir); } q = strrchr(p, '.'); if (q) q[0] = '\0'; SetEnvironmentVariable("INPUT_NAME", p); track_scale = (pow(10., file->track_gain * 0.05)); SetEnvironmentVariable("TRACK_SCALE", ftos(track_scale, "%.5lf")); SetEnvironmentVariable("TRACK_GAIN", ftos(file->track_gain, "%.2lf")); SetEnvironmentVariable("TRACK_NEW_PEAK", ftos(file->track_peak, "%.0lf")); SetEnvironmentVariable("TRACK_PEAK", ftos(file->track_peak / track_scale, "%.0lf")); SetEnvironmentVariable("DC_OFFSET_L", ftos((int)(file->dc_offset[0]*(-32768)), "%.0lf")); SetEnvironmentVariable("DC_OFFSET_R", ftos((int)(file->dc_offset[1]*(-32768)), "%.0lf")); fprintf(stderr, "\n Executing command on \"%s\":\n\n", file->filename); system(settings->cmd); } } #endif return 0; }
static void CalcReplayGain ( const char* filename, gain_info_t* G ) { FILE* fp; float buffl [NO]; float buffr [NO]; Int16_t buff [NO] [2]; size_t i; size_t len; size_t lastlen = 0; unsigned int max = 0; float level; float mult; if ((fp = pipeopen ( "mppdec --silent --scale 0.5 --gain 0 --raw - - < #", filename)) == NULL) { stderr_printf ( "Can't decode '%s'\n", filename ); exit (9); } memset ( buff, 0, sizeof(buff) ); G->Silence = 0; lastlen = len = fread (buff, 4, NO, fp); for ( i = 0; i < len; i++ ) { buffl [i] = 2. * buff [i] [0]; buffr [i] = 2. * buff [i] [1]; if ( abs(buff[i][0]) > max ) max = abs(buff[i][0]); if ( abs(buff[i][1]) > max ) max = abs(buff[i][1]); } AnalyzeSamples ( buffl, buffr, len, 2 ); level = 0.; mult = 1.; for ( i = 0; i < len; i++ ) { level += mult * (buff [i] [0] * buff [i] [0] + buff [i] [1] * buff [i] [1]); mult *= 0.95; } level = 2*sqrt(level * 0.05); if ( level > LEVEL_THR ) G->Silence |= 2; sh ( NULL, level ); while (( len = fread (buff, 4, NO, fp) ) > 0 ) { lastlen = len; for ( i = 0; i < len; i++ ) { buffl [i] = 2. * buff [i] [0]; buffr [i] = 2. * buff [i] [1]; if ( abs(buff[i][0]) > max ) max = abs(buff[i][0]); if ( abs(buff[i][1]) > max ) max = abs(buff[i][1]); } AnalyzeSamples ( buffl, buffr, len, 2 ); } level = 0.; mult = 1.; for ( i = 1; i <= NO; i++ ) { int idx = (lastlen + NO - i) % NO; level += mult * (buff [idx] [0] * buff [idx] [0] + buff [idx] [1] * buff [idx] [1]); mult *= 0.95; } level = 2*sqrt(level * 0.05); if ( level > LEVEL_THR ) G->Silence |= 1; sh(filename,level); PCLOSE (fp); #if 0 GetTitleDynamics (); #endif G -> FileName = filename; G -> TitleGain = GetTitleGain (); G -> TitlePeak = 2 * max + 1; G -> AlbumGain = GetAlbumGain (); G -> AlbumPeak = G->AlbumPeak < G->TitlePeak ? G->TitlePeak : G->AlbumPeak; return; }
double GetAudiophileGain(){ return GetAlbumGain(); }