ASIO to JACK driver for WINE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1710 lines
64KB

  1. /*
  2. * Copyright (C) 2006 Robert Reif
  3. * Portions copyright (C) 2007 Ralf Beck
  4. * Portions copyright (C) 2007 Johnny Petrantoni
  5. * Portions copyright (C) 2007 Stephane Letz
  6. * Portions copyright (C) 2008 William Steidtmann
  7. * Portions copyright (C) 2010 Peter L Jones
  8. * Portions copyright (C) 2010 Torben Hohn
  9. * Portions copyright (C) 2010 Nedko Arnaudov
  10. * Portions copyright (C) 2013 Joakim Hernberg
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <sys/mman.h>
  30. #include <pthread.h>
  31. #ifdef DEBUG
  32. #include "wine/debug.h"
  33. #else
  34. // FIXME convert MESSAGE, WARN and ERR into proper macros with fprintf
  35. #define TRACE(...) {}
  36. #define MESSAGE(...) {}
  37. #define WARN(...) {}
  38. #define ERR(...) {}
  39. #endif
  40. #include "objbase.h"
  41. #include "mmsystem.h"
  42. #include "winreg.h"
  43. #include "wine/unicode.h"
  44. #include <jack/jack.h>
  45. #include <jack/thread.h>
  46. #define IEEE754_64FLOAT 1
  47. #include "asio.h"
  48. #ifdef DEBUG
  49. WINE_DEFAULT_DEBUG_CHANNEL(asio);
  50. #endif
  51. #define MAX_ENVIRONMENT_SIZE 6
  52. #define ASIO_MAX_NAME_LENGTH 32
  53. #define ASIO_MINIMUM_BUFFERSIZE 16
  54. #define ASIO_MAXIMUM_BUFFERSIZE 8192
  55. #define ASIO_PREFERRED_BUFFERSIZE 1024
  56. /* ASIO drivers (breaking the COM specification) use the Microsoft variety of
  57. * thiscall calling convention which gcc is unable to produce. These macros
  58. * add an extra layer to fixup the registers. Borrowed from config.h and the
  59. * wine source code.
  60. */
  61. /* From config.h */
  62. #define __ASM_DEFINE_FUNC(name,suffix,code) asm(".text\n\t.align 4\n\t.globl " #name suffix "\n\t.type " #name suffix ",@function\n" #name suffix ":\n\t.cfi_startproc\n\t" code "\n\t.cfi_endproc\n\t.previous");
  63. #define __ASM_GLOBAL_FUNC(name,code) __ASM_DEFINE_FUNC(name,"",code)
  64. #define __ASM_NAME(name) name
  65. #define __ASM_STDCALL(args) ""
  66. /* From wine source */
  67. #ifdef __i386__ /* thiscall functions are i386-specific */
  68. #define THISCALL(func) __thiscall_ ## func
  69. #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
  70. #define __thiscall __stdcall
  71. #define DEFINE_THISCALL_WRAPPER(func,args) \
  72. extern void THISCALL(func)(void); \
  73. __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
  74. "popl %eax\n\t" \
  75. "pushl %ecx\n\t" \
  76. "pushl %eax\n\t" \
  77. "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
  78. #else /* __i386__ */
  79. #define THISCALL(func) func
  80. #define THISCALL_NAME(func) __ASM_NAME(#func)
  81. #define __thiscall __cdecl
  82. #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
  83. #endif /* __i386__ */
  84. /* Hide ELF symbols for the COM members - No need to to export them */
  85. #define HIDDEN __attribute__ ((visibility("hidden")))
  86. /*****************************************************************************
  87. * IWineAsio interface
  88. */
  89. #define INTERFACE IWineASIO
  90. DECLARE_INTERFACE_(IWineASIO,IUnknown)
  91. {
  92. STDMETHOD_(HRESULT, QueryInterface) (THIS_ IID riid, void** ppvObject) PURE;
  93. STDMETHOD_(ULONG, AddRef) (THIS) PURE;
  94. STDMETHOD_(ULONG, Release) (THIS) PURE;
  95. STDMETHOD_(ASIOBool, Init) (THIS_ void *sysRef) PURE;
  96. STDMETHOD_(void, GetDriverName) (THIS_ char *name) PURE;
  97. STDMETHOD_(LONG, GetDriverVersion) (THIS) PURE;
  98. STDMETHOD_(void, GetErrorMessage) (THIS_ char *string) PURE;
  99. STDMETHOD_(ASIOError, Start) (THIS) PURE;
  100. STDMETHOD_(ASIOError, Stop) (THIS) PURE;
  101. STDMETHOD_(ASIOError, GetChannels) (THIS_ LONG *numInputChannels, LONG *numOutputChannels) PURE;
  102. STDMETHOD_(ASIOError, GetLatencies) (THIS_ LONG *inputLatency, LONG *outputLatency) PURE;
  103. STDMETHOD_(ASIOError, GetBufferSize) (THIS_ LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity) PURE;
  104. STDMETHOD_(ASIOError, CanSampleRate) (THIS_ ASIOSampleRate sampleRate) PURE;
  105. STDMETHOD_(ASIOError, GetSampleRate) (THIS_ ASIOSampleRate *sampleRate) PURE;
  106. STDMETHOD_(ASIOError, SetSampleRate) (THIS_ ASIOSampleRate sampleRate) PURE;
  107. STDMETHOD_(ASIOError, GetClockSources) (THIS_ ASIOClockSource *clocks, LONG *numSources) PURE;
  108. STDMETHOD_(ASIOError, SetClockSource) (THIS_ LONG index) PURE;
  109. STDMETHOD_(ASIOError, GetSamplePosition) (THIS_ ASIOSamples *sPos, ASIOTimeStamp *tStamp) PURE;
  110. STDMETHOD_(ASIOError, GetChannelInfo) (THIS_ ASIOChannelInfo *info) PURE;
  111. STDMETHOD_(ASIOError, CreateBuffers) (THIS_ ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks) PURE;
  112. STDMETHOD_(ASIOError, DisposeBuffers) (THIS) PURE;
  113. STDMETHOD_(ASIOError, ControlPanel) (THIS) PURE;
  114. STDMETHOD_(ASIOError, Future) (THIS_ LONG selector,void *opt) PURE;
  115. STDMETHOD_(ASIOError, OutputReady) (THIS) PURE;
  116. };
  117. #undef INTERFACE
  118. typedef struct IWineASIO *LPWINEASIO;
  119. typedef struct IOChannel
  120. {
  121. ASIOBool active;
  122. jack_default_audio_sample_t *audio_buffer;
  123. char port_name[ASIO_MAX_NAME_LENGTH];
  124. jack_port_t *port;
  125. } IOChannel;
  126. typedef struct IWineASIOImpl
  127. {
  128. /* COM stuff */
  129. const IWineASIOVtbl *lpVtbl;
  130. LONG ref;
  131. /* The app's main window handle on windows, 0 on OS/X */
  132. HWND sys_ref;
  133. /* ASIO stuff */
  134. LONG asio_active_inputs;
  135. LONG asio_active_outputs;
  136. BOOL asio_buffer_index;
  137. ASIOCallbacks *asio_callbacks;
  138. BOOL asio_can_time_code;
  139. LONG asio_current_buffersize;
  140. INT asio_driver_state;
  141. ASIOSamples asio_sample_position;
  142. ASIOSampleRate asio_sample_rate;
  143. ASIOTime asio_time;
  144. BOOL asio_time_info_mode;
  145. ASIOTimeStamp asio_time_stamp;
  146. LONG asio_version;
  147. /* WineASIO configuration options */
  148. LONG wineasio_number_inputs;
  149. LONG wineasio_number_outputs;
  150. BOOL wineasio_autostart_server;
  151. BOOL wineasio_connect_to_hardware;
  152. LONG wineasio_fixed_buffersize;
  153. LONG wineasio_preferred_buffersize;
  154. /* JACK stuff */
  155. jack_client_t *jack_client;
  156. char jack_client_name[ASIO_MAX_NAME_LENGTH];
  157. int jack_num_input_ports;
  158. int jack_num_output_ports;
  159. const char **jack_input_ports;
  160. const char **jack_output_ports;
  161. /* process callback buffers */
  162. jack_default_audio_sample_t *callback_audio_buffer;
  163. IOChannel *input_channel;
  164. IOChannel *output_channel;
  165. } IWineASIOImpl;
  166. enum { Loaded, Initialized, Prepared, Running };
  167. /****************************************************************************
  168. * Interface Methods
  169. */
  170. /*
  171. * as seen from the WineASIO source
  172. */
  173. HIDDEN HRESULT STDMETHODCALLTYPE QueryInterface(LPWINEASIO iface, REFIID riid, void **ppvObject);
  174. HIDDEN ULONG STDMETHODCALLTYPE AddRef(LPWINEASIO iface);
  175. HIDDEN ULONG STDMETHODCALLTYPE Release(LPWINEASIO iface);
  176. HIDDEN ASIOBool STDMETHODCALLTYPE Init(LPWINEASIO iface, void *sysRef);
  177. HIDDEN void STDMETHODCALLTYPE GetDriverName(LPWINEASIO iface, char *name);
  178. HIDDEN LONG STDMETHODCALLTYPE GetDriverVersion(LPWINEASIO iface);
  179. HIDDEN void STDMETHODCALLTYPE GetErrorMessage(LPWINEASIO iface, char *string);
  180. HIDDEN ASIOError STDMETHODCALLTYPE Start(LPWINEASIO iface);
  181. HIDDEN ASIOError STDMETHODCALLTYPE Stop(LPWINEASIO iface);
  182. HIDDEN ASIOError STDMETHODCALLTYPE GetChannels (LPWINEASIO iface, LONG *numInputChannels, LONG *numOutputChannels);
  183. HIDDEN ASIOError STDMETHODCALLTYPE GetLatencies(LPWINEASIO iface, LONG *inputLatency, LONG *outputLatency);
  184. HIDDEN ASIOError STDMETHODCALLTYPE GetBufferSize(LPWINEASIO iface, LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity);
  185. HIDDEN ASIOError STDMETHODCALLTYPE CanSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate);
  186. HIDDEN ASIOError STDMETHODCALLTYPE GetSampleRate(LPWINEASIO iface, ASIOSampleRate *sampleRate);
  187. HIDDEN ASIOError STDMETHODCALLTYPE SetSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate);
  188. HIDDEN ASIOError STDMETHODCALLTYPE GetClockSources(LPWINEASIO iface, ASIOClockSource *clocks, LONG *numSources);
  189. HIDDEN ASIOError STDMETHODCALLTYPE SetClockSource(LPWINEASIO iface, LONG index);
  190. HIDDEN ASIOError STDMETHODCALLTYPE GetSamplePosition(LPWINEASIO iface, ASIOSamples *sPos, ASIOTimeStamp *tStamp);
  191. HIDDEN ASIOError STDMETHODCALLTYPE GetChannelInfo(LPWINEASIO iface, ASIOChannelInfo *info);
  192. HIDDEN ASIOError STDMETHODCALLTYPE CreateBuffers(LPWINEASIO iface, ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks);
  193. HIDDEN ASIOError STDMETHODCALLTYPE DisposeBuffers(LPWINEASIO iface);
  194. HIDDEN ASIOError STDMETHODCALLTYPE ControlPanel(LPWINEASIO iface);
  195. HIDDEN ASIOError STDMETHODCALLTYPE Future(LPWINEASIO iface, LONG selector, void *opt);
  196. HIDDEN ASIOError STDMETHODCALLTYPE OutputReady(LPWINEASIO iface);
  197. /*
  198. * thiscall wrappers for the vtbl (as seen from app side 32bit)
  199. */
  200. HIDDEN void __thiscall_Init(void);
  201. HIDDEN void __thiscall_GetDriverName(void);
  202. HIDDEN void __thiscall_GetDriverVersion(void);
  203. HIDDEN void __thiscall_GetErrorMessage(void);
  204. HIDDEN void __thiscall_Start(void);
  205. HIDDEN void __thiscall_Stop(void);
  206. HIDDEN void __thiscall_GetChannels(void);
  207. HIDDEN void __thiscall_GetLatencies(void);
  208. HIDDEN void __thiscall_GetBufferSize(void);
  209. HIDDEN void __thiscall_CanSampleRate(void);
  210. HIDDEN void __thiscall_GetSampleRate(void);
  211. HIDDEN void __thiscall_SetSampleRate(void);
  212. HIDDEN void __thiscall_GetClockSources(void);
  213. HIDDEN void __thiscall_SetClockSource(void);
  214. HIDDEN void __thiscall_GetSamplePosition(void);
  215. HIDDEN void __thiscall_GetChannelInfo(void);
  216. HIDDEN void __thiscall_CreateBuffers(void);
  217. HIDDEN void __thiscall_DisposeBuffers(void);
  218. HIDDEN void __thiscall_ControlPanel(void);
  219. HIDDEN void __thiscall_Future(void);
  220. HIDDEN void __thiscall_OutputReady(void);
  221. /*
  222. * Jack callbacks
  223. */
  224. static int bufsize_callback (jack_nframes_t nframes, void *arg);
  225. static int process_callback (jack_nframes_t nframes, void *arg);
  226. static int srate_callback (jack_nframes_t nframes, void *arg);
  227. /*
  228. * Support functions
  229. */
  230. HRESULT WINAPI WineASIOCreateInstance(REFIID riid, LPVOID *ppobj);
  231. static BOOL configure_driver(IWineASIOImpl *This);
  232. static DWORD WINAPI jack_thread_creator_helper(LPVOID arg);
  233. static int jack_thread_creator(pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg);
  234. /* {48D0C522-BFCC-45cc-8B84-17F25F33E6E8} */
  235. static GUID const CLSID_WineASIO = {
  236. 0x48d0c522, 0xbfcc, 0x45cc, { 0x8b, 0x84, 0x17, 0xf2, 0x5f, 0x33, 0xe6, 0xe8 } };
  237. static const IWineASIOVtbl WineASIO_Vtbl =
  238. {
  239. (void *) QueryInterface,
  240. (void *) AddRef,
  241. (void *) Release,
  242. (void *) THISCALL(Init),
  243. (void *) THISCALL(GetDriverName),
  244. (void *) THISCALL(GetDriverVersion),
  245. (void *) THISCALL(GetErrorMessage),
  246. (void *) THISCALL(Start),
  247. (void *) THISCALL(Stop),
  248. (void *) THISCALL(GetChannels),
  249. (void *) THISCALL(GetLatencies),
  250. (void *) THISCALL(GetBufferSize),
  251. (void *) THISCALL(CanSampleRate),
  252. (void *) THISCALL(GetSampleRate),
  253. (void *) THISCALL(SetSampleRate),
  254. (void *) THISCALL(GetClockSources),
  255. (void *) THISCALL(SetClockSource),
  256. (void *) THISCALL(GetSamplePosition),
  257. (void *) THISCALL(GetChannelInfo),
  258. (void *) THISCALL(CreateBuffers),
  259. (void *) THISCALL(DisposeBuffers),
  260. (void *) THISCALL(ControlPanel),
  261. (void *) THISCALL(Future),
  262. (void *) THISCALL(OutputReady)
  263. };
  264. /* structure needed to create the JACK callback thread in the wine process context */
  265. struct {
  266. void *(*jack_callback_thread) (void*);
  267. void *arg;
  268. pthread_t jack_callback_pthread_id;
  269. HANDLE jack_callback_thread_created;
  270. } jack_thread_creator_privates;
  271. /*****************************************************************************
  272. * Interface method definitions
  273. */
  274. HIDDEN HRESULT STDMETHODCALLTYPE QueryInterface(LPWINEASIO iface, REFIID riid, void **ppvObject)
  275. {
  276. IWineASIOImpl *This = (IWineASIOImpl *)iface;
  277. TRACE("iface: %p, riid: %s, ppvObject: %p)\n", iface, debugstr_guid(riid), ppvObject);
  278. if (ppvObject == NULL)
  279. return E_INVALIDARG;
  280. if (IsEqualIID(&CLSID_WineASIO, riid))
  281. {
  282. AddRef(iface);
  283. *ppvObject = This;
  284. return S_OK;
  285. }
  286. return E_NOINTERFACE;
  287. }
  288. /*
  289. * ULONG STDMETHODCALLTYPE AddRef(LPWINEASIO iface);
  290. * Function: Increment the reference count on the object
  291. * Returns: Ref count
  292. */
  293. HIDDEN ULONG STDMETHODCALLTYPE AddRef(LPWINEASIO iface)
  294. {
  295. IWineASIOImpl *This = (IWineASIOImpl *)iface;
  296. ULONG ref = InterlockedIncrement(&(This->ref));
  297. TRACE("iface: %p, ref count is %d\n", iface, ref);
  298. return ref;
  299. }
  300. /*
  301. * ULONG Release (LPWINEASIO iface);
  302. * Function: Destroy the interface
  303. * Returns: Ref count
  304. * Implies: ASIOStop() and ASIODisposeBuffers()
  305. */
  306. HIDDEN ULONG STDMETHODCALLTYPE Release(LPWINEASIO iface)
  307. {
  308. IWineASIOImpl *This = (IWineASIOImpl *)iface;
  309. ULONG ref = InterlockedDecrement(&This->ref);
  310. int i;
  311. TRACE("iface: %p, ref count is %d\n", iface, ref);
  312. if (This->asio_driver_state == Running)
  313. Stop(iface);
  314. if (This->asio_driver_state == Prepared)
  315. DisposeBuffers(iface);
  316. if (This->asio_driver_state == Initialized)
  317. {
  318. /* just for good measure we deinitialize IOChannel structures and unregister JACK ports */
  319. for (i = 0; i < This->wineasio_number_inputs; i++)
  320. {
  321. if(jack_port_unregister (This->jack_client, This->input_channel[i].port))
  322. MESSAGE("Error trying to unregister port %s\n", This->input_channel[i].port_name);
  323. This->input_channel[i].active = ASIOFalse;
  324. This->input_channel[i].port = NULL;
  325. }
  326. for (i = 0; i < This->wineasio_number_outputs; i++)
  327. {
  328. if(jack_port_unregister (This->jack_client, This->output_channel[i].port))
  329. MESSAGE("Error trying to unregister port %s\n", This->output_channel[i].port_name);
  330. This->output_channel[i].active = ASIOFalse;
  331. This->output_channel[i].port = NULL;
  332. }
  333. This->asio_active_inputs = This->asio_active_outputs = 0;
  334. TRACE("%i IOChannel structures released\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
  335. if (This->jack_output_ports)
  336. jack_free (This->jack_output_ports);
  337. if (This->jack_input_ports)
  338. jack_free (This->jack_input_ports);
  339. if (This->jack_client)
  340. if (jack_client_close(This->jack_client))
  341. MESSAGE("Error trying to close JACK client\n");
  342. if (This->input_channel)
  343. HeapFree(GetProcessHeap(), 0, This->input_channel);
  344. }
  345. TRACE("WineASIO terminated\n\n");
  346. if (ref == 0)
  347. HeapFree(GetProcessHeap(), 0, This);
  348. return ref;
  349. }
  350. /*
  351. * ASIOBool Init (void *sysRef);
  352. * Function: Initialize the driver
  353. * Parameters: Pointer to "This"
  354. * sysHanle is 0 on OS/X and on windows it contains the applications main window handle
  355. * Returns: ASIOFalse on error, and ASIOTrue on success
  356. */
  357. DEFINE_THISCALL_WRAPPER(Init,8)
  358. HIDDEN ASIOBool STDMETHODCALLTYPE Init(LPWINEASIO iface, void *sysRef)
  359. {
  360. IWineASIOImpl *This = (IWineASIOImpl *)iface;
  361. jack_status_t jack_status;
  362. jack_options_t jack_options = JackNullOption;
  363. int i;
  364. TRACE("iface: %p, sysRef: %p\n", iface, sysRef);
  365. This->sys_ref = sysRef;
  366. mlockall(MCL_FUTURE);
  367. if (!configure_driver(This))
  368. {
  369. WARN("Unable to configure WineASIO\n");
  370. return ASIOFalse;
  371. }
  372. if (!This->wineasio_autostart_server)
  373. jack_options |= JackNoStartServer;
  374. This->jack_client = jack_client_open(This->jack_client_name, jack_options, &jack_status);
  375. if (This->jack_client == NULL)
  376. {
  377. WARN("Unable to open a JACK client as: %s\n", This->jack_client_name);
  378. return ASIOFalse;
  379. }
  380. TRACE("JACK client opened as: '%s'\n", jack_get_client_name(This->jack_client));
  381. if (!(This->asio_sample_rate = jack_get_sample_rate(This->jack_client)))
  382. {
  383. WARN("Unable to get samplerate from JACK\n");
  384. return ASIOFalse;
  385. }
  386. if (!(This->asio_current_buffersize = jack_get_buffer_size(This->jack_client)))
  387. {
  388. WARN("Unable to get buffer size from JACK\n");
  389. return ASIOFalse;
  390. }
  391. /* Allocate IOChannel structures */
  392. This->input_channel = HeapAlloc(GetProcessHeap(), 0, (This->wineasio_number_inputs + This->wineasio_number_outputs) * sizeof(IOChannel));
  393. if (!This->input_channel)
  394. {
  395. jack_client_close(This->jack_client);
  396. ERR("Unable to allocate IOChannel structures for %i channels\n", This->wineasio_number_inputs);
  397. return ASIOFalse;
  398. }
  399. This->output_channel = This->input_channel + This->wineasio_number_inputs;
  400. TRACE("%i IOChannel structures allocated\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
  401. /* Get and count physical JACK ports */
  402. This->jack_input_ports = jack_get_ports(This->jack_client, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  403. for (This->jack_num_input_ports = 0; This->jack_input_ports && This->jack_input_ports[This->jack_num_input_ports]; This->jack_num_input_ports++)
  404. ;
  405. This->jack_output_ports = jack_get_ports(This->jack_client, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  406. for (This->jack_num_output_ports = 0; This->jack_output_ports && This->jack_output_ports[This->jack_num_output_ports]; This->jack_num_output_ports++)
  407. ;
  408. /* Initialize IOChannel structures */
  409. for (i = 0; i < This->wineasio_number_inputs; i++)
  410. {
  411. This->input_channel[i].active = ASIOFalse;
  412. This->input_channel[i].port = NULL;
  413. snprintf(This->input_channel[i].port_name, ASIO_MAX_NAME_LENGTH, "in_%i", i + 1);
  414. This->input_channel[i].port = jack_port_register(This->jack_client,
  415. This->input_channel[i].port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, i);
  416. /* TRACE("IOChannel structure initialized for input %d: '%s'\n", i, This->input_channel[i].port_name); */
  417. }
  418. for (i = 0; i < This->wineasio_number_outputs; i++)
  419. {
  420. This->output_channel[i].active = ASIOFalse;
  421. This->output_channel[i].port = NULL;
  422. snprintf(This->output_channel[i].port_name, ASIO_MAX_NAME_LENGTH, "out_%i", i + 1);
  423. This->output_channel[i].port = jack_port_register(This->jack_client,
  424. This->output_channel[i].port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, i);
  425. /* TRACE("IOChannel structure initialized for output %d: '%s'\n", i, This->output_channel[i].port_name); */
  426. }
  427. TRACE("%i IOChannel structures initialized\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
  428. jack_set_thread_creator(jack_thread_creator);
  429. if (jack_set_process_callback(This->jack_client, process_callback, This))
  430. {
  431. jack_client_close(This->jack_client);
  432. HeapFree(GetProcessHeap(), 0, This->input_channel);
  433. ERR("Unable to register JACK process callback\n");
  434. return ASIOFalse;
  435. }
  436. if (jack_set_buffer_size_callback(This->jack_client, bufsize_callback, This))
  437. {
  438. jack_client_close(This->jack_client);
  439. HeapFree(GetProcessHeap(), 0, This->input_channel);
  440. ERR("Unable to register JACK buffersize change callback\n");
  441. return ASIOFalse;
  442. }
  443. if (jack_set_sample_rate_callback (This->jack_client, srate_callback, This))
  444. {
  445. jack_client_close(This->jack_client);
  446. HeapFree(GetProcessHeap(), 0, This->input_channel);
  447. ERR("Unable to register JACK samplerate change callback\n");
  448. return ASIOFalse;
  449. }
  450. This->asio_driver_state = Initialized;
  451. TRACE("WineASIO 0.%.1f initialized\n",(float) This->asio_version / 10);
  452. return ASIOTrue;
  453. }
  454. /*
  455. * void GetDriverName(char *name);
  456. * Function: Returns the driver name in name
  457. */
  458. DEFINE_THISCALL_WRAPPER(GetDriverName,8)
  459. HIDDEN void STDMETHODCALLTYPE GetDriverName(LPWINEASIO iface, char *name)
  460. {
  461. TRACE("iface: %p, name: %p\n", iface, name);
  462. strcpy(name, "WineASIO");
  463. return;
  464. }
  465. /*
  466. * LONG GetDriverVersion (void);
  467. * Function: Returns the driver version number
  468. */
  469. DEFINE_THISCALL_WRAPPER(GetDriverVersion,4)
  470. HIDDEN LONG STDMETHODCALLTYPE GetDriverVersion(LPWINEASIO iface)
  471. {
  472. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  473. TRACE("iface: %p\n", iface);
  474. return This->asio_version;
  475. }
  476. /*
  477. * void GetErrorMessage(char *string);
  478. * Function: Returns an error message for the last occured error in string
  479. */
  480. DEFINE_THISCALL_WRAPPER(GetErrorMessage,8)
  481. HIDDEN void STDMETHODCALLTYPE GetErrorMessage(LPWINEASIO iface, char *string)
  482. {
  483. TRACE("iface: %p, string: %p)\n", iface, string);
  484. strcpy(string, "WineASIO does not return error messages\n");
  485. return;
  486. }
  487. /*
  488. * ASIOError Start(void);
  489. * Function: Start JACK IO processing and reset the sample counter to zero
  490. * Returns: ASE_NotPresent if IO is missing
  491. * ASE_HWMalfunction if JACK fails to start
  492. */
  493. DEFINE_THISCALL_WRAPPER(Start,4)
  494. HIDDEN ASIOError STDMETHODCALLTYPE Start(LPWINEASIO iface)
  495. {
  496. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  497. int i;
  498. #ifndef _WIN64
  499. DWORD temp_time;
  500. #endif
  501. TRACE("iface: %p\n", iface);
  502. if (This->asio_driver_state != Prepared)
  503. {
  504. ERR("Unable to start WineASIO\n");
  505. return ASE_NotPresent;
  506. }
  507. /* Zero the audio buffer */
  508. for (i = 0; i < (This->wineasio_number_inputs + This->wineasio_number_outputs) * 2 * This->asio_current_buffersize; i++)
  509. This->callback_audio_buffer[i] = 0;
  510. /* prime the callback */
  511. This->asio_buffer_index = 0;
  512. if (This->asio_callbacks)
  513. {
  514. #ifdef _WIN64
  515. This->asio_sample_position = 0;
  516. This->asio_time_stamp = timeGetTime() * 1000000;
  517. #else
  518. This->asio_sample_position.hi = This->asio_sample_position.lo = 0;
  519. temp_time = timeGetTime();
  520. This->asio_time_stamp.lo = temp_time * 1000000;
  521. This->asio_time_stamp.hi = ((unsigned long long) temp_time * 1000000) >> 32;
  522. #endif
  523. This->asio_time_info_mode = FALSE;
  524. This->asio_can_time_code = FALSE;
  525. if (This->asio_callbacks->asioMessage(kAsioSupportsTimeInfo, 0, 0, 0))
  526. {
  527. TRACE("TimeInfo mode enabled\n");
  528. This->asio_time_info_mode = TRUE;
  529. #ifdef _WIN64
  530. This->asio_time.timeInfo.systemTime = This->asio_time_stamp;
  531. This->asio_time.timeInfo.samplePosition = 0;
  532. #else
  533. This->asio_time.timeInfo.systemTime.hi = This->asio_time_stamp.hi;
  534. This->asio_time.timeInfo.systemTime.lo = This->asio_time_stamp.lo;
  535. This->asio_time.timeInfo.samplePosition.hi = This->asio_time.timeInfo.samplePosition.lo = 0;
  536. #endif
  537. This->asio_time.timeCode.speed = 0;
  538. This->asio_time.timeInfo.sampleRate = This->asio_sample_rate;
  539. This->asio_time.timeInfo.flags = kSystemTimeValid | kSamplePositionValid | kSampleRateValid;
  540. if (This->asio_callbacks->asioMessage(kAsioSupportsTimeCode, 0, 0, 0))
  541. {
  542. TRACE("TimeCode supported\n");
  543. This->asio_can_time_code = TRUE;
  544. #ifdef _WIN64
  545. This->asio_time.timeCode.timeCodeSamples = This->asio_time_stamp;
  546. #else
  547. This->asio_time.timeCode.timeCodeSamples.hi = This->asio_time_stamp.hi;
  548. This->asio_time.timeCode.timeCodeSamples.lo = This->asio_time_stamp.lo;
  549. #endif
  550. This->asio_time.timeCode.flags = ~(kTcValid | kTcRunning);
  551. }
  552. This->asio_callbacks->bufferSwitchTimeInfo(&This->asio_time, This->asio_buffer_index, ASIOTrue);
  553. }
  554. else
  555. {
  556. TRACE("Runnning simple BufferSwitch() callback\n");
  557. This->asio_callbacks->bufferSwitch(This->asio_buffer_index, ASIOTrue);
  558. }
  559. This->asio_buffer_index = This->asio_buffer_index ? 0 : 1;
  560. }
  561. else
  562. {
  563. WARN("The ASIO host supplied no callback structure\n");
  564. return ASE_NotPresent;
  565. }
  566. if (jack_activate(This->jack_client))
  567. {
  568. ERR("Unable to activate JACK client\n");
  569. return ASE_NotPresent;
  570. }
  571. /* connect to the hardware io */
  572. if (This->wineasio_connect_to_hardware)
  573. {
  574. for (i = 0; i < This->jack_num_input_ports && i < This->wineasio_number_inputs; i++)
  575. {
  576. /* TRACE("Connecting JACK port: %s to asio: %s\n", This->jack_input_ports[i], jack_port_name(This->input_channel[i].port)); */
  577. if (strstr(jack_port_type(jack_port_by_name(This->jack_client, This->jack_input_ports[i])), "audio"))
  578. if (jack_connect(This->jack_client, This->jack_input_ports[i], jack_port_name(This->input_channel[i].port)))
  579. WARN("Unable to connect %s to %s\n", This->jack_input_ports[i], jack_port_name(This->input_channel[i].port));
  580. }
  581. for (i = 0; i < This->jack_num_output_ports && i < This->wineasio_number_outputs; i++)
  582. {
  583. /* TRACE("Connecting asio: %s to jack port: %s\n", jack_port_name(This->output_channel[i].port), This->jack_output_ports[i]); */
  584. if (strstr(jack_port_type(jack_port_by_name(This->jack_client, This->jack_output_ports[i])), "audio"))
  585. if (jack_connect(This->jack_client, jack_port_name(This->output_channel[i].port), This->jack_output_ports[i]))
  586. WARN("Unable to connect to %s\n", jack_port_name(This->output_channel[i].port));
  587. }
  588. }
  589. This->asio_driver_state = Running;
  590. TRACE("WineASIO successfully loaded\n");
  591. return ASE_OK;
  592. }
  593. /*
  594. * ASIOError Stop(void);
  595. * Function: Stop JACK IO processing
  596. * Returns: ASE_NotPresent on missing IO
  597. * Note: BufferSwitch() must not called after returning
  598. */
  599. DEFINE_THISCALL_WRAPPER(Stop,4)
  600. HIDDEN ASIOError STDMETHODCALLTYPE Stop(LPWINEASIO iface)
  601. {
  602. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  603. TRACE("iface: %p\n", iface);
  604. if (This->asio_driver_state != Running)
  605. {
  606. WARN("Unable to stop WineASIO, not running\n");
  607. return ASE_NotPresent;
  608. }
  609. This->asio_driver_state = Prepared;
  610. if (jack_deactivate(This->jack_client))
  611. {
  612. ERR("Unable to deactivate JACK client\n");
  613. return ASE_NotPresent;
  614. }
  615. return ASE_OK;
  616. }
  617. /*
  618. * ASIOError GetChannels(LONG *numInputChannels, LONG *numOutputChannels);
  619. * Function: Report number of IO channels
  620. * Parameters: numInputChannels and numOutputChannels will hold number of channels on returning
  621. * Returns: ASE_NotPresent if no channels are available, otherwise AES_OK
  622. */
  623. DEFINE_THISCALL_WRAPPER(GetChannels,12)
  624. HIDDEN ASIOError STDMETHODCALLTYPE GetChannels (LPWINEASIO iface, LONG *numInputChannels, LONG *numOutputChannels)
  625. {
  626. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  627. if (!numInputChannels && !numOutputChannels)
  628. {
  629. WARN("Nullpointer argument\n");
  630. return ASE_InvalidParameter;
  631. }
  632. *numInputChannels = This->wineasio_number_inputs;
  633. *numOutputChannels = This->wineasio_number_outputs;
  634. TRACE("iface: %p, inputs: %i, outputs: %i\n", iface, This->wineasio_number_inputs, This->wineasio_number_outputs);
  635. return ASE_OK;
  636. }
  637. /*
  638. * ASIOError GetLatencies(LONG *inputLatency, LONG *outputLatency);
  639. * Function: Return latency in frames
  640. * Returns: ASE_NotPresent if no IO is available, otherwise AES_OK
  641. */
  642. DEFINE_THISCALL_WRAPPER(GetLatencies,12)
  643. HIDDEN ASIOError STDMETHODCALLTYPE GetLatencies(LPWINEASIO iface, LONG *inputLatency, LONG *outputLatency)
  644. {
  645. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  646. if (!inputLatency && !outputLatency)
  647. {
  648. WARN("Nullpointer argument\n");
  649. return ASE_InvalidParameter;
  650. }
  651. *inputLatency = *outputLatency = This->asio_current_buffersize;
  652. TRACE("iface: %p Latency = %i frames\n", iface, This->asio_current_buffersize);
  653. return ASE_OK;
  654. }
  655. /*
  656. * ASIOError GetBufferSize(LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity);
  657. * Function: Return minimum, maximum, preferred buffer sizes, and granularity
  658. * At the moment return all the same, and granularity 0
  659. * Returns: ASE_NotPresent on missing IO
  660. */
  661. DEFINE_THISCALL_WRAPPER(GetBufferSize,20)
  662. HIDDEN ASIOError STDMETHODCALLTYPE GetBufferSize(LPWINEASIO iface, LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity)
  663. {
  664. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  665. TRACE("iface: %p, minSize: %p, maxSize: %p, preferredSize: %p, granularity: %p\n", iface, minSize, maxSize, preferredSize, granularity);
  666. if (!minSize && !maxSize && !preferredSize && !granularity)
  667. {
  668. WARN("Nullpointer argument\n");
  669. return ASE_InvalidParameter;
  670. }
  671. if (This->wineasio_fixed_buffersize)
  672. {
  673. *minSize = *maxSize = *preferredSize = This->asio_current_buffersize;
  674. *granularity = 0;
  675. TRACE("Buffersize fixed at %i\n", This->asio_current_buffersize);
  676. return ASE_OK;
  677. }
  678. *minSize = ASIO_MINIMUM_BUFFERSIZE;
  679. *maxSize = ASIO_MAXIMUM_BUFFERSIZE;
  680. *preferredSize = This->wineasio_preferred_buffersize;
  681. *granularity = -1;
  682. TRACE("The ASIO host can control buffersize\nMinimum: %i, maximum: %i, preferred: %i, granularity: %i, current: %i\n",
  683. *minSize, *maxSize, *preferredSize, *granularity, This->asio_current_buffersize);
  684. return ASE_OK;
  685. }
  686. /*
  687. * ASIOError CanSampleRate(ASIOSampleRate sampleRate);
  688. * Function: Ask if specific SR is available
  689. * Returns: ASE_NoClock if SR isn't available, ASE_NotPresent on missing IO
  690. */
  691. DEFINE_THISCALL_WRAPPER(CanSampleRate,12)
  692. HIDDEN ASIOError STDMETHODCALLTYPE CanSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate)
  693. {
  694. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  695. TRACE("iface: %p, Samplerate = %li, requested samplerate = %li\n", iface, (long) This->asio_sample_rate, (long) sampleRate);
  696. if (sampleRate != This->asio_sample_rate)
  697. return ASE_NoClock;
  698. return ASE_OK;
  699. }
  700. /*
  701. * ASIOError GetSampleRate(ASIOSampleRate *currentRate);
  702. * Function: Return current SR
  703. * Parameters: currentRate will hold SR on return, 0 if unknown
  704. * Returns: ASE_NoClock if SR is unknown, ASE_NotPresent on missing IO
  705. */
  706. DEFINE_THISCALL_WRAPPER(GetSampleRate,8)
  707. HIDDEN ASIOError STDMETHODCALLTYPE GetSampleRate(LPWINEASIO iface, ASIOSampleRate *sampleRate)
  708. {
  709. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  710. TRACE("iface: %p, Sample rate is %i\n", iface, (int) This->asio_sample_rate);
  711. if (!sampleRate)
  712. {
  713. WARN("Nullpointer argument\n");
  714. return ASE_InvalidParameter;
  715. }
  716. *sampleRate = This->asio_sample_rate;
  717. return ASE_OK;
  718. }
  719. /*
  720. * ASIOError SetSampleRate(ASIOSampleRate sampleRate);
  721. * Function: Set requested SR, enable external sync if SR == 0
  722. * Returns: ASE_NoClock if unknown SR
  723. * ASE_InvalidMode if current clock is external and SR != 0
  724. * ASE_NotPresent on missing IO
  725. */
  726. DEFINE_THISCALL_WRAPPER(SetSampleRate,12)
  727. HIDDEN ASIOError STDMETHODCALLTYPE SetSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate)
  728. {
  729. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  730. TRACE("iface: %p, Sample rate %f requested\n", iface, sampleRate);
  731. if (sampleRate != This->asio_sample_rate)
  732. return ASE_NoClock;
  733. return ASE_OK;
  734. }
  735. /*
  736. * ASIOError GetClockSources(ASIOClockSource *clocks, LONG *numSources);
  737. * Function: Return available clock sources
  738. * Parameters: clocks - a pointer to an array of ASIOClockSource structures.
  739. * numSources - when called: number of allocated members
  740. * - on return: number of clock sources, the minimum is 1 - the internal clock
  741. * Returns: ASE_NotPresent on missing IO
  742. */
  743. DEFINE_THISCALL_WRAPPER(GetClockSources,12)
  744. HIDDEN ASIOError STDMETHODCALLTYPE GetClockSources(LPWINEASIO iface, ASIOClockSource *clocks, LONG *numSources)
  745. {
  746. TRACE("iface: %p, clocks: %p, numSources: %p\n", iface, clocks, numSources);
  747. if (!clocks && !numSources)
  748. {
  749. WARN("Nullpointer argument\n");
  750. return ASE_InvalidParameter;
  751. }
  752. clocks->index = 0;
  753. clocks->associatedChannel = -1;
  754. clocks->associatedGroup = -1;
  755. clocks->isCurrentSource = ASIOTrue;
  756. strcpy(clocks->name, "Internal");
  757. *numSources = 1;
  758. return ASE_OK;
  759. }
  760. /*
  761. * ASIOError SetClockSource(LONG index);
  762. * Function: Set clock source
  763. * Parameters: index returned by ASIOGetClockSources() - See asio.h for more details
  764. * Returns: ASE_NotPresent on missing IO
  765. * ASE_InvalidMode may be returned if a clock can't be selected
  766. * ASE_NoClock should not be returned
  767. */
  768. DEFINE_THISCALL_WRAPPER(SetClockSource,8)
  769. HIDDEN ASIOError STDMETHODCALLTYPE SetClockSource(LPWINEASIO iface, LONG index)
  770. {
  771. TRACE("iface: %p, index: %i\n", iface, index);
  772. if (index != 0)
  773. return ASE_NotPresent;
  774. return ASE_OK;
  775. }
  776. /*
  777. * ASIOError GetSamplePosition (ASIOSamples *sPos, ASIOTimeStamp *tStamp);
  778. * Function: Return sample position and timestamp
  779. * Parameters: sPos holds the position on return, reset to 0 on ASIOStart()
  780. * tStamp holds the system time of sPos
  781. * Return: ASE_NotPresent on missing IO
  782. * ASE_SPNotAdvancing on missing clock
  783. */
  784. DEFINE_THISCALL_WRAPPER(GetSamplePosition,12)
  785. HIDDEN ASIOError STDMETHODCALLTYPE GetSamplePosition(LPWINEASIO iface, ASIOSamples *sPos, ASIOTimeStamp *tStamp)
  786. {
  787. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  788. TRACE("iface: %p, sPos: %p, tStamp: %p\n", iface, sPos, tStamp);
  789. if (!sPos && !tStamp)
  790. {
  791. WARN("Nullpointer argument\n");
  792. return ASE_InvalidParameter;
  793. }
  794. #ifdef _WIN64
  795. *tStamp = This->asio_time_stamp;
  796. *sPos = This->asio_sample_position;
  797. #else
  798. tStamp->lo = This->asio_time_stamp.lo;
  799. tStamp->hi = This->asio_time_stamp.hi;
  800. sPos->lo = This->asio_sample_position.lo;
  801. sPos->hi = 0;
  802. #endif
  803. return ASE_OK;
  804. }
  805. /*
  806. * ASIOError GetChannelInfo (ASIOChannelInfo *info);
  807. * Function: Retrive channel info. - See asio.h for more detail
  808. * Returns: ASE_NotPresent on missing IO
  809. */
  810. DEFINE_THISCALL_WRAPPER(GetChannelInfo,8)
  811. HIDDEN ASIOError STDMETHODCALLTYPE GetChannelInfo(LPWINEASIO iface, ASIOChannelInfo *info)
  812. {
  813. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  814. /* TRACE("(iface: %p, info: %p\n", iface, info); */
  815. if (info->channel < 0 || (info->isInput ? info->channel >= This->wineasio_number_inputs : info->channel >= This->wineasio_number_outputs))
  816. {
  817. TRACE("Invalid Parameter\n");
  818. return ASE_InvalidParameter;
  819. }
  820. info->channelGroup = 0;
  821. #ifdef ASIOST32INT
  822. info->type = ASIOSTInt32LSB;
  823. #else
  824. info->type = ASIOSTFloat32LSB;
  825. #endif
  826. if (info->isInput)
  827. {
  828. info->isActive = This->input_channel[info->channel].active;
  829. memcpy(info->name, This->input_channel[info->channel].port_name, ASIO_MAX_NAME_LENGTH);
  830. }
  831. else
  832. {
  833. info->isActive = This->output_channel[info->channel].active;
  834. memcpy(info->name, This->output_channel[info->channel].port_name, ASIO_MAX_NAME_LENGTH);
  835. }
  836. return ASE_OK;
  837. }
  838. /*
  839. * ASIOError CreateBuffers(ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks);
  840. * Function: Allocate buffers for IO channels
  841. * Parameters: bufferInfo - pointer to an array of ASIOBufferInfo structures
  842. * numChannels - the total number of IO channels to be allocated
  843. * bufferSize - one of the buffer sizes retrieved with ASIOGetBufferSize()
  844. * asioCallbacks - pointer to an ASIOCallbacks structure
  845. * See asio.h for more detail
  846. * Returns: ASE_NoMemory if impossible to allocate enough memory
  847. * ASE_InvalidMode on unsupported bufferSize or invalid bufferInfo data
  848. * ASE_NotPresent on missing IO
  849. */
  850. DEFINE_THISCALL_WRAPPER(CreateBuffers,20)
  851. HIDDEN ASIOError STDMETHODCALLTYPE CreateBuffers(LPWINEASIO iface, ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks)
  852. {
  853. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  854. ASIOBufferInfo *buffer_info = bufferInfo;
  855. int i, j, k;
  856. TRACE("iface: %p, bufferInfo: %p, numChannels: %i, bufferSize: %i, asioCallbacks: %p\n", iface, bufferInfo, numChannels, bufferSize, asioCallbacks);
  857. if (This->asio_driver_state != Initialized)
  858. {
  859. WARN("Unable to create buffers, WineASIO is not in the initialized state\n");
  860. return ASE_NotPresent;
  861. }
  862. if (This->wineasio_fixed_buffersize)
  863. {
  864. if (This->asio_current_buffersize != bufferSize)
  865. {
  866. WARN("Invalid buffersize (%i) requested\n", bufferSize);
  867. return ASE_InvalidMode;
  868. }
  869. TRACE("Buffersize fixed at %i\n", This->asio_current_buffersize);
  870. }
  871. else
  872. { /* fail if not a power of two and if out of range */
  873. if (!(bufferSize > 0 && !(bufferSize&(bufferSize-1))
  874. && bufferSize >= ASIO_MINIMUM_BUFFERSIZE
  875. && bufferSize <= ASIO_MAXIMUM_BUFFERSIZE))
  876. {
  877. WARN("Invalid buffersize %i requested\n", bufferSize);
  878. return ASE_InvalidMode;
  879. }
  880. else
  881. {
  882. if (This->asio_current_buffersize == bufferSize)
  883. {
  884. TRACE("Buffer size already set to %i\n", This->asio_current_buffersize);
  885. }
  886. else
  887. {
  888. This->asio_current_buffersize = bufferSize;
  889. if (jack_set_buffer_size(This->jack_client, This->asio_current_buffersize))
  890. {
  891. WARN("JACK is unable to set buffersize to %i\n", This->asio_current_buffersize);
  892. return ASE_HWMalfunction;
  893. }
  894. TRACE("Buffer size changed to %i\n", This->asio_current_buffersize);
  895. }
  896. }
  897. }
  898. This->asio_callbacks = asioCallbacks;
  899. if (!This->asio_callbacks->asioMessage)
  900. {
  901. WARN("No asioMessage callback supplied\n");
  902. return ASE_InvalidMode;
  903. }
  904. TRACE("The ASIO host supports ASIO v%i\n", (int) This->asio_callbacks->asioMessage(kAsioEngineVersion, 0, 0, 0));
  905. if (This->asio_callbacks->asioMessage(kAsioSelectorSupported, kAsioResetRequest, 0 , 0))
  906. TRACE("The ASIO host supports kAsioResetRequest\n");
  907. if (This->asio_callbacks->asioMessage(kAsioSelectorSupported, kAsioResyncRequest, 0 , 0))
  908. TRACE("The ASIO host supports kAsioResyncRequest\n");
  909. if (This->asio_callbacks->asioMessage(kAsioSelectorSupported, kAsioBufferSizeChange, 0 , 0))
  910. TRACE("The ASIO host supports kAsioBufferSizeChange\n");
  911. /* Check for invalid channel numbers */
  912. for (i = j = k = 0; i < numChannels; i++, buffer_info++)
  913. {
  914. if (buffer_info->isInput)
  915. {
  916. if (j++ >= This->wineasio_number_inputs)
  917. {
  918. WARN("Invalid input channel requested\n");
  919. return ASE_InvalidMode;
  920. }
  921. }
  922. else
  923. {
  924. if (k++ >= This->wineasio_number_outputs)
  925. {
  926. WARN("Invalid output channel requested\n");
  927. return ASE_InvalidMode;
  928. }
  929. }
  930. }
  931. /* Allocate audio buffers */
  932. This->callback_audio_buffer = HeapAlloc(GetProcessHeap(), 0,
  933. (This->wineasio_number_inputs + This->wineasio_number_outputs) * 2 * This->asio_current_buffersize * sizeof(jack_default_audio_sample_t));
  934. if (!This->callback_audio_buffer)
  935. {
  936. ERR("Unable to allocate %i ASIO audio buffers\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
  937. return ASE_NoMemory;
  938. }
  939. TRACE("%i ASIO audio buffers allocated (%i kB)\n", This->wineasio_number_inputs + This->wineasio_number_outputs,
  940. (int) ((This->wineasio_number_inputs + This->wineasio_number_outputs) * 2 * This->asio_current_buffersize * sizeof(jack_default_audio_sample_t) / 1024));
  941. for (i = 0; i < This->wineasio_number_inputs; i++)
  942. This->input_channel[i].audio_buffer = This->callback_audio_buffer + (i * 2 * This->asio_current_buffersize);
  943. for (i = 0; i < This->wineasio_number_outputs; i++)
  944. This->output_channel[i].audio_buffer = This->callback_audio_buffer + ((This->wineasio_number_inputs + i) * 2 * This->asio_current_buffersize);
  945. /* initialize ASIOBufferInfo structures */
  946. buffer_info = bufferInfo;
  947. This->asio_active_inputs = This->asio_active_outputs = 0;
  948. for (i = 0; i < numChannels; i++, buffer_info++)
  949. {
  950. if (buffer_info->isInput)
  951. {
  952. buffer_info->buffers[0] = &This->input_channel[This->asio_active_inputs].audio_buffer[0];
  953. buffer_info->buffers[1] = &This->input_channel[This->asio_active_inputs].audio_buffer[This->asio_current_buffersize];
  954. This->input_channel[This->asio_active_inputs].active = ASIOTrue;
  955. This->asio_active_inputs++;
  956. /* TRACE("ASIO audio buffer for channel %i as input %li created\n", i, This->asio_active_inputs); */
  957. }
  958. else
  959. {
  960. buffer_info->buffers[0] = &This->output_channel[This->asio_active_outputs].audio_buffer[0];
  961. buffer_info->buffers[1] = &This->output_channel[This->asio_active_outputs].audio_buffer[This->asio_current_buffersize];
  962. This->output_channel[This->asio_active_outputs].active = ASIOTrue;
  963. This->asio_active_outputs++;
  964. /* TRACE("ASIO audio buffer for channel %i as output %li created\n", i, This->asio_active_outputs); */
  965. }
  966. }
  967. TRACE("%i audio channels initialized\n", This->asio_active_inputs + This->asio_active_outputs);
  968. /* check for TimeInfo or TimeCode mode */
  969. if (This->asio_callbacks->asioMessage(kAsioSupportsTimeInfo, 0, 0, 0))
  970. {
  971. This->asio_time_info_mode = TRUE;
  972. if (This->asio_callbacks->asioMessage(kAsioSupportsTimeCode, 0, 0, 0))
  973. This->asio_can_time_code = TRUE;
  974. }
  975. else
  976. This->asio_time_info_mode = FALSE;
  977. This->asio_driver_state = Prepared;
  978. return ASE_OK;
  979. }
  980. /*
  981. * ASIOError DisposeBuffers(void);
  982. * Function: Release allocated buffers
  983. * Returns: ASE_InvalidMode if no buffers were previously allocated
  984. * ASE_NotPresent on missing IO
  985. * Implies: ASIOStop()
  986. */
  987. DEFINE_THISCALL_WRAPPER(DisposeBuffers,4)
  988. HIDDEN ASIOError STDMETHODCALLTYPE DisposeBuffers(LPWINEASIO iface)
  989. {
  990. IWineASIOImpl *This = (IWineASIOImpl*)iface;
  991. int i;
  992. TRACE("iface: %p\n", iface);
  993. if (This->asio_driver_state == Running)
  994. Stop (iface);
  995. if (This->asio_driver_state != Prepared)
  996. {
  997. WARN("Unable to dispose buffers, wrong driver state\n");
  998. return ASE_NotPresent;
  999. }
  1000. This->asio_callbacks = NULL;
  1001. for (i = 0; i < This->wineasio_number_inputs; i++)
  1002. {
  1003. This->input_channel[i].audio_buffer = NULL;
  1004. This->input_channel[i].active = ASIOFalse;
  1005. }
  1006. for (i = 0; i < This->wineasio_number_outputs; i++)
  1007. {
  1008. This->output_channel[i].audio_buffer = NULL;
  1009. This->output_channel[i].active = ASIOFalse;
  1010. }
  1011. This->asio_active_inputs = This->asio_active_outputs = 0;
  1012. if (This->callback_audio_buffer)
  1013. HeapFree(GetProcessHeap(), 0, This->callback_audio_buffer);
  1014. This->asio_driver_state = Initialized;
  1015. return ASE_OK;
  1016. }
  1017. /*
  1018. * ASIOError ControlPanel(void);
  1019. * Function: Open a control panel for driver settings
  1020. * Returns: ASE_NotPresent if no control panel exists. Actually return code should be ignored
  1021. * Note: Call the asioMessage callback if something has changed
  1022. */
  1023. DEFINE_THISCALL_WRAPPER(ControlPanel,4)
  1024. HIDDEN ASIOError STDMETHODCALLTYPE ControlPanel(LPWINEASIO iface)
  1025. {
  1026. char *arg_list[] = { strdup ("qjackctl"), NULL };
  1027. TRACE("iface: %p\n", iface);
  1028. if (!fork())
  1029. execvp (arg_list[0], arg_list);
  1030. return ASE_OK;
  1031. }
  1032. /*
  1033. * ASIOError Future(LONG selector, void *opt);
  1034. * Function: Various, See asio.h for more detail
  1035. * Returns: Depends on the selector but in general ASE_InvalidParameter on invalid selector
  1036. * ASE_InvalidParameter if function is unsupported to disable further calls
  1037. * ASE_SUCCESS on success, do not use AES_OK
  1038. */
  1039. DEFINE_THISCALL_WRAPPER(Future,12)
  1040. HIDDEN ASIOError STDMETHODCALLTYPE Future(LPWINEASIO iface, LONG selector, void *opt)
  1041. {
  1042. IWineASIOImpl *This = (IWineASIOImpl *) iface;
  1043. TRACE("iface: %p, selector: %i, opt: %p\n", iface, selector, opt);
  1044. switch (selector)
  1045. {
  1046. case kAsioEnableTimeCodeRead:
  1047. This->asio_can_time_code = TRUE;
  1048. TRACE("The ASIO host enabled TimeCode\n");
  1049. return ASE_SUCCESS;
  1050. case kAsioDisableTimeCodeRead:
  1051. This->asio_can_time_code = FALSE;
  1052. TRACE("The ASIO host disabled TimeCode\n");
  1053. return ASE_SUCCESS;
  1054. case kAsioSetInputMonitor:
  1055. TRACE("The driver denied request to set input monitor\n");
  1056. return ASE_NotPresent;
  1057. case kAsioTransport:
  1058. TRACE("The driver denied request for ASIO Transport control\n");
  1059. return ASE_InvalidParameter;
  1060. case kAsioSetInputGain:
  1061. TRACE("The driver denied request to set input gain\n");
  1062. return ASE_InvalidParameter;
  1063. case kAsioGetInputMeter:
  1064. TRACE("The driver denied request to get input meter \n");
  1065. return ASE_InvalidParameter;
  1066. case kAsioSetOutputGain:
  1067. TRACE("The driver denied request to set output gain\n");
  1068. return ASE_InvalidParameter;
  1069. case kAsioGetOutputMeter:
  1070. TRACE("The driver denied request to get output meter\n");
  1071. return ASE_InvalidParameter;
  1072. case kAsioCanInputMonitor:
  1073. TRACE("The driver does not support input monitor\n");
  1074. return ASE_InvalidParameter;
  1075. case kAsioCanTimeInfo:
  1076. TRACE("The driver supports TimeInfo\n");
  1077. return ASE_SUCCESS;
  1078. case kAsioCanTimeCode:
  1079. TRACE("The driver supports TimeCode\n");
  1080. return ASE_SUCCESS;
  1081. case kAsioCanTransport:
  1082. TRACE("The driver denied request for ASIO Transport\n");
  1083. return ASE_InvalidParameter;
  1084. case kAsioCanInputGain:
  1085. TRACE("The driver does not support input gain\n");
  1086. return ASE_InvalidParameter;
  1087. case kAsioCanInputMeter:
  1088. TRACE("The driver does not support input meter\n");
  1089. return ASE_InvalidParameter;
  1090. case kAsioCanOutputGain:
  1091. TRACE("The driver does not support output gain\n");
  1092. return ASE_InvalidParameter;
  1093. case kAsioCanOutputMeter:
  1094. TRACE("The driver does not support output meter\n");
  1095. return ASE_InvalidParameter;
  1096. case kAsioSetIoFormat:
  1097. TRACE("The driver denied request to set DSD IO format\n");
  1098. return ASE_NotPresent;
  1099. case kAsioGetIoFormat:
  1100. TRACE("The driver denied request to get DSD IO format\n");
  1101. return ASE_NotPresent;
  1102. case kAsioCanDoIoFormat:
  1103. TRACE("The driver does not support DSD IO format\n");
  1104. return ASE_NotPresent;
  1105. default:
  1106. TRACE("ASIOFuture() called with undocumented selector\n");
  1107. return ASE_InvalidParameter;
  1108. }
  1109. }
  1110. /*
  1111. * ASIOError OutputReady(void);
  1112. * Function: Tells the driver that output bufffers are ready
  1113. * Returns: ASE_OK if supported
  1114. * ASE_NotPresent to disable
  1115. */
  1116. DEFINE_THISCALL_WRAPPER(OutputReady,4)
  1117. HIDDEN ASIOError STDMETHODCALLTYPE OutputReady(LPWINEASIO iface)
  1118. {
  1119. /* disabled to stop stand alone NI programs from spamming the console
  1120. TRACE("iface: %p\n", iface); */
  1121. return ASE_NotPresent;
  1122. }
  1123. /****************************************************************************
  1124. * JACK callbacks
  1125. */
  1126. static int bufsize_callback(jack_nframes_t nframes, void *arg)
  1127. {
  1128. IWineASIOImpl *This = (IWineASIOImpl*)arg;
  1129. if(This->asio_driver_state != Running)
  1130. return 0;
  1131. if (This->asio_callbacks->asioMessage(kAsioSelectorSupported, kAsioResetRequest, 0 , 0))
  1132. This->asio_callbacks->asioMessage(kAsioResetRequest, 0, 0, 0);
  1133. return 0;
  1134. }
  1135. static int process_callback(jack_nframes_t nframes, void *arg)
  1136. {
  1137. IWineASIOImpl *This = (IWineASIOImpl*)arg;
  1138. int i;
  1139. jack_transport_state_t jack_transport_state;
  1140. jack_position_t jack_position;
  1141. #ifdef ASIOST32INT
  1142. jack_default_audio_sample_t *in, *out;
  1143. jack_nframes_t j;
  1144. #endif
  1145. #ifndef _WIN64
  1146. DWORD temp_time;
  1147. #endif
  1148. // Output silence if the ASIO callback isn't running yet
  1149. if (This->asio_driver_state != Running)
  1150. {
  1151. for (i = 0; i < This->asio_active_outputs; i++)
  1152. memset(jack_port_get_buffer(This->output_channel[i].port, nframes), 0, sizeof (jack_default_audio_sample_t) * nframes);
  1153. return 0;
  1154. }
  1155. /* get the input data from JACK and copy it to the ASIO buffers */
  1156. #ifdef ASIOST32INT
  1157. for (i = 0; i < This->asio_active_inputs; i++)
  1158. if (This->input_channel[i].active == ASIOTrue)
  1159. {
  1160. in = jack_port_get_buffer(This->input_channel[i].port, nframes);
  1161. for (j = 0; j < nframes; j++)
  1162. ((int *)This->input_channel[i].audio_buffer)[nframes * This->asio_buffer_index + j] = in[j] * 0x7fffffff;
  1163. }
  1164. #else
  1165. for (i = 0; i < This->asio_active_inputs; i++)
  1166. if (This->input_channel[i].active == ASIOTrue)
  1167. memcpy (&This->input_channel[i].audio_buffer[nframes * This->asio_buffer_index],
  1168. jack_port_get_buffer(This->input_channel[i].port, nframes),
  1169. sizeof (jack_default_audio_sample_t) * nframes);
  1170. #endif
  1171. #ifdef _WIN64
  1172. This->asio_sample_position += nframes;
  1173. This->asio_sample_position &= 0xffffffff; /* make 32bit since JACK's position is 32bit anyways */
  1174. This->asio_time_stamp = timeGetTime() * 1000000;
  1175. #else
  1176. This->asio_sample_position.lo += nframes;
  1177. temp_time = timeGetTime();
  1178. This->asio_time_stamp.lo = temp_time * 1000000;
  1179. This->asio_time_stamp.hi = ((unsigned long long) temp_time * 1000000) >> 32;
  1180. #endif
  1181. /* time info & time code */
  1182. if (This->asio_time_info_mode)
  1183. {
  1184. #ifdef _WIN64
  1185. This->asio_time.timeInfo.samplePosition = This->asio_sample_position;
  1186. This->asio_time.timeInfo.systemTime = This->asio_time_stamp;
  1187. #else
  1188. This->asio_time.timeInfo.samplePosition.lo = This->asio_sample_position.lo;
  1189. This->asio_time.timeInfo.samplePosition.hi = 0;
  1190. This->asio_time.timeInfo.systemTime.lo = This->asio_time_stamp.lo;
  1191. This->asio_time.timeInfo.systemTime.hi = This->asio_time_stamp.hi;
  1192. #endif
  1193. This->asio_time.timeInfo.sampleRate = This->asio_sample_rate;
  1194. This->asio_time.timeInfo.flags = kSystemTimeValid | kSamplePositionValid | kSampleRateValid;
  1195. if (This->asio_can_time_code)
  1196. {
  1197. jack_transport_state = jack_transport_query(This->jack_client, &jack_position);
  1198. #ifdef _WIN64
  1199. This->asio_time.timeCode.timeCodeSamples = jack_position.frame;
  1200. #else
  1201. This->asio_time.timeCode.timeCodeSamples.lo = jack_position.frame;
  1202. This->asio_time.timeCode.timeCodeSamples.hi = 0;
  1203. #endif
  1204. This->asio_time.timeCode.flags = kTcValid;
  1205. if (jack_transport_state == JackTransportRolling)
  1206. This->asio_time.timeCode.flags |= kTcRunning;
  1207. }
  1208. This->asio_callbacks->bufferSwitchTimeInfo(&This->asio_time, This->asio_buffer_index, ASIOTrue);
  1209. }
  1210. else
  1211. This->asio_callbacks->bufferSwitch(This->asio_buffer_index, ASIOTrue);
  1212. /* copy the ASIO data to JACK */
  1213. #ifdef ASIOST32INT
  1214. for (i = 0; i < This->asio_active_outputs; i++)
  1215. if (This->output_channel[i].active == ASIOTrue)
  1216. {
  1217. out = jack_port_get_buffer(This->output_channel[i].port, nframes);
  1218. for (j = 0; j < nframes; j++)
  1219. out[j] = ((int *) This->output_channel[i].audio_buffer)[nframes * This->asio_buffer_index + j] / (float) 0x7fffffff;
  1220. }
  1221. #else
  1222. for (i = 0; i < This->asio_active_outputs; i++)
  1223. if (This->output_channel[i].active == ASIOTrue)
  1224. memcpy(jack_port_get_buffer(This->output_channel[i].port, nframes),
  1225. &This->output_channel[i].audio_buffer[nframes * This->asio_buffer_index],
  1226. sizeof (jack_default_audio_sample_t) * nframes);
  1227. #endif
  1228. This->asio_buffer_index = This->asio_buffer_index ? 0 : 1;
  1229. return 0;
  1230. }
  1231. static int srate_callback(jack_nframes_t nframes, void *arg)
  1232. {
  1233. IWineASIOImpl *This = (IWineASIOImpl*)arg;
  1234. if(This->asio_driver_state != Running)
  1235. return 0;
  1236. This->asio_sample_rate = nframes;
  1237. This->asio_callbacks->sampleRateDidChange(nframes);
  1238. return 0;
  1239. }
  1240. /*****************************************************************************
  1241. * Support functions
  1242. */
  1243. /* Function called by JACK to create a thread in the wine process context,
  1244. * uses the global structure jack_thread_creator_privates to communicate with jack_thread_creator_helper() */
  1245. static int jack_thread_creator(pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
  1246. {
  1247. TRACE("arg: %p, thread_id: %p, attr: %p, function: %p\n", arg, thread_id, attr, function);
  1248. jack_thread_creator_privates.jack_callback_thread = function;
  1249. jack_thread_creator_privates.arg = arg;
  1250. jack_thread_creator_privates.jack_callback_thread_created = CreateEventW(NULL, FALSE, FALSE, NULL);
  1251. CreateThread( NULL, 0, jack_thread_creator_helper, arg, 0,0 );
  1252. WaitForSingleObject(jack_thread_creator_privates.jack_callback_thread_created, INFINITE);
  1253. *thread_id = jack_thread_creator_privates.jack_callback_pthread_id;
  1254. return 0;
  1255. }
  1256. /* internal helper function for returning the posix thread_id of the newly created callback thread */
  1257. static DWORD WINAPI jack_thread_creator_helper(LPVOID arg)
  1258. {
  1259. TRACE("arg: %p\n", arg);
  1260. jack_thread_creator_privates.jack_callback_pthread_id = pthread_self();
  1261. SetEvent(jack_thread_creator_privates.jack_callback_thread_created);
  1262. jack_thread_creator_privates.jack_callback_thread(jack_thread_creator_privates.arg);
  1263. return 0;
  1264. }
  1265. static BOOL configure_driver(IWineASIOImpl *This)
  1266. {
  1267. HKEY hkey;
  1268. LONG result, value;
  1269. DWORD type, size;
  1270. WCHAR application_path [MAX_PATH];
  1271. WCHAR *application_name;
  1272. char environment_variable[MAX_ENVIRONMENT_SIZE];
  1273. /* Unicode strings used for the registry */
  1274. static const WCHAR key_software_wine_wineasio[] =
  1275. { 'S','o','f','t','w','a','r','e','\\',
  1276. 'W','i','n','e','\\',
  1277. 'W','i','n','e','A','S','I','O',0 };
  1278. static const WCHAR value_wineasio_number_inputs[] =
  1279. { 'N','u','m','b','e','r',' ','o','f',' ','i','n','p','u','t','s',0 };
  1280. static const WCHAR value_wineasio_number_outputs[] =
  1281. { 'N','u','m','b','e','r',' ','o','f',' ','o','u','t','p','u','t','s',0 };
  1282. static const WCHAR value_wineasio_fixed_buffersize[] =
  1283. { 'F','i','x','e','d',' ','b','u','f','f','e','r','s','i','z','e',0 };
  1284. static const WCHAR value_wineasio_preferred_buffersize[] =
  1285. { 'P','r','e','f','e','r','r','e','d',' ','b','u','f','f','e','r','s','i','z','e',0 };
  1286. static const WCHAR wineasio_autostart_server[] =
  1287. { 'A','u','t','o','s','t','a','r','t',' ','s','e','r','v','e','r',0 };
  1288. static const WCHAR value_wineasio_connect_to_hardware[] =
  1289. { 'C','o','n','n','e','c','t',' ','t','o',' ','h','a','r','d','w','a','r','e',0 };
  1290. /* Initialise most member variables,
  1291. * asio_sample_position, asio_time, & asio_time_stamp are initialized in Start()
  1292. * jack_num_input_ports & jack_num_output_ports are initialized in Init() */
  1293. This->asio_active_inputs = 0;
  1294. This->asio_active_outputs = 0;
  1295. This->asio_buffer_index = 0;
  1296. This->asio_callbacks = NULL;
  1297. This->asio_can_time_code = FALSE;
  1298. This->asio_current_buffersize = 0;
  1299. This->asio_driver_state = Loaded;
  1300. This->asio_sample_rate = 0;
  1301. This->asio_time_info_mode = FALSE;
  1302. This->asio_version = 92;
  1303. This->wineasio_number_inputs = 16;
  1304. This->wineasio_number_outputs = 16;
  1305. This->wineasio_autostart_server = FALSE;
  1306. This->wineasio_connect_to_hardware = TRUE;
  1307. This->wineasio_fixed_buffersize = TRUE;
  1308. This->wineasio_preferred_buffersize = ASIO_PREFERRED_BUFFERSIZE;
  1309. This->jack_client = NULL;
  1310. This->jack_client_name[0] = 0;
  1311. This->jack_input_ports = NULL;
  1312. This->jack_output_ports = NULL;
  1313. This->callback_audio_buffer = NULL;
  1314. This->input_channel = NULL;
  1315. This->output_channel = NULL;
  1316. /* create registry entries with defaults if not present */
  1317. result = RegCreateKeyExW(HKEY_CURRENT_USER, key_software_wine_wineasio, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
  1318. if (result != ERROR_SUCCESS)
  1319. {
  1320. ERR("Unable to open registry\n");
  1321. return FALSE;
  1322. }
  1323. /* get/set number of asio inputs */
  1324. size = sizeof(DWORD);
  1325. if (RegQueryValueExW(hkey, value_wineasio_number_inputs, NULL, &type, (LPBYTE) &value, &size) == ERROR_SUCCESS)
  1326. {
  1327. if (type == REG_DWORD)
  1328. This->wineasio_number_inputs = value;
  1329. }
  1330. else
  1331. {
  1332. type = REG_DWORD;
  1333. size = sizeof(DWORD);
  1334. value = This->wineasio_number_inputs;
  1335. result = RegSetValueExW(hkey, value_wineasio_number_inputs, 0, REG_DWORD, (LPBYTE) &value, size);
  1336. }
  1337. /* get/set number of asio outputs */
  1338. size = sizeof(DWORD);
  1339. if (RegQueryValueExW(hkey, value_wineasio_number_outputs, NULL, &type, (LPBYTE) &value, &size) == ERROR_SUCCESS)
  1340. {
  1341. if (type == REG_DWORD)
  1342. This->wineasio_number_outputs = value;
  1343. }
  1344. else
  1345. {
  1346. type = REG_DWORD;
  1347. size = sizeof(DWORD);
  1348. value = This->wineasio_number_outputs;
  1349. result = RegSetValueExW(hkey, value_wineasio_number_outputs, 0, REG_DWORD, (LPBYTE) &value, size);
  1350. }
  1351. /* allow changing of asio buffer sizes */
  1352. size = sizeof(DWORD);
  1353. if (RegQueryValueExW(hkey, value_wineasio_fixed_buffersize, NULL, &type, (LPBYTE) &value, &size) == ERROR_SUCCESS)
  1354. {
  1355. if (type == REG_DWORD)
  1356. This->wineasio_fixed_buffersize = value;
  1357. }
  1358. else
  1359. {
  1360. type = REG_DWORD;
  1361. size = sizeof(DWORD);
  1362. value = This->wineasio_fixed_buffersize;
  1363. result = RegSetValueExW(hkey, value_wineasio_fixed_buffersize, 0, REG_DWORD, (LPBYTE) &value, size);
  1364. }
  1365. /* preferred buffer size (if changing buffersize is allowed) */
  1366. size = sizeof(DWORD);
  1367. if (RegQueryValueExW(hkey, value_wineasio_preferred_buffersize, NULL, &type, (LPBYTE) &value, &size) == ERROR_SUCCESS)
  1368. {
  1369. if (type == REG_DWORD)
  1370. This->wineasio_preferred_buffersize = value;
  1371. }
  1372. else
  1373. {
  1374. type = REG_DWORD;
  1375. size = sizeof(DWORD);
  1376. value = This->wineasio_preferred_buffersize;
  1377. result = RegSetValueExW(hkey, value_wineasio_preferred_buffersize, 0, REG_DWORD, (LPBYTE) &value, size);
  1378. }
  1379. /* get/set JACK autostart */
  1380. size = sizeof(DWORD);
  1381. if (RegQueryValueExW(hkey, wineasio_autostart_server, NULL, &type, (LPBYTE) &value, &size) == ERROR_SUCCESS)
  1382. {
  1383. if (type == REG_DWORD)
  1384. This->wineasio_autostart_server = value;
  1385. }
  1386. else
  1387. {
  1388. type = REG_DWORD;
  1389. size = sizeof(DWORD);
  1390. value = This->wineasio_autostart_server;
  1391. result = RegSetValueExW(hkey, wineasio_autostart_server, 0, REG_DWORD, (LPBYTE) &value, size);
  1392. }
  1393. /* get/set JACK connect to physical io */
  1394. size = sizeof(DWORD);
  1395. if (RegQueryValueExW(hkey, value_wineasio_connect_to_hardware, NULL, &type, (LPBYTE) &value, &size) == ERROR_SUCCESS)
  1396. {
  1397. if (type == REG_DWORD)
  1398. This->wineasio_connect_to_hardware = value;
  1399. }
  1400. else
  1401. {
  1402. type = REG_DWORD;
  1403. size = sizeof(DWORD);
  1404. value = This->wineasio_connect_to_hardware;
  1405. result = RegSetValueExW(hkey, value_wineasio_connect_to_hardware, 0, REG_DWORD, (LPBYTE) &value, size);
  1406. }
  1407. /* get client name by stripping path and extension */
  1408. GetModuleFileNameW(0, application_path, MAX_PATH);
  1409. application_name = strrchrW(application_path, L'.');
  1410. *application_name = 0;
  1411. application_name = strrchrW(application_path, L'\\');
  1412. application_name++;
  1413. WideCharToMultiByte(CP_ACP, WC_SEPCHARS, application_name, -1, This->jack_client_name, ASIO_MAX_NAME_LENGTH, NULL, NULL);
  1414. RegCloseKey(hkey);
  1415. /* Look for environment variables to override registry config values */
  1416. if (GetEnvironmentVariableA("WINEASIO_NUMBER_INPUTS", environment_variable, MAX_ENVIRONMENT_SIZE))
  1417. {
  1418. errno = 0;
  1419. result = strtol(environment_variable, 0, 10);
  1420. if (errno != ERANGE)
  1421. This->wineasio_number_inputs = result;
  1422. }
  1423. if (GetEnvironmentVariableA("WINEASIO_NUMBER_OUTPUTS", environment_variable, MAX_ENVIRONMENT_SIZE))
  1424. {
  1425. errno = 0;
  1426. result = strtol(environment_variable, 0, 10);
  1427. if (errno != ERANGE)
  1428. This->wineasio_number_outputs = result;
  1429. }
  1430. if (GetEnvironmentVariableA("WINEASIO_AUTOSTART_SERVER", environment_variable, MAX_ENVIRONMENT_SIZE))
  1431. {
  1432. if (!strcasecmp(environment_variable, "on"))
  1433. This->wineasio_autostart_server = TRUE;
  1434. else if (!strcasecmp(environment_variable, "off"))
  1435. This->wineasio_autostart_server = FALSE;
  1436. }
  1437. if (GetEnvironmentVariableA("WINEASIO_CONNECT_TO_HARDWARE", environment_variable, MAX_ENVIRONMENT_SIZE))
  1438. {
  1439. if (!strcasecmp(environment_variable, "on"))
  1440. This->wineasio_connect_to_hardware = TRUE;
  1441. else if (!strcasecmp(environment_variable, "off"))
  1442. This->wineasio_connect_to_hardware = FALSE;
  1443. }
  1444. if (GetEnvironmentVariableA("WINEASIO_FIXED_BUFFERSIZE", environment_variable, MAX_ENVIRONMENT_SIZE))
  1445. {
  1446. if (!strcasecmp(environment_variable, "on"))
  1447. This->wineasio_fixed_buffersize = TRUE;
  1448. else if (!strcasecmp(environment_variable, "off"))
  1449. This->wineasio_fixed_buffersize = FALSE;
  1450. }
  1451. if (GetEnvironmentVariableA("WINEASIO_PREFERRED_BUFFERSIZE", environment_variable, MAX_ENVIRONMENT_SIZE))
  1452. {
  1453. errno = 0;
  1454. result = strtol(environment_variable, 0, 10);
  1455. if (errno != ERANGE)
  1456. This->wineasio_preferred_buffersize = result;
  1457. }
  1458. /* over ride the JACK client name gotten from the application name */
  1459. size = GetEnvironmentVariableA("WINEASIO_CLIENT_NAME", environment_variable, ASIO_MAX_NAME_LENGTH);
  1460. if (size > 0 && size < ASIO_MAX_NAME_LENGTH)
  1461. strcpy(This->jack_client_name, environment_variable);
  1462. /* if wineasio_preferred_buffersize is not a power of two or if out of range, then set to ASIO_PREFERRED_BUFFERSIZE */
  1463. if (!(This->wineasio_preferred_buffersize > 0 && !(This->wineasio_preferred_buffersize&(This->wineasio_preferred_buffersize-1))
  1464. && This->wineasio_preferred_buffersize >= ASIO_MINIMUM_BUFFERSIZE
  1465. && This->wineasio_preferred_buffersize <= ASIO_MAXIMUM_BUFFERSIZE))
  1466. This->wineasio_preferred_buffersize = ASIO_PREFERRED_BUFFERSIZE;
  1467. return TRUE;
  1468. }
  1469. /* Allocate the interface pointer and associate it with the vtbl/WineASIO object */
  1470. HRESULT WINAPI WineASIOCreateInstance(REFIID riid, LPVOID *ppobj)
  1471. {
  1472. IWineASIOImpl *pobj;
  1473. /* TRACE("riid: %s, ppobj: %p\n", debugstr_guid(riid), ppobj); */
  1474. pobj = HeapAlloc(GetProcessHeap(), 0, sizeof(*pobj));
  1475. if (pobj == NULL)
  1476. {
  1477. WARN("out of memory\n");
  1478. return E_OUTOFMEMORY;
  1479. }
  1480. pobj->lpVtbl = &WineASIO_Vtbl;
  1481. pobj->ref = 1;
  1482. TRACE("pobj = %p\n", pobj);
  1483. *ppobj = pobj;
  1484. /* TRACE("return %p\n", *ppobj); */
  1485. return S_OK;
  1486. }