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

📄 xaplayer.c

📁 一个非常好用的MP3音乐播放控件
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
|
|      MPEG audio decoder. Sample Player
|
|      (c) 1996-1998 MpegTV, LLC
|      Author: Gilles Boccon-Gibod (gilles@mpegtv.com)
|
 ****************************************************************/

/*
** This sample application shows how to use the Xaudio SYNC API to
** play a bistream in a program that does not need the decoder to run
** in its own thread or process. This is typically the case of a command
** line program, even though a command like program could very well use
** the ASYNC API also.
*/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/* xaudio general includes */
#include "xaudio.h"
#include "decoder.h"

/* xaudio plug-in modules */
#include "file_input.h"
#include "file_output.h"
#include "audio_output.h"
#include "mpeg_codec.h"
#include "wave_codec.h"

// uncomment the following line if you have the AAC codec module
//#define MODULES_ENABLE_AAC_CODEC
#ifdef MODULES_ENABLE_AAC_CODEC
#include "aac_codec.h"
#endif

// uncomment the following line if you have the QDesign codec module
//#define MODULES_ENABLE_QDMC_CODEC
#ifdef MODULES_ENABLE_QDMC_CODEC
#include "qdmc_codec.h"
#endif

/*----------------------------------------------------------------------
|       global options
+---------------------------------------------------------------------*/
struct {
    struct {
        int port;
        int volume;
        char *name;
    } output;
} Options;

/*----------------------------------------------------------------------
|       constants
+---------------------------------------------------------------------*/
#define PROGTITLE       "XAUDIO. MPEG Audio Player"
#define VERSION         "1.0a"
#define PROGNAME        "xaudio"

char *ModeStrings[4] = {
    "stereo", "joint-stereo", "dual-channel", "single-channel"
};

char *LayerStrings[4] = {
    "invalid", "I", "II", "III"
};

/*----------------------------------------------------------------------
|       cmdline_print_version
+---------------------------------------------------------------------*/
static void
cmdline_print_version(void)
{
    unsigned long version = xaudio_get_version(XA_VERSION_ID_IMPLEMENTATION);
    fprintf(stderr, PROGNAME " version " VERSION " [decoder library %d.%d.%d]\n",
            (version>>16)&0xFF,
            (version>> 8)&0xFF,
            (version    )&0xFF);
}

/*----------------------------------------------------------------------
|       cmdline_error
+---------------------------------------------------------------------*/
static void
cmdline_error(void)
{
    cmdline_print_version();
    fprintf(stderr, "use '" PROGNAME " -h' for a list of options\n");
    exit(1);
}

/*----------------------------------------------------------------------
|       cmdline_usage
+---------------------------------------------------------------------*/
static void
cmdline_usage(void)
{
    fprintf(stderr, 
            PROGTITLE "\n"
            "Version " VERSION "\n\n"
            "USAGE: " PROGNAME " [options] [<bitstream url>]\n"
            " <bitstream url> specifies input bitstream from.\n"
            " it can be one of:\n"
            "    <filename> or file:<filename>\n"
            "    http://[[user]:[pass]@]<hostname>[:port]/<path> (standard http URL)\n" 
            "    ftp://[[user]:[pass]@]<hostname>[:port]/<path> (standard ftp URL)\n"
            "    udp://<port> (listens for UDP broadcast on port <port>\n\n"
            " GENERAL OPTIONS:\n"
            "  -h, -help            prints this usage information\n"
            " OUTPUT OPTIONS:\n"
            "  -mono                output mono audio (left channel only)\n"
            "  -mono=left           same as -mono\n"
            "  -mono=right          output mono audio (right channel only)\n"
            "  -mono=mix            output mono audio (average left and right channels)\n\n"
            "  -output=<name>       specifies the name of the output.\n"
            "                       the output module is chosen based on the\n"
            "                       name (use 'xaudio -m' for a list of modules)\n"
            "                       with the builtin output modules, you can use:\n"
            "                       raw:<filename>, or just <filename> for raw PCM output\n"
            "                         or\n"
            "                       wav:<filename> for WAV output\n"
            "                         or\n"
            "                       - (just the characher '-') for standard output\n" 
            "  -port=line           enable audio on the sound card's line out\n"
            "  -port=speaker        enable audio on the speaker\n"
            "  -port=headphone      enable audio on the headphone\n"
            "                       (note: more than one -port=... option can be selected)\n"
            "  -volume=<n>          sets the volume of the audio output\n"
            "                       <n> is between 0 and 100\n");
    exit(0);
}

/*----------------------------------------------------------------------
|       cmdline_parse_argv
+---------------------------------------------------------------------*/
static char **
cmdline_parse_argv(XA_DecoderInfo *decoder, char **argv)
{
    char *option;

    while ((option = *argv++)) {
        if (!strcmp(option, "-h") || !strcmp(option, "-help")) {
            cmdline_usage();
        } else if (!strcmp(option, "-mono") || !strcmp(option, "-mono=left")) {
            decoder_codec_set_channels(decoder, XA_OUTPUT_MONO_LEFT);
        } else if (!strcmp(option, "-mono=right")) {
            decoder_codec_set_channels(decoder, XA_OUTPUT_MONO_RIGHT);
        } else if (!strcmp(option, "-mono=mix")) {
            decoder_codec_set_channels(decoder, XA_OUTPUT_MONO_MIX);
        } else if (!strncmp(option, "-output=", sizeof("-output=")-1)) {
            char *output = option+sizeof("-output=")-1;
            if (*output == '\0') {
                fprintf(stderr, PROGNAME ": invalid output filename\n");
                cmdline_error();
            }
            Options.output.name = output;
        } else if (!strncmp(option, "-port=", sizeof("-port=")-1)) {
            char *port = option+sizeof("-port=")-1;
            if (!strcmp(port, "line")) {
                Options.output.port |= XA_DECODER_CONTROL_OUTPUT_LINE;
            } else if (!strcmp(port, "speaker")) {
                Options.output.port |= XA_DECODER_CONTROL_OUTPUT_SPEAKER;
            } else  if (!strcmp(port, "headphone")) {
                Options.output.port |= XA_DECODER_CONTROL_OUTPUT_HEADPHONE;
            } else {
                fprintf(stderr, PROGNAME ": invalid port name [%s]\n",
                        option);
                cmdline_error();
            }
        } else if (!strncmp(option, "-volume=", sizeof("-volume=")-1)) {
            int volume = strtol(option+sizeof("-volume=")-1, NULL, 0);
            if (errno == EINVAL) {
                fprintf(stderr, PROGNAME ": invalid volume value [%s]\n", 
                        option);
                cmdline_error();
            }
            if (volume < 0 || volume > 100) {
                fprintf(stderr, PROGNAME ": volume out of range [%d]\n",
                        volume);
                cmdline_error();
            }
            Options.output.volume = volume;
        } else {
            /* filename */
            if (option[0] == '-') {
                fprintf(stderr, PROGNAME ": invalid otpion %s\n", option);
                cmdline_error();
            }
            return --argv;
        }
    }
    return --argv;
}

/*----------------------------------------------------------------------
|       cmdline_notify_progress
+---------------------------------------------------------------------*/
static void
cmdline_notify_progress(void *client, int source, int code, long value, 
                        const char *message)
{
    fprintf(stderr, "XAUDIO: progress code=%d value=%ld %s\n",
            code, value, message);
}

/*----------------------------------------------------------------------
|       cmdline_notify_debug
+---------------------------------------------------------------------*/
static void
cmdline_notify_debug(void *client, int source, int level, 
                     const char *message, const char *reason)
{
    fprintf(stderr, "XAUDIO: debug, source=%d, level=%d, %s ", 
            source, level, message);
    if (reason && strlen(reason)) {
        fprintf(stderr, "(%s)", reason);
    }
    fprintf(stderr, "\n");
}

/*----------------------------------------------------------------------
|       cmdline_notify_error
+---------------------------------------------------------------------*/
static void
cmdline_notify_error(void *client, int source, int code, 
                     const char *message, const char *reason)
{
    fprintf(stderr, "XAUDIO: error, source=%d, code=%d, %s", 
            source, code, message);
    if (reason && strlen(reason)) {
        fprintf(stderr, "(%s)", reason);
    }
    fprintf(stderr, "\n");
}

/*----------------------------------------------------------------------
|       cmdline_init
+---------------------------------------------------------------------*/
static XA_DecoderInfo *
cmdline_init(void)
{
    XA_DecoderInfo *decoder;
    int             status;

    /* create a decoder object */
    status = decoder_new(&decoder);
    if (status != XA_SUCCESS) {
        fprintf(stderr, 
                "XAUDIO: cannot init decoder (%s)",
                xaudio_error_string(status));
        exit(1);
    }

    /* register the codecs */
    {

⌨️ 快捷键说明

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