示例#1
0
文件: audio.c 项目: wuyutaott/Test
static void capline(mpg123_handle *mh, long rate)
{
    int enci;
    const int  *encs;
    size_t      num_encs;
    mpg123_encodings(&encs, &num_encs);
    fprintf(stderr," %5ld |", pitch_rate(rate));
    for(enci=0; enci<num_encs; ++enci)
    {
        switch(mpg123_format_support(mh, rate, encs[enci]))
        {
        case MPG123_MONO:
            fprintf(stderr, "   M   |");
            break;
        case MPG123_STEREO:
            fprintf(stderr, "   S   |");
            break;
        case MPG123_MONO|MPG123_STEREO:
            fprintf(stderr, "  M/S  |");
            break;
        default:
            fprintf(stderr, "       |");
        }
    }
    fprintf(stderr, "\n");
}
示例#2
0
static snd_stream_t *S_MP3_CodecOpenStream (const char *filename)
{
	snd_stream_t *stream;
	long rate = 0;
	int encoding = 0, channels = 0;
	mp3_priv_t *priv = NULL;

	stream = S_CodecUtilOpen(filename, &mp3_codec);
	if (!stream)
		return NULL;

	stream->priv = Z_Malloc(sizeof(mp3_priv_t));
	priv = (mp3_priv_t *) stream->priv;
	priv->handle = mpg123_new(NULL, NULL);
	if (priv->handle == NULL)
	{
		Con_Printf("Unable to allocate mpg123 handle\n");
		goto _fail;
	}
	priv->handle_newed = 1;

	if (mpg123_replace_reader_handle(priv->handle, mp3_read, mp3_seek, NULL) != MPG123_OK ||
	    mpg123_open_handle(priv->handle, &stream->fh) != MPG123_OK)
	{
		Con_Printf("Unable to open mpg123 handle\n");
		goto _fail;
	}
	priv->handle_opened = 1;

	if (mpg123_getformat(priv->handle, &rate, &channels, &encoding) != MPG123_OK)
	{
		Con_Printf("Unable to retrieve mpg123 format for %s\n", filename);
		goto _fail;
	}

	switch (channels)
	{
	case MPG123_MONO:
		stream->info.channels = 1;
		break;
	case MPG123_STEREO:
		stream->info.channels = 2;
		break;
	default:
		Con_Printf("Unsupported number of channels %d in %s\n", channels, filename);
		goto _fail;
	}

	stream->info.rate = rate;

	switch (encoding)
	{
	case MPG123_ENC_UNSIGNED_8:
		stream->info.width = 1;
		break;
	case MPG123_ENC_SIGNED_8:
	/* unsupported: force mpg123 to convert */
		stream->info.width = 1;
		encoding = MPG123_ENC_UNSIGNED_8;
		break;
	case MPG123_ENC_SIGNED_16:
		stream->info.width = 2;
		break;
	case MPG123_ENC_UNSIGNED_16:
	default:
	/* unsupported: force mpg123 to convert */
		stream->info.width = 2;
		encoding = MPG123_ENC_SIGNED_16;
		break;
	}
	if (mpg123_format_support(priv->handle, rate, encoding) == 0)
	{
		Con_Printf("Unsupported format for %s\n", filename);
		goto _fail;
	}
	mpg123_format_none(priv->handle);
	mpg123_format(priv->handle, rate, channels, encoding);

	return stream;
_fail:
	if (priv)
	{
		if (priv->handle_opened)
			mpg123_close(priv->handle);
		if (priv->handle_newed)
			mpg123_delete(priv->handle);
		Z_Free(stream->priv);
	}
	S_CodecUtilClose(&stream);
	return NULL;
}
示例#3
0
bool nuiAudioDecoder::ReadInfo()
{
    if (!mpPrivate)
    {
        return false;
    }

    int err;

    int read;
    int toread = DECODER_INPUT_SIZE;
    unsigned char* pInput = new unsigned char[toread];
    do
    {
        read = mrStream.ReadUInt8(pInput, toread);

        size_t done = 0;
        err = mpg123_decode(mpPrivate->mpHandle, pInput, read, NULL, 0, &done);
//    err = mpg123_feed(mpPrivate->mpHandle, pInput, read);
    } while (err != MPG123_NEW_FORMAT && read > 0);

    mpg123_frameinfo frameinfo;
    err = mpg123_info(mpPrivate->mpHandle, &frameinfo);
    if (err != MPG123_OK)
    {
        return false;
    }

    int channels;
    if (frameinfo.mode == MPG123_M_MONO)
        channels = 1;
    else
        channels = 2;

    double samplerate = 44100;
    int encoding = MPG123_ENC_SIGNED_16;

    int supportedChannelConfig = mpg123_format_support(mpPrivate->mpHandle, samplerate, encoding);
    if (supportedChannelConfig == 0)
    {
        return false;
    }

    int channelConfig;
    bool mono = (supportedChannelConfig & MPG123_MONO) && (channels == 1);
    bool stereo = (supportedChannelConfig & MPG123_STEREO) && (channels == 2);
    if (mono)
        channelConfig = MPG123_MONO;
    else if (stereo)
        channelConfig = MPG123_STEREO;
    else
    {
        return false;
    }

    mpg123_format_none(mpPrivate->mpHandle);

    err = mpg123_format(mpPrivate->mpHandle, samplerate, channelConfig, encoding);
    if (err != MPG123_OK)
    {
        return false;
    }

    off_t sampleframes = mpg123_length(mpPrivate->mpHandle);
    if (sampleframes <= 0)
    {
        int bitrate = frameinfo.bitrate;
        nglFileSize streamSize = mrStream.Available();
        sampleframes = (streamSize / (bitrate * 1000.f  / 8.f)) * samplerate;
    }

    int BitsPerSample;
    if (encoding == MPG123_ENC_FLOAT_32)
        BitsPerSample = 32;
    else if (encoding == MPG123_ENC_SIGNED_16)
        BitsPerSample = 16;
    else
    {
        return false;
    }

    mInfo.SetSampleFrames(sampleframes);
    mInfo.SetSampleRate(samplerate);
    mInfo.SetChannels(channels);
    mInfo.SetBitsPerSample(BitsPerSample);
    mInfo.SetFileFormat(eAudioCompressed);
    mInfo.SetStartFrame(0);
    mInfo.SetStopFrame(mInfo.GetSampleFrames());

    return true;
}