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.

1610 lines
61KB

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