Use the audio engine MITA to play audio source code

Source: Internet
Author: User

# 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 | mmmmmm 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 = 4410020.f;/** <@ brief 44.1 KHz */
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;

Printf (";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; \ n ");
Printf ("; Copyright (C) Hangzhou citrus Technology Co., Ltd. 2008-2011. All Rights Reserved .;; \ N ");
Printf (";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; \ n ");
Printf ("; PlayPath 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_HSOUND hSound;
MITA_SIZE I, n;
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/stereo.ogg ";
MITA_Ins_ParseSong (& Param );
MITA_CheckError ();
If (Param. hItemArray = MITA_NULL | MITA_Array_GetCount (Param. hItemArray) = 0)
{
Printf ("ERROR: No valid audio data. \ n ");
If (Param. hItemArray)
MITA_CloseHandle (Param. hItemArray );
MITA_CloseHandle (hEngine); // Release Engine Object
MITA_CloseHandle (gInstance); // Release global instance handle.
Exit (-1 );
}
//////////////////////////////////////// //////////////////////////////////
// Multiple returned results may be returned, for example, cue and rar. Here, only the first one is selected.
MITA_Array_Get (Param. hItemArray, 0, & hSound );
//////////////////////////////////////// //////////////////////////////////
// Delete unnecessary items
N = MITA_Array_GetCount (Param. hItemArray );
For (I = 1; I <n; I ++)
{
MITA_HSOUND DupSound;
MITA_Array_Get (Param. hItemArray, I, & DupSound );
MITA_CloseHandle (DupSound );
}
MITA_CloseHandle (Param. hItemArray );
//////////////////////////////////////// //////////////////////////////////
// MITA_Input_LoadBySound
MITA_Input_LoadBySound (gInstance, hSound, & hItem1 );
MITA_CheckError ();
}
MITA_Engine_AddSource (hEngine, hItem1 );

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 = 100366f;
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 ("% 10 s % 02u: % 02u: % 02u/% 02u: % 02u: % 02u [% u channels] \ 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 ));
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.