当前位置: 首页 > 工具软件 > Vibro > 使用案例 >

使用音频引擎MITA添加dsp Vibro的源代码

丰超
2023-12-01

#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;
}
} 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);
}

void main(void)
{
MITA_HINSTANCE gInstance = MITA_NULL;
MITA_CHAR szVersion[64];
MITA_SIZE PluginCount = 0;
MITA_HINPUT hItem1;
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_HDSP hTestDSP;

printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n");
printf(";;Copyright(C) 杭州蜜柑科技有限公司 2008 - 2011. 保留所有权利。;;\n");
printf(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n");
printf(";;DSP Vibro 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_Input_LoadByPath(gInstance, L"../media/stereo.ogg", &hItem1);
MITA_CheckError();
MITA_Engine_AddSource(hEngine, hItem1);
MITA_Input_SetResampler(hItem1, MITA_RSR_CUBIC);
MITA_Ins_CreateObject(gInstance, &IID_DspVibro, &hTestDSP);
MITA_Input_AddLaterDSP(hItem1, hTestDSP);
{
MITA_FLOAT Deepth = 10.0f;
MITA_FLOAT Speed = 0.9f;
MITA_DSP_SetParam(hTestDSP, MITA_DSPPARAM_VIBRO_DEEPTH, &Deepth);
MITA_DSP_SetParam(hTestDSP, MITA_DSPPARAM_VIBRO_SPEED, &Speed);
}

printf("\nPress P to start play.\n");
printf("Press L to start looping play.\n");
printf("Press D to start 3D-Effect play.\n");
printf("Press X to pause.\n");
printf("Press Z to resume.\n");
printf("Press S to stop play.\n");
printf("Press Esc to exit\n");
do
{
if (kbhit())
{
Key = getch();

switch (Key)
{
case 's':
case 'S':
MITA_Input_Stop(hItem1, MITA_TRUE);
break;
case 'x':
case 'X':
MITA_Input_Pause(hItem1);
break;
case 'z':
case 'Z':
MITA_Input_Resume(hItem1);
break;
case 'p':
case 'P':
{
MITA_BYTE Loop = 1;
MITA_Input_Play(hItem1, &Loop);
}break;
case 'l':
case 'L':
{
MITA_BYTE Loop = 0xFF;
MITA_Input_Play(hItem1, &Loop);
}break;
case 'd':
case 'D':
{
MITA_INT32 ticksToPlay = 10000;
MITA_INT32 currentTick = 0;
MITA_FLOAT rot = 0, x, z;
MITA_FLOAT str = 2.0f;
MITA_BYTE Loop = 0xFF;
MITA_FLOAT minDis = 1.0f, maxDis = 100.0f;
MITA_HLISTENER hListener;

MITA_Listener_Create(gInstance, &hListener);
MITA_Listener_SetPosition(hListener, 0, 0, 0);

MITA_Input_Play3D(hItem1, &Loop, 0, 0, 0, &str);
MITA_Input_BindListener3D(hItem1, hListener);
MITA_Input_SetDistance3D(hItem1, &minDis, &maxDis);
while(currentTick < ticksToPlay)
{
//Figure out the location of our rotated sound
rot+=0.1f * 0.017453293f; //0.1 degrees a frame
//Sound "starts" at x=5, y=0, z=0
x = 5.0f * cosf(rot) - 0.0f * sinf(rot);
z = 0.0f * cosf(rot) + 5.0f * sinf(rot);
MITA_Input_Move3D(hItem1, x, 0, z);
++currentTick;
printf("playing 3D tick %d \r", currentTick);
if (kbhit())
{
Key = getch();
if (Key == 27)
break;
}
Sleep(1);
}
Key = 0;
MITA_Input_Stop(hItem1, MITA_TRUE);
MITA_CloseHandle(hListener);
}break;
}
}

MITA_Source_GetStatus(hItem1, &Status);
MITA_Source_GetCurTime(hItem1, &CurTime);
MITA_Input_GetDuration(hItem1, &DurTime);
MITA_Input_GetChannelFormat(hItem1, &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][%0.1fKHz]\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), ChannelFmt.sps/1000.0f);
Sleep(10);
} while (Key != 27);

MITA_Engine_RemoveSource(hEngine, hItem1);
MITA_Engine_RemoveSource(hEngine, hOutput);

MITA_CloseHandle(hItem1);
MITA_CloseHandle(hOutput);
MITA_CloseHandle(hEngine); //Release Engine Object
MITA_CloseHandle(gInstance); //Release global instance handle.
}

#pragma warning(pop)

 类似资料: