使用音频引擎MITA读取cue文件源代码

来源:互联网 发布:多多返利建站系统源码 编辑:程序博客网 时间:2024/06/05 21:50

#include "../../include/mita.h"/**< @brief MITA SDK Header */
#ifdef _DEBUG
#pragma comment(lib, "../../library/mitaD.lib")
#else
#pragma comment(lib, "../../library/mita.lib")
#endif

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

#pragma warning(push)
#pragma warning(disable:4996)

MITA_STATIC
MITA_INLINE
MITA_VOID
MITA_CheckError(MITA_VOID)
{
MITA_ERROR lastError = MITA_GetLastError();
if (lastError != MITA_ERROR_OK)
{
printf("ERROR: MITA Framework error with %u codes\n", lastError);
exit(lastError);
}
}

MITA_STATIC
MITA_INLINE
MITA_BYTE
MITA_EnterKey(MITA_VOID)
{
MITA_INT32 key = 0xFF;
do
{
if (kbhit())
{
key = getch();
break;
}
Sleep(10);
} while (1);
if (key >= 'a' && key <= 'z')
key -= 32;
return key;
}

//////////////////////////////////////////////////////////////////////////
//ChannelMask must has channel number in high 8 bits.
// | 00000000 | MMMMMMMM MMMMMMMM MMMMMMMM |
// Nums ChannelMask
//How to set number?
//MITA_CHNFMT_SetNum(Mask, 2)
//Also you can use MITA_GuessChannelMode.
//
// The channel number must equal with channel mask.
// ChannelMask = MITA_CP_FRONT_LEFT|MITA_CP_FRONT_RIGHT;//Use two speaker
// MITA_CHNFMT_SetNum(ChannelMask, 2);//Set 2
//
MITA_STATIC
MITA_INLINE
MITA_HOUTPUT
MITA_CreateDefaultOutput(MITA_HINSTANCE hInstance, MITA_DWORD ChannelMask)
{
MITA_HOUTPUT hOutput;
MITA_HDEVICE hDevice;
MITA_DEVICEPARAM Param;
MITA_SIZE i, n;
MITA_DEVICEINFO DevInfo;
MITA_BYTE Key = 'A';
MITA_DWORD Ids[10];
MITA_BYTE MaxIds = 0;

MITA_Ins_CreateObject(hInstance, &IID_DevWinMME, &hDevice);
MITA_CheckError();

MITA_Device_GetDeviceNums(hDevice, &n);
MITA_CheckError();

printf("\nSelect a output device: \n");
_try_again:
Key = 'A';
for (i = 0; i < n; i++)
{
MITA_Device_GetDevice(hDevice, i, &DevInfo);
MITA_CheckError();
if (DevInfo.Type == MITA_CT_OUTPUT)
{
Ids[Key - 'A'] = DevInfo.DevId;
printf("[%c] Device %s [%f - %f]\n", Key, MITA_Text_ToSystemS(DevInfo.DevName, MITA_TCT_DEFAULT), DevInfo.Latency[0], DevInfo.Latency[1]);
Key++;
MaxIds++;
}
}
Key = MITA_EnterKey() - 'A';
if (Key >= MaxIds)
{
printf("ERROR: Invalid input keys, please try again.\n");
goto _try_again;
}

Param.Callback= MITA_NULL;
Param.CallbackHandle= MITA_NULL;
Param.Customer= MITA_NULL;/**< @brief for future use */
Param.ChannelFmt.mask= ChannelMask;/**< @brief The mask of channels. */
Param.ChannelFmt.sfmt= MITA_SDT_F32;/**< @brief Float32 Sample */
Param.ChannelFmt.sps= 44100.0f;/**< @brief 44.1KHz */
Param.Latency= 0.1f;/**< @brief 100 ms latency */
Param.Type= MITA_CT_OUTPUT;/**< @brief For output. */
Param.DevId= Ids[Key];/**< @brief The id of device */

MITA_Output_CreateByDevice(hInstance, hDevice, &Param, MITA_FALSE, &hOutput);
MITA_CheckError();

return hOutput;
}

MITA_STATIC
MITA_INLINE
MITA_VOID
MITA_PrintDecSupportExts(MITA_HINSTANCE hInstance)
{
MITA_HFACTORY DecFactory;
MITA_HOBJECTLIST DecList;
MITA_SIZE i, n, j, jn;
MITA_AFD afd;
MITA_HDECODER hDecoder;
MITA_Ins_GetFactory(hInstance, MITA_MT_DECODER, &DecFactory);
MITA_CheckError();
MITA_ObjectList_Build(hInstance, DecFactory, MITA_FALSE, &DecList);
MITA_CheckError();
MITA_ObjectList_Count(DecList, &n);
for (i = 0; i < n; i++)
{
MITA_ObjectList_Get(DecList, i, &hDecoder);
MITA_Decoder_GetFileDescribeNums(hDecoder, &jn);
for (j = 0; j < jn; j++)
{
MITA_Decoder_GetFileDescribe(hDecoder, j, &afd);
printf("%s;", MITA_Text_ToSystemS(afd.ext, MITA_TCT_DEFAULT));
}
}
printf("\n");
MITA_CloseHandle(DecList);
}

MITA_STATIC
MITA_INLINE
MITA_VOID
MITA_PrintSoundArray(MITA_HARRAY hItemArray)
{
MITA_SIZEi, n;
MITA_HSOUNDhSoundItem;
MITA_WCHARszPath[MITA_MAXPATH];
MITA_UUIDcoderGuid;

printf("\n");
n = MITA_Array_GetCount(hItemArray);
for (i = 0; i < n; i++)
{
MITA_Array_Get(hItemArray, i, &hSoundItem);
MITA_Sound_GetPath(hSoundItem, szPath, MITA_MAXPATH);
MITA_Sound_GetCoderGUID(hSoundItem, &coderGuid);
printf("Path: %s\n", MITA_Text_ToSystemS(szPath, MITA_TCT_DEFAULT));
}
}

MITA_STATIC
MITA_INLINE
MITA_VOID
MITA_DeleteSoundArray(MITA_HARRAY hItemArray)
{
MITA_SIZEi, n;
MITA_HSOUNDhSoundItem;

n = MITA_Array_GetCount(hItemArray);
for (i = 0; i < n; i++)
{
MITA_Array_Get(hItemArray, i, &hSoundItem);
MITA_CloseHandle(hSoundItem);
}
MITA_CloseHandle(hItemArray);
}

void main(void)
{
MITA_HINSTANCE gInstance = MITA_NULL;
MITA_CHAR szVersion[64];
MITA_SIZE PluginCount = 0;
MITA_HINPUT hCurItem = MITA_NULL;
MITA_HOUTPUT hOutput;
MITA_HENGINE hEngine;
MITA_DWORD ChannelMask;
MITA_BYTE Key = 0;
MITA_TIME CurTime;
MITA_TIME DurTime;
MITA_DWORD cur_ms, dur_ms;
MITA_CHANNELFORMAT ChannelFmt;
MITA_STATUS Status;
MITA_HARRAY hItemArray = MITA_NULL;
MITA_SIZECurItemIndex = 0;
MITA_SIZETotalItem = 0;

printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n");
printf(";;Copyright(C) 杭州蜜柑科技有限公司 2008 - 2011. 保留所有权利。;;\n");
printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n");
printf(";;Decode Cue Example ;;\n");
printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n");

//////////////////////////////////////////////////////////////////////////
//1. Create a global instance handle.
gInstance = MITA_Initialize(MITA_NULL);
MITA_CheckError();

//////////////////////////////////////////////////////////////////////////
//2. Check SDK version, this was not necessary.
MITA_GetVersionStringA(szVersion, 64);
MITA_CheckError();
printf(";;MITA: Version %s\n", szVersion);

//////////////////////////////////////////////////////////////////////////
//3. Load all existed plugins.
MITA_Ins_LoadPluginFromDir(gInstance, L"../../plugins", &PluginCount);
MITA_CheckError();
printf(";;MITA: Load %u plugins\n", PluginCount);
printf(";;Support: ;;\n");
MITA_PrintDecSupportExts(gInstance);
printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n");

//////////////////////////////////////////////////////////////////////////
//4. Create Engine Object
MITA_Engine_Create(gInstance, MITA_ENGINEFLAG_CHECKPREPARE|MITA_ENGINEFLAG_CHECKRENDER, &hEngine);
MITA_CheckError();

//////////////////////////////////////////////////////////////////////////
//5. Create Output Source
MITA_GuessChannelMode(8, &ChannelMask);
hOutput = MITA_CreateDefaultOutput(gInstance, ChannelMask);

//////////////////////////////////////////////////////////////////////////
//6. Add source to engine.
MITA_Engine_AddSource(hEngine, hOutput);
MITA_CheckError();
MITA_Output_Startup(hOutput);
MITA_CheckError();

//////////////////////////////////////////////////////////////////////////
//7. Load input source
{
MITA_PARAMPARSESONG Param;

Param.dwTagFlag= MITA_TAGREAD_NONE;
Param.hDecoderChain= MITA_NULL;
Param.hInstance= gInstance;
Param.hItemArray= MITA_NULL;
Param.hStream= MITA_NULL;
Param.nStreamBeginPos= 0;
Param.lpPath= L"..\\Media\\test.cue";

MITA_Ins_ParseSong(&Param);
MITA_CheckError();

hItemArray = Param.hItemArray;
}
TotalItem = MITA_Array_GetCount(hItemArray);
{
MITA_HSOUND hSound;

/////////////////////////////////////////////////////
//阻止MITA_Engine_DeleteSource自动移除hSound对象
for (CurItemIndex = 0; CurItemIndex < TotalItem; CurItemIndex++)
{
MITA_Array_Get(hItemArray, CurItemIndex, &hSound);
MITA_Sound_ReadOnly(hSound, MITA_TRUE);
}
CurItemIndex = 0;

MITA_Array_Get(hItemArray, CurItemIndex, &hSound);
MITA_Input_LoadBySound(gInstance, hSound, &hCurItem);
MITA_Engine_AddSource(hEngine, hCurItem);
MITA_Input_Play(hCurItem, MITA_NULL);
}

printf("\n");
printf("Press O to prev sound.\n");
printf("Press P to next sound.\n");
printf("Press Esc to exit\n");
do
{
if (kbhit())
{
Key = getch();

switch (Key)
{
case 'o':
case 'O':
{
//////////////////////////////////////////////////
//To Prev
MITA_HSOUND hSound;
MITA_Engine_DeleteSource(hEngine, hCurItem);//Delete Current Source
CurItemIndex = (CurItemIndex-1+TotalItem)%TotalItem;//Move to prev source
MITA_Array_Get(hItemArray, CurItemIndex, &hSound);
MITA_Input_LoadBySound(gInstance, hSound, &hCurItem);//load new source
MITA_Engine_AddSource(hEngine, hCurItem);
MITA_Input_Play(hCurItem, MITA_NULL);
}break;
case 'p':
case 'P':
{
//////////////////////////////////////////////////
//To Next
MITA_HSOUND hSound;
MITA_Engine_DeleteSource(hEngine, hCurItem);//Delete Current Source
CurItemIndex = (CurItemIndex+1)%TotalItem;//Move to next source
MITA_Array_Get(hItemArray, CurItemIndex, &hSound);
MITA_Input_LoadBySound(gInstance, hSound, &hCurItem);//load new source
MITA_Engine_AddSource(hEngine, hCurItem);
MITA_Input_Play(hCurItem, MITA_NULL);
}break;
}
}

MITA_Source_GetStatus(hCurItem, &Status);
MITA_Source_GetCurTime(hCurItem, &CurTime);
MITA_Input_GetDuration(hCurItem, &DurTime);
MITA_Input_GetChannelFormat(hCurItem, &ChannelFmt);
MITA_Time_Cvt(&ChannelFmt, &CurTime, MITA_TIMEUNIT_MICRSECOND, &CurTime);
MITA_Time_Cvt(&ChannelFmt, &DurTime, MITA_TIMEUNIT_MICRSECOND, &DurTime);
cur_ms = (MITA_DWORD)MITA_Time_GetData(CurTime);
dur_ms = (MITA_DWORD)MITA_Time_GetData(DurTime);

printf("%10s %02u:%02u:%02u/%02u:%02u:%02u [%u channels][%u/%u]\r", Status==MITA_STATUS_RUNNING?"playing":(Status==MITA_STATUS_PAUSE?"pause":"stop"),
cur_ms / 1000 / 60,
cur_ms / 1000 % 60,
cur_ms / 10 % 100,
dur_ms / 1000 / 60,
dur_ms / 1000 % 60,
dur_ms / 10 % 100,
MITA_CHNFMT_GetNum(ChannelFmt.mask), CurItemIndex+1, TotalItem);
if (Status == MITA_STATUS_IDLE)
{
//////////////////////////////////////////////////
//To Next
MITA_HSOUND hSound;
MITA_Engine_DeleteSource(hEngine, hCurItem);//Delete Current Source
CurItemIndex = (CurItemIndex+1)%TotalItem;//Move to next source
MITA_Array_Get(hItemArray, CurItemIndex, &hSound);
MITA_Input_LoadBySound(gInstance, hSound, &hCurItem);//load new source
MITA_Engine_AddSource(hEngine, hCurItem);
MITA_Input_Play(hCurItem, MITA_NULL);
}

Sleep(10);
} while (Key != 27);

MITA_Engine_DeleteSource(hEngine, hCurItem);
MITA_DeleteSoundArray(hItemArray);
MITA_CloseHandle(hEngine);//Release Engine Object
MITA_CloseHandle(gInstance);//Release global instance handle.
}

#pragma warning(pop)

原创粉丝点击