WineWrapper.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #if defined(MPT_BUILD_WINESUPPORT_WRAPPER)
  2. #include "NativeConfig.h"
  3. #include <windows.h>
  4. #include <stdint.h>
  5. #include "Native.h"
  6. #include "NativeUtils.h"
  7. #include "NativeSoundDevice.h"
  8. #ifdef _MSC_VER
  9. #pragma warning(disable:4098) /* 'void' function returning a value */
  10. #endif
  11. #define WINE_THREAD
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_Init(void) {
  16. return OpenMPT_Init();
  17. }
  18. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_Fini(void) {
  19. return OpenMPT_Fini();
  20. }
  21. OPENMPT_WINESUPPORT_WRAPPER_API void * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_Alloc( size_t size ) {
  22. return OpenMPT_Alloc( size );
  23. }
  24. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_Free( void * mem ) {
  25. return OpenMPT_Free( mem );
  26. }
  27. OPENMPT_WINESUPPORT_WRAPPER_API size_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_String_Length( const char * str ) {
  28. return OpenMPT_String_Length( str );
  29. }
  30. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_String_Duplicate( const char * src ) {
  31. return OpenMPT_String_Duplicate( src );
  32. }
  33. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_String_Free( char * str ) {
  34. return OpenMPT_String_Free( str );
  35. }
  36. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_EnumerateDevices() {
  37. return OpenMPT_SoundDevice_EnumerateDevices();
  38. }
  39. typedef struct OpenMPT_Wine_Wrapper_SoundDevice_IMessageReceiver {
  40. void * inst;
  41. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundDeviceMessageFunc)( void * inst, uintptr_t level, const char * message );
  42. } OpenMPT_Wine_Wrapper_SoundDevice_IMessageReceiver;
  43. typedef struct OpenMPT_Wine_Wrapper_SoundDevice_ICallback {
  44. void * inst;
  45. // main thread
  46. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackGetReferenceClockNowNanosecondsFunc)( void * inst, uint64_t * result );
  47. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackPreStartFunc)( void * inst );
  48. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackPostStopFunc)( void * inst );
  49. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackIsLockedByCurrentThreadFunc)( void * inst, uintptr_t * result );
  50. // audio thread
  51. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockFunc)( void * inst );
  52. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedGetReferenceClockNowNanosecondsFunc)( void * inst, uint64_t * result );
  53. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessPrepareFunc)( void * inst, const OpenMPT_SoundDevice_TimeInfo * timeInfo );
  54. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessUint8Func)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, uint8_t * buffer, const uint8_t * inputBuffer );
  55. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessInt8Func)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int8_t * buffer, const int8_t * inputBuffer );
  56. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessInt16Func)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int16_t * buffer, const int16_t * inputBuffer );
  57. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessInt24Func)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, OpenMPT_int24 * buffer, const OpenMPT_int24 * inputBuffer );
  58. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessInt32Func)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int32_t * buffer, const int32_t * inputBuffer );
  59. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessFloatFunc)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, float * buffer, const float * inputBuffer );
  60. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessDoubleFunc)( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, double * buffer, const double * inputBuffer );
  61. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackLockedProcessDoneFunc)( void * inst, const OpenMPT_SoundDevice_TimeInfo * timeInfo );
  62. void (OPENMPT_WINESUPPORT_WRAPPER_CALL * SoundCallbackUnlockFunc)( void * inst );
  63. } OpenMPT_Wine_Wrapper_SoundDevice_ICallback;
  64. #ifdef WINE_THREAD
  65. typedef enum OpenMPT_Wine_Wrapper_AudioThreadCommand {
  66. AudioThreadCommandInvalid = -1
  67. , AudioThreadCommandExit = 0
  68. , AudioThreadCommandLock = 1
  69. , AudioThreadCommandClock = 2
  70. , AudioThreadCommandReadPrepare = 3
  71. , AudioThreadCommandReadUint8 = 4
  72. , AudioThreadCommandReadInt8 = 5
  73. , AudioThreadCommandReadInt16 = 6
  74. , AudioThreadCommandReadInt24 = 7
  75. , AudioThreadCommandReadInt32 = 8
  76. , AudioThreadCommandReadFloat = 9
  77. , AudioThreadCommandReadDouble = 10
  78. , AudioThreadCommandReadDone = 11
  79. , AudioThreadCommandUnlock = 12
  80. } OpenMPT_Wine_Wrapper_AudioThreadCommand;
  81. #endif
  82. typedef struct OpenMPT_Wine_Wrapper_SoundDevice {
  83. OpenMPT_SoundDevice * impl;
  84. OpenMPT_SoundDevice_IMessageReceiver native_receiver;
  85. OpenMPT_SoundDevice_ICallback native_callback;
  86. OpenMPT_Wine_Wrapper_SoundDevice_IMessageReceiver wine_receiver;
  87. OpenMPT_Wine_Wrapper_SoundDevice_ICallback wine_callback;
  88. #ifdef WINE_THREAD
  89. HANDLE audiothread_startup_done;
  90. OpenMPT_Semaphore * audiothread_sem_request;
  91. OpenMPT_Semaphore * audiothread_sem_done;
  92. OpenMPT_Wine_Wrapper_AudioThreadCommand audiothread_command;
  93. const OpenMPT_SoundDevice_TimeInfo * audiothread_command_timeInfo;
  94. const OpenMPT_SoundDevice_BufferFormat * audiothread_command_bufferFormat;
  95. const OpenMPT_SoundDevice_BufferAttributes * audiothread_command_bufferAttributes;
  96. uintptr_t audiothread_command_numFrames;
  97. union {
  98. uint8_t * buf_uint8;
  99. int8_t * buf_int8;
  100. int16_t * buf_int16;
  101. OpenMPT_int24 * buf_int24;
  102. int32_t * buf_int32;
  103. float * buf_float;
  104. double * buf_double;
  105. } audiothread_command_buffer;
  106. union {
  107. const uint8_t * buf_uint8;
  108. const int8_t * buf_int8;
  109. const int16_t * buf_int16;
  110. const OpenMPT_int24 * buf_int24;
  111. const int32_t * buf_int32;
  112. const float * buf_float;
  113. const double * buf_double;
  114. } audiothread_command_inputBuffer;
  115. uint64_t * audiothread_command_result;
  116. HANDLE audiothread;
  117. OpenMPT_PriorityBooster * priority_booster;
  118. #endif
  119. } OpenMPT_Wine_Wrapper_SoundDevice;
  120. static void OPENMPT_WINESUPPORT_CALL SoundDeviceMessageFunc( void * inst, uintptr_t level, const char * message ) {
  121. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  122. if ( !sd ) {
  123. return;
  124. }
  125. return sd->wine_receiver.SoundDeviceMessageFunc( sd->wine_receiver.inst, level, message );
  126. }
  127. static void OPENMPT_WINESUPPORT_CALL SoundCallbackGetReferenceClockNowNanosecondsFunc( void * inst, uint64_t * result ) {
  128. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  129. if ( !sd ) {
  130. return;
  131. }
  132. return sd->wine_callback.SoundCallbackGetReferenceClockNowNanosecondsFunc( sd->wine_callback.inst, result );
  133. }
  134. static void OPENMPT_WINESUPPORT_CALL SoundCallbackPreStartFunc( void * inst ) {
  135. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  136. if ( !sd ) {
  137. return;
  138. }
  139. return sd->wine_callback.SoundCallbackPreStartFunc( sd->wine_callback.inst );
  140. }
  141. static void OPENMPT_WINESUPPORT_CALL SoundCallbackPostStopFunc( void * inst ) {
  142. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  143. if ( !sd ) {
  144. return;
  145. }
  146. return sd->wine_callback.SoundCallbackPostStopFunc( sd->wine_callback.inst );
  147. }
  148. static void OPENMPT_WINESUPPORT_CALL SoundCallbackIsLockedByCurrentThreadFunc( void * inst, uintptr_t * result ) {
  149. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  150. if ( !sd ) {
  151. return;
  152. }
  153. return sd->wine_callback.SoundCallbackIsLockedByCurrentThreadFunc( sd->wine_callback.inst, result );
  154. }
  155. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockFunc( void * inst ) {
  156. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  157. if ( !sd ) {
  158. return;
  159. }
  160. #ifdef WINE_THREAD
  161. sd->audiothread_command = AudioThreadCommandLock;
  162. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  163. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  164. #else
  165. return sd->wine_callback.SoundCallbackLockFunc( sd->wine_callback.inst );
  166. #endif
  167. }
  168. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedGetReferenceClockNowNanosecondsFunc( void * inst, uint64_t * result ) {
  169. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  170. if ( !sd ) {
  171. return;
  172. }
  173. #ifdef WINE_THREAD
  174. sd->audiothread_command = AudioThreadCommandClock;
  175. sd->audiothread_command_result = result;
  176. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  177. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  178. #else
  179. return sd->wine_callback.SoundCallbackLockedGetReferenceClockNowNanosecondsFunc( sd->wine_callback.inst, result );
  180. #endif
  181. }
  182. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessPrepareFunc( void * inst, const OpenMPT_SoundDevice_TimeInfo * timeInfo ) {
  183. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  184. if ( !sd ) {
  185. return;
  186. }
  187. #ifdef WINE_THREAD
  188. sd->audiothread_command = AudioThreadCommandReadPrepare;
  189. sd->audiothread_command_timeInfo = timeInfo;
  190. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  191. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  192. #else
  193. return sd->wine_callback.SoundCallbackLockedProcessPrepareFunc( sd->wine_callback.inst, timeInfo );
  194. #endif
  195. }
  196. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessUint8Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, uint8_t * buffer, const uint8_t * inputBuffer ) {
  197. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  198. if ( !sd ) {
  199. return;
  200. }
  201. #ifdef WINE_THREAD
  202. sd->audiothread_command = AudioThreadCommandReadUint8;
  203. sd->audiothread_command_bufferFormat = bufferFormat;
  204. sd->audiothread_command_numFrames = numFrames;
  205. sd->audiothread_command_buffer.buf_uint8 = buffer;
  206. sd->audiothread_command_inputBuffer.buf_uint8 = inputBuffer;
  207. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  208. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  209. #else
  210. return sd->wine_callback.SoundCallbackLockedProcessUint8Func( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  211. #endif
  212. }
  213. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessInt8Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int8_t * buffer, const int8_t * inputBuffer ) {
  214. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  215. if ( !sd ) {
  216. return;
  217. }
  218. #ifdef WINE_THREAD
  219. sd->audiothread_command = AudioThreadCommandReadInt8;
  220. sd->audiothread_command_bufferFormat = bufferFormat;
  221. sd->audiothread_command_numFrames = numFrames;
  222. sd->audiothread_command_buffer.buf_int8 = buffer;
  223. sd->audiothread_command_inputBuffer.buf_int8 = inputBuffer;
  224. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  225. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  226. #else
  227. return sd->wine_callback.SoundCallbackLockedProcessInt8Func( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  228. #endif
  229. }
  230. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessInt16Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int16_t * buffer, const int16_t * inputBuffer ) {
  231. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  232. if ( !sd ) {
  233. return;
  234. }
  235. #ifdef WINE_THREAD
  236. sd->audiothread_command = AudioThreadCommandReadInt16;
  237. sd->audiothread_command_bufferFormat = bufferFormat;
  238. sd->audiothread_command_numFrames = numFrames;
  239. sd->audiothread_command_buffer.buf_int16 = buffer;
  240. sd->audiothread_command_inputBuffer.buf_int16 = inputBuffer;
  241. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  242. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  243. #else
  244. return sd->wine_callback.SoundCallbackLockedProcessInt16Func( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  245. #endif
  246. }
  247. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessInt24Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, OpenMPT_int24 * buffer, const OpenMPT_int24 * inputBuffer ) {
  248. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  249. if ( !sd ) {
  250. return;
  251. }
  252. #ifdef WINE_THREAD
  253. sd->audiothread_command = AudioThreadCommandReadInt24;
  254. sd->audiothread_command_bufferFormat = bufferFormat;
  255. sd->audiothread_command_numFrames = numFrames;
  256. sd->audiothread_command_buffer.buf_int24 = buffer;
  257. sd->audiothread_command_inputBuffer.buf_int24 = inputBuffer;
  258. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  259. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  260. #else
  261. return sd->wine_callback.SoundCallbackLockedProcessInt24Func( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  262. #endif
  263. }
  264. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessInt32Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int32_t * buffer, const int32_t * inputBuffer ) {
  265. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  266. if ( !sd ) {
  267. return;
  268. }
  269. #ifdef WINE_THREAD
  270. sd->audiothread_command = AudioThreadCommandReadInt32;
  271. sd->audiothread_command_bufferFormat = bufferFormat;
  272. sd->audiothread_command_numFrames = numFrames;
  273. sd->audiothread_command_buffer.buf_int32 = buffer;
  274. sd->audiothread_command_inputBuffer.buf_int32 = inputBuffer;
  275. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  276. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  277. #else
  278. return sd->wine_callback.SoundCallbackLockedProcessInt32Func( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  279. #endif
  280. }
  281. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessFloatFunc( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, float * buffer, const float * inputBuffer ) {
  282. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  283. if ( !sd ) {
  284. return;
  285. }
  286. #ifdef WINE_THREAD
  287. sd->audiothread_command = AudioThreadCommandReadFloat;
  288. sd->audiothread_command_bufferFormat = bufferFormat;
  289. sd->audiothread_command_numFrames = numFrames;
  290. sd->audiothread_command_buffer.buf_float = buffer;
  291. sd->audiothread_command_inputBuffer.buf_float = inputBuffer;
  292. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  293. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  294. #else
  295. return sd->wine_callback.SoundCallbackLockedProcessFloatFunc( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  296. #endif
  297. }
  298. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessDoubleFunc( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, double * buffer, const double * inputBuffer ) {
  299. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  300. if ( !sd ) {
  301. return;
  302. }
  303. #ifdef WINE_THREAD
  304. sd->audiothread_command = AudioThreadCommandReadDouble;
  305. sd->audiothread_command_bufferFormat = bufferFormat;
  306. sd->audiothread_command_numFrames = numFrames;
  307. sd->audiothread_command_buffer.buf_double = buffer;
  308. sd->audiothread_command_inputBuffer.buf_double = inputBuffer;
  309. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  310. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  311. #else
  312. return sd->wine_callback.SoundCallbackLockedProcessDoubleFunc( sd->wine_callback.inst, bufferFormat, bufferAttributes, numFrames, buffer, inputBuffer );
  313. #endif
  314. }
  315. static void OPENMPT_WINESUPPORT_CALL SoundCallbackLockedProcessDoneFunc( void * inst, const OpenMPT_SoundDevice_TimeInfo * timeInfo ) {
  316. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  317. if ( !sd ) {
  318. return;
  319. }
  320. #ifdef WINE_THREAD
  321. sd->audiothread_command = AudioThreadCommandReadDone;
  322. sd->audiothread_command_timeInfo = timeInfo;
  323. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  324. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  325. #else
  326. return sd->wine_callback.SoundCallbackLockedProcessDoneFunc( sd->wine_callback.inst, timeInfo );
  327. #endif
  328. }
  329. static void OPENMPT_WINESUPPORT_CALL SoundCallbackUnlockFunc( void * inst ) {
  330. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)inst;
  331. if ( !sd ) {
  332. return;
  333. }
  334. #ifdef WINE_THREAD
  335. sd->audiothread_command = AudioThreadCommandUnlock;
  336. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  337. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  338. #else
  339. return sd->wine_callback.SoundCallbackUnlockFunc( sd->wine_callback.inst );
  340. #endif
  341. }
  342. #ifdef WINE_THREAD
  343. static DWORD WINAPI AudioThread( LPVOID userdata ) {
  344. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)userdata;
  345. sd->priority_booster = OpenMPT_PriorityBooster_Construct_From_SoundDevice( sd->impl );
  346. SetEvent( sd->audiothread_startup_done );
  347. {
  348. int exit = 0;
  349. while(!exit)
  350. {
  351. OpenMPT_Semaphore_Wait( sd->audiothread_sem_request );
  352. switch ( sd->audiothread_command ) {
  353. case AudioThreadCommandExit:
  354. exit = 1;
  355. break;
  356. case AudioThreadCommandLock:
  357. if(sd->wine_callback.SoundCallbackLockFunc)
  358. sd->wine_callback.SoundCallbackLockFunc( sd->wine_callback.inst );
  359. break;
  360. case AudioThreadCommandClock:
  361. if(sd->wine_callback.SoundCallbackLockedGetReferenceClockNowNanosecondsFunc)
  362. sd->wine_callback.SoundCallbackLockedGetReferenceClockNowNanosecondsFunc( sd->wine_callback.inst, sd->audiothread_command_result );
  363. break;
  364. case AudioThreadCommandReadPrepare:
  365. if(sd->wine_callback.SoundCallbackLockedProcessPrepareFunc)
  366. sd->wine_callback.SoundCallbackLockedProcessPrepareFunc
  367. ( sd->wine_callback.inst
  368. , sd->audiothread_command_timeInfo
  369. );
  370. break;
  371. case AudioThreadCommandReadUint8:
  372. if(sd->wine_callback.SoundCallbackLockedProcessUint8Func)
  373. sd->wine_callback.SoundCallbackLockedProcessUint8Func
  374. ( sd->wine_callback.inst
  375. , sd->audiothread_command_bufferFormat
  376. , sd->audiothread_command_numFrames
  377. , sd->audiothread_command_buffer.buf_uint8
  378. , sd->audiothread_command_inputBuffer.buf_uint8
  379. );
  380. break;
  381. case AudioThreadCommandReadInt8:
  382. if(sd->wine_callback.SoundCallbackLockedProcessInt8Func)
  383. sd->wine_callback.SoundCallbackLockedProcessInt8Func
  384. ( sd->wine_callback.inst
  385. , sd->audiothread_command_bufferFormat
  386. , sd->audiothread_command_numFrames
  387. , sd->audiothread_command_buffer.buf_int8
  388. , sd->audiothread_command_inputBuffer.buf_int8
  389. );
  390. break;
  391. case AudioThreadCommandReadInt16:
  392. if(sd->wine_callback.SoundCallbackLockedProcessInt16Func)
  393. sd->wine_callback.SoundCallbackLockedProcessInt16Func
  394. ( sd->wine_callback.inst
  395. , sd->audiothread_command_bufferFormat
  396. , sd->audiothread_command_numFrames
  397. , sd->audiothread_command_buffer.buf_int16
  398. , sd->audiothread_command_inputBuffer.buf_int16
  399. );
  400. break;
  401. case AudioThreadCommandReadInt24:
  402. if(sd->wine_callback.SoundCallbackLockedProcessInt24Func)
  403. sd->wine_callback.SoundCallbackLockedProcessInt24Func
  404. ( sd->wine_callback.inst
  405. , sd->audiothread_command_bufferFormat
  406. , sd->audiothread_command_numFrames
  407. , sd->audiothread_command_buffer.buf_int24
  408. , sd->audiothread_command_inputBuffer.buf_int24
  409. );
  410. break;
  411. case AudioThreadCommandReadInt32:
  412. if(sd->wine_callback.SoundCallbackLockedProcessInt32Func)
  413. sd->wine_callback.SoundCallbackLockedProcessInt32Func
  414. ( sd->wine_callback.inst
  415. , sd->audiothread_command_bufferFormat
  416. , sd->audiothread_command_numFrames
  417. , sd->audiothread_command_buffer.buf_int32
  418. , sd->audiothread_command_inputBuffer.buf_int32
  419. );
  420. break;
  421. case AudioThreadCommandReadFloat:
  422. if(sd->wine_callback.SoundCallbackLockedProcessFloatFunc)
  423. sd->wine_callback.SoundCallbackLockedProcessFloatFunc
  424. ( sd->wine_callback.inst
  425. , sd->audiothread_command_bufferFormat
  426. , sd->audiothread_command_numFrames
  427. , sd->audiothread_command_buffer.buf_float
  428. , sd->audiothread_command_inputBuffer.buf_float
  429. );
  430. break;
  431. case AudioThreadCommandReadDouble:
  432. if(sd->wine_callback.SoundCallbackLockedProcessDoubleFunc)
  433. sd->wine_callback.SoundCallbackLockedProcessDoubleFunc
  434. ( sd->wine_callback.inst
  435. , sd->audiothread_command_bufferFormat
  436. , sd->audiothread_command_numFrames
  437. , sd->audiothread_command_buffer.buf_double
  438. , sd->audiothread_command_inputBuffer.buf_double
  439. );
  440. break;
  441. case AudioThreadCommandReadDone:
  442. if(sd->wine_callback.SoundCallbackLockedProcessDoneFunc)
  443. sd->wine_callback.SoundCallbackLockedProcessDoneFunc
  444. ( sd->wine_callback.inst
  445. , sd->audiothread_command_timeInfo
  446. );
  447. break;
  448. case AudioThreadCommandUnlock:
  449. if(sd->wine_callback.SoundCallbackUnlockFunc)
  450. sd->wine_callback.SoundCallbackUnlockFunc( sd->wine_callback.inst );
  451. break;
  452. default:
  453. break;
  454. }
  455. OpenMPT_Semaphore_Post( sd->audiothread_sem_done );
  456. }
  457. }
  458. OpenMPT_PriorityBooster_Destruct(sd->priority_booster);
  459. sd->priority_booster = NULL;
  460. return 0;
  461. }
  462. #endif
  463. OPENMPT_WINESUPPORT_WRAPPER_API OpenMPT_Wine_Wrapper_SoundDevice * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Construct( const char * info ) {
  464. OpenMPT_Wine_Wrapper_SoundDevice * sd = (OpenMPT_Wine_Wrapper_SoundDevice*)OpenMPT_Wine_Wrapper_Alloc( sizeof( OpenMPT_Wine_Wrapper_SoundDevice ) );
  465. sd->impl = OpenMPT_SoundDevice_Construct( info );
  466. return sd;
  467. }
  468. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Destruct( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  469. OpenMPT_SoundDevice_Destruct( sd->impl );
  470. sd->impl = NULL;
  471. OpenMPT_Wine_Wrapper_Free( sd );
  472. }
  473. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_SetMessageReceiver( OpenMPT_Wine_Wrapper_SoundDevice * sd, const OpenMPT_Wine_Wrapper_SoundDevice_IMessageReceiver * receiver ) {
  474. OpenMPT_SoundDevice_SetMessageReceiver( sd->impl, NULL );
  475. ZeroMemory( &( sd->wine_receiver ), sizeof( sd->wine_receiver ) );
  476. if(receiver)
  477. {
  478. sd->wine_receiver = *receiver;
  479. }
  480. sd->native_receiver.inst = sd;
  481. sd->native_receiver.SoundDeviceMessageFunc = &SoundDeviceMessageFunc;
  482. OpenMPT_SoundDevice_SetMessageReceiver( sd->impl, &sd->native_receiver );
  483. }
  484. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_SetCallback( OpenMPT_Wine_Wrapper_SoundDevice * sd, const OpenMPT_Wine_Wrapper_SoundDevice_ICallback * callback ) {
  485. OpenMPT_SoundDevice_SetCallback( sd->impl, NULL );
  486. ZeroMemory( &( sd->wine_callback ), sizeof( sd->wine_callback) );
  487. if(callback)
  488. {
  489. sd->wine_callback = *callback;
  490. }
  491. sd->native_callback.inst = sd;
  492. sd->native_callback.SoundCallbackGetReferenceClockNowNanosecondsFunc = &SoundCallbackGetReferenceClockNowNanosecondsFunc;
  493. sd->native_callback.SoundCallbackPreStartFunc = &SoundCallbackPreStartFunc;
  494. sd->native_callback.SoundCallbackPostStopFunc = &SoundCallbackPostStopFunc;
  495. sd->native_callback.SoundCallbackIsLockedByCurrentThreadFunc = &SoundCallbackIsLockedByCurrentThreadFunc;
  496. sd->native_callback.SoundCallbackLockFunc = &SoundCallbackLockFunc;
  497. sd->native_callback.SoundCallbackLockedGetReferenceClockNowNanosecondsFunc = &SoundCallbackLockedGetReferenceClockNowNanosecondsFunc;
  498. sd->native_callback.SoundCallbackLockedProcessPrepareFunc = &SoundCallbackLockedProcessPrepareFunc;
  499. sd->native_callback.SoundCallbackLockedProcessUint8Func = &SoundCallbackLockedProcessUint8Func;
  500. sd->native_callback.SoundCallbackLockedProcessInt8Func = &SoundCallbackLockedProcessInt8Func;
  501. sd->native_callback.SoundCallbackLockedProcessInt16Func = &SoundCallbackLockedProcessInt16Func;
  502. sd->native_callback.SoundCallbackLockedProcessInt24Func = &SoundCallbackLockedProcessInt24Func;
  503. sd->native_callback.SoundCallbackLockedProcessInt32Func = &SoundCallbackLockedProcessInt32Func;
  504. sd->native_callback.SoundCallbackLockedProcessFloatFunc = &SoundCallbackLockedProcessFloatFunc;
  505. sd->native_callback.SoundCallbackLockedProcessDoubleFunc = &SoundCallbackLockedProcessDoubleFunc;
  506. sd->native_callback.SoundCallbackLockedProcessDoneFunc = &SoundCallbackLockedProcessDoneFunc;
  507. sd->native_callback.SoundCallbackUnlockFunc = &SoundCallbackUnlockFunc;
  508. OpenMPT_SoundDevice_SetCallback( sd->impl, &sd->native_callback );
  509. }
  510. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetDeviceInfo( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  511. return OpenMPT_SoundDevice_GetDeviceInfo( sd->impl );
  512. }
  513. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetDeviceCaps( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  514. return OpenMPT_SoundDevice_GetDeviceCaps( sd->impl );
  515. }
  516. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetDeviceDynamicCaps( OpenMPT_Wine_Wrapper_SoundDevice * sd, const char * baseSampleRates ) {
  517. return OpenMPT_SoundDevice_GetDeviceDynamicCaps( sd->impl, baseSampleRates );
  518. }
  519. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Init( OpenMPT_Wine_Wrapper_SoundDevice * sd, const char * appInfo ) {
  520. return OpenMPT_SoundDevice_Init( sd->impl, appInfo );
  521. }
  522. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Open( OpenMPT_Wine_Wrapper_SoundDevice * sd, const char * settings ) {
  523. uintptr_t result = 0;
  524. result = OpenMPT_SoundDevice_Open( sd->impl, settings );
  525. #ifdef WINE_THREAD
  526. if ( result ) {
  527. DWORD threadId = 0;
  528. sd->audiothread_startup_done = CreateEvent( NULL, FALSE, FALSE, NULL );
  529. sd->audiothread_sem_request = OpenMPT_Semaphore_Construct();
  530. sd->audiothread_sem_done = OpenMPT_Semaphore_Construct();
  531. sd->audiothread_command = AudioThreadCommandInvalid;
  532. sd->audiothread = CreateThread( NULL, 0, &AudioThread, sd, 0, &threadId );
  533. WaitForSingleObject( sd->audiothread_startup_done, INFINITE );
  534. }
  535. #endif
  536. return result;
  537. }
  538. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Close( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  539. uintptr_t result = 0;
  540. #ifdef WINE_THREAD
  541. if ( OpenMPT_SoundDevice_IsOpen( sd->impl ) ) {
  542. sd->audiothread_command = AudioThreadCommandExit;
  543. OpenMPT_Semaphore_Post( sd->audiothread_sem_request );
  544. OpenMPT_Semaphore_Wait( sd->audiothread_sem_done );
  545. sd->audiothread_command = AudioThreadCommandInvalid;
  546. WaitForSingleObject( sd->audiothread, INFINITE );
  547. CloseHandle( sd->audiothread );
  548. sd->audiothread = NULL;
  549. OpenMPT_Semaphore_Destruct( sd->audiothread_sem_done );
  550. sd->audiothread_sem_done = NULL;
  551. OpenMPT_Semaphore_Destruct( sd->audiothread_sem_request );
  552. sd->audiothread_sem_request = NULL;
  553. CloseHandle( sd->audiothread_startup_done );
  554. sd->audiothread_startup_done = NULL;
  555. }
  556. #endif
  557. result = OpenMPT_SoundDevice_Close( sd->impl );
  558. return result;
  559. }
  560. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Start( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  561. return OpenMPT_SoundDevice_Start( sd->impl );
  562. }
  563. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_Stop( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  564. return OpenMPT_SoundDevice_Stop( sd->impl );
  565. }
  566. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetRequestFlags( const OpenMPT_Wine_Wrapper_SoundDevice * sd, uint32_t * result) {
  567. return OpenMPT_SoundDevice_GetRequestFlags( sd->impl, result );
  568. }
  569. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_IsInited( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  570. return OpenMPT_SoundDevice_IsInited( sd->impl );
  571. }
  572. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_IsOpen( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  573. return OpenMPT_SoundDevice_IsOpen( sd->impl );
  574. }
  575. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_IsAvailable( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  576. return OpenMPT_SoundDevice_IsAvailable( sd->impl );
  577. }
  578. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_IsPlaying( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  579. return OpenMPT_SoundDevice_IsPlaying( sd->impl );
  580. }
  581. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_IsPlayingSilence( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  582. return OpenMPT_SoundDevice_IsPlayingSilence( sd->impl );
  583. }
  584. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_StopAndAvoidPlayingSilence( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  585. return OpenMPT_SoundDevice_StopAndAvoidPlayingSilence( sd->impl );
  586. }
  587. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_EndPlayingSilence( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  588. return OpenMPT_SoundDevice_EndPlayingSilence( sd->impl );
  589. }
  590. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_OnIdle( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  591. return OpenMPT_SoundDevice_OnIdle( sd->impl );
  592. }
  593. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetSettings( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  594. return OpenMPT_SoundDevice_GetSettings( sd->impl );
  595. }
  596. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetActualSampleFormat( const OpenMPT_Wine_Wrapper_SoundDevice * sd, int32_t * result ) {
  597. return OpenMPT_SoundDevice_GetActualSampleFormat( sd->impl, result );
  598. }
  599. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetEffectiveBufferAttributes( const OpenMPT_Wine_Wrapper_SoundDevice * sd, OpenMPT_SoundDevice_BufferAttributes * result ) {
  600. return OpenMPT_SoundDevice_GetEffectiveBufferAttributes( sd->impl, result );
  601. }
  602. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetTimeInfo( const OpenMPT_Wine_Wrapper_SoundDevice * sd, OpenMPT_SoundDevice_TimeInfo * result ) {
  603. return OpenMPT_SoundDevice_GetTimeInfo( sd->impl, result );
  604. }
  605. OPENMPT_WINESUPPORT_WRAPPER_API void OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetStreamPosition( const OpenMPT_Wine_Wrapper_SoundDevice * sd, OpenMPT_SoundDevice_StreamPosition * result ) {
  606. return OpenMPT_SoundDevice_GetStreamPosition( sd->impl, result );
  607. }
  608. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_DebugIsFragileDevice( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  609. return OpenMPT_SoundDevice_DebugIsFragileDevice( sd->impl );
  610. }
  611. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_DebugInRealtimeCallback( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  612. return OpenMPT_SoundDevice_DebugInRealtimeCallback( sd->impl );
  613. }
  614. OPENMPT_WINESUPPORT_WRAPPER_API char * OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_GetStatistics( const OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  615. return OpenMPT_SoundDevice_GetStatistics( sd->impl );
  616. }
  617. OPENMPT_WINESUPPORT_WRAPPER_API uintptr_t OPENMPT_WINESUPPORT_WRAPPER_CALL OpenMPT_Wine_Wrapper_SoundDevice_OpenDriverSettings( OpenMPT_Wine_Wrapper_SoundDevice * sd ) {
  618. return OpenMPT_SoundDevice_OpenDriverSettings( sd->impl );
  619. }
  620. #ifdef __cplusplus
  621. } // extern "C"
  622. #endif
  623. #endif // MPT_BUILD_WINESUPPORT_WRAPPER