-
Notifications
You must be signed in to change notification settings - Fork 3
/
asio.c
1696 lines (1474 loc) · 63.6 KB
/
asio.c
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 <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
#include <pthread.h>
#include "wine/debug.h"
#include "objbase.h"
#include "mmsystem.h"
#include "winreg.h"
#include "wine/unicode.h"
#include <jack/jack.h>
#include <jack/thread.h>
#define IEEE754_64FLOAT 1
#include "asio.h"
WINE_DEFAULT_DEBUG_CHANNEL(asio);
#define MAX_ENVIRONMENT_SIZE 6
#define ASIO_MAX_NAME_LENGTH 32
#define ASIO_MINIMUM_BUFFERSIZE 16
#define ASIO_MAXIMUM_BUFFERSIZE 8192
#define ASIO_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\n_" #name suffix ":\n\t" code "");
#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 __cdecl
#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")))
/*****************************************************************************
* 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_(ASIOBool, 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_(ASIOError, Start) (THIS) PURE;
STDMETHOD_(ASIOError, Stop) (THIS) PURE;
STDMETHOD_(ASIOError, GetChannels) (THIS_ LONG *numInputChannels, LONG *numOutputChannels) PURE;
STDMETHOD_(ASIOError, GetLatencies) (THIS_ LONG *inputLatency, LONG *outputLatency) PURE;
STDMETHOD_(ASIOError, GetBufferSize) (THIS_ LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity) PURE;
STDMETHOD_(ASIOError, CanSampleRate) (THIS_ ASIOSampleRate sampleRate) PURE;
STDMETHOD_(ASIOError, GetSampleRate) (THIS_ ASIOSampleRate *sampleRate) PURE;
STDMETHOD_(ASIOError, SetSampleRate) (THIS_ ASIOSampleRate sampleRate) PURE;
STDMETHOD_(ASIOError, GetClockSources) (THIS_ ASIOClockSource *clocks, LONG *numSources) PURE;
STDMETHOD_(ASIOError, SetClockSource) (THIS_ LONG index) PURE;
STDMETHOD_(ASIOError, GetSamplePosition) (THIS_ ASIOSamples *sPos, ASIOTimeStamp *tStamp) PURE;
STDMETHOD_(ASIOError, GetChannelInfo) (THIS_ ASIOChannelInfo *info) PURE;
STDMETHOD_(ASIOError, CreateBuffers) (THIS_ ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks) PURE;
STDMETHOD_(ASIOError, DisposeBuffers) (THIS) PURE;
STDMETHOD_(ASIOError, ControlPanel) (THIS) PURE;
STDMETHOD_(ASIOError, Future) (THIS_ LONG selector,void *opt) PURE;
STDMETHOD_(ASIOError, OutputReady) (THIS) PURE;
};
#undef INTERFACE
typedef struct IWineASIO *LPWINEASIO;
typedef struct IOChannel
{
ASIOBool active;
jack_default_audio_sample_t *audio_buffer;
char port_name[ASIO_MAX_NAME_LENGTH];
jack_port_t *port;
} 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;
/* ASIO stuff */
LONG asio_active_inputs;
LONG asio_active_outputs;
BOOL asio_buffer_index;
ASIOCallbacks *asio_callbacks;
BOOL asio_can_time_code;
LONG asio_current_buffersize;
INT asio_driver_state;
ASIOSamples asio_sample_position;
ASIOSampleRate asio_sample_rate;
ASIOTime asio_time;
BOOL asio_time_info_mode;
ASIOTimeStamp asio_time_stamp;
LONG asio_version;
/* WineASIO configuration options */
LONG wineasio_number_inputs;
LONG wineasio_number_outputs;
BOOL wineasio_autostart_server;
BOOL wineasio_connect_to_hardware;
LONG wineasio_fixed_buffersize;
LONG wineasio_preferred_buffersize;
/* JACK stuff */
jack_client_t *jack_client;
char jack_client_name[ASIO_MAX_NAME_LENGTH];
int jack_num_input_ports;
int jack_num_output_ports;
const char **jack_input_ports;
const char **jack_output_ports;
/* 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 ASIOBool 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 ASIOError STDMETHODCALLTYPE Start(LPWINEASIO iface);
HIDDEN ASIOError STDMETHODCALLTYPE Stop(LPWINEASIO iface);
HIDDEN ASIOError STDMETHODCALLTYPE GetChannels (LPWINEASIO iface, LONG *numInputChannels, LONG *numOutputChannels);
HIDDEN ASIOError STDMETHODCALLTYPE GetLatencies(LPWINEASIO iface, LONG *inputLatency, LONG *outputLatency);
HIDDEN ASIOError STDMETHODCALLTYPE GetBufferSize(LPWINEASIO iface, LONG *minSize, LONG *maxSize, LONG *preferredSize, LONG *granularity);
HIDDEN ASIOError STDMETHODCALLTYPE CanSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate);
HIDDEN ASIOError STDMETHODCALLTYPE GetSampleRate(LPWINEASIO iface, ASIOSampleRate *sampleRate);
HIDDEN ASIOError STDMETHODCALLTYPE SetSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate);
HIDDEN ASIOError STDMETHODCALLTYPE GetClockSources(LPWINEASIO iface, ASIOClockSource *clocks, LONG *numSources);
HIDDEN ASIOError STDMETHODCALLTYPE SetClockSource(LPWINEASIO iface, LONG index);
HIDDEN ASIOError STDMETHODCALLTYPE GetSamplePosition(LPWINEASIO iface, ASIOSamples *sPos, ASIOTimeStamp *tStamp);
HIDDEN ASIOError STDMETHODCALLTYPE GetChannelInfo(LPWINEASIO iface, ASIOChannelInfo *info);
HIDDEN ASIOError STDMETHODCALLTYPE CreateBuffers(LPWINEASIO iface, ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks);
HIDDEN ASIOError STDMETHODCALLTYPE DisposeBuffers(LPWINEASIO iface);
HIDDEN ASIOError STDMETHODCALLTYPE ControlPanel(LPWINEASIO iface);
HIDDEN ASIOError STDMETHODCALLTYPE Future(LPWINEASIO iface, LONG selector, void *opt);
HIDDEN ASIOError 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 int bufsize_callback (jack_nframes_t nframes, void *arg);
static int process_callback (jack_nframes_t nframes, void *arg);
static int srate_callback (jack_nframes_t nframes, void *arg);
/*
* Support functions
*/
HRESULT WINAPI WineASIOCreateInstance(REFIID riid, LPVOID *ppobj);
static BOOL 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 */
struct {
void *(*jack_callback_thread) (void*);
void *arg;
pthread_t jack_callback_pthread_id;
HANDLE jack_callback_thread_created;
} jack_thread_creator_privates;
/*****************************************************************************
* 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 %d\n", iface, ref);
return ref;
}
/*
* ULONG Release (LPWINEASIO iface);
* Function: Destroy the interface
* Returns: Ref count
* Implies: ASIOStop() and ASIODisposeBuffers()
*/
HIDDEN ULONG STDMETHODCALLTYPE Release(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl *)iface;
ULONG ref = InterlockedDecrement(&This->ref);
int i;
TRACE("iface: %p, ref count is %d\n", iface, ref);
if (This->asio_driver_state == Running)
Stop(iface);
if (This->asio_driver_state == Prepared)
DisposeBuffers(iface);
if (This->asio_driver_state == Initialized)
{
/* just for good measure we deinitialize IOChannel structures and unregister JACK ports */
for (i = 0; i < This->wineasio_number_inputs; i++)
{
if(jack_port_unregister (This->jack_client, This->input_channel[i].port))
MESSAGE("Error trying to unregister port %s\n", This->input_channel[i].port_name);
This->input_channel[i].active = ASIOFalse;
This->input_channel[i].port = NULL;
}
for (i = 0; i < This->wineasio_number_outputs; i++)
{
if(jack_port_unregister (This->jack_client, This->output_channel[i].port))
MESSAGE("Error trying to unregister port %s\n", This->output_channel[i].port_name);
This->output_channel[i].active = ASIOFalse;
This->output_channel[i].port = NULL;
}
This->asio_active_inputs = This->asio_active_outputs = 0;
TRACE("%i IOChannel structures released\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
if (This->jack_output_ports)
jack_free (This->jack_output_ports);
if (This->jack_input_ports)
jack_free (This->jack_input_ports);
if (This->jack_client)
if (jack_client_close(This->jack_client))
MESSAGE("Error trying to close JACK client\n");
if (This->input_channel)
HeapFree(GetProcessHeap(), 0, This->input_channel);
}
TRACE("WineASIO terminated\n\n");
if (ref == 0)
HeapFree(GetProcessHeap(), 0, This);
return ref;
}
/*
* ASIOBool 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: ASIOFalse on error, and ASIOTrue on success
*/
DEFINE_THISCALL_WRAPPER(Init,8)
HIDDEN ASIOBool STDMETHODCALLTYPE Init(LPWINEASIO iface, void *sysRef)
{
IWineASIOImpl *This = (IWineASIOImpl *)iface;
jack_status_t jack_status;
jack_options_t jack_options = JackNullOption;
int i;
TRACE("iface: %p, sysRef: %p\n", iface, sysRef);
This->sys_ref = sysRef;
mlockall(MCL_FUTURE);
if (!configure_driver(This))
{
WARN("Unable to configure WineASIO\n");
return ASIOFalse;
}
if (!This->wineasio_autostart_server)
jack_options |= JackNoStartServer;
This->jack_client = jack_client_open(This->jack_client_name, jack_options, &jack_status);
if (This->jack_client == NULL)
{
WARN("Unable to open a JACK client as: %s\n", This->jack_client_name);
return ASIOFalse;
}
TRACE("JACK client opened as: '%s'\n", jack_get_client_name(This->jack_client));
if (!(This->asio_sample_rate = jack_get_sample_rate(This->jack_client)))
{
WARN("Unable to get samplerate from JACK\n");
return ASIOFalse;
}
if (!(This->asio_current_buffersize = jack_get_buffer_size(This->jack_client)))
{
WARN("Unable to get buffer size from JACK\n");
return ASIOFalse;
}
/* Allocate IOChannel structures */
This->input_channel = HeapAlloc(GetProcessHeap(), 0, (This->wineasio_number_inputs + This->wineasio_number_outputs) * sizeof(IOChannel));
if (!This->input_channel)
{
jack_client_close(This->jack_client);
ERR("Unable to allocate IOChannel structures for %i channels\n", This->wineasio_number_inputs);
return ASIOFalse;
}
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 = jack_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 = jack_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 = ASIOFalse;
This->input_channel[i].port = NULL;
snprintf(This->input_channel[i].port_name, ASIO_MAX_NAME_LENGTH, "in_%i", i + 1);
This->input_channel[i].port = jack_port_register(This->jack_client,
This->input_channel[i].port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, i);
/* TRACE("IOChannel structure initialized for input %d: '%s'\n", i, This->input_channel[i].port_name); */
}
for (i = 0; i < This->wineasio_number_outputs; i++)
{
This->output_channel[i].active = ASIOFalse;
This->output_channel[i].port = NULL;
snprintf(This->output_channel[i].port_name, ASIO_MAX_NAME_LENGTH, "out_%i", i + 1);
This->output_channel[i].port = jack_port_register(This->jack_client,
This->output_channel[i].port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, i);
/* TRACE("IOChannel structure initialized for output %d: '%s'\n", i, This->output_channel[i].port_name); */
}
TRACE("%i IOChannel structures initialized\n", This->wineasio_number_inputs + This->wineasio_number_outputs);
jack_set_thread_creator(jack_thread_creator);
if (jack_set_process_callback(This->jack_client, process_callback, This))
{
jack_client_close(This->jack_client);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK process callback\n");
return ASIOFalse;
}
if (jack_set_buffer_size_callback(This->jack_client, bufsize_callback, This))
{
jack_client_close(This->jack_client);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK buffersize change callback\n");
return ASIOFalse;
}
if (jack_set_sample_rate_callback (This->jack_client, srate_callback, This))
{
jack_client_close(This->jack_client);
HeapFree(GetProcessHeap(), 0, This->input_channel);
ERR("Unable to register JACK samplerate change callback\n");
return ASIOFalse;
}
This->asio_driver_state = Initialized;
TRACE("WineASIO 0.%.1f initialized\n",(float) This->asio_version / 10);
return ASIOTrue;
}
/*
* 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->asio_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;
}
/*
* ASIOError Start(void);
* Function: Start JACK IO processing and reset the sample counter to zero
* Returns: ASE_NotPresent if IO is missing
* ASE_HWMalfunction if JACK fails to start
*/
DEFINE_THISCALL_WRAPPER(Start,4)
HIDDEN ASIOError STDMETHODCALLTYPE Start(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
int i;
#ifndef _WIN64
DWORD temp_time;
#endif
TRACE("iface: %p\n", iface);
if (This->asio_driver_state != Prepared)
{
ERR("Unable to start WineASIO\n");
return ASE_NotPresent;
}
/* Zero the audio buffer */
for (i = 0; i < (This->wineasio_number_inputs + This->wineasio_number_outputs) * 2 * This->asio_current_buffersize; i++)
This->callback_audio_buffer[i] = 0;
/* prime the callback */
This->asio_buffer_index = 0;
if (This->asio_callbacks)
{
#ifdef _WIN64
This->asio_sample_position = 0;
This->asio_time_stamp = timeGetTime() * 1000000;
#else
This->asio_sample_position.hi = This->asio_sample_position.lo = 0;
temp_time = timeGetTime();
This->asio_time_stamp.lo = temp_time * 1000000;
This->asio_time_stamp.hi = ((unsigned long long) temp_time * 1000000) >> 32;
#endif
This->asio_time_info_mode = FALSE;
This->asio_can_time_code = FALSE;
if (This->asio_callbacks->asioMessage(kAsioSupportsTimeInfo, 0, 0, 0))
{
TRACE("TimeInfo mode enabled\n");
This->asio_time_info_mode = TRUE;
#ifdef _WIN64
This->asio_time.timeInfo.systemTime = This->asio_time_stamp;
This->asio_time.timeInfo.samplePosition = 0;
#else
This->asio_time.timeInfo.systemTime.hi = This->asio_time_stamp.hi;
This->asio_time.timeInfo.systemTime.lo = This->asio_time_stamp.lo;
This->asio_time.timeInfo.samplePosition.hi = This->asio_time.timeInfo.samplePosition.lo = 0;
#endif
This->asio_time.timeCode.speed = 0;
This->asio_time.timeInfo.sampleRate = This->asio_sample_rate;
This->asio_time.timeInfo.flags = kSystemTimeValid | kSamplePositionValid | kSampleRateValid;
if (This->asio_callbacks->asioMessage(kAsioSupportsTimeCode, 0, 0, 0))
{
TRACE("TimeCode supported\n");
This->asio_can_time_code = TRUE;
#ifdef _WIN64
This->asio_time.timeCode.timeCodeSamples = This->asio_time_stamp;
#else
This->asio_time.timeCode.timeCodeSamples.hi = This->asio_time_stamp.hi;
This->asio_time.timeCode.timeCodeSamples.lo = This->asio_time_stamp.lo;
#endif
This->asio_time.timeCode.flags = ~(kTcValid | kTcRunning);
}
This->asio_callbacks->bufferSwitchTimeInfo(&This->asio_time, This->asio_buffer_index, ASIOTrue);
}
else
{
TRACE("Runnning simple BufferSwitch() callback\n");
This->asio_callbacks->bufferSwitch(This->asio_buffer_index, ASIOTrue);
}
This->asio_buffer_index = This->asio_buffer_index ? 0 : 1;
}
else
{
WARN("The ASIO host supplied no callback structure\n");
return ASE_NotPresent;
}
if (jack_activate(This->jack_client))
{
ERR("Unable to activate JACK client\n");
return ASE_NotPresent;
}
/* connect to the hardware io */
if (This->wineasio_connect_to_hardware)
{
for (i = 0; i < This->jack_num_input_ports && i < This->wineasio_number_inputs; i++)
{
/* TRACE("Connecting JACK port: %s to asio: %s\n", This->jack_input_ports[i], jack_port_name(This->input_channel[i].port)); */
if (strstr(jack_port_type(jack_port_by_name(This->jack_client, This->jack_input_ports[i])), "audio"))
if (jack_connect(This->jack_client, This->jack_input_ports[i], jack_port_name(This->input_channel[i].port)))
WARN("Unable to connect %s to %s\n", This->jack_input_ports[i], jack_port_name(This->input_channel[i].port));
}
for (i = 0; i < This->jack_num_output_ports && i < This->wineasio_number_outputs; i++)
{
/* TRACE("Connecting asio: %s to jack port: %s\n", jack_port_name(This->output_channel[i].port), This->jack_output_ports[i]); */
if (strstr(jack_port_type(jack_port_by_name(This->jack_client, This->jack_output_ports[i])), "audio"))
if (jack_connect(This->jack_client, jack_port_name(This->output_channel[i].port), This->jack_output_ports[i]))
WARN("Unable to connect to %s\n", jack_port_name(This->output_channel[i].port));
}
}
This->asio_driver_state = Running;
TRACE("WineASIO successfully loaded\n");
return ASE_OK;
}
/*
* ASIOError Stop(void);
* Function: Stop JACK IO processing
* Returns: ASE_NotPresent on missing IO
* Note: BufferSwitch() must not called after returning
*/
DEFINE_THISCALL_WRAPPER(Stop,4)
HIDDEN ASIOError STDMETHODCALLTYPE Stop(LPWINEASIO iface)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p\n", iface);
if (This->asio_driver_state != Running)
{
WARN("Unable to stop WineASIO, not running\n");
return ASE_NotPresent;
}
This->asio_driver_state = Prepared;
if (jack_deactivate(This->jack_client))
{
ERR("Unable to deactivate JACK client\n");
return ASE_NotPresent;
}
return ASE_OK;
}
/*
* ASIOError GetChannels(LONG *numInputChannels, LONG *numOutputChannels);
* Function: Report number of IO channels
* Parameters: numInputChannels and numOutputChannels will hold number of channels on returning
* Returns: ASE_NotPresent if no channels are available, otherwise AES_OK
*/
DEFINE_THISCALL_WRAPPER(GetChannels,12)
HIDDEN ASIOError STDMETHODCALLTYPE GetChannels (LPWINEASIO iface, LONG *numInputChannels, LONG *numOutputChannels)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
if (!numInputChannels && !numOutputChannels)
{
WARN("Nullpointer argument\n");
return ASE_InvalidParameter;
}
*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 ASE_OK;
}
/*
* ASIOError GetLatencies(LONG *inputLatency, LONG *outputLatency);
* Function: Return latency in frames
* Returns: ASE_NotPresent if no IO is available, otherwise AES_OK
*/
DEFINE_THISCALL_WRAPPER(GetLatencies,12)
HIDDEN ASIOError STDMETHODCALLTYPE GetLatencies(LPWINEASIO iface, LONG *inputLatency, LONG *outputLatency)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
if (!inputLatency && !outputLatency)
{
WARN("Nullpointer argument\n");
return ASE_InvalidParameter;
}
*inputLatency = *outputLatency = This->asio_current_buffersize;
TRACE("iface: %p Latency = %i frames\n", iface, This->asio_current_buffersize);
return ASE_OK;
}
/*
* ASIOError 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: ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetBufferSize,20)
HIDDEN ASIOError 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)
{
WARN("Nullpointer argument\n");
return ASE_InvalidParameter;
}
if (This->wineasio_fixed_buffersize)
{
*minSize = *maxSize = *preferredSize = This->asio_current_buffersize;
*granularity = 0;
TRACE("Buffersize fixed at %i\n", This->asio_current_buffersize);
return ASE_OK;
}
*minSize = ASIO_MINIMUM_BUFFERSIZE;
*maxSize = ASIO_MAXIMUM_BUFFERSIZE;
*preferredSize = This->wineasio_preferred_buffersize;
*granularity = -1;
TRACE("The ASIO host can control buffersize\nMinimum: %i, maximum: %i, preferred: %i, granularity: %i, current: %i\n",
*minSize, *maxSize, *preferredSize, *granularity, This->asio_current_buffersize);
return ASE_OK;
}
/*
* ASIOError CanSampleRate(ASIOSampleRate sampleRate);
* Function: Ask if specific SR is available
* Returns: ASE_NoClock if SR isn't available, ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(CanSampleRate,12)
HIDDEN ASIOError STDMETHODCALLTYPE CanSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, Samplerate = %li, requested samplerate = %li\n", iface, (long) This->asio_sample_rate, (long) sampleRate);
if (sampleRate != This->asio_sample_rate)
return ASE_NoClock;
return ASE_OK;
}
/*
* ASIOError GetSampleRate(ASIOSampleRate *currentRate);
* Function: Return current SR
* Parameters: currentRate will hold SR on return, 0 if unknown
* Returns: ASE_NoClock if SR is unknown, ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetSampleRate,8)
HIDDEN ASIOError STDMETHODCALLTYPE GetSampleRate(LPWINEASIO iface, ASIOSampleRate *sampleRate)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, Sample rate is %i\n", iface, (int) This->asio_sample_rate);
if (!sampleRate)
{
WARN("Nullpointer argument\n");
return ASE_InvalidParameter;
}
*sampleRate = This->asio_sample_rate;
return ASE_OK;
}
/*
* ASIOError SetSampleRate(ASIOSampleRate sampleRate);
* Function: Set requested SR, enable external sync if SR == 0
* Returns: ASE_NoClock if unknown SR
* ASE_InvalidMode if current clock is external and SR != 0
* ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(SetSampleRate,12)
HIDDEN ASIOError STDMETHODCALLTYPE SetSampleRate(LPWINEASIO iface, ASIOSampleRate sampleRate)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, Sample rate %f requested\n", iface, sampleRate);
if (sampleRate != This->asio_sample_rate)
return ASE_NoClock;
return ASE_OK;
}
/*
* ASIOError GetClockSources(ASIOClockSource *clocks, LONG *numSources);
* Function: Return available clock sources
* Parameters: clocks - a pointer to an array of ASIOClockSource structures.
* numSources - when called: number of allocated members
* - on return: number of clock sources, the minimum is 1 - the internal clock
* Returns: ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetClockSources,12)
HIDDEN ASIOError STDMETHODCALLTYPE GetClockSources(LPWINEASIO iface, ASIOClockSource *clocks, LONG *numSources)
{
TRACE("iface: %p, clocks: %p, numSources: %p\n", iface, clocks, numSources);
if (!clocks && !numSources)
{
WARN("Nullpointer argument\n");
return ASE_InvalidParameter;
}
clocks->index = 0;
clocks->associatedChannel = -1;
clocks->associatedGroup = -1;
clocks->isCurrentSource = ASIOTrue;
strcpy(clocks->name, "Internal");
*numSources = 1;
return ASE_OK;
}
/*
* ASIOError SetClockSource(LONG index);
* Function: Set clock source
* Parameters: index returned by ASIOGetClockSources() - See asio.h for more details
* Returns: ASE_NotPresent on missing IO
* ASE_InvalidMode may be returned if a clock can't be selected
* ASE_NoClock should not be returned
*/
DEFINE_THISCALL_WRAPPER(SetClockSource,8)
HIDDEN ASIOError STDMETHODCALLTYPE SetClockSource(LPWINEASIO iface, LONG index)
{
TRACE("iface: %p, index: %i\n", iface, index);
if (index != 0)
return ASE_NotPresent;
return ASE_OK;
}
/*
* ASIOError GetSamplePosition (ASIOSamples *sPos, ASIOTimeStamp *tStamp);
* Function: Return sample position and timestamp
* Parameters: sPos holds the position on return, reset to 0 on ASIOStart()
* tStamp holds the system time of sPos
* Return: ASE_NotPresent on missing IO
* ASE_SPNotAdvancing on missing clock
*/
DEFINE_THISCALL_WRAPPER(GetSamplePosition,12)
HIDDEN ASIOError STDMETHODCALLTYPE GetSamplePosition(LPWINEASIO iface, ASIOSamples *sPos, ASIOTimeStamp *tStamp)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
TRACE("iface: %p, sPos: %p, tStamp: %p\n", iface, sPos, tStamp);
if (!sPos && !tStamp)
{
WARN("Nullpointer argument\n");
return ASE_InvalidParameter;
}
#ifdef _WIN64
*tStamp = This->asio_time_stamp;
*sPos = This->asio_sample_position;
#else
tStamp->lo = This->asio_time_stamp.lo;
tStamp->hi = This->asio_time_stamp.hi;
sPos->lo = This->asio_sample_position.lo;
sPos->hi = 0;
#endif
return ASE_OK;
}
/*
* ASIOError GetChannelInfo (ASIOChannelInfo *info);
* Function: Retrive channel info. - See asio.h for more detail
* Returns: ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(GetChannelInfo,8)
HIDDEN ASIOError STDMETHODCALLTYPE GetChannelInfo(LPWINEASIO iface, ASIOChannelInfo *info)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
/* TRACE("(iface: %p, info: %p\n", iface, info); */
if (info->channel < 0 || (info->isInput ? info->channel >= This->wineasio_number_inputs : info->channel >= This->wineasio_number_outputs))
{
TRACE("Invalid Parameter\n");
return ASE_InvalidParameter;
}
info->channelGroup = 0;
#ifdef ASIOST32INT
info->type = ASIOSTInt32LSB;
#else
info->type = ASIOSTFloat32LSB;
#endif
if (info->isInput)
{
info->isActive = This->input_channel[info->channel].active;
memcpy(info->name, This->input_channel[info->channel].port_name, ASIO_MAX_NAME_LENGTH);
}
else
{
info->isActive = This->output_channel[info->channel].active;
memcpy(info->name, This->output_channel[info->channel].port_name, ASIO_MAX_NAME_LENGTH);
}
return ASE_OK;
}
/*
* ASIOError CreateBuffers(ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks);
* Function: Allocate buffers for IO channels
* Parameters: bufferInfo - pointer to an array of ASIOBufferInfo structures
* numChannels - the total number of IO channels to be allocated
* bufferSize - one of the buffer sizes retrieved with ASIOGetBufferSize()
* asioCallbacks - pointer to an ASIOCallbacks structure
* See asio.h for more detail
* Returns: ASE_NoMemory if impossible to allocate enough memory
* ASE_InvalidMode on unsupported bufferSize or invalid bufferInfo data
* ASE_NotPresent on missing IO
*/
DEFINE_THISCALL_WRAPPER(CreateBuffers,20)
HIDDEN ASIOError STDMETHODCALLTYPE CreateBuffers(LPWINEASIO iface, ASIOBufferInfo *bufferInfo, LONG numChannels, LONG bufferSize, ASIOCallbacks *asioCallbacks)
{
IWineASIOImpl *This = (IWineASIOImpl*)iface;
ASIOBufferInfo *buffer_info = bufferInfo;
int i, j, k;
TRACE("iface: %p, bufferInfo: %p, numChannels: %i, bufferSize: %i, asioCallbacks: %p\n", iface, bufferInfo, numChannels, bufferSize, asioCallbacks);
if (This->asio_driver_state != Initialized)
{
WARN("Unable to create buffers, WineASIO is not in the initialized state\n");
return ASE_NotPresent;
}
if (This->wineasio_fixed_buffersize)
{
if (This->asio_current_buffersize != bufferSize)