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.

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