Source code for using the audio engine Mita binary static playback of audio files

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 "../Media/1. Static. s"

# 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 ("; playstatic 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_size length = length;
Mita_channelformat channelfmt = channel_format;
Mita_input_createbystatic (ginstance, source, length, & channelfmt, & 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.