out_midi.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. #include "main.h"
  2. #include "seq.h"
  3. #include <math.h>
  4. #include "resource.h"
  5. // {76B6A32D-99A9-4d51-B1F5-DAD3F47FE75D}
  6. static const GUID midiout_guid =
  7. { 0x76b6a32d, 0x99a9, 0x4d51, { 0xb1, 0xf5, 0xda, 0xd3, 0xf4, 0x7f, 0xe7, 0x5d } };
  8. // {7F00BC9C-AEA3-472a-BBB9-D74ABD4FCA58}
  9. static const GUID midiout_driver_guid =
  10. { 0x7f00bc9c, 0xaea3, 0x472a, { 0xbb, 0xb9, 0xd7, 0x4a, 0xbd, 0x4f, 0xca, 0x58 } };
  11. static MMRESULT midiOutOpen_wrap(HMIDIOUT * hMo,int id)
  12. {
  13. try {
  14. return midiOutOpen(hMo,(UINT)id,0,0,CALLBACK_NULL);
  15. }
  16. catch(...)
  17. {
  18. return -1;
  19. }
  20. }
  21. class MIDI_device_midiout : public MIDI_device
  22. {
  23. private:
  24. GUID guid;
  25. UINT id;
  26. DWORD flags;
  27. UINT type;
  28. virtual player_base * create();
  29. virtual GUID get_guid() {return guid;}
  30. virtual bool is_default() {return id==(UINT)(-1);}
  31. virtual bool volctrl_happy() {return (type==7) && (flags & MIDICAPS_VOLUME);}
  32. public:
  33. MIDI_device_midiout(UINT p_id,DWORD p_flags,UINT p_type,const wchar_t * p_name,const wchar_t * p_info)
  34. {
  35. id=p_id;
  36. guid = midiout_guid;
  37. *(DWORD*)&guid+=id;
  38. flags = p_flags;
  39. type = p_type;
  40. set_name(p_name);
  41. set_info(p_info);
  42. }
  43. inline DWORD get_flags() {return flags;}
  44. inline DWORD get_id() {return id;}
  45. };
  46. static void midiout_sysex(HMIDIOUT hMo,BYTE* p,UINT len)
  47. {
  48. MIDIHDR h;
  49. ZeroMemory(&h,sizeof(h));
  50. h.dwBytesRecorded=h.dwBufferLength=len;
  51. h.lpData=(char*)p;
  52. if (FAILED(midiOutPrepareHeader(hMo,&h,sizeof(h)))) {
  53. #ifdef USE_LOG
  54. log_write("unable to send sysex");
  55. #endif
  56. return;
  57. }
  58. if (SUCCEEDED(midiOutLongMsg(hMo,&h,sizeof(h))))
  59. {
  60. while(!(h.dwFlags&MHDR_DONE)) MIDI_callback::Idle();
  61. }
  62. midiOutUnprepareHeader(hMo,&h,sizeof(h));
  63. //log_write("sysex sent OK");
  64. }
  65. void midiout_sysex(HMIDIOUT hMo,BYTE* p,UINT len);
  66. static void sysex_startup_midiout(UINT m_id)
  67. {
  68. if (need_sysex_start())
  69. {
  70. // MessageBox(GetActiveWindow(),"blah",0,0);
  71. HMIDIOUT hMo;
  72. MMRESULT r=midiOutOpen_wrap(&hMo,m_id);
  73. if (!r)
  74. {
  75. sysex_startup((SYSEXFUNC)midiout_sysex,hMo);
  76. midiOutClose(hMo);
  77. }
  78. }
  79. }
  80. class midiout_volctrl
  81. {
  82. private:
  83. HMIDIOUT hMo;
  84. int vol,pan;
  85. void _setvol();
  86. static UINT map_vol(UINT volume,UINT scale);
  87. public:
  88. void volctrl_init(HMIDIOUT,MIDI_device_midiout*);
  89. int volctrl_setvol(int);
  90. int volctrl_setpan(int);
  91. };
  92. class player_midiout : public seq_base, private midiout_volctrl
  93. {
  94. public:
  95. virtual ~player_midiout();
  96. virtual int setvol(int i) {return volctrl_setvol(i);};
  97. virtual int setpan(int i) {return volctrl_setpan(i);};
  98. player_midiout(MIDI_device_midiout * p_dev)
  99. {
  100. dev=p_dev;
  101. hMo=0;
  102. }
  103. int play();
  104. private:
  105. MIDI_device_midiout * dev;
  106. HMIDIOUT hMo;
  107. virtual void seq_shortmsg(DWORD msg) {midiOutShortMsg(hMo,msg);}
  108. virtual void seq_sysex(BYTE* ptr,UINT len) {midiout_sysex(hMo,ptr,len);}
  109. virtual int seq_play_start();
  110. virtual void seq_play_stop();
  111. };
  112. int player_midiout::seq_play_start()
  113. {
  114. return 1;
  115. }
  116. void player_midiout::seq_play_stop()
  117. {
  118. if (hMo)
  119. {
  120. #ifdef USE_LOG
  121. log_write("midiOutClose");
  122. #endif
  123. DWORD r=midiOutClose(hMo);
  124. if (r==MIDIERR_STILLPLAYING)
  125. {
  126. log_write("still playing (?), calling midiOutReset");
  127. midiOutReset(hMo);
  128. r=midiOutClose(hMo);
  129. }
  130. #ifdef USE_LOG
  131. if (r) log_write("warning: unable to close midiOut");
  132. else log_write("midiOut closed OK");
  133. #endif
  134. }
  135. hMo=0;
  136. }
  137. int player_midiout::play()
  138. {
  139. DWORD r=midiOutOpen_wrap(&hMo,dev->get_id());
  140. if (r)
  141. {
  142. if (r!=-1) MIDI_core::MM_error(r);
  143. return 0;
  144. }
  145. volctrl_init(hMo,dev);
  146. if (!seq_cmd_start(0))
  147. {
  148. midiOutClose(hMo);
  149. hMo=0;
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. player_midiout::~player_midiout()
  155. {
  156. #ifdef USE_LOG
  157. log_write("shutting down midiOut");
  158. #endif
  159. seq_cmd_stop();
  160. }
  161. class MIDI_driver_midiout : MIDI_driver
  162. {
  163. virtual void do_init()
  164. {
  165. MIDIOUTCAPSW caps;
  166. UINT n_mo_dev=midiOutGetNumDevs()+1;
  167. UINT n;
  168. for(n=0;n<n_mo_dev;n++)
  169. {
  170. midiOutGetDevCapsW(n-1,&caps,sizeof(MIDIOUTCAPSW));
  171. //d.id = TYPE_MIDIOUT | n;
  172. //d.name=(char*)_strdup(caps.szPname);
  173. string_w info;
  174. {
  175. wchar_t moo[128], *t=0;
  176. switch(caps.wTechnology)
  177. {
  178. case MOD_FMSYNTH:
  179. t=WASABI_API_LNGSTRINGW_BUF(STRING_MOCAPS_FM,moo,128);
  180. break;
  181. case MOD_MAPPER:
  182. t=WASABI_API_LNGSTRINGW_BUF(STRING_MOCAPS_MAPPER,moo,128);
  183. break;
  184. case MOD_MIDIPORT:
  185. t=WASABI_API_LNGSTRINGW_BUF(STRING_MOCAPS_HWPORT,moo,128);
  186. break;
  187. case MOD_SQSYNTH:
  188. t=WASABI_API_LNGSTRINGW_BUF(STRING_MOCAPS_SQUARE,moo,128);
  189. break;
  190. case MOD_SYNTH:
  191. t=WASABI_API_LNGSTRINGW_BUF(STRING_MOCAPS_SYNTH,moo,128);
  192. break;
  193. case 6:
  194. t=WASABI_API_LNGSTRINGW_BUF(STRING_MOCAPS_WAVETABLE,moo,128);
  195. break;
  196. case 7:
  197. t=WASABI_API_LNGSTRINGW_BUF(STRING_DMCAPS_SOFTSYNTH,moo,128);
  198. break;
  199. default:
  200. wsprintfW(moo,WASABI_API_LNGSTRINGW(STRING_UNKNOWN),caps.wTechnology);
  201. t=moo;
  202. break;
  203. }
  204. if (t)
  205. {
  206. info+=WASABI_API_LNGSTRINGW(STRING_DEVICE_TYPE);
  207. info+=t;
  208. info+=L"\x0d\x0a";
  209. }
  210. if (caps.dwSupport & MIDICAPS_STREAM)
  211. {
  212. info+=WASABI_API_LNGSTRINGW(STRING_DIRECT_MIDISTREAM);
  213. info+=L"\x0d\x0a";
  214. }
  215. }
  216. add_device(new MIDI_device_midiout(n-1,caps.dwSupport,caps.wTechnology,caps.szPname,info));
  217. }
  218. }
  219. virtual const wchar_t * get_name() {return L"midiOut";}
  220. virtual bool is_default() {return 1;}
  221. virtual GUID get_guid() {return midiout_driver_guid;}
  222. };
  223. static MIDI_driver_midiout midi_driver_midiout;
  224. #define WM_SEEK (WM_USER+4)
  225. #define GET_TIME timeGetTime()
  226. int IS_SPEC_C(int x);
  227. #define BUF_MAX 0x1000//0x3C00
  228. #define BUF_MAX_F (BUF_MAX+0x40)
  229. #define N_BUFS 4
  230. #define BUF_MASK (N_BUFS-1)
  231. typedef struct
  232. {
  233. MIDIHDR h;
  234. DWORD data[BUF_MAX_F];
  235. } MIDIBUF;
  236. class player_midistream : public player_base, private midiout_volctrl
  237. {
  238. public:
  239. int gettime();
  240. int settime(int);
  241. void pause();
  242. void unpause();
  243. virtual int setvol(int i) {return volctrl_setvol(i);};
  244. virtual int setpan(int i) {return volctrl_setpan(i);};
  245. int play();
  246. player_midistream(MIDI_device_midiout *);
  247. ~player_midistream();
  248. private:
  249. UINT renderbuf(MIDIHDR* buf,DWORD ts,UINT start,UINT end);
  250. UINT renderbuf_seek(UINT start,UINT end);
  251. void renderbuf_wait(MIDIHDR* buf,UINT len);
  252. DWORD pos4time(DWORD t);
  253. MIDI_device_midiout * dev;
  254. UINT m_id;
  255. UINT c_loop;
  256. CSysexMap* smap;
  257. HMIDISTRM hMo;
  258. UINT n_events;
  259. MIDI_EVENT* events;
  260. DWORD tm_ofs,p_time;
  261. UINT loop_start,total_len;
  262. MIDIBUF hdrs[N_BUFS];
  263. MIDIBUF seekbuf;
  264. UINT cur_buf;
  265. UINT in_mm,n_free;
  266. DWORD ct,cp;
  267. bool got_eof,paused,quitting;
  268. UINT seek_to;
  269. HWND wnd;
  270. DWORD trd_id;
  271. void do_bufs();
  272. void buf_done(MIDIHDR*);
  273. static LRESULT WINAPI midiOutProc(HWND wnd,UINT msg,WPARAM wp,LPARAM lp);
  274. };
  275. /*
  276. static void _sysex(player_midistream* pl,BYTE* p,UINT len)
  277. {//assumes thread to be OK
  278. HMIDISTRM hMo=pl->hMo;
  279. MIDIHDR h;
  280. ZeroMemory(&h,sizeof(h));
  281. h.dwUser=1;
  282. DWORD l=12+len;
  283. if (l&3) l=(l+4)&~3;
  284. DWORD* ev=(DWORD*)alloca(l);
  285. ev[0]=ev[1]=0;
  286. ev[2]=MEVT_F_LONG|len;
  287. memcpy(ev+3,p,len);
  288. h.dwBytesRecorded=h.dwBufferLength=l;
  289. h.lpData=(char*)ev;
  290. if (FAILED(midiOutPrepareHeader((HMIDIOUT)hMo,&h,sizeof(h)))) return;
  291. if (FAILED(midiStreamOut(hMo,&h,sizeof(h))))
  292. {
  293. midiOutUnprepareHeader((HMIDIOUT)hMo,&h,sizeof(h));
  294. return;
  295. }
  296. pl->in_mm++;
  297. do_messages(pl->wnd,(bool*)&h.dwUser);
  298. log_write("sysex sent OK");
  299. }*/
  300. DWORD player_midistream::pos4time(DWORD t)
  301. {
  302. DWORD r=0;
  303. while(r<n_events && events[r].tm<t) r++;
  304. return r;
  305. }
  306. static cfg_int cfg_midistream_quick_seek("midistream_quick_seek",0);
  307. void player_midistream::do_bufs()
  308. {
  309. if (seek_to!=-1)
  310. {
  311. UINT sp=pos4time(ct=seek_to);
  312. if (!cfg_midistream_quick_seek)
  313. {
  314. UINT st=cp;
  315. if (sp<cp) st=0;
  316. if (renderbuf_seek(st,sp)!=-1)
  317. {
  318. if (!midiOutPrepareHeader((HMIDIOUT)hMo,&seekbuf.h,sizeof(MIDIHDR)))
  319. {
  320. if (!midiStreamOut(hMo,&seekbuf.h,sizeof(MIDIHDR)))
  321. {
  322. in_mm++;
  323. }
  324. else midiOutUnprepareHeader((HMIDIOUT)hMo,&seekbuf.h,sizeof(MIDIHDR));
  325. }
  326. }
  327. }
  328. cp=sp;
  329. seek_to=-1;
  330. }
  331. while(n_free && !got_eof)
  332. {
  333. MIDIHDR* hdr=&hdrs[cur_buf].h;
  334. cp=renderbuf(hdr,ct,cp,-1);
  335. if (cp==-1)
  336. {
  337. if (loop_start!=-1 && c_loop>1)
  338. {
  339. c_loop--;
  340. cp=pos4time(ct=loop_start);
  341. continue;
  342. }
  343. else
  344. {
  345. got_eof=1;
  346. if (cfg_eof_delay)
  347. {
  348. renderbuf_wait(hdr,cfg_eof_delay);
  349. }
  350. else break;
  351. }
  352. }
  353. if (midiOutPrepareHeader((HMIDIOUT)hMo,hdr,sizeof(MIDIHDR)))
  354. {
  355. got_eof=1;
  356. break;
  357. }
  358. if (midiStreamOut(hMo,hdr,sizeof(MIDIHDR)))
  359. {
  360. got_eof=1;
  361. break;
  362. }
  363. cur_buf=(cur_buf+1)&BUF_MASK;
  364. in_mm++;
  365. n_free--;
  366. if (!got_eof)
  367. ct=cp ? events[cp-1].tm : 0;
  368. }
  369. }
  370. UINT player_midistream::renderbuf(MIDIHDR* buf,DWORD ts,UINT start,UINT end)
  371. {
  372. UINT n=start;
  373. UINT p=0;
  374. DWORD* pEv=(DWORD*)buf->lpData;
  375. UINT c_t=ts;
  376. while(n<end && n<n_events && p<(BUF_MAX-3))
  377. {
  378. int dt=events[n].tm-c_t;
  379. if (dt<0) dt=0;
  380. pEv[p++]=dt;
  381. c_t+=dt;
  382. pEv[p++]=0;
  383. if (events[n].ev&0x80000000)
  384. {
  385. SYSEX_ENTRY* se=&smap->events[events[n].ev&0x7FFFFFFF];
  386. if (p+(se->len>>2)>=BUF_MAX)
  387. {
  388. p-=2;
  389. break;
  390. }
  391. pEv[p++]=MEVT_F_LONG|se->len;
  392. DWORD d=se->len>>2;
  393. if (se->len&3)
  394. {
  395. pEv[p+(d++)]=0;
  396. }
  397. memcpy(pEv+p,smap->data+se->ofs,se->len);
  398. p+=d;
  399. }
  400. else
  401. {
  402. pEv[p++]=events[n].ev;
  403. }
  404. n++;
  405. }
  406. if (p==0)
  407. return -1;
  408. buf->dwBufferLength=p<<2;
  409. buf->dwBytesRecorded=p<<2;
  410. buf->dwFlags=0;
  411. return n;
  412. }
  413. void player_midistream::renderbuf_wait(MIDIHDR* buf,UINT len)
  414. {
  415. UINT p=0;
  416. DWORD* pEv=(DWORD*)buf->lpData;
  417. pEv[p++]=len<<3;
  418. pEv[p++]=0;
  419. pEv[p++]=MEVT_NOP<<24;
  420. buf->dwBufferLength=p<<2;
  421. buf->dwBytesRecorded=p<<2;
  422. buf->dwFlags=0;
  423. }
  424. UINT player_midistream::renderbuf_seek(UINT start,UINT end)
  425. {
  426. BYTE ins_tab[16] = {0};
  427. memset(ins_tab,-1,sizeof(ins_tab));
  428. BYTE ctrl_tab[16][128] = {0};
  429. memset(ctrl_tab,-1,sizeof(ctrl_tab));
  430. UINT n=start;
  431. DWORD* pEv=(DWORD*)seekbuf.h.lpData;
  432. while(n<end)
  433. {
  434. DWORD ec=events[n].ev;
  435. if (ec&0x80000000) {n++;continue;}
  436. UINT ch,cd;
  437. ch=ec&0xF;
  438. cd=ec&0xF0;
  439. if (cd==0xB0) ctrl_tab[ch][(ec>>8)&0x7F]=(BYTE)(ec>>16);
  440. else if (cd==0xC0) ins_tab[ch]=(BYTE)(ec>>8);
  441. n++;
  442. }
  443. UINT c;
  444. UINT p=0;
  445. for(c=0;c<16;c++)
  446. {
  447. for(n=0;n<128;n++)
  448. {
  449. if (!(ctrl_tab[c][n]&0x80))
  450. {
  451. pEv[p++]=0;
  452. pEv[p++]=0;
  453. pEv[p++]=0xB0|c|(n<<8)|(ctrl_tab[c][n]<<16);
  454. }
  455. if (p>=BUF_MAX)
  456. goto q;
  457. }
  458. if (!(ins_tab[c]&0x80))
  459. {
  460. pEv[p++]=0;
  461. pEv[p++]=0;
  462. pEv[p++]=0xC0|c|(ins_tab[c]<<8);
  463. }
  464. if (p>=BUF_MAX)
  465. goto q;
  466. }
  467. q:
  468. if (p==0) return -1;
  469. seekbuf.h.dwBufferLength=p<<2;
  470. seekbuf.h.dwBytesRecorded=p<<2;
  471. seekbuf.h.dwFlags=0;
  472. return n;
  473. }
  474. void player_midistream::buf_done(MIDIHDR* h)
  475. {
  476. in_mm--;
  477. midiOutUnprepareHeader((HMIDIOUT)hMo,h,sizeof(MIDIHDR));
  478. if (h->dwUser)
  479. {
  480. h->dwUser=0;
  481. return;
  482. }
  483. if (h==&seekbuf.h) return;
  484. n_free++;
  485. if (quitting) return;
  486. if (!in_mm && got_eof)
  487. MIDI_core::Eof();
  488. else if (!got_eof)
  489. {
  490. do_bufs();
  491. }
  492. }
  493. LRESULT WINAPI player_midistream::midiOutProc(HWND wnd,UINT msg,WPARAM wp,LPARAM lp)
  494. {
  495. if (msg==MM_MOM_DONE)
  496. {
  497. player_midistream* p=(player_midistream*)GetWindowLong(wnd,0);
  498. if (p) p->buf_done((MIDIHDR*)lp);
  499. }
  500. else if (msg==WM_SEEK)
  501. {
  502. player_midistream* p=(player_midistream*)GetWindowLong(wnd,0);
  503. if (p) p->settime(lp);
  504. }
  505. return DefWindowProc(wnd,msg,wp,lp);
  506. }
  507. player_midistream::player_midistream(MIDI_device_midiout * p_dev)
  508. {
  509. dev=p_dev;
  510. m_id=0;
  511. c_loop=0;
  512. smap=0;
  513. hMo=0;
  514. n_events=0;
  515. events=0;
  516. tm_ofs=0;p_time=0;
  517. loop_start=0;
  518. total_len=0;
  519. memset(&hdrs,0,sizeof(hdrs));
  520. memset(&seekbuf,0,sizeof(seekbuf));
  521. cur_buf=0;
  522. in_mm=0;n_free=0;
  523. ct=0;cp=0;
  524. got_eof=0;
  525. paused=0;
  526. quitting=0;
  527. seek_to=0;
  528. wnd=0;
  529. trd_id=0;
  530. static ATOM cb_class;
  531. if (!cb_class) cb_class=do_callback_class(midiOutProc);
  532. wnd=create_callback_wnd(cb_class,this);
  533. c_loop=cfg_loop_infinite ? -1 : cfg_loop_count;
  534. }
  535. player_base* MIDI_device_midiout::create()
  536. {
  537. if (cfg_playback_mode)
  538. {
  539. player_midiout *p=new player_midiout(this);
  540. if (p)
  541. {
  542. if (!p->play()) {delete p;p=0;}
  543. }
  544. return p;
  545. }
  546. else
  547. {
  548. player_midistream *p=new player_midistream(this);
  549. if (p)
  550. {
  551. if (!p->play()) {delete p;p=0;}
  552. }
  553. return p;
  554. }
  555. }
  556. //extern bool cfg_alt_sysex;
  557. int player_midistream::play()
  558. {
  559. trd_id=GetCurrentThreadId();
  560. UINT n;
  561. for(n=0;n<N_BUFS;n++)
  562. {
  563. hdrs[n].h.lpData=(char*)hdrs[n].data;
  564. }
  565. seekbuf.h.lpData=(char*)seekbuf.data;
  566. //bool alt_sysex=cfg_alt_sysex;
  567. //if (alt_sysex)
  568. {
  569. sysex_startup_midiout(dev->get_id());
  570. }
  571. #ifdef USE_LOG
  572. log_write("starting midiOut / streamed");
  573. #endif
  574. {
  575. UINT id=dev->get_id();
  576. DWORD err=midiStreamOpen(&hMo,&id,1,(DWORD)wnd,0,CALLBACK_WINDOW);
  577. if (err)
  578. {
  579. MIDI_core::MM_error(err);
  580. return 0;
  581. }
  582. MIDIPROPTIMEDIV td;
  583. td.cbStruct=sizeof(td);
  584. td.dwTimeDiv=1*8;//tix / q
  585. err=midiStreamProperty(hMo,(BYTE*)&td,MIDIPROP_SET|MIDIPROP_TIMEDIV);
  586. if (err)
  587. {
  588. midiStreamClose(hMo);
  589. MIDI_core::MM_error(err);
  590. return 0;
  591. }
  592. MIDIPROPTEMPO tempo;
  593. tempo.cbStruct=sizeof(tempo);
  594. tempo.dwTempo=1000;//ns / q
  595. err=midiStreamProperty(hMo,(BYTE*)&tempo,MIDIPROP_SET|MIDIPROP_TEMPO);
  596. if (err)
  597. {
  598. midiStreamClose(hMo);
  599. MIDI_core::MM_error(err);
  600. return 0;
  601. }
  602. }
  603. events=do_table(MIDI_core::getFile(),8,&n_events,&loop_start,0);
  604. if (!events)
  605. {
  606. midiStreamClose(hMo);
  607. hMo=0;
  608. return 0;
  609. }
  610. total_len=events[n_events-1].tm>>3;
  611. if (!cfg_nosysex && MIDI_core::getFile()->smap && MIDI_core::getFile()->smap->pos)
  612. {
  613. smap=MIDI_core::getFile()->smap;
  614. }
  615. else smap=0;
  616. paused=0;
  617. volctrl_init((HMIDIOUT)hMo,dev);
  618. midiStreamPause(hMo);
  619. //sysex_startup((SYSEXFUNC)midiout_sysex,hMo);
  620. seek_to=-1;
  621. tm_ofs=GET_TIME;
  622. cur_buf=0;
  623. in_mm=0;
  624. n_free=N_BUFS;
  625. ct=cp=0;
  626. do_bufs();
  627. #ifdef USE_LOG
  628. log_write("started OK");
  629. #endif
  630. midiStreamRestart(hMo);
  631. return 1;
  632. }
  633. player_midistream::~player_midistream()
  634. {
  635. // bool alt_sysex=cfg_alt_sysex;
  636. #ifdef USE_LOG
  637. log_write("shutting down midistream");
  638. #endif
  639. if (hMo)
  640. {
  641. //ASSERT(trd_id!=GetCurrentThreadId());
  642. quitting=1;
  643. #ifdef USE_LOG
  644. log_write("midiStreamStop");
  645. #endif
  646. midiStreamStop(hMo);
  647. // do_messages(wnd,(bool*)&in_mm);
  648. if (n_free!=N_BUFS)
  649. {
  650. UINT n;
  651. for(n=0;n<N_BUFS;n++)
  652. {
  653. if (hdrs[n].h.dwFlags&MHDR_PREPARED)
  654. {
  655. midiOutUnprepareHeader((HMIDIOUT)hMo,&hdrs[n].h,sizeof(MIDIHDR));
  656. in_mm--;
  657. n_free++;
  658. }
  659. }
  660. }
  661. #ifdef HUNT_LEAKS
  662. if (n_free!=N_BUFS) Warning("Not all buffers collected.");
  663. #endif
  664. #ifdef USE_LOG
  665. log_write("midiStreamClose");
  666. #endif
  667. midiStreamClose(hMo);
  668. //if (midiStreamClose(hMo)) Warning(STRING_MIDISTREAM_WARNING);
  669. }
  670. if (events) free(events);
  671. if (wnd) DestroyWindow(wnd);
  672. #ifdef USE_LOG
  673. log_write("midistream shut down");
  674. #endif
  675. }
  676. int player_midistream::gettime()
  677. {
  678. DWORD ret;
  679. if (paused) ret=p_time;
  680. else if (!tm_ofs) ret=0;
  681. else
  682. {
  683. ret=GET_TIME-tm_ofs;
  684. if (loop_start!=-1 && ret>total_len)
  685. {
  686. UINT _ls=loop_start>>3;
  687. ret=(ret-_ls)%(total_len-_ls)+_ls;
  688. }
  689. }
  690. return ret;
  691. }
  692. int player_midistream::settime(int tm)
  693. {
  694. if (!paused)
  695. {
  696. if (trd_id==GetCurrentThreadId())
  697. {
  698. seek_to=tm<<3;
  699. got_eof=0;
  700. tm_ofs=GET_TIME-tm;
  701. midiStreamStop(hMo);
  702. midiStreamPause(hMo);
  703. quitting=1;
  704. do_messages(wnd,(bool*)&in_mm);
  705. quitting=0;
  706. do_bufs();
  707. midiStreamRestart(hMo);
  708. }
  709. else PostMessage(wnd,WM_SEEK,0,tm);
  710. }
  711. else
  712. {
  713. p_time=tm;
  714. }
  715. return 1;
  716. }
  717. void player_midistream::pause()
  718. {
  719. p_time=GET_TIME-tm_ofs;
  720. paused=1;
  721. midiStreamPause(hMo);
  722. }
  723. void player_midistream::unpause()
  724. {
  725. tm_ofs=GET_TIME-p_time;
  726. paused=0;
  727. if (seek_to!=-1)
  728. {
  729. midiStreamStop(hMo);
  730. midiStreamPause(hMo);
  731. if (trd_id==GetCurrentThreadId())
  732. {
  733. quitting=1;
  734. do_messages(wnd,(bool*)&in_mm);
  735. quitting=0;
  736. do_bufs();
  737. }
  738. }
  739. midiStreamRestart(hMo);
  740. }
  741. UINT midiout_volctrl::map_vol(UINT volume,UINT scale)
  742. {
  743. double _vol=volume>0 ? 20*log10((double)volume/(double)scale) : -60.0;//in negative db
  744. _vol=_vol/60.0+1;
  745. if (_vol<0) _vol=0;
  746. return (UINT)(_vol*(double)scale);
  747. }
  748. void midiout_volctrl::_setvol()
  749. {
  750. DWORD _vol=257*vol;
  751. DWORD vol1=_vol,vol2=_vol;
  752. if (pan!=666)
  753. {
  754. if (pan<0)
  755. {
  756. vol2=(vol2*(128+pan))>>7;
  757. }
  758. else if (pan>0)
  759. {
  760. vol1=(vol1*(128-pan))>>7;
  761. }
  762. }
  763. if (cfg_logvol)
  764. {
  765. vol1=map_vol(vol1,0xFFFF);
  766. vol2=map_vol(vol2,0xFFFF);
  767. }
  768. midiOutSetVolume((HMIDIOUT)hMo,(vol2<<16)|vol1);
  769. }
  770. void midiout_volctrl::volctrl_init(HMIDIOUT _hMo,MIDI_device_midiout * dev)
  771. {
  772. hMo=_hMo;
  773. pan=(dev->get_flags()&MIDICAPS_LRVOLUME) ? MIDI_core::player_getPan() : 666;
  774. vol=(dev->get_flags()&MIDICAPS_VOLUME) ? MIDI_core::player_getVol() : 666;
  775. _setvol();
  776. }
  777. int midiout_volctrl::volctrl_setvol(int _vol)
  778. {
  779. if (vol!=666)
  780. {
  781. vol=_vol;
  782. _setvol();
  783. return 1;
  784. }
  785. else return 0;
  786. }
  787. int midiout_volctrl::volctrl_setpan(int _pan)
  788. {
  789. if (pan!=666)
  790. {
  791. pan=_pan;
  792. _setvol();
  793. return 1;
  794. }
  795. else return 0;
  796. }