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.

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