国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用openal與mpg123播放MP3,(轉)

2019-11-14 11:49:28
字體:
來源:轉載
供稿:網友

使用openal與mpg123播放MP3,附帶工程文件(轉)

2013-12-09 01:47 本站整理 瀏覽(5)

使用openal與mpg123播放MP3,附帶工程文件

使用openal和mpg123播放MP3文件使用靜態編譯,相關文件都在附件里相關工程文件:openal_mpg123_player.7z使用visual studio 2010編譯,理論上是跨平臺的相關說明請看源代碼注釋以及mpg123.h和openal文檔
/*AUTHOR bowman han* NOTE  this is a test PRogram for mpg123 basic usage* EMAIL  fancycode+blogcn123@Gmail.com* DATA 6/2 2012* Please DO NOT REMOVE THIS*/#include "openal/include/al.h"#include "openal/include/alc.h"#include <stdio.h>#include <Windows.h>#define NUM_BUFFERS 4#include "mpg123/mpg123.h"#include <conio.h>#pragma comment (lib, "./mpg123/libmpg123.lib")#pragma comment (lib, "./openal/lib/OpenAL32.lib")int main(){    ALuint g_Buffers[NUM_BUFFERS];    ALuint uiSource;    ALuint uiBuffer;    ALCdevice * pDevice = NULL;    ALCcontext *pContext = NULL;    ALboolean g_bEAX;    ALenum error, eBufferFormat;    ALint iDataSize, iFrequency;    ALuint ulFormat;    int iLoop,iBuffersProcessed, iTotalBuffersProcessed;    unsigned long ulDataSize = 0;    unsigned long ulFrequency = 0;    unsigned long ulBufferSize;    unsigned long ulBytesWritten;    long lRate;    int iEncoding, iChannels;    int encoding;    void *        pData = NULL;    WAVEFORMATEX *pwfex;    int iState, iQueuedBuffers;    mpg123_handle *mpg123 ;    int iMpg123_error;    //init mpg123 library    if(MPG123_OK != (iMpg123_error = mpg123_init()))    {        printf("failed to init mpg123/n");        return -1;    }    //open a default mpg123 decoder    mpg123 = mpg123_new(mpg123_decoders()[0], &iMpg123_error);    //open a openal default device    pDevice = alcOpenDevice(NULL); //select the perfered device        if(pDevice){        pContext = alcCreateContext(pDevice, NULL);        alcMakeContextCurrent(pContext);    }else    {        printf("failed to get a openal decice/n");        return -2;    }    g_bEAX = alIsExtensionPresent("EAX2.0");    //Generate openal  Buffers    alGetError();  //clear all error code        alGenBuffers(NUM_BUFFERS, g_Buffers);    if((error = alGetError()) != AL_NO_ERROR)    {        fprintf(stderr, "alGenBuffers :%s", error);    }    alGenSources(1, &uiSource);    //try to open a mp3 file, modify to your own mp3 files    if(MPG123_OK != (iMpg123_error = mpg123_open(mpg123,"D://sound//320k.mp3")))    {        fprintf(stderr,"error in open mp3 file/n");        return -1;    }    //get mp3 format infomation    mpg123_getformat(mpg123, &lRate, &iChannels,  &iEncoding);    //there only parse stereo mp3 file    if(iChannels ==2)    {        ulFormat = alGetEnumValue("AL_FORMAT_STEREO16");        ulBufferSize = lRate;  //set buffer to 250ms        ulBufferSize -= (ulBufferSize % 4);  //set pcm Block align        ulFrequency = lRate;  //set pcm sample rate    }    else    {        printf("channels info%i/n", iChannels);        return -3;    }    pData = malloc(ulBufferSize);    //feed data to openal buffer    for(iLoop= 0; iLoop < 4; iLoop++)    {        mpg123_read(mpg123, (char *)pData, ulBufferSize,&ulBytesWritten);        alBufferData(g_Buffers[iLoop], ulFormat, pData, ulBytesWritten, ulFrequency);        alSourceQueueBuffers(uiSource, 1, &g_Buffers[iLoop]);    }    alSourcePlay(uiSource);    iTotalBuffersProcessed = 0;    printf("playing/n");    //wait for key press    while(!_kbhit())    {        Sleep(20);        iBuffersProcessed = 0;        alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed);        iTotalBuffersProcessed += iBuffersProcessed;        printf("Buffers total Processed %d/r", iTotalBuffersProcessed);        while(iBuffersProcessed)        {            uiBuffer = 0;            alSourceUnqueueBuffers(uiSource, 1, &uiBuffer);            mpg123_read(mpg123, (char *)pData, ulBufferSize, &ulBytesWritten);            if(ulBytesWritten)            {                alBufferData(uiBuffer, ulFormat, pData, ulBytesWritten, ulFrequency);                alSourceQueueBuffers(uiSource, 1, &uiBuffer);            }                iBuffersProcessed--;        }        alGetSourcei(uiSource, AL_SOURCE_STATE, &iState);        if(iState != AL_PLAYING)        {            alGetSourcei(uiSource, AL_BUFFERS_QUEUED, &iQueuedBuffers);                        if(iQueuedBuffers)            {                alSourcePlay(uiSource);//buffers have data, play it            }            else            {                //there is no data any more                break;            }        }    }    //close mpg123    mpg123_close(mpg123);    //stop the source and clear the queue    alSourceStop(uiSource);    alSourcei(uiSource, AL_BUFFER, 0);    free(pData);    pData = NULL;        return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南投市| 阳原县| 浦县| 台东县| 东宁县| 汝阳县| 富平县| 安多县| 平遥县| 香格里拉县| 丽江市| 松江区| 固原市| 桦甸市| 扶余县| 南充市| 津南区| 珠海市| 河西区| 沂源县| 广德县| 牟定县| 册亨县| 疏勒县| 拜城县| 万全县| 阳谷县| 新竹县| 台中市| 蓝田县| 尉氏县| 阳原县| 亚东县| 左云县| 镇康县| 龙游县| 呼图壁县| 兴海县| 甘泉县| 东明县| 甘孜县|