⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replaygain.c

📁 tcpmp.src.0.72RC1 优秀的多媒体播放器TCPMP的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* grabbag - Convenience lib for various routines common to several tools * Copyright (C) 2002,2003,2004,2005  Josh Coalson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */#include "share/grabbag.h"#include "share/replaygain_analysis.h"#include "FLAC/assert.h"#include "FLAC/file_decoder.h"#include "FLAC/metadata.h"#include <locale.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#if defined _MSC_VER || defined __MINGW32__#include <io.h> /* for chmod() */#endif#include <sys/stat.h> /* for stat(), maybe chmod() */#ifdef local_min#undef local_min#endif#define local_min(a,b) ((a)<(b)?(a):(b))#ifdef local_max#undef local_max#endif#define local_max(a,b) ((a)>(b)?(a):(b))static const FLAC__byte *tag_title_gain_ = "REPLAYGAIN_TRACK_GAIN";static const FLAC__byte *tag_title_peak_ = "REPLAYGAIN_TRACK_PEAK";static const FLAC__byte *tag_album_gain_ = "REPLAYGAIN_ALBUM_GAIN";static const FLAC__byte *tag_album_peak_ = "REPLAYGAIN_ALBUM_PEAK";static const char *peak_format_ = "%s=%1.8f";static const char *gain_format_ = "%s=%+2.2f dB";static double album_peak_, title_peak_;const unsigned GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED = 148;/*	FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 +	FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12 +	FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 +	FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12*/static FLAC__bool get_file_stats_(const char *filename, struct stat *stats){	FLAC__ASSERT(0 != filename);	FLAC__ASSERT(0 != stats);	return (0 == stat(filename, stats));}static void set_file_stats_(const char *filename, struct stat *stats){	FLAC__ASSERT(0 != filename);	FLAC__ASSERT(0 != stats);	(void)chmod(filename, stats->st_mode);}static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, const FLAC__byte *name, float value){	char buffer[256];	char *saved_locale;	FLAC__StreamMetadata_VorbisComment_Entry entry;	FLAC__ASSERT(0 != block);	FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);	FLAC__ASSERT(0 != name);	FLAC__ASSERT(0 != value);	buffer[sizeof(buffer)-1] = '\0';	/*	 * We need to save the old locale and switch to "C" because the locale	 * influences the formatting of %f and we want it a certain way.	 */	saved_locale = setlocale(LC_ALL, 0);	setlocale(LC_ALL, "C");#if defined _MSC_VER || defined __MINGW32__	_snprintf(buffer, sizeof(buffer)-1, format, name, value);#else	snprintf(buffer, sizeof(buffer)-1, format, name, value);#endif	setlocale(LC_ALL, saved_locale);	entry.entry = (FLAC__byte *)buffer;	entry.length = strlen(buffer);	return FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true);}FLAC__bool grabbag__replaygain_is_valid_sample_frequency(unsigned sample_frequency){	static const unsigned valid_sample_rates[] = {		8000,		11025,		12000,		16000,		22050,		24000,		32000,		44100,		48000	};	static const unsigned n_valid_sample_rates = sizeof(valid_sample_rates) / sizeof(valid_sample_rates[0]);	unsigned i;	for(i = 0; i < n_valid_sample_rates; i++)		if(sample_frequency == valid_sample_rates[i])			return true;	return false;}FLAC__bool grabbag__replaygain_init(unsigned sample_frequency){	title_peak_ = album_peak_ = 0.0;	return InitGainAnalysis((long)sample_frequency) == INIT_GAIN_ANALYSIS_OK;}FLAC__bool grabbag__replaygain_analyze(const FLAC__int32 * const input[], FLAC__bool is_stereo, unsigned bps, unsigned samples){	/* using a small buffer improves data locality; we'd like it to fit easily in the dcache */	static Float_t lbuffer[2048], rbuffer[2048];	static const unsigned nbuffer = sizeof(lbuffer) / sizeof(lbuffer[0]);	FLAC__int32 block_peak = 0, s;	unsigned i, j;	FLAC__ASSERT(bps >= 4 && bps <= FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE);	FLAC__ASSERT(FLAC__MIN_BITS_PER_SAMPLE == 4);	/*	 * We use abs() on a FLAC__int32 which is undefined for the most negative value.	 * If the reference codec ever handles 32bps we will have to write a special	 * case here.	 */	FLAC__ASSERT(FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE < 32);	if(bps == 16) {		if(is_stereo) {			j = 0;			while(samples > 0) {				const unsigned n = local_min(samples, nbuffer);				for(i = 0; i < n; i++, j++) {					s = input[0][j];					lbuffer[i] = (Float_t)s;					s = abs(s);					block_peak = local_max(block_peak, s);					s = input[1][j];					rbuffer[i] = (Float_t)s;					s = abs(s);					block_peak = local_max(block_peak, s);				}				samples -= n;				if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK)					return false;			}		}		else {			j = 0;			while(samples > 0) {				const unsigned n = local_min(samples, nbuffer);				for(i = 0; i < n; i++, j++) {					s = input[0][j];					lbuffer[i] = (Float_t)s;					s = abs(s);					block_peak = local_max(block_peak, s);				}				samples -= n;				if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK)					return false;			}		}	}	else { /* bps must be < 32 according to above assertion */		const double scale = (			(bps > 16)?				(double)1. / (double)(1u << (bps - 16)) :				(double)(1u << (16 - bps))		);		if(is_stereo) {			j = 0;			while(samples > 0) {				const unsigned n = local_min(samples, nbuffer);				for(i = 0; i < n; i++, j++) {					s = input[0][j];					lbuffer[i] = (Float_t)(scale * (double)s);					s = abs(s);					block_peak = local_max(block_peak, s);					s = input[1][j];					rbuffer[i] = (Float_t)(scale * (double)s);					s = abs(s);					block_peak = local_max(block_peak, s);				}				samples -= n;				if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK)					return false;			}		}		else {			j = 0;			while(samples > 0) {				const unsigned n = local_min(samples, nbuffer);				for(i = 0; i < n; i++, j++) {					s = input[0][j];					lbuffer[i] = (Float_t)(scale * (double)s);					s = abs(s);					block_peak = local_max(block_peak, s);				}				samples -= n;				if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK)					return false;			}		}	}	{		const double peak_scale = (double)(1u << (bps - 1));		double peak = (double)block_peak / peak_scale;		if(peak > title_peak_)			title_peak_ = peak;		if(peak > album_peak_)			album_peak_ = peak;	}	return true;}void grabbag__replaygain_get_album(float *gain, float *peak){	*gain = (float)GetAlbumGain();	*peak = (float)album_peak_;	album_peak_ = 0.0;}void grabbag__replaygain_get_title(float *gain, float *peak){	*gain = (float)GetTitleGain();	*peak = (float)title_peak_;	title_peak_ = 0.0;}typedef struct {	unsigned channels;	unsigned bits_per_sample;	unsigned sample_rate;	FLAC__bool error;} DecoderInstance;static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data){	DecoderInstance *instance = (DecoderInstance*)client_data;	const unsigned bits_per_sample = frame->header.bits_per_sample;	const unsigned channels = frame->header.channels;	const unsigned sample_rate = frame->header.sample_rate;	const unsigned samples = frame->header.blocksize;	(void)decoder;	if(		!instance->error &&		(channels == 2 || channels == 1) &&		bits_per_sample == instance->bits_per_sample &&		channels == instance->channels &&		sample_rate == instance->sample_rate	) {		instance->error = !grabbag__replaygain_analyze(buffer, channels==2, bits_per_sample, samples);	}	else {		instance->error = true;	}	if(!instance->error)		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;	else		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;}static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data){	DecoderInstance *instance = (DecoderInstance*)client_data;	(void)decoder;	if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {		instance->bits_per_sample = metadata->data.stream_info.bits_per_sample;		instance->channels = metadata->data.stream_info.channels;		instance->sample_rate = metadata->data.stream_info.sample_rate;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -