Second patch from X-Fixer: file info dialog, tag editor, tag formatting in plugin config

This commit is contained in:
Josh Coalson
2003-01-14 09:01:31 +00:00
parent ecf57b7514
commit 4563fa26a1
14 changed files with 1845 additions and 680 deletions

View File

@@ -27,27 +27,23 @@
#include "plugin_common/all.h"
#include "share/grabbag.h"
#include "config.h"
#include "infobox.h"
#include "tagz.h"
BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
/* post this to the main window at end of file (after playback as stopped) */
#define WM_WA_MPEG_EOF WM_USER+2
typedef struct {
FLAC__bool abort_flag;
int seek_to;
int paused;
unsigned total_samples;
unsigned bits_per_sample;
unsigned output_bits_per_sample;
unsigned channels;
unsigned sample_rate;
unsigned length_in_msec;
DitherContext dither_context;
FLAC__bool has_replaygain;
double replay_scale;
DitherContext dither_context;
} file_info_struct;
@@ -59,14 +55,13 @@ static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__Str
static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void get_description_(const char *filename, char *description, unsigned max_size);
In_Module mod_; /* the input module (declared near the bottom of this file) */
char ini_name[MAX_PATH];
flac_config_t flac_cfg;
static output_config_t cfg; /* local copy */
static In_Module mod_; /* the input module (declared near the bottom of this file) */
static char lastfn_[MAX_PATH]; /* currently playing file (used for getting info on the current file) */
static int decode_pos_ms_; /* current decoding position, in milliseconds */
static int paused_; /* are we paused? */
static int seek_needed_; /* if != -1, it is the point that the decode thread should seek to, in ms. */
#define SAMPLES_PER_WRITE 576
static FLAC__int32 reservoir_[FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
@@ -76,14 +71,24 @@ static file_info_struct file_info_;
static FLAC__FileDecoder *decoder_;
static volatile int killDecodeThread = 0; /* the kill switch for the decode thread */
HANDLE thread_handle = INVALID_HANDLE_VALUE; /* the handle to the decode thread */
static HANDLE thread_handle = NULL; /* the handle to the decode thread */
static DWORD WINAPI DecodeThread(void *b); /* the decode thread procedure */
static void show_error(const char *message,...)
{
char foo[512];
va_list args;
va_start(args, message);
vsprintf(foo, message, args);
va_end(args);
MessageBox(mod_.hMainWindow, foo, "FLAC Plug-in Error", MB_ICONSTOP);
}
void config(HWND hwndParent)
{
if (DoConfig(hwndParent))
if (DoConfig(mod_.hDllInstance, hwndParent))
WriteConfig();
}
@@ -98,7 +103,7 @@ void init()
decoder_ = FLAC__file_decoder_new();
strcpy(lastfn_, "");
// read config
/* read config */
GetModuleFileName(NULL, ini_name, sizeof(ini_name));
p = strrchr(ini_name, '.');
if (!p) p = ini_name + strlen(ini_name);
@@ -115,85 +120,91 @@ void quit()
}
int isourfile(char *fn) { return 0; }
/* used for detecting URL streams.. unused here. strncmp(fn, "http://", 7) to detect HTTP streams, etc */
int play(char *fn)
{
int maxlatency;
int thread_id;
HANDLE input_file;
DWORD file_size; /*@@@ fixme 64-bit */
if(0 == decoder_)
if (decoder_ == 0)
return 1;
input_file = CreateFile(fn, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(input_file == INVALID_HANDLE_VALUE)
return -1;
file_size = GetFileSize(input_file, NULL);
CloseHandle(input_file);
file_info_.abort_flag = false;
file_info_.has_replaygain = false;
if(!safe_decoder_init_(fn, decoder_))
if (!safe_decoder_init_(fn, decoder_))
return 1;
cfg = flac_cfg.output;
strcpy(lastfn_, fn);
wide_samples_in_reservoir_ = 0;
file_info_.output_bits_per_sample = file_info_.has_replaygain && flac_cfg.output.replaygain.enable ?
flac_cfg.output.resolution.replaygain.dither ? flac_cfg.output.resolution.replaygain.bps_out : file_info_.bits_per_sample :
flac_cfg.output.resolution.normal.dither_24_to_16 ? min(file_info_.bits_per_sample, 16) : file_info_.bits_per_sample;
file_info_.output_bits_per_sample = file_info_.has_replaygain && cfg.replaygain.enable ?
cfg.resolution.replaygain.bps_out :
cfg.resolution.normal.dither_24_to_16 ? min(file_info_.bits_per_sample, 16) : file_info_.bits_per_sample;
if (file_info_.has_replaygain && flac_cfg.output.replaygain.enable && flac_cfg.output.resolution.replaygain.dither)
FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.bits_per_sample, flac_cfg.output.resolution.replaygain.noise_shaping);
if (file_info_.has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)
FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.bits_per_sample, cfg.resolution.replaygain.noise_shaping);
maxlatency = mod_.outMod->Open(file_info_.sample_rate, file_info_.channels, file_info_.output_bits_per_sample, -1, -1);
if(maxlatency < 0) /* error opening device */
if (maxlatency < 0) /* error opening device */
return 1;
/* dividing by 1000 for the first parameter of setinfo makes it */
/* display 'H'... for hundred.. i.e. 14H Kbps. */
mod_.SetInfo((file_info_.sample_rate*file_info_.bits_per_sample*file_info_.channels)/1000, file_info_.sample_rate/1000, file_info_.channels, 1);
mod_.SetInfo((int)(file_size/(125.*file_info_.total_samples/file_info_.sample_rate)), file_info_.sample_rate/1000, file_info_.channels, 1);
/* initialize vis stuff */
mod_.SAVSAInit(maxlatency, file_info_.sample_rate);
mod_.VSASetInfo(file_info_.sample_rate, file_info_.channels);
/* set the output plug-ins default volume */
mod_.outMod->SetVolume(-666);
paused_ = 0;
file_info_.paused = 0;
file_info_.seek_to = -1;
decode_pos_ms_ = 0;
seek_needed_ = -1;
killDecodeThread = 0;
thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) DecodeThread, NULL, 0, &thread_id);
thread_handle = CreateThread(NULL, 0, DecodeThread, NULL, 0, &thread_id);
if (!thread_handle)
return 1;
return 0;
}
void pause()
{
paused_ = 1;
file_info_.paused = 1;
mod_.outMod->Pause(1);
}
void unpause()
{
paused_ = 0;
file_info_.paused = 0;
mod_.outMod->Pause(0);
}
int ispaused()
{
return paused_;
return file_info_.paused;
}
void stop()
{
if(thread_handle != INVALID_HANDLE_VALUE) {
if (thread_handle) {
killDecodeThread = 1;
if(WaitForSingleObject(thread_handle, 2000) == WAIT_TIMEOUT) {
MessageBox(mod_.hMainWindow, "error asking thread to die!\n", "error killing decode thread", 0);
show_error("Error while stopping decoding thread.");
TerminateThread(thread_handle, 0);
}
CloseHandle(thread_handle);
thread_handle = INVALID_HANDLE_VALUE;
thread_handle = NULL;
}
safe_decoder_finish_(decoder_);
@@ -214,7 +225,7 @@ int getoutputtime()
void setoutputtime(int time_in_ms)
{
seek_needed_ = time_in_ms;
file_info_.seek_to = time_in_ms;
}
void setvolume(int volume) { mod_.outMod->SetVolume(volume); }
@@ -222,15 +233,7 @@ void setpan(int pan) { mod_.outMod->SetPan(pan); }
int infoDlg(char *fn, HWND hwnd)
{
/* @@@TODO: implement info dialog. */
if (!stricmp(fn, lastfn_)) {
char buffer[512];
sprintf(buffer, "%s\nLength: %d:%02d, ReplayGain: %spresent\n%dHz, %d channel(s), %dbps (%dbps on output)",
lastfn_, file_info_.length_in_msec/60000, (file_info_.length_in_msec/1000)%60, file_info_.has_replaygain ? "" : "not ",
file_info_.sample_rate, file_info_.channels, file_info_.bits_per_sample, file_info_.output_bits_per_sample);
MessageBox(hwnd, buffer, "FLAC Info", 0);
}
DoInfoBox(mod_.hDllInstance, hwnd, fn);
return 0;
}
@@ -240,27 +243,24 @@ void getfileinfo(char *filename, char *title, int *length_in_msec)
if (!filename || !*filename) {
filename = lastfn_;
if(length_in_msec) {
if (length_in_msec) {
*length_in_msec = getlength();
length_in_msec = 0; /* force skip in following code */
}
}
if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
MessageBox(mod_.hMainWindow, filename, "ERROR: invalid/missing FLAC metadata", 0);
if(title) {
static const char *errtitle = "Invalid FLAC File: ";
sprintf(title, "%s\"%s\"", errtitle, filename);
}
if(length_in_msec)
if (!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
if (title)
sprintf(title, "Invalid FLAC: %s", filename);
if (length_in_msec)
*length_in_msec = -1;
return;
}
if(title) {
get_description_(filename, title, MAX_PATH);
}
if(length_in_msec)
if (title)
get_description_(filename, title, 400);
if (length_in_msec)
*length_in_msec = (int)(streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100));
}
@@ -314,12 +314,12 @@ static DWORD WINAPI DecodeThread(void *unused)
const unsigned target_bps = file_info_.output_bits_per_sample;
const unsigned sample_rate = file_info_.sample_rate;
if(seek_needed_ != -1) {
const double distance = (double)seek_needed_ / (double)getlength();
if(file_info_.seek_to != -1) {
const double distance = (double)file_info_.seek_to / (double)getlength();
const unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
decode_pos_ms_ = (int)(distance * (double)getlength());
seek_needed_ = -1;
file_info_.seek_to = -1;
done = 0;
mod_.outMod->Flush(decode_pos_ms_);
}
@@ -338,7 +338,7 @@ static DWORD WINAPI DecodeThread(void *unused)
break;
}
else if(!FLAC__file_decoder_process_single(decoder_)) {
MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder_)], "READ ERROR processing frame", 0);
show_error("Error while processing frame (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder_)]);
done = 1;
break;
}
@@ -353,7 +353,7 @@ static DWORD WINAPI DecodeThread(void *unused)
int bytes;
unsigned i;
if(flac_cfg.output.replaygain.enable && file_info_.has_replaygain) {
if(cfg.replaygain.enable && file_info_.has_replaygain) {
bytes = (int)FLAC__plugin_common__apply_gain(
sample_buffer_,
reservoir_,
@@ -362,9 +362,9 @@ static DWORD WINAPI DecodeThread(void *unused)
bits_per_sample,
target_bps,
(float)file_info_.replay_scale,
flac_cfg.output.replaygain.hard_limit,
flac_cfg.output.resolution.replaygain.dither,
(NoiseShaping)flac_cfg.output.resolution.replaygain.noise_shaping,
cfg.replaygain.hard_limit,
cfg.resolution.replaygain.dither,
(NoiseShaping)cfg.resolution.replaygain.noise_shaping,
&file_info_.dither_context
);
}
@@ -400,7 +400,7 @@ static DWORD WINAPI DecodeThread(void *unused)
In_Module mod_ =
{
IN_VER,
"Reference FLAC Player v" VERSION,
"FLAC Reference Player v" VERSION,
0, /* hMainWindow */
0, /* hDllInstance */
"FLAC\0FLAC Audio File (*.FLAC)\0"
@@ -440,21 +440,22 @@ In_Module mod_ =
};
__declspec( dllexport ) In_Module * winampGetInModule2()
__declspec(dllexport) In_Module *winampGetInModule2()
{
return &mod_;
}
BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
/***********************************************************************
* local routines
**********************************************************************/
FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
{
if(decoder == 0) {
MessageBox(mod_.hMainWindow, "Decoder instance is NULL", "ERROR initializing decoder", 0);
return false;
}
FLAC__ASSERT(0 != decoder);
safe_decoder_finish_(decoder);
@@ -467,18 +468,19 @@ FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
FLAC__file_decoder_set_write_callback(decoder, write_callback_);
FLAC__file_decoder_set_error_callback(decoder, error_callback_);
FLAC__file_decoder_set_client_data(decoder, &file_info_);
if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR initializing decoder", 0);
show_error("Error while initializing decoder (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
return false;
}
if(!FLAC__file_decoder_process_until_end_of_metadata(decoder)) {
MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR processing metadata", 0);
show_error("Error while processing metadata (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
return false;
}
if(file_info_.abort_flag)
return false; /* metadata callback already popped up the error dialog */
if (file_info_.abort_flag)
return false; /* metadata callback already popped up the error dialog */
return true;
}
@@ -524,13 +526,13 @@ void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMeta
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xfffffffful);
file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
file_info->channels = metadata->data.stream_info.channels;
file_info->sample_rate = metadata->data.stream_info.sample_rate;
if(file_info->bits_per_sample != 8 && file_info->bits_per_sample != 16 && file_info->bits_per_sample != 24) {
MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16/24-bit samples\n", "ERROR: plugin can only handle 8/16/24-bit samples", 0);
if(file_info->bits_per_sample!=8 && file_info->bits_per_sample!=16 && file_info->bits_per_sample!=24) {
show_error("This plugin can only handle 8/16/24-bit samples.");
file_info->abort_flag = true;
return;
}
@@ -538,71 +540,101 @@ void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMeta
}
else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
double gain, peak;
if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
if(grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, &gain, &peak)) {
file_info_.has_replaygain = true;
file_info_.replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)flac_cfg.output.replaygain.preamp, /*prevent_clipping=*/!flac_cfg.output.replaygain.hard_limit);
file_info_.replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);
}
}
}
void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
file_info_struct *file_info = (file_info_struct *)client_data;
file_info_struct *file_info = (file_info_struct*)client_data;
(void)decoder;
if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
file_info->abort_flag = true;
}
static FLAC__bool local__is_blank(const char *s)
/*
* title formatting
*/
typedef struct
{
return 0 == s || *s == '\0';
FLAC_Plugin__CanonicalTag t;
const char *filename;
} tag_param_t;
static __inline char *GetFileName(const char *fullname)
{
const char *c = fullname + strlen(fullname) - 1;
while (c > fullname)
{
if (*c=='\\' || *c=='/')
{
c++;
break;
}
c--;
}
return (char*)c;
}
static const T_CHAR *get_tag(const T_CHAR *tag, void *param)
{
tag_param_t *p = (tag_param_t*)param;
if (!stricmp(tag, "path") || !stricmp(tag, "filepath") || !stricmp(tag, "url"))
return p->filename;
else if (!stricmp(tag, "filename"))
{
static char foo[MAX_PATH];
char *c;
strcpy(foo, GetFileName(p->filename));
if (c = strrchr(foo, '.')) *c = 0;
return foo;
}
else if (!stricmp(tag, "title"))
return p->t.title;
else if (!stricmp(tag, "artist"))
return p->t.performer ? p->t.performer : p->t.composer;
else if (!stricmp(tag, "composer"))
return p->t.composer;
else if (!stricmp(tag, "performer"))
return p->t.performer;
else if (!stricmp(tag, "album"))
return p->t.album;
else if (!stricmp(tag, "year") || !stricmp(tag, "date"))
return p->t.year_recorded ? p->t.year_recorded : p->t.year_performed;
else if (!stricmp(tag, "year_recorded"))
return p->t.year_recorded;
else if (!stricmp(tag, "year_performed"))
return p->t.year_performed;
else if (!stricmp(tag, "track_number"))
return p->t.track_number;
else if (!stricmp(tag, "tracks_in_album"))
return p->t.tracks_in_album;
else if (!stricmp(tag, "genre"))
return p->t.genre;
else if (!stricmp(tag, "comment") || !stricmp(tag, "description"))
return p->t.comment;
else return NULL;
}
void get_description_(const char *filename, char *description, unsigned max_size)
{
FLAC_Plugin__CanonicalTag tag;
tag_param_t param;
FLAC_plugin__canonical_tag_init(&tag);
FLAC_plugin__canonical_tag_get_combined(filename, &tag);
FLAC_plugin__canonical_tag_init(&param.t);
FLAC_plugin__canonical_tag_get_combined(filename, &param.t);
param.filename = filename;
/* @@@ when config window is done, add code here for converting to user charset ala plugin_xmms */
tagz_format(flac_cfg.title.tag_format, get_tag, NULL, &param, description, max_size);
if(local__is_blank(tag.performer) && local__is_blank(tag.composer) && local__is_blank(tag.title)) {
/* set the description to the filename */
char *p;
const char *temp = strrchr(filename, '\\');
if(0 == temp)
temp = strrchr(filename, '/');
if(0 == temp)
temp = filename;
else
temp++;
strncpy(description, temp, max_size);
description[max_size-1] = '\0';
if(0 != (p = strrchr(description, '.')))
*p = '\0';
}
else {
char *artist = !local__is_blank(tag.performer)? tag.performer : !local__is_blank(tag.composer)? tag.composer : "Unknown Artist";
char *title = !local__is_blank(tag.title)? tag.title : "Untitled";
/* there's no snprintf in VC6 so we get sloppy */
#if 0
snprintf(description, max_size, "%s - %s", artist, title);
#else
const unsigned needed = strlen(artist) + strlen(title) + 3 + 1;
if(needed <= max_size)
sprintf(description, "%s - %s", artist, title);
else {
char *p = malloc(needed);
if(0 != p) {
sprintf(p, "%s - %s", artist, title);
p[max_size-1] = '\0';
strcpy(description, p);
}
}
#endif
}
FLAC_plugin__canonical_tag_clear(&tag);
FLAC_plugin__canonical_tag_clear(&param.t);
}