double test_true_peak(const char* filename) { SF_INFO file_info; SNDFILE* file; sf_count_t nr_frames_read; int i; ebur128_state* st = NULL; double true_peak; double max_true_peak = -HUGE_VAL; double* buffer; memset(&file_info, '\0', sizeof(file_info)); file = sf_open(filename, SFM_READ, &file_info); if (!file) { fprintf(stderr, "Could not open file %s!\n", filename); return 0.0; } st = ebur128_init((unsigned) file_info.channels, (unsigned) file_info.samplerate, EBUR128_MODE_TRUE_PEAK); if (file_info.channels == 5) { ebur128_set_channel(st, 0, EBUR128_LEFT); ebur128_set_channel(st, 1, EBUR128_RIGHT); ebur128_set_channel(st, 2, EBUR128_CENTER); ebur128_set_channel(st, 3, EBUR128_LEFT_SURROUND); ebur128_set_channel(st, 4, EBUR128_RIGHT_SURROUND); } buffer = (double*) malloc(st->samplerate * st->channels * sizeof(double)); while ((nr_frames_read = sf_readf_double(file, buffer, (sf_count_t) st->samplerate))) { ebur128_add_frames_double(st, buffer, (size_t) nr_frames_read); } for (i = 0; i < file_info.channels; i++) { ebur128_true_peak(st, (unsigned)i, &true_peak); if (true_peak > max_true_peak) max_true_peak = true_peak; } /* clean up */ ebur128_destroy(&st); free(buffer); buffer = NULL; if (sf_close(file)) { fprintf(stderr, "Could not close input file!\n"); } return 20 * log10(max_true_peak); }
static void analyze_audio( mlt_filter filter, void* buffer, int samples ) { mlt_properties properties = MLT_FILTER_PROPERTIES( filter ); private_data* pdata = (private_data*)filter->child; int result = -1; double loudness = 0.0; ebur128_add_frames_float( pdata->r128, buffer, samples ); if( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "calc_program" ) ) { result = ebur128_loudness_global( pdata->r128, &loudness ); if( result == EBUR128_SUCCESS && loudness != HUGE_VAL && loudness != -HUGE_VAL ) { mlt_properties_set_double( properties, "program", loudness ); } } if( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "calc_shortterm" ) ) { result = ebur128_loudness_shortterm( pdata->r128, &loudness ); if( result == EBUR128_SUCCESS && loudness != HUGE_VAL && loudness != -HUGE_VAL ) { mlt_properties_set_double( properties, "shortterm", loudness ); } } if( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "calc_momentary" ) ) { result = ebur128_loudness_momentary( pdata->r128, &loudness ); if( result == EBUR128_SUCCESS && loudness != HUGE_VAL && loudness != -HUGE_VAL ) { mlt_properties_set_double( properties, "momentary", loudness ); } } if( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "calc_range" ) ) { double range = 0; result = ebur128_loudness_range( pdata->r128, &range ); if( result == EBUR128_SUCCESS && range != HUGE_VAL && range != -HUGE_VAL ) { mlt_properties_set_double( properties, "range", range ); } } if( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "calc_peak" ) ) { double prev_peak = 0.0; double max_peak = 0.0; int c = 0; for( c = 0; c < pdata->r128->channels; c++ ) { double peak; result = ebur128_sample_peak( pdata->r128, c, &peak ); if( result == EBUR128_SUCCESS && peak != HUGE_VAL && peak > max_peak ) { max_peak = peak; } result = ebur128_prev_sample_peak( pdata->r128, c, &peak ); if( result == EBUR128_SUCCESS && peak != HUGE_VAL && peak > prev_peak ) { prev_peak = peak; } } mlt_properties_set_double( properties, "max_peak", 20 * log10(max_peak) ); mlt_properties_set_double( properties, "peak", 20 * log10(prev_peak) ); } if( mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "calc_true_peak" ) ) { double prev_peak = 0.0; double max_peak = 0.0; int c = 0; for( c = 0; c < pdata->r128->channels; c++ ) { double peak; result = ebur128_true_peak( pdata->r128, c, &peak ); if( result == EBUR128_SUCCESS && peak != HUGE_VAL && peak > max_peak ) { max_peak = peak; } result = ebur128_prev_true_peak( pdata->r128, c, &peak ); if( result == EBUR128_SUCCESS && peak != HUGE_VAL && peak > prev_peak ) { prev_peak = peak; } } mlt_properties_set_double( properties, "max_true_peak", 20 * log10(max_peak) ); mlt_properties_set_double( properties, "true_peak", 20 * log10(prev_peak) ); } mlt_properties_set_position( properties, "frames_processed", mlt_properties_get_position( properties, "frames_processed" ) + 1 ); }
void init_state_and_scan_work_item(struct filename_list_node *fln, struct scan_opts *opts) { struct file_data *fd = (struct file_data *) fln->d; struct input_ops* ops = NULL; struct input_handle* ih = NULL; int r128_mode = EBUR128_MODE_I; unsigned int i; int *channel_map; int result; float *buffer = NULL; size_t nr_frames_read; #ifdef USE_SNDFILE SNDFILE *outfile = NULL; #endif result = open_plugin(fln->fr->raw, fln->fr->display, &ops, &ih); if (result) { g_mutex_lock(progress_mutex); elapsed_frames += fd->number_of_frames; g_cond_broadcast(progress_cond); g_mutex_unlock(progress_mutex); goto free; } if (opts->lra) r128_mode |= EBUR128_MODE_LRA; if (opts->peak) { if (!strcmp(opts->peak, "sample") || !strcmp(opts->peak, "all")) r128_mode |= EBUR128_MODE_SAMPLE_PEAK; #ifdef USE_SPEEX_RESAMPLER if (!strcmp(opts->peak, "true") || !strcmp(opts->peak, "dbtp") || !strcmp(opts->peak, "all")) r128_mode |= EBUR128_MODE_TRUE_PEAK; #endif } if (opts->histogram) r128_mode |= EBUR128_MODE_HISTOGRAM; fd->st = ebur128_init(ops->get_channels(ih), ops->get_samplerate(ih), r128_mode); channel_map = g_malloc(fd->st->channels * sizeof(int)); if (!ops->set_channel_map(ih, channel_map)) { for (i = 0; i < fd->st->channels; ++i) { ebur128_set_channel(fd->st, i, channel_map[i]); } } free(channel_map); if (fd->st->channels == 1 && opts->force_dual_mono) { ebur128_set_channel(fd->st, 0, EBUR128_DUAL_MONO); } result = ops->allocate_buffer(ih); if (result) abort(); buffer = ops->get_buffer(ih); #ifdef USE_SNDFILE if (opts->decode_file) { SF_INFO sf_info; memset(&sf_info, '\0', sizeof sf_info); sf_info.samplerate = (int) fd->st->samplerate; sf_info.channels = (int) fd->st->channels; sf_info.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; outfile = sf_open(opts->decode_file, SFM_WRITE, &sf_info); if (!outfile) { fprintf(stderr, "output file could not be opened\n"); exit(EXIT_FAILURE); } } #endif while ((nr_frames_read = ops->read_frames(ih))) { g_mutex_lock(progress_mutex); elapsed_frames += nr_frames_read; g_cond_broadcast(progress_cond); g_mutex_unlock(progress_mutex); fd->number_of_elapsed_frames += nr_frames_read; result = ebur128_add_frames_float(fd->st, buffer, nr_frames_read); #ifdef USE_SNDFILE if (opts->decode_file) { if (sf_writef_float(outfile, buffer, (sf_count_t) nr_frames_read) != (sf_count_t) nr_frames_read) sf_perror(outfile); } #endif if (result) abort(); } #ifdef USE_SNDFILE if (opts->decode_file) { sf_close(outfile); } #endif if (fd->number_of_elapsed_frames != fd->number_of_frames) { if (verbose) { fprintf(stderr, "Warning: Could not read full file" " or determine right length: " "Expected: %lu Got: %lu", fd->number_of_frames, fd->number_of_elapsed_frames); } g_mutex_lock(progress_mutex); total_frames = total_frames + fd->number_of_elapsed_frames - fd->number_of_frames; g_cond_broadcast(progress_cond); g_mutex_unlock(progress_mutex); } ebur128_loudness_global(fd->st, &fd->loudness); if (opts->lra) { result = ebur128_loudness_range(fd->st, &fd->lra); if (result) abort(); } if ((fd->st->mode & EBUR128_MODE_SAMPLE_PEAK) == EBUR128_MODE_SAMPLE_PEAK) { for (i = 0; i < fd->st->channels; ++i) { double sp; ebur128_sample_peak(fd->st, i, &sp); if (sp > fd->peak) { fd->peak = sp; } } } #ifdef USE_SPEEX_RESAMPLER if ((fd->st->mode & EBUR128_MODE_TRUE_PEAK) == EBUR128_MODE_TRUE_PEAK) { for (i = 0; i < fd->st->channels; ++i) { double tp; ebur128_true_peak(fd->st, i, &tp); if (tp > fd->true_peak) { fd->true_peak = tp; } } } #endif fd->scanned = TRUE; if (ih) ops->free_buffer(ih); free: if (!result) ops->close_file(ih); if (ih) ops->handle_destroy(&ih); }
void TrackScanner::run() { bool ffmpegIsFloat=false; #ifdef FFMPEG_FOUND FfmpegInput *ffmpeg=new FfmpegInput(file); if (*ffmpeg) { input=ffmpeg; ffmpegIsFloat=ffmpeg->isFloatCodec(); } else { delete ffmpeg; ffmpeg=0; } #endif #if MPG123_FOUND if (file.endsWith(".mp3", Qt::CaseInsensitive) && (!input || !ffmpegIsFloat)) { Mpg123Input *mpg123=new Mpg123Input(file); if (*mpg123) { input=mpg123; #ifdef FFMPEG_FOUND if (ffmpeg) { delete ffmpeg; } #endif } else { delete mpg123; } } #endif if (!input) { setFinishedStatus(false); return; } state=ebur128_init(input->channels(), input->sampleRate(), EBUR128_MODE_M|EBUR128_MODE_I|EBUR128_MODE_SAMPLE_PEAK); int *channelMap=new int [state->channels]; if (input->setChannelMap(channelMap)) { for (unsigned int i = 0; i < state->channels; ++i) { ebur128_set_channel(state, i, channelMap[i]); } } delete [] channelMap; //if (1==state->channels && opts->force_dual_mono) { // ebur128_set_channel(state, 0, EBUR128_DUAL_MONO); //} size_t numFramesRead=0; size_t totalRead=0; input->allocateBuffer(); while ((numFramesRead = input->readFrames())) { if (abortRequested) { setFinishedStatus(false); return; } totalRead+=numFramesRead; emit progress((int)((totalRead*100.0/input->totalFrames())+0.5)); if (ebur128_add_frames_float(state, input->buffer(), numFramesRead)) { setFinishedStatus(false); return; } } if (abortRequested) { setFinishedStatus(false); return; } ebur128_loudness_global(state, &data.loudness); // if (opts->lra) { // result = ebur128_loudness_range(ebur, &lra); // if (result) abort(); // } if (EBUR128_MODE_SAMPLE_PEAK==(state->mode & EBUR128_MODE_SAMPLE_PEAK)) { for (unsigned i = 0; i < state->channels; ++i) { double sp; ebur128_sample_peak(state, i, &sp); if (sp > data.peak) { data.peak = sp; } } } if (EBUR128_MODE_TRUE_PEAK==(state->mode & EBUR128_MODE_TRUE_PEAK)) { for (unsigned i = 0; i < state->channels; ++i) { double tp; ebur128_true_peak(state, i, &tp); if (tp > data.truePeak) { data.truePeak = tp; } } } setFinishedStatus(true); }