ds2.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. //#define USE_LOG
  2. //^^ for debug logging to c:\ds2.txt
  3. #include "ds2.h"
  4. #include <dsound.h>
  5. #include <math.h>
  6. #include <ks.h>
  7. #include "ksmedia.h"
  8. #include "../winamp/wa_ipc.h"
  9. extern Out_Module mod;
  10. static const int kMaxChannelsToMask = 8;
  11. static const unsigned int kChannelsToMask[kMaxChannelsToMask + 1] =
  12. {
  13. 0,
  14. // 1 = Mono
  15. SPEAKER_FRONT_CENTER,
  16. // 2 = Stereo
  17. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
  18. // 3 = Stereo + Center
  19. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER,
  20. // 4 = Quad
  21. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
  22. SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT,
  23. // 5 = 5.0
  24. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER |
  25. SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT,
  26. // 6 = 5.1
  27. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
  28. SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY |
  29. SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT,
  30. // 7 = 6.1
  31. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
  32. SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY |
  33. SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT |
  34. SPEAKER_BACK_CENTER,
  35. // 8 = 7.1
  36. SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
  37. SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY |
  38. SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT |
  39. SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT
  40. // Add additional masks for 7.2 and beyond.
  41. };
  42. DS2::tDirectSoundCreate DS2::pDirectSoundCreate = 0;
  43. #ifdef DS2_HAVE_DEVICES
  44. DS2::tDirectSoundEnumerate DS2::pDirectSoundEnumerate = 0;
  45. #endif
  46. static UINT refresh_timer = 10;
  47. static const GUID NULL_GUID;
  48. HRESULT DS2::myDirectSoundCreate(const GUID* g, IDirectSound** out)
  49. {
  50. HRESULT r;
  51. try
  52. {
  53. r = DS2::pDirectSoundCreate((!g || *g == NULL_GUID) ? (const GUID*)0 : g, out, 0);
  54. }
  55. catch (...)
  56. {
  57. *out = 0;
  58. r = DSERR_GENERIC;
  59. }
  60. return r;
  61. }
  62. #define ftest(X) (!!(flags & FLAG_##X))
  63. #define fset(X) flags|=FLAG_##X
  64. #define funset(X) flags&=~FLAG_##X
  65. #define fsetc(X,Y) {if (Y) fset(X); else funset(X);}
  66. static HINSTANCE hdsound;
  67. static bool g_delayed_deinit = 1;
  68. static __int64 g_total_time;
  69. static HANDLE g_hEvent;
  70. static CriticalSection g_sync;
  71. static bool g_quitting, g_quitting_waiting;
  72. #define SYNCFUNC T_SYNC SYNC(g_sync);
  73. void DS2::SYNC_IN() { g_sync.Enter(); }
  74. void DS2::SYNC_OUT() { g_sync.Leave(); }
  75. static DWORD last_rel_time;
  76. static DWORD coop_mode;
  77. IDirectSound* DS2::pDS = 0;
  78. static IDirectSoundBuffer* pPrimary;
  79. static UINT prim_bps, prim_nch, prim_sr;
  80. static GUID cur_dev;
  81. static DS2* ds2s = nullptr;
  82. static HANDLE g_hThread;
  83. static bool create_primary = 0;
  84. #ifdef USE_LOG
  85. static void _log_write(char* msg, DS2* foo)
  86. {
  87. char tmp[512];
  88. SYSTEMTIME st;
  89. GetSystemTime(&st);
  90. wsprintf(tmp, "DS2: %02u:%02u.%03u %08x %s\n", st.wMinute, st.wSecond, st.wMilliseconds, foo, msg);
  91. #if 1
  92. static HANDLE hLog;
  93. if (!hLog) hLog = CreateFile("c:\\ds2.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  94. DWORD bw = 0;
  95. WriteFile(hLog, tmp, strlen(tmp), &bw, 0);
  96. #else
  97. OutputDebugString(tmp);//bleh flood, getting holes in the log, blame micro$oft
  98. #endif
  99. }
  100. #define log_write(x) _log_write(x,this)
  101. #else
  102. #define _log_write(x,y)
  103. #define log_write(x)
  104. #endif
  105. static int calc_silence(float _db, int bps)//_db is in -0.1db
  106. {
  107. return (int)(pow(10.0, _db / (-20.0)) * pow(2.0, (double)bps));
  108. }
  109. void DS2::test_silence(char* buf, int len, int* first, int* last)
  110. {
  111. int bps = fmt_bps >> 3;
  112. if (bps > 4 || bps < 1)
  113. {
  114. if (first) *first = 0;
  115. if (last) *last = (len - fmt_nch * bps);
  116. return;
  117. }
  118. int ptr = 0;
  119. while (ptr < len)
  120. {
  121. int p = ptr;
  122. UINT n;
  123. for (n = 0; n < fmt_nch; n++)
  124. {
  125. int s;
  126. void* _p = buf + p;
  127. switch (bps)
  128. {
  129. case 1:
  130. s = (UINT) * (BYTE*)_p - 0x80;
  131. break;
  132. case 2:
  133. s = *(short*)_p;
  134. break;
  135. case 3:
  136. {
  137. long poo = 0;
  138. memcpy(&poo, _p, 3);
  139. if (poo & 0x800000) poo |= 0xFF000000;
  140. s = poo;
  141. }
  142. break;
  143. case 4:
  144. s = *(long*)_p;
  145. break;
  146. }
  147. if (s < 0) s = -s;
  148. if (s > silence_delta)
  149. {
  150. if (first && *first < 0) *first = ptr;
  151. if (last) *last = ptr;
  152. }
  153. p += bps;
  154. }
  155. ptr = p;
  156. }
  157. }
  158. DS2::DS2(DS2config* cfg) : BlockList(cfg->bps == 8 ? 0x80 : 0)
  159. #ifdef DS2_HAVE_FADES
  160. , VolCtrl(cfg->volmode, cfg->logvol_min, cfg->logfades)
  161. #endif
  162. {
  163. #ifdef _DEBUG
  164. srand(GetTickCount());
  165. serial = rand();
  166. sync_n = 0;
  167. #endif
  168. next = ds2s;
  169. ds2s = this;
  170. wait = 0;
  171. flags = 0;
  172. LockCount = 0;
  173. Underruns = 0;
  174. fsetc(USE_CPU_MNGMNT, cfg->use_cpu_management);
  175. refresh_timer = cfg->refresh;
  176. if (refresh_timer < 1) refresh_timer = 1;
  177. else if (refresh_timer > 100) refresh_timer = 100;
  178. pDSB = 0;
  179. myDS = 0;
  180. }
  181. DS2::~DS2()
  182. {
  183. log_write("~DS2");
  184. SYNC_IN();
  185. ds_kill();
  186. SYNC_OUT();
  187. }
  188. DS2* DS2::Create(DS2config* cfg)
  189. {
  190. Init();
  191. if (!hdsound) return 0;
  192. _log_write("Create", 0);
  193. SYNC_IN();
  194. DS2* r = new DS2(cfg);
  195. if (!r->Open(cfg))
  196. {
  197. delete r;
  198. r = 0;
  199. }
  200. else SetEvent(g_hEvent);//wake update thread up
  201. SYNC_OUT();
  202. return r;
  203. }
  204. void DS2::ds_kill()
  205. {
  206. if (wait)
  207. {
  208. delete wait;
  209. wait = 0;
  210. }
  211. if (pDSB)
  212. {
  213. if (ftest(PLAYING) && !ftest(PAUSED)) pDSB->Stop();
  214. pDSB->Release();
  215. pDSB = 0;
  216. last_rel_time = GetTickCount();
  217. }
  218. if (myDS)
  219. {
  220. myDS->Release();
  221. myDS = 0;
  222. }
  223. do_reset_vars();
  224. //UGLY moved from destructor
  225. DS2* foo = ds2s;
  226. DS2** foo2 = &ds2s;
  227. while (foo)
  228. {
  229. if (foo == this) { *foo2 = next; break; }
  230. foo2 = &foo->next; foo = *foo2;
  231. }
  232. }
  233. int DS2::WriteData(void* _data, UINT size, bool* killswitch)
  234. {//note: calling code may or may not care about CanWrite() (but if they do, we wont sleep)
  235. if (ftest(PAUSED)) return 0;
  236. log_write("entering writedata");
  237. char* data = (char*)_data;
  238. size = _align_var(size);//avoid evil shit
  239. SYNC_IN();
  240. if (silence_delta >= 0)//no need to sync this
  241. {
  242. if (ftest(STARTSIL))
  243. {
  244. int first = -1;
  245. test_silence(data, size, &first, 0);
  246. if (first >= 0)
  247. {
  248. size -= first;
  249. data += first;
  250. funset(STARTSIL);
  251. }
  252. else
  253. {
  254. log_write("block was silent, leaving writedata");
  255. SYNC_OUT();
  256. return 1;
  257. }
  258. }
  259. int last = -1;
  260. test_silence(data, size, 0, &last);
  261. if (last != -1)
  262. {
  263. log_write("WriteData / last_nonsil update");
  264. last_nonsil = last + data_written + BlockList.DataSize();
  265. }
  266. }
  267. log_write("WriteData");
  268. BlockList.AddBlock(data, size);
  269. if (data_buffered < clear_size) SetEvent(g_hEvent);
  270. else while (!*killswitch && CanWrite() < 0)
  271. {
  272. SYNC_OUT();
  273. Sleep(1);
  274. log_write("WriteData");
  275. SYNC_IN();
  276. }
  277. SYNC_OUT();
  278. log_write("writedata done");
  279. return 1;
  280. }
  281. int DS2::WriteDataNow(void* data, UINT size)
  282. {
  283. log_write("WriteDataNow");
  284. SYNC_IN();
  285. int cw = CanWrite();
  286. int rv = 0;
  287. if (cw > 0)
  288. {
  289. if (size > (UINT)cw) size = (UINT)cw;
  290. if (ForceWriteData(data, size)) rv = size;
  291. }
  292. SYNC_OUT();
  293. return rv;
  294. }
  295. int DS2::ForceWriteData(void* data, UINT size)
  296. {
  297. log_write("ForceWriteData");
  298. SYNC_IN();
  299. bool killswitch = 1;
  300. int r = WriteData(data, size, &killswitch);
  301. SYNC_OUT();
  302. return r;
  303. }
  304. DWORD WINAPI DS2::ThreadFunc(void* zzz)
  305. {
  306. _log_write("ThreadFunc", 0);
  307. SYNC_IN();
  308. while (1)
  309. {
  310. DS2* foo = ds2s;
  311. while (foo)
  312. {
  313. foo->flags &= ~FLAG_UPDATED;
  314. foo = foo->next;
  315. }
  316. foo = ds2s;
  317. while (foo)
  318. {
  319. if (!(foo->flags & FLAG_UPDATED) && foo->Update())
  320. {//one *or more* of instances got deleted
  321. foo = ds2s;
  322. }
  323. else
  324. {
  325. foo->flags |= FLAG_UPDATED;
  326. foo = foo->next;
  327. }
  328. }
  329. DWORD t = ds2s ? refresh_timer : (pDS ? 1000 : -1);
  330. SYNC_OUT();
  331. WaitForSingleObject(g_hEvent, t);
  332. //use g_hEvent to wake thread up when something's going on
  333. _log_write("ThreadFunc", 0);
  334. SYNC_IN();
  335. if (g_quitting) break;
  336. if (!ds2s && pDS)
  337. {
  338. if (pPrimary) { pPrimary->Release(); pPrimary = 0; }
  339. if (!g_delayed_deinit || GetTickCount() - last_rel_time > 3000)
  340. {
  341. pDS->Release();
  342. pDS = 0;
  343. }
  344. }
  345. }
  346. while (ds2s) delete ds2s;
  347. if (pPrimary) { pPrimary->Release(); pPrimary = 0; }
  348. if (pDS) { pDS->Release(); pDS = 0; }
  349. SYNC_OUT();
  350. return 0;
  351. }
  352. //static void __cdecl __quit() {DS2::Quit(0);}
  353. bool DS2::InitDLL()
  354. {
  355. if (!hdsound)
  356. {
  357. hdsound = LoadLibraryW(L"dsound.dll");
  358. if (!hdsound) return false;//ouch
  359. pDirectSoundCreate = (tDirectSoundCreate)GetProcAddress((HMODULE)hdsound, "DirectSoundCreate");
  360. if (!pDirectSoundCreate) { FreeLibrary(hdsound); hdsound = 0; return false; }
  361. #ifdef DS2_HAVE_DEVICES
  362. pDirectSoundEnumerate = (tDirectSoundEnumerate)GetProcAddress((HMODULE)hdsound, "DirectSoundEnumerateW");
  363. if (!pDirectSoundEnumerate) { pDirectSoundCreate = 0; FreeLibrary(hdsound); hdsound = 0; return false; }
  364. #endif
  365. }
  366. return true;
  367. }
  368. void DS2::Init()
  369. {
  370. SYNC_IN();
  371. InitDLL();
  372. if (g_hThread || !hdsound) { SYNC_OUT(); return; }
  373. pDS = 0;
  374. ds2s = 0;
  375. g_quitting = 0;
  376. g_quitting_waiting = 0;
  377. g_hEvent = CreateEvent(0, 0, 0, 0);
  378. DWORD id;
  379. g_hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ThreadFunc, 0, 0, &id);
  380. if (!g_hThread)
  381. {
  382. return;
  383. }
  384. else
  385. {
  386. SetThreadPriority(g_hThread, THREAD_PRIORITY_TIME_CRITICAL);
  387. }
  388. SYNC_OUT();
  389. }
  390. void DS2::Quit(bool wait)
  391. {
  392. if (!g_hThread) return;
  393. g_quitting_waiting = 1;
  394. if (wait) while (ds2s) Sleep(3);
  395. g_quitting = 1;
  396. SetEvent(g_hEvent);
  397. WaitForSingleObject(g_hThread, INFINITE);
  398. CloseHandle(g_hThread);
  399. g_hThread = 0;
  400. CloseHandle(g_hEvent);
  401. g_hEvent = 0;
  402. if (hdsound)
  403. {
  404. FreeLibrary(hdsound);
  405. pDirectSoundCreate = 0;
  406. #ifdef DS2_HAVE_DEVICES
  407. pDirectSoundEnumerate = 0;
  408. #endif
  409. hdsound = 0;
  410. }
  411. }
  412. void DS2::ds_stop()
  413. {
  414. log_write("ds_stop");
  415. if (ftest(PLAYING))
  416. {
  417. if (pDSB)
  418. {
  419. pDSB->Stop();
  420. pDSB->SetCurrentPosition(0);
  421. }
  422. }
  423. do_reset_vars();
  424. }
  425. void DS2::update_pos()//AKA update P.O.S.
  426. {
  427. //called from Update(), no need for shit condition tests
  428. DWORD play_pos, play_pos_w;
  429. try
  430. {
  431. pDSB->GetCurrentPosition(&play_pos, &play_pos_w);
  432. }
  433. catch (...)
  434. {
  435. return;
  436. }
  437. #ifdef USE_LOG
  438. char moo[256];
  439. wsprintf(moo, "update_pos: %u %u (%u)", play_pos, play_pos_w, buf_size);
  440. log_write(moo);
  441. #endif
  442. UINT write_pos = (UINT)(data_written % buf_size);
  443. data_buffered = write_pos > play_pos ? write_pos - play_pos : write_pos + buf_size - play_pos;
  444. #ifdef DS2_HAVE_FADES
  445. VolCtrl.SetTime(GetCurPos());
  446. VolCtrl.Apply(pDSB);
  447. #endif
  448. }
  449. bool DS2::Update()//inside sync already
  450. {
  451. log_write("Update");
  452. if (g_quitting_waiting && (!ftest(PLAYING) || ftest(PAUSED) || !pDSB))
  453. {
  454. delete this;
  455. return 1;
  456. }
  457. if (!pDSB || ftest(PAUSED))
  458. {
  459. return 0;
  460. }
  461. {
  462. UINT min_refresh = bytes2ms(clear_size) >> 1;
  463. if (refresh_timer > min_refresh) refresh_timer = min_refresh;
  464. }
  465. if (ftest(PLAYING)) update_pos();
  466. #ifdef USE_LOG
  467. {
  468. char foo[256];
  469. wsprintf(foo, "Update: %u(%u)+%u / %u(%u)", (int)data_written, (int)data_written % buf_size, BlockList.DataSize(), (int)GetCurPos(), (int)GetCurPos() % buf_size);
  470. log_write(foo);
  471. }
  472. #endif
  473. if (!ftest(PLAYING) && data_written + BlockList.DataSize() >= (int)prebuf && !wait)
  474. {
  475. log_write("done prebuffering");
  476. fset(NEED_PLAY_NOW);
  477. }
  478. DoLock();
  479. if (wait)
  480. {
  481. #ifdef DS2_HAVE_FADES
  482. if (wait->GetLatency() <= waitfade)
  483. {
  484. wait->FadeAndForget(waitfade);
  485. wait = 0;
  486. if (!ftest(PLAYING)) fset(NEED_PLAY_NOW);
  487. }
  488. #else
  489. if (wait->GetLatency() <= 0)
  490. {
  491. delete wait;
  492. wait = 0;
  493. if (!ftest(PLAYING)) fset(NEED_PLAY_NOW);
  494. }
  495. #endif
  496. }
  497. if (ftest(NEED_PLAY_NOW) && data_buffered > 0/* && !(ftest(PLAYING)*/)
  498. {
  499. log_write("starting playback");
  500. if (!ftest(PAUSED))
  501. {
  502. HRESULT res = pDSB->Play(0, 0, DSBPLAY_LOOPING);
  503. if (FAILED(res))
  504. {
  505. if (res == DSERR_BUFFERLOST) pDSB->Restore();
  506. return 0;
  507. }
  508. pos_delta = GetOutputTime(); pos_delta2 = data_written;
  509. }
  510. PostMessage(mod.hMainWindow, WM_WA_IPC, 0, IPC_OUTPUT_STARTED);
  511. fset(PLAYING);
  512. }
  513. funset(NEED_PLAY_NOW);
  514. if (ftest(PLAYING))
  515. {
  516. {
  517. DWORD foo = 0;
  518. pDSB->GetStatus(&foo);
  519. if (foo & DSBSTATUS_BUFFERLOST)
  520. pDSB->Restore();
  521. if (foo != (DSBSTATUS_PLAYING | DSBSTATUS_LOOPING))
  522. pDSB->Play(0, 0, DSBPLAY_LOOPING);
  523. }
  524. #ifdef DS2_HAVE_FADES
  525. if (!VolCtrl.Fading())
  526. {
  527. if (ftest(FADEPAUSING)) { Pause(1); funset(FADEPAUSING); }
  528. if (ftest(DIE_ON_STOP) || g_quitting_waiting)
  529. {
  530. delete this;
  531. return 1;
  532. }
  533. }
  534. #endif
  535. if (data_buffered <= silence_buffered)
  536. {
  537. log_write("underrun");
  538. ds_stop();
  539. if (ftest(DIE_ON_STOP) || g_quitting_waiting)
  540. {
  541. delete this;
  542. return 1;
  543. }
  544. else if (ftest(CLOSE_ON_STOP))
  545. {
  546. log_write("closeonstop");
  547. ds_kill();
  548. }
  549. #ifdef DS2_HAVE_FADES
  550. else if (ftest(FADEPAUSING)) { Pause(1); funset(FADEPAUSING); }
  551. #endif
  552. else Underruns++;
  553. }
  554. }
  555. return 0;
  556. }
  557. int DS2::Open(DS2config* cfg)
  558. {
  559. log_write("Open");
  560. // SYNCFUNC; //inside sync already
  561. HRESULT hr;
  562. g_delayed_deinit = cfg->delayed_shutdown;
  563. if (cfg->sil_db > 0) { silence_delta = calc_silence(cfg->sil_db, (int)cfg->bps); fset(STARTSIL); }
  564. else silence_delta = -1;
  565. create_primary = cfg->create_primary;
  566. UINT _p_bps = 0, _p_nch = 0, _p_sr = 0;
  567. if (cfg->prim_override)
  568. {
  569. _p_bps = cfg->_p_bps;
  570. _p_nch = cfg->_p_nch;
  571. _p_sr = cfg->_p_sr;
  572. }
  573. if (cfg->guid != cur_dev && pDS)
  574. {
  575. pDS->Release();
  576. pDS = 0;
  577. }
  578. if (!pDS)
  579. {
  580. log_write("Creating IDirectSound");
  581. cur_dev = cfg->guid;
  582. hr = myDirectSoundCreate(&cur_dev, &pDS);
  583. if (!pDS)
  584. {
  585. #ifdef DS2_HAVE_DEVICES
  586. cfg->SetErrorCodeMsgA(DsDevEnumGuid(cur_dev) ? WASABI_API_LNGSTRINGW(IDS_BAD_DS_DRIVER) : WASABI_API_LNGSTRINGW(IDS_DEVICE_NOT_FOUND_SELECT_ANOTHER), hr);
  587. #else
  588. cfg->SetErrorCodeMsg(WASABI_API_LNGSTRING(IDS_BAD_DS_DRIVER), hr);
  589. #endif
  590. return 0;
  591. }
  592. coop_mode = 0;
  593. }
  594. fmt_sr = (int)cfg->sr;
  595. fmt_nch = (WORD)cfg->nch;
  596. fmt_bps = (UINT)cfg->bps;
  597. if ((signed)fmt_sr <= 0 || (signed)fmt_bps <= 0 || (signed)fmt_nch <= 0) return 0;
  598. fmt_mul = fmt_sr * (fmt_bps >> 3) * fmt_nch;
  599. if (!_p_bps) _p_bps = fmt_bps;
  600. if (!_p_nch) _p_nch = fmt_nch;
  601. if (!_p_sr) _p_sr = fmt_sr;
  602. WAVEFORMATEX wfx =
  603. {
  604. WAVE_FORMAT_PCM,
  605. (WORD)fmt_nch,
  606. fmt_sr,
  607. fmt_mul,
  608. (WORD)(fmt_nch * (fmt_bps >> 3)),
  609. (WORD)fmt_bps,
  610. 0
  611. };
  612. {
  613. static DWORD coop_tab[3] = { DSSCL_NORMAL,DSSCL_PRIORITY,DSSCL_EXCLUSIVE };
  614. DWORD new_coop = coop_tab[cfg->coop];
  615. if (pPrimary && !create_primary)
  616. {
  617. pPrimary->Release();
  618. pPrimary = 0;
  619. }
  620. if (coop_mode != new_coop)
  621. {
  622. if (FAILED(hr = pDS->SetCooperativeLevel(cfg->wnd, coop_mode = new_coop)))
  623. {
  624. pDS->Release(); pDS = 0;
  625. cfg->SetErrorCodeMsgA(WASABI_API_LNGSTRINGW(IDS_ERROR_SETTING_DS_COOPERATIVE_LEVEL), hr);
  626. return 0;
  627. }
  628. }
  629. if (create_primary && !pPrimary)
  630. {
  631. DSBUFFERDESC desc =
  632. {
  633. sizeof(DSBUFFERDESC),
  634. DSBCAPS_PRIMARYBUFFER,
  635. 0,
  636. 0,
  637. 0
  638. };
  639. pDS->CreateSoundBuffer(&desc, &pPrimary, 0);
  640. prim_nch = prim_bps = prim_sr = 0;
  641. }
  642. if (pPrimary && (_p_bps != prim_bps || _p_nch != prim_nch || _p_sr != prim_sr))
  643. {
  644. WAVEFORMATEX wfx1 =
  645. {
  646. WAVE_FORMAT_PCM,
  647. (WORD)_p_nch,
  648. _p_sr,
  649. _p_sr * (_p_bps >> 3) * _p_nch,
  650. (WORD)(_p_nch * (_p_bps >> 3)),
  651. (WORD)_p_bps,
  652. 0
  653. };
  654. pPrimary->SetFormat(&wfx1);
  655. prim_bps = _p_bps;
  656. prim_nch = _p_nch;
  657. prim_sr = _p_sr;
  658. }
  659. }
  660. UINT new_buf_ms = cfg->ms;
  661. if (new_buf_ms < 100) new_buf_ms = 100;// <= DO NOT TOUCH
  662. else if (new_buf_ms > 100000) new_buf_ms = 100000;
  663. log_write("Done with IDirectSound, creating buffer");
  664. buf_size = _align_var(ms2bytes(new_buf_ms));
  665. prebuf = ms2bytes(cfg->preb);
  666. if (prebuf > buf_size) prebuf = buf_size;
  667. else if (prebuf < 0) prebuf = 0;
  668. DSBUFFERDESC desc =
  669. {
  670. sizeof(DSBUFFERDESC),
  671. DSBCAPS_GETCURRENTPOSITION2 |
  672. DSBCAPS_STICKYFOCUS |
  673. DSBCAPS_GLOBALFOCUS |
  674. DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME
  675. #ifdef DS2_HAVE_PITCH
  676. | (cfg->have_pitch ? DSBCAPS_CTRLFREQUENCY : 0)
  677. #endif
  678. ,
  679. buf_size,
  680. 0,
  681. &wfx
  682. };
  683. switch (cfg->mixing)
  684. {
  685. case DS2config::MIXING_FORCE_HARDWARE:
  686. desc.dwFlags |= DSBCAPS_LOCHARDWARE;
  687. break;
  688. case DS2config::MIXING_FORCE_SOFTWARE:
  689. desc.dwFlags |= DSBCAPS_LOCSOFTWARE;
  690. break;
  691. }
  692. // TODO:If an attempt is made to create a buffer with the DSBCAPS_LOCHARDWARE flag on a system where hardware acceleration is not available, the method fails with either DSERR_CONTROLUNAVAIL or DSERR_INVALIDCALL, depending on the operating system.
  693. do
  694. {
  695. WAVEFORMATEXTENSIBLE wfxe = { 0 };
  696. wfxe.Format = wfx;
  697. wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
  698. wfxe.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
  699. wfxe.Format.nChannels = fmt_nch;
  700. wfxe.Format.nBlockAlign = (wfxe.Format.nChannels *
  701. wfxe.Format.wBitsPerSample) / 8;
  702. wfxe.Format.nAvgBytesPerSec = wfxe.Format.nBlockAlign *
  703. wfxe.Format.nSamplesPerSec;
  704. wfxe.Samples.wReserved = 0;
  705. if (fmt_nch > kMaxChannelsToMask) {
  706. wfxe.dwChannelMask = kChannelsToMask[kMaxChannelsToMask];
  707. }
  708. else {
  709. wfxe.dwChannelMask = kChannelsToMask[fmt_nch];
  710. }
  711. wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
  712. wfxe.Samples.wValidBitsPerSample = wfxe.Format.wBitsPerSample;
  713. desc.lpwfxFormat = &wfxe.Format;
  714. hr = pDS->CreateSoundBuffer(&desc, &pDSB, 0);
  715. if (SUCCEEDED(hr))
  716. {
  717. hr = 0;
  718. break;
  719. }
  720. } while (0);
  721. if (FAILED(hr) || !pDSB)
  722. {
  723. cfg->SetErrorCodeMsgA(WASABI_API_LNGSTRINGW(IDS_ERROR_CREATING_DS_BUFFER), hr);
  724. return 0;
  725. }
  726. pDS->AddRef();
  727. myDS = pDS;
  728. {
  729. DSBCAPS caps;
  730. memset(&caps, 0, sizeof(caps));
  731. caps.dwSize = sizeof(caps);
  732. pDSB->GetCaps(&caps);
  733. if (caps.dwFlags & DSBCAPS_LOCSOFTWARE) fset(SWMIXED);
  734. }
  735. clear_size = ms2bytes(200);
  736. if (clear_size > buf_size >> 2) clear_size = buf_size >> 2;
  737. clear_size = _align_var(clear_size);
  738. if (prebuf < clear_size + (clear_size >> 1)) prebuf = clear_size + (clear_size >> 1);
  739. VolCtrl.Apply(pDSB);
  740. reset_vars();
  741. //pDSB->SetVolume(DSBVOLUME_MAX);
  742. log_write("Open : done");
  743. return 1;
  744. }
  745. void DS2::reset_vars()
  746. {
  747. pos_delta = 0; pos_delta2 = 0;
  748. flags &= ~(FLAG_NEED_PLAY_NOW | FLAG_PLAYING);
  749. data_buffered = 0;
  750. data_written = 0;
  751. silence_buffered = 0;
  752. last_nonsil = -1;
  753. BlockList.Reset();
  754. VolCtrl.Reset();
  755. }
  756. void DS2::do_reset_vars()
  757. {
  758. g_total_time += GetOutputTime();
  759. reset_vars();
  760. }
  761. bool DS2::DoLock()
  762. {
  763. void* p1 = 0, * p2 = 0;
  764. DWORD s1 = 0, s2 = 0;
  765. UINT LockSize = (UINT)BlockList.DataSize();
  766. if (LockSize > 0 && silence_buffered)
  767. {
  768. data_written -= silence_buffered;
  769. __int64 min = GetSafeWrite();
  770. if (data_written < min) data_written = min;
  771. silence_buffered = 0;
  772. }
  773. UINT MaxData = buf_size;
  774. int FooScale = ftest(SWMIXED) ? 6 : 4;
  775. MaxData = _align_var(MaxData - (MaxData >> FooScale));
  776. UINT MaxLock = MaxData > data_buffered ? MaxData - data_buffered : 0;
  777. if (!MaxLock) return 0;
  778. if (
  779. ((ftest(PLAYING)) || ftest(NEED_PLAY_NOW))
  780. && data_buffered + LockSize < clear_size)
  781. //underrun warning, put extra silence
  782. LockSize = clear_size - data_buffered;
  783. if (LockSize > MaxLock) LockSize = MaxLock;
  784. if (LockSize == 0) return 0;//final check for useless locks
  785. if (data_buffered > clear_size && LockSize<buf_size >> FooScale) return 0;
  786. log_write("locking");//lock away!
  787. while (1)
  788. {
  789. HRESULT hr = pDSB->Lock((UINT)(data_written % (__int64)buf_size), LockSize, &p1, &s1, &p2, &s2, 0);
  790. if (SUCCEEDED(hr))
  791. {
  792. LockCount++;
  793. UINT written;
  794. written = (UINT)BlockList.DumpBlocks(p1, s1);
  795. if (p2 && s2) written += (UINT)BlockList.DumpBlocks(p2, s2);
  796. //note: we fill with silence when not enough data
  797. UINT total = s1 + s2;
  798. data_written = data_written + total;
  799. data_buffered += total;
  800. if (written > 0) silence_buffered = total - written;
  801. else silence_buffered += total;
  802. pDSB->Unlock(p1, s1, p2, s2);
  803. break;
  804. }
  805. else if (hr == DSERR_BUFFERLOST) {
  806. if (FAILED(pDSB->Restore())) break;
  807. }
  808. else break;
  809. }
  810. return 1;
  811. }
  812. int DS2::CanWrite()//result can be negative !
  813. {
  814. log_write("CanWrite");
  815. SYNC_IN();
  816. if (ftest(PAUSED)) { SYNC_OUT(); return 0; }
  817. int rv;
  818. int m = buf_size - (int)(data_buffered + BlockList.DataSize());
  819. if (ftest(USE_CPU_MNGMNT) && ftest(PLAYING))// && data_written<buf_size && GetCurPos()<buf_size)
  820. {
  821. __int64 t = ((GetCurPos() - pos_delta) << 2) - (data_written - pos_delta2 + BlockList.DataSize());
  822. rv = t > m ? m : (int)t;
  823. }
  824. else
  825. {
  826. rv = m;
  827. }
  828. if (wait) rv -= ms2bytes(wait->GetLatency());
  829. #ifdef USE_LOG
  830. char moo[256];
  831. wsprintf(moo, "CanWrite : %i", rv);
  832. log_write(moo);
  833. #endif
  834. SYNC_OUT();
  835. return _align_var(rv);
  836. }
  837. void DS2::Pause(int new_state)
  838. {
  839. SYNC_IN();
  840. #ifdef USE_LOG
  841. log_write("Pause");
  842. if (ftest(PAUSED)) log_write("is_paused");
  843. if (new_state) log_write("new_state");
  844. #endif
  845. if (new_state && !ftest(PAUSED))
  846. {//pause
  847. log_write("pausing");
  848. if (ftest(PLAYING) && pDSB)
  849. {
  850. pDSB->Stop();
  851. #ifdef USE_LOG
  852. char foo[256];
  853. wsprintf(foo, "stopping buffer - %u", GetCurPos());
  854. log_write(foo);
  855. #endif
  856. }
  857. fset(PAUSED);
  858. }
  859. else if (!new_state)
  860. {
  861. if (ftest(PAUSED))
  862. {//unpause
  863. log_write("unpausing");
  864. if (ftest(PLAYING)) fset(NEED_PLAY_NOW);
  865. #ifdef DS2_HAVE_FADES
  866. if (ftest(FADEPAUSE))
  867. {
  868. VolCtrl.SetTime(GetCurPos());
  869. VolCtrl.SetFadeVol(ms2bytes(fadepause_time), fadepause_orgvol);
  870. }
  871. #endif
  872. log_write("unpausing");
  873. }
  874. #ifdef DS2_HAVE_FADES
  875. else if (ftest(FADEPAUSING))//abort fadeout
  876. {
  877. VolCtrl.SetTime(GetCurPos());
  878. VolCtrl.SetFadeVol(VolCtrl.RelFade(ms2bytes(fadepause_time), fadepause_orgvol), fadepause_orgvol);
  879. }
  880. funset(FADEPAUSE);
  881. funset(FADEPAUSING);
  882. #endif
  883. funset(PAUSED);
  884. }
  885. if (wait)
  886. {
  887. log_write("wait pause too");
  888. wait->Pause(new_state);
  889. }
  890. log_write("pause done");
  891. SYNC_OUT();
  892. }
  893. void DS2::SetVolume(double v)
  894. {
  895. SYNC_IN();
  896. if (!ftest(DIE_ON_STOP) && pDSB)
  897. {
  898. VolCtrl.SetVolume(v);
  899. VolCtrl.Apply(pDSB);
  900. }
  901. if (wait) wait->SetVolume(v);
  902. SYNC_OUT();
  903. }
  904. void DS2::SetPan(double p)
  905. {
  906. SYNC_IN();
  907. if (!ftest(DIE_ON_STOP) && pDSB)
  908. {
  909. VolCtrl.SetPan(p);
  910. VolCtrl.Apply(pDSB);
  911. }
  912. if (wait) wait->SetPan(p);
  913. SYNC_OUT();
  914. }
  915. UINT DS2::GetLatency()
  916. {
  917. SYNC_IN();
  918. UINT bDataSize = (UINT)BlockList.DataSize();
  919. int bytes;
  920. if (bDataSize) bytes = data_buffered + (UINT)BlockList.DataSize();
  921. else bytes = data_buffered - silence_buffered;
  922. if (bytes < 0) bytes = 0;
  923. UINT rv = bytes2ms((UINT)bytes);
  924. if (wait) rv += wait->GetLatency();
  925. #ifdef USE_LOG
  926. {
  927. char foo[128];
  928. wsprintf(foo, "GetLatency: %u (%u %u)", rv, data_written - GetCurPos(), BlockList.DataSize());
  929. log_write(foo);
  930. }
  931. #endif
  932. SYNC_OUT();
  933. return rv;
  934. }
  935. #ifdef DS2_HAVE_FADES
  936. void DS2::Fade(UINT time, double destvol)
  937. {
  938. SYNC_IN();
  939. VolCtrl.SetFadeVol(ms2bytes(time), destvol);
  940. SYNC_OUT();
  941. }
  942. void DS2::FadeAndForget(UINT time)
  943. {
  944. SYNC_IN();
  945. if (!pDSB || time == 0 || ftest(PAUSED) || (!data_written && !BlockList.DataSize()))
  946. {
  947. delete this;
  948. }
  949. else
  950. {
  951. fset(DIE_ON_STOP);
  952. if (!ftest(PLAYING)) fset(NEED_PLAY_NOW);
  953. __int64 fadetime = ms2bytes(time);
  954. __int64 max = data_written + BlockList.DataSize() - GetCurPos();
  955. if (max < 0) max = 0;
  956. if (fadetime > max) fadetime = max;
  957. VolCtrl.SetFadeVol(fadetime, 0);
  958. }
  959. SYNC_OUT();
  960. }
  961. void DS2::FadeX(UINT time, double dest)
  962. {
  963. SYNC_IN();
  964. if (ftest(PAUSED) && ftest(FADEPAUSE))
  965. {
  966. fadepause_orgvol = dest;
  967. }
  968. VolCtrl.SetFadeVol(VolCtrl.RelFade(ms2bytes(time), dest), dest);
  969. SYNC_OUT();
  970. }
  971. void DS2::FadePause(UINT time)
  972. {
  973. SYNC_IN();
  974. if (!time)
  975. {
  976. Pause(1);
  977. }
  978. else
  979. {
  980. if (wait)
  981. {
  982. wait->FadeAndForget(time);
  983. wait = 0;
  984. }
  985. if (!ftest(PLAYING))
  986. {
  987. fset(PAUSED);
  988. }
  989. else
  990. {
  991. fadepause_time = time;
  992. fset(FADEPAUSE);
  993. fset(FADEPAUSING);
  994. fadepause_orgvol = VolCtrl.GetDestVol();
  995. VolCtrl.SetFadeVol(ms2bytes(time), 0);
  996. }
  997. }
  998. SYNC_OUT();
  999. }
  1000. #endif
  1001. UINT DS2::InstanceCount()
  1002. {
  1003. _log_write("InstanceCount", 0);
  1004. SYNC_IN();
  1005. UINT rv = 0;
  1006. DS2* p = ds2s;
  1007. while (p) { rv++; p = p->next; }
  1008. SYNC_OUT();
  1009. return rv;
  1010. }
  1011. __int64 DS2::GetSafeWrite()
  1012. {
  1013. return GetCurPos() + clear_size + ms2bytes(refresh_timer);
  1014. }
  1015. void DS2::KillEndGap()
  1016. {
  1017. SYNC_IN();
  1018. if (silence_delta >= 0 && last_nonsil >= 0)
  1019. {
  1020. __int64 cp = GetSafeWrite();
  1021. if (cp < data_written)
  1022. {
  1023. __int64 dest = last_nonsil < cp ? cp : last_nonsil;
  1024. if (dest > data_written)
  1025. {//need to take data from blocklist
  1026. UINT s = (UINT)BlockList.DataSize();
  1027. char* temp0r = (char*)malloc(s);
  1028. BlockList.DumpBlocks(temp0r, s);
  1029. BlockList.Reset();
  1030. BlockList.AddBlock(temp0r, (UINT)(dest - data_written));
  1031. free(temp0r);
  1032. }
  1033. else
  1034. {
  1035. BlockList.Reset();
  1036. data_written = dest;
  1037. }
  1038. }
  1039. last_nonsil = -1;
  1040. fset(STARTSIL);
  1041. }
  1042. SYNC_OUT();
  1043. }
  1044. void DS2::Flush()
  1045. {
  1046. log_write("Flush");
  1047. SYNC_IN();
  1048. ds_stop();
  1049. SYNC_OUT();
  1050. }
  1051. void DS2::ForcePlay()
  1052. {
  1053. SYNC_IN();
  1054. if (!ftest(PAUSED) && !ftest(PLAYING) && !wait && data_buffered + BlockList.DataSize() > 0)
  1055. {
  1056. log_write("forceplay");
  1057. fset(NEED_PLAY_NOW);
  1058. }
  1059. SYNC_OUT();
  1060. }
  1061. void DS2::WaitFor(DS2* prev, UINT fade)
  1062. {
  1063. SYNC_IN();
  1064. if (wait) delete wait;
  1065. wait = prev;
  1066. #ifdef DS2_HAVE_FADES
  1067. waitfade = fade;
  1068. #endif
  1069. wait->flags |= FLAG_WAITED;
  1070. wait->ForcePlay();
  1071. SYNC_OUT();
  1072. }
  1073. void DS2::StartNewStream()
  1074. {
  1075. SYNC_IN();
  1076. if (last_nonsil > data_written + (UINT)BlockList.DataSize()) last_nonsil = data_written + (UINT)BlockList.DataSize();
  1077. pos_delta = GetCurPos(); pos_delta2 = data_written;
  1078. SYNC_OUT();
  1079. }
  1080. void DS2::SetCloseOnStop(bool b)
  1081. {
  1082. SYNC_IN();
  1083. log_write("setcloseonstop");
  1084. fsetc(CLOSE_ON_STOP, b);
  1085. if (b && !ftest(PLAYING)) ds_kill();
  1086. SYNC_OUT();
  1087. }
  1088. bool DS2::IsClosed()
  1089. {
  1090. SYNC_IN();
  1091. bool rv = pDSB ? 0 : 1;
  1092. SYNC_OUT();
  1093. return rv;
  1094. }
  1095. void DS2::GetRealtimeStat(DS2_REALTIME_STAT* stat)
  1096. {
  1097. log_write("GetRealtimeStat");
  1098. SYNC_IN();
  1099. __int64 curpos = GetCurPos();
  1100. stat->sr = fmt_sr;
  1101. stat->bps = fmt_bps;
  1102. stat->nch = fmt_nch;
  1103. stat->buf_size_bytes = buf_size;
  1104. stat->buf_size_ms = bytes2ms(buf_size);
  1105. stat->pos_play = (UINT)(curpos % buf_size);
  1106. stat->pos_write = (UINT)(data_written % buf_size);
  1107. stat->latency = data_buffered + (UINT)BlockList.DataSize();
  1108. if (stat->latency < 0) stat->latency = 0;
  1109. stat->latency_ms = bytes2ms(stat->latency);
  1110. stat->lock_count = LockCount;
  1111. stat->underruns = Underruns;
  1112. stat->bytes_async = BlockList.DataSize();
  1113. stat->bytes_written = data_written + BlockList.DataSize();
  1114. stat->bytes_played = curpos;
  1115. stat->have_primary_buffer = pPrimary ? true : false;
  1116. stat->current_device = cur_dev;
  1117. stat->vol_left = VolCtrl.Stat_GetVolLeft();
  1118. stat->vol_right = VolCtrl.Stat_GetVolRight();
  1119. if (pDSB)
  1120. {
  1121. DSBCAPS caps;
  1122. memset(&caps, 0, sizeof(caps));
  1123. caps.dwSize = sizeof(caps);
  1124. pDSB->GetCaps(&caps);
  1125. stat->dscaps_flags = caps.dwFlags;
  1126. }
  1127. else stat->dscaps_flags = 0;
  1128. if (pPrimary)
  1129. {
  1130. DSBCAPS caps;
  1131. memset(&caps, 0, sizeof(caps));
  1132. caps.dwSize = sizeof(caps);
  1133. pPrimary->GetCaps(&caps);
  1134. stat->dscaps_flags_primary = caps.dwFlags;
  1135. }
  1136. else stat->dscaps_flags_primary = 0;
  1137. stat->paused = !!ftest(PAUSED);
  1138. SYNC_OUT();
  1139. }
  1140. bool DS2::GetRealtimeStatStatic(DS2_REALTIME_STAT* stat)
  1141. {
  1142. bool rv = 0;
  1143. SYNC_IN();
  1144. if (ds2s) { ds2s->GetRealtimeStat(stat); rv = 1; }
  1145. SYNC_OUT();
  1146. return rv;
  1147. }
  1148. void DS2::SetTotalTime(__int64 z)
  1149. {
  1150. _log_write("SetTotalTime", 0);
  1151. SYNC_IN();
  1152. g_total_time = z;
  1153. SYNC_OUT();
  1154. }
  1155. __int64 DS2::GetTotalTime()
  1156. {
  1157. _log_write("GetTotalTime", 0);
  1158. SYNC_IN();
  1159. __int64 r = g_total_time;
  1160. DS2* p = ds2s;
  1161. while (p)
  1162. {
  1163. r += p->GetOutputTime();
  1164. p = p->next;
  1165. }
  1166. SYNC_OUT();
  1167. return r;
  1168. }
  1169. __int64 DS2::GetOutputTime()
  1170. {
  1171. if (!fmt_bps || !fmt_nch || !fmt_sr) return 0;
  1172. SYNC_IN();//need __int64, cant do bytes2ms
  1173. __int64 r = (GetCurPos()) / ((fmt_bps >> 3) * fmt_nch) * 1000 / fmt_sr;
  1174. SYNC_OUT();
  1175. return r;
  1176. }
  1177. #ifdef DS2_HAVE_PITCH
  1178. void DS2::SetPitch(double p)
  1179. {
  1180. SYNC_IN();
  1181. DWORD f = (DWORD)(p * (double)fmt_sr);
  1182. if (f < DSBFREQUENCY_MIN) f = DSBFREQUENCY_MIN;
  1183. else if (f > DSBFREQUENCY_MAX) f = DSBFREQUENCY_MAX;
  1184. if (pDSB) pDSB->SetFrequency(f);
  1185. SYNC_OUT();
  1186. }
  1187. #endif
  1188. #ifdef DS2_HAVE_DEVICES
  1189. GUID DS2::GetCurDev() { return cur_dev; }
  1190. #endif