forked from wineasio/wineasio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasio.c
More file actions
1683 lines (1443 loc) · 61.3 KB
/
asio.c
File metadata and controls
1683 lines (1443 loc) · 61.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 Robert Reif
* Portions copyright (C) 2007 Ralf Beck
* Portions copyright (C) 2007 Johnny Petrantoni
* Portions copyright (C) 2007 Stephane Letz
* Portions copyright (C) 2008 William Steidtmann
* Portions copyright (C) 2010 Peter L Jones
* Portions copyright (C) 2010 Torben Hohn
* Portions copyright (C) 2010 Nedko Arnaudov
* Portions copyright (C) 2013 Joakim Hernberg
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <pthread.h>
#ifdef DEBUG
#include "wine/debug.h"
#else
#define TRACE(...) {}
#define WARN(fmt, ...) {} fprintf(stdout, "[wineasio] " fmt, ##__VA_ARGS__)
#define ERR(fmt, ...) {} fprintf(stderr, "[wineasio] " fmt, ##__VA_ARGS__)
#endif
#include <objbase.h>
#include <mmsystem.h>
#include <winreg.h>
#ifdef WINE_WITH_UNICODE
#include <wine/unicode.h>
#endif
#include "jackbridge.h"
#ifdef DEBUG
WINE_DEFAULT_DEBUG_CHANNEL(asio);
#endif
#define MAX_ENVIRONMENT_SIZE 6
#define WINEASIO_MAX_NAME_LENGTH 32
#define WINEASIO_MINIMUM_BUFFERSIZE 16
#define WINEASIO_MAXIMUM_BUFFERSIZE 8192
#define WINEASIO_PREFERRED_BUFFERSIZE 1024
/* ASIO drivers (breaking the COM specification) use the Microsoft variety of
* thiscall calling convention which gcc is unable to produce. These macros
* add an extra layer to fixup the registers. Borrowed from config.h and the
* wine source code.
*/
/* From config.h */
#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");
#define __ASM_GLOBAL_FUNC(name,code) __ASM_DEFINE_FUNC(name,"",code)
#define __ASM_NAME(name) name
#define __ASM_STDCALL(args) ""
/* From wine source */
#ifdef __i386__ /* thiscall functions are i386-specific */
#define THISCALL(func) __thiscall_ ## func
#define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
#define __thiscall __stdcall
#define DEFINE_THISCALL_WRAPPER(func,args) \
extern void THISCALL(func)(void); \
__ASM_GLOBAL_FUNC(__thiscall_ ## func, \
"popl %eax\n\t" \
"pushl %ecx\n\t" \
"pushl %eax\n\t" \
"jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
#else /* __i386__ */
#define THISCALL(func) func
#define THISCALL_NAME(func) __ASM_NAME(#func)
#define __thiscall __stdcall
#define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
#endif /* __i386__ */
/* Hide ELF symbols for the COM members - No need to to export them */
#define HIDDEN __attribute__ ((visibility("hidden")))
#ifdef _WIN64
#define WINEASIO_CALLBACK CALLBACK
#else
#define WINEASIO_CALLBACK
#endif
typedef struct w_int64_t {
ULONG hi;
ULONG lo;
} w_int64_t;
typedef struct BufferInformation
{
LONG isInputType;
LONG channelNumber;
void *audioBufferStart;
void *audioBufferEnd;
} BufferInformation;
typedef struct TimeInformation
{
LONG _1[4];
double _2;
w_int64_t timeStamp;
w_int64_t numSamples;
double sampleRate;
ULONG flags;
char _3[12];
double speedForTimeCode;
w_int64_t timeStampForTimeCode;
ULONG flagsForTimeCode;
char _4[64];
} TimeInformation;
typedef struct Callbacks
{
void (WINEASIO_CALLBACK *swapBuffers) (LONG, LONG);
void (WINEASIO_CALLBACK *sampleRateChanged) (double);
LONG (WINEASIO_CALLBACK *sendNotification) (LONG, LONG, void*, double*);
void* (WINEASIO_CALLBACK *swapBuffersWithTimeInfo) (TimeInformation*, LONG, LONG);
} Callbacks;
/*****************************************************************************
* IWineAsio interface
*/
#define INTERFACE IWineASIO
DECLARE_INTERFACE_(IWineASIO,IUnknown)
{
STDMETHOD_(HRESULT, QueryInterface) (THIS_ IID riid, void** ppvObject) PURE;
STDMETHOD_(ULONG, AddRef) (THIS) PURE;
STDMETHOD_(ULONG, Release) (THIS) PURE;
STDMETHOD_(LONG, Init) (THIS_ void *sysRef) PURE;
STDMETHOD_(void, GetDriverName) (THIS_ char *name) PURE;
STDMETHOD_(LONG, GetDriverVersion) (THIS) PURE;
STDMETHOD_(void, GetErrorMessage) (THIS_ char *string) PURE;
STDMETHOD_(LONG, Start) (THIS) PURE;
STDMETHOD_(LONG, Stop) (THIS) PURE;
STDMETHOD_(LONG, GetChannels) (THIS_ LONG *numInputChannels, LONG *numOutputChannels) PURE;
STDMETHOD_(LONG, GetLatencies) (THIS_ LONG *inputLatency, LONG *outputLatency) PURE;
STDMETHOD_(LONG, GetBufferSize) (THIS_ LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity) PURE;
STDMETHOD_(LONG, CanSampleRate) (THIS_ double sampleRate) PURE;
STDMETHOD_(LONG, GetSampleRate) (THIS_ double *sampleRate) PURE;
STDMETHOD_(LONG, SetSampleRate) (THIS_ double sampleRate) PURE;
STDMETHOD_(LONG, GetClockSources) (THIS_ void *clocks, LONG *numSources) PURE;
STDMETHOD_(LONG, SetClockSource) (THIS_ LONG index) PURE;
STDMETHOD_(LONG, GetSamplePosition) (THIS_ w_int64_t *sPos, w_int64_t *tStamp) PURE;
STDMETHOD_(LONG, GetChannelInfo) (THIS_ void *info) PURE;
STDMETHOD_(LONG, CreateBuffers) (THIS_ BufferInformation *bufferInfo, LONG numChannels, LONG bufferSize, Callbacks *callbacks) PURE;
STDMETHOD_(LONG, DisposeBuffers) (THIS) PURE;
STDMETHOD_(LONG, ControlPanel) (THIS) PURE;
STDMETHOD_(LONG, Future) (THIS_ LONG selector,void *opt) PURE;
STDMETHOD_(LONG, OutputReady) (THIS) PURE;
};
#undef INTERFACE
typedef struct IWineASIO *LPWINEASIO;
typedef struct IOChannel
{
jack_default_audio_sample_t *audio_buffer;
char port_name[WINEASIO_MAX_NAME_LENGTH];
jack_port_t *port;
bool active;
} IOChannel;
typedef struct IWineASIOImpl
{
/* COM stuff */
const IWineASIOVtbl *lpVtbl;
LONG ref;
/* The app's main window handle on windows, 0 on OS/X */
HWND sys_ref;
/* Host stuff */
LONG host_active_inputs;
LONG host_active_outputs;
BOOL host_buffer_index;
Callbacks *host_callbacks;
BOOL host_can_time_code;
LONG host_current_buffersize;
volatile INT host_driver_state;
w_int64_t host_num_samples;
double host_sample_rate;
TimeInformation host_time;
BOOL host_time_info_mode;
w_int64_t host_time_stamp;
LONG host_version;
/* WineASIO configuration options */
int wineasio_number_inputs;
int wineasio_number_outputs;
BOOL wineasio_autostart_server;
BOOL wineasio_connect_to_hardware;
BOOL wineasio_fixed_buffersize;
LONG wineasio_preferred_buffersize;
/* JACK stuff */
jack_client_t *jack_client;
char jack_client_name[WINEASIO_MAX_NAME_LENGTH];
int jack_num_input_ports;
int jack_num_output_ports;
const char **jack_input_ports;
const char **jack_output_ports;
/* jack process callback buffers */
jack_default_audio_sample_t *callback_audio_buffer;
IOChannel *input_channel;
IOChannel *output_channel;
} IWineASIOImpl;
enum { Loaded, Initialized, Prepared, Running };
/****************************************************************************
* Interface Methods
*/
/*
* as seen from the WineASIO source
*/
HIDDEN HRESULT STDMETHODCALLTYPE QueryInterface(LPWINEASIO iface, REFIID riid, void **ppvObject);
HIDDEN ULONG STDMETHODCALLTYPE AddRef(LPWINEASIO iface);
HIDDEN ULONG STDMETHODCALLTYPE Release(LPWINEASIO iface);
HIDDEN LONG STDMETHODCALLTYPE Init(LPWINEASIO iface, void *sysRef);
HIDDEN void STDMETHODCALLTYPE GetDriverName(LPWINEASIO iface, char *name);
HIDDEN LONG STDMETHODCALLTYPE GetDriverVersion(LPWINEASIO iface);
HIDDEN void STDMETHODCALLTYPE GetErrorMessage(LPWINEASIO iface, char *string);
HIDDEN LONG STDMETHODCALLTYPE Start(LPWINEASIO iface);
HIDDEN LONG STDMETHODCALLTYPE Stop(LPWINEASIO iface);
HIDDEN LONG STDMETHODCALLTYPE GetChannels (LPWINEASIO iface, LONG *numInputChannels, LONG *numOutputChannels);
HIDDEN LONG STDMETHODCALLTYPE GetLatencies(LPWINEASIO iface, LONG *inputLatency, LONG *outputLatency);
HIDDEN LONG STDMETHODCALLTYPE GetBufferSize(LPWINEASIO iface, LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity);
HIDDEN LONG STDMETHODCALLTYPE CanSampleRate(LPWINEASIO iface, double sampleRate);
HIDDEN LONG STDMETHODCALLTYPE GetSampleRate(LPWINEASIO iface, double *sampleRate);
HIDDEN LONG STDMETHODCALLTYPE SetSampleRate(LPWINEASIO iface, double sampleRate);
HIDDEN LONG STDMETHODCALLTYPE GetClockSources(LPWINEASIO iface, void *clocks, LONG *numSources);
HIDDEN LONG STDMETHODCALLTYPE SetClockSource(LPWINEASIO iface, LONG index);
HIDDEN LONG STDMETHODCALLTYPE GetSamplePosition(LPWINEASIO iface, w_int64_t *sPos, w_int64_t *tStamp);
HIDDEN LONG STDMETHODCALLTYPE GetChannelInfo(LPWINEASIO iface, void *info);
HIDDEN LONG STDMETHODCALLTYPE CreateBuffers(LPWINEASIO iface, BufferInformation *bufferInfo, LONG numChannels, LONG bufferSize, Callbacks *callbacks);
HIDDEN LONG STDMETHODCALLTYPE DisposeBuffers(LPWINEASIO iface);
HIDDEN LONG STDMETHODCALLTYPE ControlPanel(LPWINEASIO iface);
HIDDEN LONG STDMETHODCALLTYPE Future(LPWINEASIO iface, LONG selector, void *opt);
HIDDEN LONG STDMETHODCALLTYPE OutputReady(LPWINEASIO iface);
/*
* thiscall wrappers for the vtbl (as seen from app side 32bit)
*/
HIDDEN void __thiscall_Init(void);
HIDDEN void __thiscall_GetDriverName(void);
HIDDEN void __thiscall_GetDriverVersion(void);
HIDDEN void __thiscall_GetErrorMessage(void);
HIDDEN void __thiscall_Start(void);
HIDDEN void __thiscall_Stop(void);
HIDDEN void __thiscall_GetChannels(void);
HIDDEN void __thiscall_GetLatencies(void);
HIDDEN void __thiscall_GetBufferSize(void);
HIDDEN void __thiscall_CanSampleRate(void);
HIDDEN void __thiscall_GetSampleRate(void);
HIDDEN void __thiscall_SetSampleRate(void);
HIDDEN void __thiscall_GetClockSources(void);
HIDDEN void __thiscall_SetClockSource(void);
HIDDEN void __thiscall_GetSamplePosition(void);
HIDDEN void __thiscall_GetChannelInfo(void);
HIDDEN void __thiscall_CreateBuffers(void);
HIDDEN void __thiscall_DisposeBuffers(void);
HIDDEN void __thiscall_ControlPanel(void);
HIDDEN void __thiscall_Future(void);
HIDDEN void __thiscall_OutputReady(void);
/*
* Jack callbacks
*/
static inline int jack_buffer_size_callback (jack_nframes_t nframes, void *arg);
static inline void jack_latency_callback(jack_latency_callback_mode_t mode, void *arg);
static inline int jack_process_callback (jack_nframes_t nframes, void *arg);
static inline int jack_sample_rate_callback (jack_nframes_t nframes, void *arg);
/*
* Support functions
*/
HRESULT WINAPI WineASIOCreateInstance(REFIID riid, LPVOID *ppobj);
static VOID configure_driver(IWineASIOImpl *This);
static DWORD WINAPI jack_thread_creator_helper(LPVOID arg);
static int jack_thread_creator(pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg);
/* {48D0C522-BFCC-45cc-8B84-17F25F33E6E8} */
static GUID const CLSID_WineASIO = {
0x48d0c522, 0xbfcc, 0x45cc, { 0x8b, 0x84, 0x17, 0xf2, 0x5f, 0x33, 0xe6, 0xe8 } };
static const IWineASIOVtbl WineASIO_Vtbl =
{
(void *) QueryInterface,
(void *) AddRef,
(void *) Release,
(void *) THISCALL(Init),
(void *) THISCALL(GetDriverName),
(void *) THISCALL(GetDriverVersion),
(void *) THISCALL(GetErrorMessage),
(void *) THISCALL(Start),
(void *) THISCALL(Stop),
(void *) THISCALL(GetChannels),
(void *) THISCALL(GetLatencies),
(void *) THISCALL(GetBufferSize),
(void *) THISCALL(CanSampleRate),
(void *) THISCALL(GetSampleRate),
(void *) THISCALL(SetSampleRate),
(void *) THISCALL(GetClockSources),
(void *) THISCALL(SetClockSource),
(void *) THISCALL(GetSamplePosition),
(void *) THISCALL(GetChannelInfo),
(void *) THISCALL(CreateBuffers),
(void *) THISCALL(DisposeBuffers),
(void *) THISCALL(ControlPanel),
(void *) THISCALL(Future),
(void *) THISCALL(OutputReady)
};
/* structure needed to create the JACK callback thread in the wine process context */
static struct {
void *(*jack_callback_thread) (void*);
void *arg;
pthread_t jack_callback_pthread_id;
HANDLE jack_callback_thread_created;
pthread_mutex_t lock;
} jack_thread_creator_privates = { .lock = PTHREAD_MUTEX_INITIALIZER };
/*****************************************************************************
* Interface method definitions
*/
HIDDEN HRESULT STDMETHODCALLTYPE QueryInterface(LPWINEASIO iface, REFIID riid, void **ppvObject)
{
IWineASIOImpl *This = (IWineASIOImpl *)iface;
TRACE("iface: %p, riid: %s, ppvObject: %p)\n", iface, debugstr_guid(riid), ppvObject);
if (ppvObject == NULL)
return E_INVALIDARG;
if (IsEqualIID(&CLSID_WineASIO, riid))
{
AddRef(iface);
*ppvObject = This;
return S_OK;
}
return E_NOINTERFACE;
}
/*
* ULONG STDMETHODCALLTYPE AddRef(LPWINEASIO iface);
* Function: Increment the reference count on the object
* Returns: Ref count
*/
HIDDEN ULONG STDMETHODCALLTYPE AddRef(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl *)iface;
ULONG ref = InterlockedIncrement(&(This->ref));
TRACE("iface: %p, ref count is %u\n", iface, (unsigned)ref);
return ref;
}
/*
* ULONG Release (LPWINEASIO iface);
* Function: Destroy the interface
* Returns: Ref count
* Implies: Stop() and DisposeBuffers()
*/
HIDDEN ULONG STDMETHODCALLTYPE Release(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl *)iface;
ULONG ref = InterlockedDecrement(&This->ref);
TRACE("iface: %p, ref count is %u\n", iface, (unsigned)ref);
if (This->host_driver_state == Running)
Stop(iface);
if (This->host_driver_state == Prepared)
DisposeBuffers(iface);
if (This->host_driver_state == Initialized)
{
/* just for good measure we deinitialize IOChannel structures and unregister JACK ports */
for (int i = 0; i < This->wineasio_number_inputs; i++)
{
jackbridge_port_unregister (This->jack_client, This->input_channel[i].port);
This->input_channel[i].active = false;
This->input_channel[i].port = NULL;
}
for (int i = 0; i < This->wineasio_number_outputs; i++)
{
jackbridge_port_unregister (This->jack_client, This->output_channel[i].port);
This->output_channel[i].active = false;
This->output_channel[i].port = NULL;
}
This->host_active_inputs = This->host_active_outputs = 0;
TRACE("%i IOChannel structures released\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
jackbridge_free (This->jack_output_ports);
jackbridge_free (This->jack_input_ports);
jackbridge_client_close(This->jack_client);
if (This->input_channel)
HeapFree(GetProcessHeap(), 0, This->input_channel);
}
TRACE("WineASIO terminated\n\n");
if (ref == 0)
HeapFree(GetProcessHeap(), 0, This);
return ref;
}
/*
* LONG Init (void *sysRef);
* Function: Initialize the driver
* Parameters: Pointer to "This"
* sysHanle is 0 on OS/X and on windows it contains the applications main window handle
* Returns: 0 on error, and 1 on success
*/
DEFINE_THISCALL_WRAPPER(Init,8)
HIDDEN LONG STDMETHODCALLTYPE Init(LPWINEASIO iface, void *sysRef)
{
IWineASIOImpl *This = (IWineASIOImpl *)iface;
jack_status_t jack_status;
jack_options_t jack_options = This->wineasio_autostart_server ? JackNullOption : JackNoStartServer;
int i;
This->sys_ref = sysRef;
/* mlockall(MCL_FUTURE) removed — it locked ALL future allocations in the
* entire process (GUI, plugins, etc.), not just audio buffers. JACK handles
* memory locking for its own RT thread internally. The old call caused
* "Cannot lock down 107MB" warnings and excessive memory pressure. */
configure_driver(This);
if (!(This->jack_client = jackbridge_client_open(This->jack_client_name, jack_options, &jack_status)))
{
WARN("Unable to open a JACK client as: %s\n", This->jack_client_name);
return 0;
}
TRACE("JACK client opened as: '%s'\n", jackbridge_get_client_name(This->jack_client));
This->host_sample_rate = jackbridge_get_sample_rate(This->jack_client);
This->host_current_buffersize = jackbridge_get_buffer_size(This->jack_client);
/* Allocate IOChannel structures */
This->input_channel = HeapAlloc(GetProcessHeap(), 0, (This->wineasio_number_inputs + This->wineasio_number_outputs) * sizeof(IOChannel));
if (!This->input_channel)
{
jackbridge_client_close(This->jack_client);
ERR("Unable to allocate IOChannel structures for %i channels\n", This->wineasio_number_inputs);
return 0;
}
This->output_channel = This->input_channel + This->wineasio_number_inputs;
TRACE("%i IOChannel structures allocated\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
/* Get and count physical JACK ports */
This->jack_input_ports = jackbridge_get_ports(This->jack_client, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
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++)
;
This->jack_output_ports = jackbridge_get_ports(This->jack_client, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
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++)
;
/* Initialize IOChannel structures */
for (i = 0; i < This->wineasio_number_inputs; i++)
{
This->input_channel[i].active = false;
This->input_channel[i].port = NULL;
snprintf(This->input_channel[i].port_name, WINEASIO_MAX_NAME_LENGTH, "in_%i", i + 1);
This->input_channel[i].port = jackbridge_port_register(This->jack_client,
This->input_channel[i].port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, i);
if (!This->input_channel[i].port)
{
ERR("Failed to register JACK input port %d\n", i);
jackbridge_client_close(This->jack_client);
HeapFree(GetProcessHeap(), 0, This->input_channel);
return 0;
}
}
for (i = 0; i < This->wineasio_number_outputs; i++)
{
This->output_channel[i].active = false;
This->output_channel[i].port = NULL;
snprintf(This->output_channel[i].port_name, WINEASIO_MAX_NAME_LENGTH, "out_%i", i + 1);
This->output_channel[i].port = jackbridge_port_register(This->jack_client,
This->output_channel[i].port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, i);
if (!This->output_channel[i].port)
{
ERR("Failed to register JACK output port %d\n", i);
jackbridge_client_close(This->jack_client);
HeapFree(GetProcessHeap(), 0, This->input_channel);
return 0;
}
}
TRACE("%i IOChannel structures initialized\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
jackbridge_set_thread_creator(jack_thread_creator);
if (!jackbridge_set_buffer_size_callback(This->jack_client, jack_buffer_size_callback, This))
{
jackbridge_client_close(This->jack_client);
jackbridge_free(This->jack_input_ports);
jackbridge_free(This->jack_output_ports);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK buffer size change callback\n");
return 0;
}
if (!jackbridge_set_latency_callback(This->jack_client, jack_latency_callback, This))
{
jackbridge_client_close(This->jack_client);
jackbridge_free(This->jack_input_ports);
jackbridge_free(This->jack_output_ports);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK latency callback\n");
return 0;
}
if (!jackbridge_set_process_callback(This->jack_client, jack_process_callback, This))
{
jackbridge_client_close(This->jack_client);
jackbridge_free(This->jack_input_ports);
jackbridge_free(This->jack_output_ports);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK process callback\n");
return 0;
}
if (!jackbridge_set_sample_rate_callback (This->jack_client, jack_sample_rate_callback, This))
{
jackbridge_client_close(This->jack_client);
jackbridge_free(This->jack_input_ports);
jackbridge_free(This->jack_output_ports);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK sample rate change callback\n");
return 0;
}
This->host_driver_state = Initialized;
TRACE("WineASIO 0.%.1f initialized\n",(float) This->host_version / 10);
return 1;
}
/*
* void GetDriverName(char *name);
* Function: Returns the driver name in name
*/
DEFINE_THISCALL_WRAPPER(GetDriverName,8)
HIDDEN void STDMETHODCALLTYPE GetDriverName(LPWINEASIO iface, char *name)
{
TRACE("iface: %p, name: %p\n", iface, name);
strcpy(name, "WineASIO");
return;
}
/*
* LONG GetDriverVersion (void);
* Function: Returns the driver version number
*/
DEFINE_THISCALL_WRAPPER(GetDriverVersion,4)
HIDDEN LONG STDMETHODCALLTYPE GetDriverVersion(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p\n", iface);
return This->host_version;
}
/*
* void GetErrorMessage(char *string);
* Function: Returns an error message for the last occured error in string
*/
DEFINE_THISCALL_WRAPPER(GetErrorMessage,8)
HIDDEN void STDMETHODCALLTYPE GetErrorMessage(LPWINEASIO iface, char *string)
{
TRACE("iface: %p, string: %p)\n", iface, string);
strcpy(string, "WineASIO does not return error messages\n");
return;
}
/*
* LONG Start(void);
* Function: Start JACK IO processing and reset the sample counter to zero
* Returns: -1000 if IO is missing
* -999 if JACK fails to start
*/
DEFINE_THISCALL_WRAPPER(Start,4)
HIDDEN LONG STDMETHODCALLTYPE Start(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
int i;
DWORD time;
TRACE("iface: %p\n", iface);
if (This->host_driver_state != Prepared)
{
return -1000;
}
/* Zero the audio buffer */
for (i = 0; i < (This->wineasio_number_inputs + This->wineasio_number_outputs) * 2 * This->host_current_buffersize; i++)
This->callback_audio_buffer[i] = 0;
/* prime the callback by preprocessing one outbound host bufffer */
This->host_buffer_index = 0;
This->host_num_samples.hi = This->host_num_samples.lo = 0;
time = timeGetTime();
This->host_time_stamp.lo = time * 1000000;
This->host_time_stamp.hi = ((unsigned long long) time * 1000000) >> 32;
if (This->host_time_info_mode) /* use the newer swapBuffersWithTimeInfo method if supported */
{
This->host_time.numSamples.lo = This->host_time.numSamples.hi = 0;
This->host_time.timeStamp.lo = This->host_time_stamp.lo;
This->host_time.timeStamp.hi = This->host_time_stamp.hi;
This->host_time.sampleRate = This->host_sample_rate;
This->host_time.flags = 0x7;
if (This->host_can_time_code) /* addionally use time code if supported */
{
This->host_time.speedForTimeCode = 1; /* FIXME */
This->host_time.timeStampForTimeCode.lo = This->host_time_stamp.lo;
This->host_time.timeStampForTimeCode.hi = This->host_time_stamp.hi;
This->host_time.flagsForTimeCode = ~(0x3);
}
This->host_callbacks->swapBuffersWithTimeInfo(&This->host_time, This->host_buffer_index, 1);
}
else
{ /* use the old swapBuffers method */
This->host_callbacks->swapBuffers(This->host_buffer_index, 1);
}
/* switch host buffer */
This->host_buffer_index = This->host_buffer_index ? 0 : 1;
This->host_driver_state = Running;
TRACE("WineASIO successfully loaded\n");
return 0;
}
/*
* LONG Stop(void);
* Function: Stop JACK IO processing
* Returns: -1000 on missing IO
* Note: swapBuffers() must not called after returning
*/
DEFINE_THISCALL_WRAPPER(Stop,4)
HIDDEN LONG STDMETHODCALLTYPE Stop(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p\n", iface);
if (This->host_driver_state != Running)
{
return -1000;
}
This->host_driver_state = Prepared;
return 0;
}
/*
* LONG GetChannels(LONG *numInputChannels, LONG *numOutputChannels);
* Function: Report number of IO channels
* Parameters: numInputChannels and numOutputChannels will hold number of channels on returning
* Returns: -1000 if no channels are available, otherwise AES_OK
*/
DEFINE_THISCALL_WRAPPER(GetChannels,12)
HIDDEN LONG STDMETHODCALLTYPE GetChannels (LPWINEASIO iface, LONG *numInputChannels, LONG *numOutputChannels)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
if (!numInputChannels || !numOutputChannels)
return -998;
*numInputChannels = This->wineasio_number_inputs;
*numOutputChannels = This->wineasio_number_outputs;
TRACE("iface: %p, inputs: %i, outputs: %i\n", iface, This->wineasio_number_inputs, This->wineasio_number_outputs);
return 0;
}
/*
* LONG GetLatencies(LONG *inputLatency, LONG *outputLatency);
* Function: Return latency in frames
* Returns: -1000 if no IO is available, otherwise AES_OK
*/
DEFINE_THISCALL_WRAPPER(GetLatencies,12)
HIDDEN LONG STDMETHODCALLTYPE GetLatencies(LPWINEASIO iface, LONG *inputLatency, LONG *outputLatency)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
jack_latency_range_t range;
if (!inputLatency || !outputLatency)
return -998;
if (This->host_driver_state == Loaded)
return -1000;
jackbridge_port_get_latency_range(This->input_channel[0].port, JackCaptureLatency, &range);
*inputLatency = range.max;
jackbridge_port_get_latency_range(This->output_channel[0].port, JackPlaybackLatency, &range);
*outputLatency = range.max;
TRACE("iface: %p, input latency: %d, output latency: %d\n", iface, (int)*inputLatency, (int)*outputLatency);
return 0;
}
/*
* LONG GetBufferSize(LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity);
* Function: Return minimum, maximum, preferred buffer sizes, and granularity
* At the moment return all the same, and granularity 0
* Returns: -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetBufferSize,20)
HIDDEN LONG STDMETHODCALLTYPE GetBufferSize(LPWINEASIO iface, LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, minSize: %p, maxSize: %p, preferredSize: %p, granularity: %p\n", iface, minSize, maxSize, preferredSize, granularity);
if (!minSize || !maxSize || !preferredSize || !granularity)
return -998;
if (This->wineasio_fixed_buffersize)
{
*minSize = *maxSize = *preferredSize = This->host_current_buffersize;
*granularity = 0;
TRACE("Buffersize fixed at %d\n", (int)This->host_current_buffersize);
return 0;
}
*minSize = WINEASIO_MINIMUM_BUFFERSIZE;
*maxSize = WINEASIO_MAXIMUM_BUFFERSIZE;
*preferredSize = This->wineasio_preferred_buffersize;
*granularity = -1;
TRACE("The host can control buffersize\nMinimum: %d, maximum: %d, preferred: %d, granularity: %d, current: %d\n",
(int)*minSize, (int)*maxSize, (int)*preferredSize, (int)*granularity, (int)This->host_current_buffersize);
return 0;
}
/*
* LONG CanSampleRate(double sampleRate);
* Function: Ask if specific SR is available
* Returns: -995 if SR isn't available, -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(CanSampleRate,12)
HIDDEN LONG STDMETHODCALLTYPE CanSampleRate(LPWINEASIO iface, double sampleRate)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, Samplerate = %li, requested samplerate = %li\n", iface, (long) This->host_sample_rate, (long) sampleRate);
if (sampleRate != This->host_sample_rate)
{
return -995;
}
return 0;
}
/*
* LONG GetSampleRate(double *currentRate);
* Function: Return current SR
* Parameters: currentRate will hold SR on return, 0 if unknown
* Returns: -995 if SR is unknown, -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetSampleRate,8)
HIDDEN LONG STDMETHODCALLTYPE GetSampleRate(LPWINEASIO iface, double *sampleRate)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, Sample rate is %i\n", iface, (int) This->host_sample_rate);
if (!sampleRate)
{
return -998;
}
*sampleRate = This->host_sample_rate;
return 0;
}
/*
* LONG SetSampleRate(double sampleRate);
* Function: Set requested SR, enable external sync if SR == 0
* Returns: -995 if unknown SR
* -997 if current clock is external and SR != 0
* -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(SetSampleRate,12)
HIDDEN LONG STDMETHODCALLTYPE SetSampleRate(LPWINEASIO iface, double sampleRate)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, Sample rate %f requested\n", iface, sampleRate);
if (sampleRate != This->host_sample_rate)
return -995;
return 0;
}
/*
* LONG GetClockSources(void *clocks, LONG *numSources);
* Function: Return available clock sources
* Parameters: clocks - a pointer to an array of clock source structures.
* numSources - when called: number of allocated members
* - on return: number of clock sources, the minimum is 1 - the internal clock
* Returns: -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetClockSources,12)
HIDDEN LONG STDMETHODCALLTYPE GetClockSources(LPWINEASIO iface, void *clocks, LONG *numSources)
{
LONG *lclocks = (LONG*)clocks;
TRACE("iface: %p, clocks: %p, numSources: %p\n", iface, clocks, numSources);
if (!clocks || !numSources)
return -998;
*lclocks++ = 0;
*lclocks++ = -1;
*lclocks++ = -1;
*lclocks++ = 1;
strcpy((char*)lclocks, "Internal");
*numSources = 1;
return 0;
}
/*
* LONG SetClockSource(LONG index);
* Function: Set clock source
* Parameters: index returned by GetClockSources()
* Returns: -1000 on missing IO
* -997 may be returned if a clock can't be selected
* -995 should not be returned
*/
DEFINE_THISCALL_WRAPPER(SetClockSource,8)
HIDDEN LONG STDMETHODCALLTYPE SetClockSource(LPWINEASIO iface, LONG index)
{
TRACE("iface: %p, index: %d\n", iface, (int)index);
if (index != 0)
return -1000;
return 0;
}
/*
* LONG GetSamplePosition (w_int64_t *sPos, w_int64_t *tStamp);
* Function: Return sample position and timestamp
* Parameters: sPos holds the position on return, reset to 0 on Start()
* tStamp holds the system time of sPos
* Return: -1000 on missing IO
* -996 on missing clock
*/
DEFINE_THISCALL_WRAPPER(GetSamplePosition,12)
HIDDEN LONG STDMETHODCALLTYPE GetSamplePosition(LPWINEASIO iface, w_int64_t *sPos, w_int64_t *tStamp)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, sPos: %p, tStamp: %p\n", iface, sPos, tStamp);
if (!sPos || !tStamp)
return -998;
tStamp->lo = This->host_time_stamp.lo;
tStamp->hi = This->host_time_stamp.hi;
sPos->lo = This->host_num_samples.lo;
sPos->hi = This->host_num_samples.hi;
return 0;
}
/*
* LONG GetChannelInfo (void *info);
* Function: Retrive channel info
* Returns: -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetChannelInfo,8)
HIDDEN LONG STDMETHODCALLTYPE GetChannelInfo(LPWINEASIO iface, void *info)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
LONG *linfo = (LONG*)info;
const LONG channelNumber = *linfo++;
const LONG isInputType = *linfo++;
/* TRACE("(iface: %p, info: %p\n", iface, info); */
if (channelNumber < 0 || (isInputType ? channelNumber >= This->wineasio_number_inputs : channelNumber >= This->wineasio_number_outputs))
return -998;
*linfo++ = (isInputType ? This->input_channel : This->output_channel)[channelNumber].active;
*linfo++ = 0;
*linfo++ = 19;
memcpy(linfo, (isInputType ? This->input_channel : This->output_channel)[channelNumber].port_name, WINEASIO_MAX_NAME_LENGTH);
return 0;
}
/*
* LONG CreateBuffers(BufferInformation *bufferInfo, LONG numChannels, LONG bufferSize, Callbacks *callbacks);
* Function: Allocate buffers for IO channels
* Parameters: bufferInfo - pointer to an array of BufferInformation structures
* numChannels - the total number of IO channels to be allocated
* bufferSize - one of the buffer sizes retrieved with GetBufferSize()
* callbacks - pointer to a Callbacks structure
* Returns: -994 if impossible to allocate enough memory
* -997 on unsupported bufferSize or invalid bufferInfo data
* -1000 on missing IO
*/
DEFINE_THISCALL_WRAPPER(CreateBuffers,20)
HIDDEN LONG STDMETHODCALLTYPE CreateBuffers(LPWINEASIO iface, BufferInformation *bufferInfo, LONG numChannels, LONG bufferSize, Callbacks *callbacks)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
BufferInformation *bufferInfoPerChannel = bufferInfo;
int i, j, k;
TRACE("iface: %p, bufferInfo: %p, numChannels: %d, bufferSize: %d, callbacks: %p\n", iface, bufferInfo, (int)numChannels, (int)bufferSize, callbacks);
if (This->host_driver_state != Initialized)
return -1000;
if (!bufferInfo || !callbacks)
return -997;
/* Check for invalid channel numbers */
for (i = j = k = 0; i < numChannels; i++, bufferInfoPerChannel++)
{
if (bufferInfoPerChannel->isInputType)
{
if (j++ >= This->wineasio_number_inputs)
{
WARN("Invalid input channel requested\n");
return -997;
}
}
else
{
if (k++ >= This->wineasio_number_outputs)
{
WARN("Invalid output channel requested\n");
return -997;
}
}
}
/* set buf_size */
if (This->wineasio_fixed_buffersize)
{
if (This->host_current_buffersize != bufferSize)
return -997;