Commit ae105ae1c718c98d2c7c078b4f1d09e149fc8249

Ethan Lee 2022-07-10T12:59:33

windows: Move IMMDevice work to common file, implement DirectSound enumeration support

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
diff --git a/Makefile.w32 b/Makefile.w32
index 1ca1fa2..978afab 100644
--- a/Makefile.w32
+++ b/Makefile.w32
@@ -73,7 +73,7 @@ SRCS+= SDL_locale.c SDL_syslocale.c
 SRCS+= SDL_url.c SDL_sysurl.c
 
 SRCS+= SDL_winmm.c SDL_directsound.c SDL_wasapi.c SDL_wasapi_win32.c
-SRCS+= SDL_hid.c SDL_windows.c SDL_xinput.c
+SRCS+= SDL_hid.c SDL_immdevice.c SDL_windows.c SDL_xinput.c
 SRCS+= SDL_dinputhaptic.c SDL_windowshaptic.c SDL_xinputhaptic.c
 SRCS+= SDL_dinputjoystick.c SDL_rawinputjoystick.c SDL_windowsjoystick.c SDL_windows_gaming_input.c SDL_xinputjoystick.c
 SRCS+= SDL_syspower.c
diff --git a/VisualC-GDK/SDL/SDL.vcxproj b/VisualC-GDK/SDL/SDL.vcxproj
index 1d31c8f..c511fda 100644
--- a/VisualC-GDK/SDL/SDL.vcxproj
+++ b/VisualC-GDK/SDL/SDL.vcxproj
@@ -382,6 +382,7 @@
     <ClInclude Include="..\..\src\core\gdk\SDL_gdk.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_directx.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_hid.h" />
+    <ClInclude Include="..\..\src\core\windows\SDL_immdevice.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_windows.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_xinput.h" />
     <ClInclude Include="..\..\src\dynapi\SDL_dynapi.h" />
@@ -538,6 +539,7 @@
     <ClCompile Include="..\..\src\audio\wasapi\SDL_wasapi.c" />
     <ClCompile Include="..\..\src\audio\wasapi\SDL_wasapi_win32.c" />
     <ClCompile Include="..\..\src\core\windows\SDL_hid.c" />
+    <ClCompile Include="..\..\src\core\windows\SDL_immdevice.c" />
     <ClCompile Include="..\..\src\core\windows\SDL_windows.c" />
     <ClCompile Include="..\..\src\core\windows\SDL_xinput.c">
       <CompileAs Condition="'$(Configuration)|$(Platform)'=='Debug|Gaming.Xbox.Scarlett.x64'">CompileAsCpp</CompileAs>
diff --git a/VisualC-GDK/SDL/SDL.vcxproj.filters b/VisualC-GDK/SDL/SDL.vcxproj.filters
index 71aeb24..59a3d72 100644
--- a/VisualC-GDK/SDL/SDL.vcxproj.filters
+++ b/VisualC-GDK/SDL/SDL.vcxproj.filters
@@ -432,6 +432,9 @@
     <ClInclude Include="..\..\src\core\windows\SDL_hid.h">
       <Filter>core\windows</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\core\windows\SDL_immdevice.h">
+      <Filter>core\windows</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\src\core\windows\SDL_windows.h">
       <Filter>core\windows</Filter>
     </ClInclude>
@@ -876,6 +879,9 @@
     <ClCompile Include="..\..\src\core\windows\SDL_hid.c">
       <Filter>core\windows</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\core\windows\SDL_immdevice.c">
+      <Filter>core\windows</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\src\core\windows\SDL_windows.c">
       <Filter>core\windows</Filter>
     </ClCompile>
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index 418a18f..cb386bc 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -305,6 +305,7 @@
     <ClInclude Include="..\..\src\audio\winmm\SDL_winmm.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_directx.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_hid.h" />
+    <ClInclude Include="..\..\src\core\windows\SDL_immdevice.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_windows.h" />
     <ClInclude Include="..\..\src\core\windows\SDL_xinput.h" />
     <ClInclude Include="..\..\src\dynapi\SDL_dynapi.h" />
@@ -456,6 +457,7 @@
     <ClCompile Include="..\..\src\audio\wasapi\SDL_wasapi.c" />
     <ClCompile Include="..\..\src\audio\wasapi\SDL_wasapi_win32.c" />
     <ClCompile Include="..\..\src\core\windows\SDL_hid.c" />
+    <ClCompile Include="..\..\src\core\windows\SDL_immdevice.c" />
     <ClCompile Include="..\..\src\core\windows\SDL_windows.c" />
     <ClCompile Include="..\..\src\core\windows\SDL_xinput.c" />
     <ClCompile Include="..\..\src\cpuinfo\SDL_cpuinfo.c" />
diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters
index 0e23dc5..c6efccb 100644
--- a/VisualC/SDL/SDL.vcxproj.filters
+++ b/VisualC/SDL/SDL.vcxproj.filters
@@ -432,6 +432,9 @@
     <ClInclude Include="..\..\src\core\windows\SDL_hid.h">
       <Filter>core\windows</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\core\windows\SDL_immdevice.h">
+      <Filter>core\windows</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\src\core\windows\SDL_windows.h">
       <Filter>core\windows</Filter>
     </ClInclude>
@@ -869,6 +872,9 @@
     <ClCompile Include="..\..\src\core\windows\SDL_hid.c">
       <Filter>core\windows</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\core\windows\SDL_immdevice.c">
+      <Filter>core\windows</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\src\core\windows\SDL_windows.c">
       <Filter>core\windows</Filter>
     </ClCompile>
diff --git a/src/audio/directsound/SDL_directsound.c b/src/audio/directsound/SDL_directsound.c
index 0242b13..b4b2fa2 100644
--- a/src/audio/directsound/SDL_directsound.c
+++ b/src/audio/directsound/SDL_directsound.c
@@ -29,11 +29,19 @@
 #include "SDL_audio.h"
 #include "../SDL_audio_c.h"
 #include "SDL_directsound.h"
+#if HAVE_MMDEVICEAPI_H
+#include "../../core/windows/SDL_immdevice.h"
+#endif /* HAVE_MMDEVICEAPI_H */
 
 #ifndef WAVE_FORMAT_IEEE_FLOAT
 #define WAVE_FORMAT_IEEE_FLOAT 0x0003
 #endif
 
+/* For Vista+, we can enumerate DSound devices with IMMDevice */
+#if HAVE_MMDEVICEAPI_H
+static SDL_bool SupportsIMMDevice = SDL_FALSE;
+#endif /* HAVE_MMDEVICEAPI_H */
+
 /* DirectX function pointers for audio */
 static void* DSoundDLL = NULL;
 typedef HRESULT (WINAPI *fnDirectSoundCreate8)(LPGUID,LPDIRECTSOUND*,LPUNKNOWN);
@@ -172,8 +180,16 @@ FindAllDevs(LPGUID guid, LPCWSTR desc, LPCWSTR module, LPVOID data)
 static void
 DSOUND_DetectDevices(void)
 {
-    pDirectSoundCaptureEnumerateW(FindAllDevs, (void *) ((size_t) 1));
-    pDirectSoundEnumerateW(FindAllDevs, (void *) ((size_t) 0));
+#if HAVE_MMDEVICEAPI_H
+    if (SupportsIMMDevice) {
+        SDL_IMMDevice_EnumerateEndpoints(SDL_TRUE);
+    } else {
+#endif /* HAVE_MMDEVICEAPI_H */
+        pDirectSoundCaptureEnumerateW(FindAllDevs, (void *)((size_t)1));
+        pDirectSoundEnumerateW(FindAllDevs, (void *)((size_t)0));
+#if HAVE_MMDEVICEAPI_H
+    }
+#endif /* HAVE_MMDEVICEAPI_H*/
 }
 
 
@@ -567,6 +583,12 @@ DSOUND_OpenDevice(_THIS, const char *devname)
 static void
 DSOUND_Deinitialize(void)
 {
+#if HAVE_MMDEVICEAPI_H
+    if (SupportsIMMDevice) {
+        SDL_IMMDevice_Quit();
+        SupportsIMMDevice = SDL_FALSE;
+    }
+#endif /* HAVE_MMDEVICEAPI_H */
     DSOUND_Unload();
 }
 
@@ -578,6 +600,10 @@ DSOUND_Init(SDL_AudioDriverImpl * impl)
         return SDL_FALSE;
     }
 
+#if HAVE_MMDEVICEAPI_H
+    SupportsIMMDevice = !(SDL_IMMDevice_Init() < 0);
+#endif /* HAVE_MMDEVICEAPI_H */
+
     /* Set the function pointers */
     impl->DetectDevices = DSOUND_DetectDevices;
     impl->OpenDevice = DSOUND_OpenDevice;
diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c
index 5b966aa..a678ca3 100644
--- a/src/audio/wasapi/SDL_wasapi.c
+++ b/src/audio/wasapi/SDL_wasapi.c
@@ -23,13 +23,13 @@
 #if SDL_AUDIO_DRIVER_WASAPI
 
 #include "../../core/windows/SDL_windows.h"
+#include "../../core/windows/SDL_immdevice.h"
 #include "SDL_audio.h"
 #include "SDL_timer.h"
 #include "../SDL_audio_c.h"
 #include "../SDL_sysaudio.h"
 
 #define COBJMACROS
-#include <mmdeviceapi.h>
 #include <audioclient.h>
 
 #include "SDL_wasapi.h"
@@ -45,10 +45,6 @@
 #define AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM 0x80000000
 #endif
 
-/* these increment as default devices change. Opened default devices pick up changes in their threads. */
-SDL_atomic_t WASAPI_DefaultPlaybackGeneration;
-SDL_atomic_t WASAPI_DefaultCaptureGeneration;
-
 /* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
 typedef struct DevIdList
 {
@@ -61,93 +57,6 @@ static DevIdList *deviceid_list = NULL;
 /* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
 static const IID SDL_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483,{ 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2 } };
 static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0,{ 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17 } };
-static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
-static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
-
-
-void
-WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid)
-{
-    DevIdList *i;
-    DevIdList *next;
-    DevIdList *prev = NULL;
-    for (i = deviceid_list; i; i = next) {
-        next = i->next;
-        if (SDL_wcscmp(i->str, devid) == 0) {
-            if (prev) {
-                prev->next = next;
-            } else {
-                deviceid_list = next;
-            }
-            SDL_RemoveAudioDevice(iscapture, i->str);
-            SDL_free(i->str);
-            SDL_free(i);
-        }
-        prev = i;
-    }
-}
-
-static SDL_AudioFormat
-WaveFormatToSDLFormat(WAVEFORMATEX *waveformat)
-{
-    if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
-        return AUDIO_F32SYS;
-    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) {
-        return AUDIO_S16SYS;
-    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) {
-        return AUDIO_S32SYS;
-    } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
-        const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *) waveformat;
-        if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
-            return AUDIO_F32SYS;
-        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 16)) {
-            return AUDIO_S16SYS;
-        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
-            return AUDIO_S32SYS;
-        }
-    }
-    return 0;
-}
-
-void
-WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENSIBLE *fmt, LPCWSTR devid)
-{
-    DevIdList *devidlist;
-    SDL_AudioSpec spec;
-
-    /* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever).
-       In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for
-       phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be
-       available and switch automatically. (!!! FIXME...?) */
-
-    /* see if we already have this one. */
-    for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
-        if (SDL_wcscmp(devidlist->str, devid) == 0) {
-            return;  /* we already have this. */
-        }
-    }
-
-    devidlist = (DevIdList *) SDL_malloc(sizeof (*devidlist));
-    if (!devidlist) {
-        return;  /* oh well. */
-    }
-
-    devid = SDL_wcsdup(devid);
-    if (!devid) {
-        SDL_free(devidlist);
-        return;  /* oh well. */
-    }
-
-    devidlist->str = (WCHAR *) devid;
-    devidlist->next = deviceid_list;
-    deviceid_list = devidlist;
-
-    SDL_zero(spec);
-    spec.channels = (Uint8)fmt->Format.nChannels;
-    spec.freq = fmt->Format.nSamplesPerSec;
-    spec.format = WaveFormatToSDLFormat((WAVEFORMATEX *) fmt);
-    SDL_AddAudioDevice(iscapture, devname, &spec, (void *) devid);
-}
 
 static void
 WASAPI_DetectDevices(void)
@@ -235,7 +144,7 @@ RecoverWasapiDevice(_THIS)
     ReleaseWasapiDevice(this);  /* dump the lost device's handles. */
 
     if (this->hidden->default_device_generation) {
-        this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ?  &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
+        this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ?  &SDL_IMMDevice_DefaultCaptureGeneration : &SDL_IMMDevice_DefaultPlaybackGeneration);
     }
 
     /* this can fail for lots of reasons, but the most likely is we had a
@@ -268,7 +177,7 @@ RecoverWasapiIfLost(_THIS)
     }
 
     if (!lost && (generation > 0)) { /* is a default device? */
-        const int newgen = SDL_AtomicGet(this->iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
+        const int newgen = SDL_AtomicGet(this->iscapture ? &SDL_IMMDevice_DefaultCaptureGeneration : &SDL_IMMDevice_DefaultPlaybackGeneration);
         if (generation != newgen) {  /* the desired default device was changed, jump over to it. */
             lost = SDL_TRUE;
         }
@@ -655,7 +564,7 @@ WASAPI_OpenDevice(_THIS, const char *devname)
     WASAPI_RefDevice(this);   /* so CloseDevice() will unref to zero. */
 
     if (!devid) {  /* is default device? */
-        this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
+        this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ? &SDL_IMMDevice_DefaultCaptureGeneration : &SDL_IMMDevice_DefaultPlaybackGeneration);
     } else {
         this->hidden->devid = SDL_wcsdup(devid);
         if (!this->hidden->devid) {
@@ -709,9 +618,6 @@ WASAPI_Deinitialize(void)
 static SDL_bool
 WASAPI_Init(SDL_AudioDriverImpl * impl)
 {
-    SDL_AtomicSet(&WASAPI_DefaultPlaybackGeneration, 1);
-    SDL_AtomicSet(&WASAPI_DefaultCaptureGeneration, 1);
-
     if (WASAPI_PlatformInit() == -1) {
         return SDL_FALSE;
     }
diff --git a/src/audio/wasapi/SDL_wasapi.h b/src/audio/wasapi/SDL_wasapi.h
index af79637..d4e81a3 100644
--- a/src/audio/wasapi/SDL_wasapi.h
+++ b/src/audio/wasapi/SDL_wasapi.h
@@ -55,16 +55,10 @@ struct SDL_PrivateAudioData
     SDL_atomic_t just_activated;
 };
 
-/* these increment as default devices change. Opened default devices pick up changes in their threads. */
-extern SDL_atomic_t WASAPI_DefaultPlaybackGeneration;
-extern SDL_atomic_t WASAPI_DefaultCaptureGeneration;
-
 /* win32 and winrt implementations call into these. */
 int WASAPI_PrepDevice(_THIS, const SDL_bool updatestream);
 void WASAPI_RefDevice(_THIS);
 void WASAPI_UnrefDevice(_THIS);
-void WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENSIBLE *fmt, LPCWSTR devid);
-void WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid);
 
 /* These are functions that are implemented differently for Windows vs WinRT. */
 int WASAPI_PlatformInit(void);
diff --git a/src/audio/wasapi/SDL_wasapi_win32.c b/src/audio/wasapi/SDL_wasapi_win32.c
index 137f370..ba8962f 100644
--- a/src/audio/wasapi/SDL_wasapi_win32.c
+++ b/src/audio/wasapi/SDL_wasapi_win32.c
@@ -29,28 +29,16 @@
 #if SDL_AUDIO_DRIVER_WASAPI && !defined(__WINRT__)
 
 #include "../../core/windows/SDL_windows.h"
+#include "../../core/windows/SDL_immdevice.h"
 #include "SDL_audio.h"
 #include "SDL_timer.h"
 #include "../SDL_audio_c.h"
 #include "../SDL_sysaudio.h"
 
-#define COBJMACROS
-#include <mmdeviceapi.h>
 #include <audioclient.h>
 
 #include "SDL_wasapi.h"
 
-static const ERole SDL_WASAPI_role = eConsole;  /* !!! FIXME: should this be eMultimedia? Should be a hint? */
-
-/* This is global to the WASAPI target, to handle hotplug and default device lookup. */
-static IMMDeviceEnumerator *enumerator = NULL;
-
-/* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */
-#ifdef PropVariantInit
-#undef PropVariantInit
-#endif
-#define PropVariantInit(p) SDL_zerop(p)
-
 /* handle to Avrt.dll--Vista and later!--for flagging the callback thread as "Pro Audio" (low latency). */
 static HMODULE libavrt = NULL;
 typedef HANDLE(WINAPI *pfnAvSetMmThreadCharacteristicsW)(LPCWSTR, LPDWORD);
@@ -59,203 +47,13 @@ static pfnAvSetMmThreadCharacteristicsW pAvSetMmThreadCharacteristicsW = NULL;
 static pfnAvRevertMmThreadCharacteristics pAvRevertMmThreadCharacteristics = NULL;
 
 /* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
-static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c,{ 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } };
-static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35,{ 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } };
-static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85,{ 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } };
-static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,{ 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } };
 static const IID SDL_IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32,{ 0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2 } };
-static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd,{ 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 };
-static const PROPERTYKEY SDL_PKEY_AudioEngine_DeviceFormat = { { 0xf19f064d, 0x82c, 0x4e27,{ 0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c, } }, 0 };
-
-
-static void
-GetWasapiDeviceInfo(IMMDevice *device, char **utf8dev, WAVEFORMATEXTENSIBLE *fmt)
-{
-    /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be
-       "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in
-       its own UIs, like Volume Control, etc. */
-    IPropertyStore *props = NULL;
-    *utf8dev = NULL;
-    SDL_zerop(fmt);
-    if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) {
-        PROPVARIANT var;
-        PropVariantInit(&var);
-        if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) {
-            *utf8dev = WIN_StringToUTF8W(var.pwszVal);
-        }
-        PropVariantClear(&var);
-        if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_AudioEngine_DeviceFormat, &var))) {
-            SDL_memcpy(fmt, var.blob.pBlobData, SDL_min(var.blob.cbSize, sizeof(WAVEFORMATEXTENSIBLE)));
-        }
-        PropVariantClear(&var);
-        IPropertyStore_Release(props);
-    }
-}
-
-
-/* We need a COM subclass of IMMNotificationClient for hotplug support, which is
-   easy in C++, but we have to tapdance more to make work in C.
-   Thanks to this page for coaching on how to make this work:
-     https://www.codeproject.com/Articles/13601/COM-in-plain-C */
-
-typedef struct SDLMMNotificationClient
-{
-    const IMMNotificationClientVtbl *lpVtbl;
-    SDL_atomic_t refcount;
-} SDLMMNotificationClient;
-
-static HRESULT STDMETHODCALLTYPE
-SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv)
-{
-    if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient)))
-    {
-        *ppv = this;
-        this->lpVtbl->AddRef(this);
-        return S_OK;
-    }
-
-    *ppv = NULL;
-    return E_NOINTERFACE;
-}
-
-static ULONG STDMETHODCALLTYPE
-SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis)
-{
-    SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
-    return (ULONG) (SDL_AtomicIncRef(&this->refcount) + 1);
-}
-
-static ULONG STDMETHODCALLTYPE
-SDLMMNotificationClient_Release(IMMNotificationClient *ithis)
-{
-    /* this is a static object; we don't ever free it. */
-    SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis;
-    const ULONG retval = SDL_AtomicDecRef(&this->refcount);
-    if (retval == 0) {
-        SDL_AtomicSet(&this->refcount, 0);  /* uhh... */
-        return 0;
-    }
-    return retval - 1;
-}
-
-/* These are the entry points called when WASAPI device endpoints change. */
-static HRESULT STDMETHODCALLTYPE
-SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId)
-{
-    if (role != SDL_WASAPI_role) {
-        return S_OK;  /* ignore it. */
-    }
-
-    /* Increment the "generation," so opened devices will pick this up in their threads. */
-    switch (flow) {
-        case eRender:
-            SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1);
-            break;
-
-        case eCapture:
-            SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1);
-            break;
-
-        case eAll:
-            SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1);
-            SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1);
-            break;
-
-        default:
-            SDL_assert(!"uhoh, unexpected OnDefaultDeviceChange flow!");
-            break;
-    }
-
-    return S_OK;
-}
-
-static HRESULT STDMETHODCALLTYPE
-SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
-{
-    /* we ignore this; devices added here then progress to ACTIVE, if appropriate, in 
-       OnDeviceStateChange, making that a better place to deal with device adds. More 
-       importantly: the first time you plug in a USB audio device, this callback will 
-       fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT).
-       Plugging it back in won't fire this callback again. */
-    return S_OK;
-}
-
-static HRESULT STDMETHODCALLTYPE
-SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
-{
-    /* See notes in OnDeviceAdded handler about why we ignore this. */
-    return S_OK;
-}
-
-static HRESULT STDMETHODCALLTYPE
-SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState)
-{
-    IMMDevice *device = NULL;
-
-    if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) {
-        IMMEndpoint *endpoint = NULL;
-        if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **) &endpoint))) {
-            EDataFlow flow;
-            if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) {
-                const SDL_bool iscapture = (flow == eCapture);
-                if (dwNewState == DEVICE_STATE_ACTIVE) {
-                    char *utf8dev;
-                    WAVEFORMATEXTENSIBLE fmt;
-                    GetWasapiDeviceInfo(device, &utf8dev, &fmt);
-                    if (utf8dev) {
-                        WASAPI_AddDevice(iscapture, utf8dev, &fmt, pwstrDeviceId);
-                        SDL_free(utf8dev);
-                    }
-                } else {
-                    WASAPI_RemoveDevice(iscapture, pwstrDeviceId);
-                }
-            }
-            IMMEndpoint_Release(endpoint);
-        }
-        IMMDevice_Release(device);
-    }
-
-    return S_OK;
-}
-
-static HRESULT STDMETHODCALLTYPE
-SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
-{
-    return S_OK;  /* we don't care about these. */
-}
-
-static const IMMNotificationClientVtbl notification_client_vtbl = {
-    SDLMMNotificationClient_QueryInterface,
-    SDLMMNotificationClient_AddRef,
-    SDLMMNotificationClient_Release,
-    SDLMMNotificationClient_OnDeviceStateChanged,
-    SDLMMNotificationClient_OnDeviceAdded,
-    SDLMMNotificationClient_OnDeviceRemoved,
-    SDLMMNotificationClient_OnDefaultDeviceChanged,
-    SDLMMNotificationClient_OnPropertyValueChanged
-};
-
-static SDLMMNotificationClient notification_client = { &notification_client_vtbl, { 1 } };
-
 
 int
 WASAPI_PlatformInit(void)
 {
-    HRESULT ret;
-
-    /* just skip the discussion with COM here. */
-    if (!WIN_IsWindowsVistaOrGreater()) {
-        return SDL_SetError("WASAPI support requires Windows Vista or later");
-    }
-
-    if (FAILED(WIN_CoInitialize())) {
-        return SDL_SetError("WASAPI: CoInitialize() failed");
-    }
-
-    ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID *) &enumerator);
-    if (FAILED(ret)) {
-        WIN_CoUninitialize();
-        return WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret);
+    if (SDL_IMMDevice_Init() < 0) {
+        return -1; /* This is set by SDL_IMMDevice_Init */
     }
 
     libavrt = LoadLibrary(TEXT("avrt.dll"));  /* this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now! */
@@ -270,11 +68,7 @@ WASAPI_PlatformInit(void)
 void
 WASAPI_PlatformDeinit(void)
 {
-    if (enumerator) {
-        IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) &notification_client);
-        IMMDeviceEnumerator_Release(enumerator);
-        enumerator = NULL;
-    }
+    SDL_IMMDevice_Quit();
 
     if (libavrt) {
         FreeLibrary(libavrt);
@@ -320,21 +114,12 @@ WASAPI_PlatformThreadDeinit(_THIS)
 int
 WASAPI_ActivateDevice(_THIS, const SDL_bool isrecovery)
 {
-    LPCWSTR devid = this->hidden->devid;
     IMMDevice *device = NULL;
     HRESULT ret;
 
-    if (devid == NULL) {
-        const EDataFlow dataflow = this->iscapture ? eCapture : eRender;
-        ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_WASAPI_role, &device);
-    } else {
-        ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, &device);
-    }
-
-    if (FAILED(ret)) {
-        SDL_assert(device == NULL);
+    if (SDL_IMMDevice_Get(this->hidden->devid, &device, this->iscapture) < 0) {
         this->hidden->client = NULL;
-        return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret);
+        return -1; /* This is already set by SDL_IMMDevice_Get */
     }
 
     /* this is not async in standard win32, yay! */
@@ -354,99 +139,10 @@ WASAPI_ActivateDevice(_THIS, const SDL_bool isrecovery)
     return 0;  /* good to go. */
 }
 
-
-typedef struct
-{
-    LPWSTR devid;
-    char *devname;
-    WAVEFORMATEXTENSIBLE fmt;
-} EndpointItem;
-
-static int SDLCALL sort_endpoints(const void *_a, const void *_b)
-{
-    LPWSTR a = ((const EndpointItem *) _a)->devid;
-    LPWSTR b = ((const EndpointItem *) _b)->devid;
-    if (!a && b) {
-        return -1;
-    } else if (a && !b) {
-        return 1;
-    }
-
-    while (SDL_TRUE) {
-        if (*a < *b) {
-            return -1;
-        } else if (*a > *b) {
-            return 1;
-        } else if (*a == 0) {
-            break;
-        }
-        a++;
-        b++;
-    }
-
-    return 0;
-}
-
-static void
-WASAPI_EnumerateEndpointsForFlow(const SDL_bool iscapture)
-{
-    IMMDeviceCollection *collection = NULL;
-    EndpointItem *items;
-    UINT i, total;
-
-    /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
-       ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
-
-    if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) {
-        return;
-    }
-
-    if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
-        IMMDeviceCollection_Release(collection);
-        return;
-    }
-
-    items = (EndpointItem *) SDL_calloc(total, sizeof (EndpointItem));
-    if (!items) {
-        return;  /* oh well. */
-    }
-
-    for (i = 0; i < total; i++) {
-        EndpointItem *item = items + i;
-        IMMDevice *device = NULL;
-        if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
-            if (SUCCEEDED(IMMDevice_GetId(device, &item->devid))) {
-                GetWasapiDeviceInfo(device, &item->devname, &item->fmt);
-            }
-            IMMDevice_Release(device);
-        }
-    }
-
-    /* sort the list of devices by their guid so list is consistent between runs */
-    SDL_qsort(items, total, sizeof (*items), sort_endpoints);
-
-    /* Send the sorted list on to the SDL's higher level. */
-    for (i = 0; i < total; i++) {
-        EndpointItem *item = items + i;
-        if ((item->devid) && (item->devname)) {
-            WASAPI_AddDevice(iscapture, item->devname, &item->fmt, item->devid);
-        }
-        SDL_free(item->devname);
-        CoTaskMemFree(item->devid);
-    }
-
-    SDL_free(items);
-    IMMDeviceCollection_Release(collection);
-}
-
 void
 WASAPI_EnumerateEndpoints(void)
 {
-    WASAPI_EnumerateEndpointsForFlow(SDL_FALSE);  /* playback */
-    WASAPI_EnumerateEndpointsForFlow(SDL_TRUE);  /* capture */
-
-    /* if this fails, we just won't get hotplug events. Carry on anyhow. */
-    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) &notification_client);
+    SDL_IMMDevice_EnumerateEndpoints(SDL_FALSE);
 }
 
 void
diff --git a/src/audio/wasapi/SDL_wasapi_winrt.cpp b/src/audio/wasapi/SDL_wasapi_winrt.cpp
index e063e6a..11ea0de 100644
--- a/src/audio/wasapi/SDL_wasapi_winrt.cpp
+++ b/src/audio/wasapi/SDL_wasapi_winrt.cpp
@@ -55,6 +55,13 @@ using namespace Microsoft::WRL;
 
 static Platform::String^ SDL_PKEY_AudioEngine_DeviceFormat = L"{f19f064d-082c-4e27-bc73-6882a1bb8e4c} 0";
 
+static void WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENSIBLE *fmt, LPCWSTR devid);
+static void WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid);
+extern "C" {
+    SDL_atomic_t SDL_IMMDevice_DefaultPlaybackGeneration;
+    SDL_atomic_t SDL_IMMDevice_DefaultCaptureGeneration;
+}
+
 class SDL_WasapiDeviceEventHandler
 {
 public:
@@ -174,14 +181,14 @@ void
 SDL_WasapiDeviceEventHandler::OnDefaultRenderDeviceChanged(Platform::Object^ sender, DefaultAudioRenderDeviceChangedEventArgs^ args)
 {
     SDL_assert(this->iscapture);
-    SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1);
+    SDL_AtomicAdd(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
 }
 
 void
 SDL_WasapiDeviceEventHandler::OnDefaultCaptureDeviceChanged(Platform::Object^ sender, DefaultAudioCaptureDeviceChangedEventArgs^ args)
 {
     SDL_assert(!this->iscapture);
-    SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1);
+    SDL_AtomicAdd(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
 }
 
 
@@ -190,6 +197,8 @@ static SDL_WasapiDeviceEventHandler *capture_device_event_handler;
 
 int WASAPI_PlatformInit(void)
 {
+    SDL_AtomicSet(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
+    SDL_AtomicSet(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
     return 0;
 }
 
@@ -317,6 +326,105 @@ WASAPI_PlatformThreadDeinit(_THIS)
     // !!! FIXME: set this thread to "Pro Audio" priority.
 }
 
+/* Everything below was copied from SDL_wasapi.c, before it got moved to SDL_immdevice.c! */
+
+static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
+static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
+
+extern "C" SDL_AudioFormat
+WaveFormatToSDLFormat(WAVEFORMATEX *waveformat)
+{
+    if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
+        return AUDIO_F32SYS;
+    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) {
+        return AUDIO_S16SYS;
+    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) {
+        return AUDIO_S32SYS;
+    } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
+        const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *)waveformat;
+        if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
+            return AUDIO_F32SYS;
+        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 16)) {
+            return AUDIO_S16SYS;
+        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
+            return AUDIO_S32SYS;
+        }
+    }
+    return 0;
+}
+
+/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
+typedef struct DevIdList
+{
+    WCHAR *str;
+    struct DevIdList *next;
+} DevIdList;
+
+static DevIdList *deviceid_list = NULL;
+
+static void
+WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid)
+{
+    DevIdList *i;
+    DevIdList *next;
+    DevIdList *prev = NULL;
+    for (i = deviceid_list; i; i = next) {
+        next = i->next;
+        if (SDL_wcscmp(i->str, devid) == 0) {
+            if (prev) {
+                prev->next = next;
+            }
+            else {
+                deviceid_list = next;
+            }
+            SDL_RemoveAudioDevice(iscapture, i->str);
+            SDL_free(i->str);
+            SDL_free(i);
+        }
+        prev = i;
+    }
+}
+
+static void
+WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENSIBLE *fmt, LPCWSTR devid)
+{
+    DevIdList *devidlist;
+    SDL_AudioSpec spec;
+
+    /* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever).
+       In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for
+       phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be
+       available and switch automatically. (!!! FIXME...?) */
+
+       /* see if we already have this one. */
+    for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
+        if (SDL_wcscmp(devidlist->str, devid) == 0) {
+            return;  /* we already have this. */
+        }
+    }
+
+    devidlist = (DevIdList *)SDL_malloc(sizeof(*devidlist));
+    if (!devidlist) {
+        return;  /* oh well. */
+    }
+
+    devid = SDL_wcsdup(devid);
+    if (!devid) {
+        SDL_free(devidlist);
+        return;  /* oh well. */
+    }
+
+    devidlist->str = (WCHAR *)devid;
+    devidlist->next = deviceid_list;
+    deviceid_list = devidlist;
+
+    SDL_zero(spec);
+    spec.channels = (Uint8)fmt->Format.nChannels;
+    spec.freq = fmt->Format.nSamplesPerSec;
+    spec.format = WaveFormatToSDLFormat((WAVEFORMATEX *)fmt);
+    SDL_AddAudioDevice(iscapture, devname, &spec, (void *)devid);
+}
+
 #endif  // SDL_AUDIO_DRIVER_WASAPI && defined(__WINRT__)
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/core/windows/SDL_immdevice.c b/src/core/windows/SDL_immdevice.c
new file mode 100644
index 0000000..415f983
--- /dev/null
+++ b/src/core/windows/SDL_immdevice.c
@@ -0,0 +1,486 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#if (defined(__WIN32__) || defined(__GDK__)) && HAVE_MMDEVICEAPI_H
+
+#include "SDL_windows.h"
+#include "SDL_immdevice.h"
+#include "../../audio/SDL_sysaudio.h"
+#include <objbase.h> /* For CLSIDFromString */
+
+static const ERole SDL_IMMDevice_role = eConsole;  /* !!! FIXME: should this be eMultimedia? Should be a hint? */
+
+/* This is global to the WASAPI target, to handle hotplug and default device lookup. */
+static IMMDeviceEnumerator *enumerator = NULL;
+
+/* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */
+#ifdef PropVariantInit
+#undef PropVariantInit
+#endif
+#define PropVariantInit(p) SDL_zerop(p)
+
+/* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
+static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c,{ 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } };
+static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35,{ 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } };
+static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85,{ 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } };
+static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,{ 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } };
+static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd,{ 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 };
+static const PROPERTYKEY SDL_PKEY_AudioEngine_DeviceFormat = { { 0xf19f064d, 0x82c, 0x4e27,{ 0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c, } }, 0 };
+static const PROPERTYKEY SDL_PKEY_AudioEndpoint_GUID = { { 0x1da5d803, 0xd492, 0x4edd,{ 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e, } }, 4 };
+static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
+static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
+
+/* these increment as default devices change. Opened default devices pick up changes in their threads. */
+SDL_atomic_t SDL_IMMDevice_DefaultPlaybackGeneration;
+SDL_atomic_t SDL_IMMDevice_DefaultCaptureGeneration;
+
+static void
+GetMMDeviceInfo(IMMDevice *device, char **utf8dev, WAVEFORMATEXTENSIBLE *fmt, GUID *guid)
+{
+    /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be
+       "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in
+       its own UIs, like Volume Control, etc. */
+    IPropertyStore *props = NULL;
+    *utf8dev = NULL;
+    SDL_zerop(fmt);
+    if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) {
+        PROPVARIANT var;
+        PropVariantInit(&var);
+        if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) {
+            *utf8dev = WIN_StringToUTF8W(var.pwszVal);
+        }
+        PropVariantClear(&var);
+        if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_AudioEngine_DeviceFormat, &var))) {
+            SDL_memcpy(fmt, var.blob.pBlobData, SDL_min(var.blob.cbSize, sizeof(WAVEFORMATEXTENSIBLE)));
+        }
+        PropVariantClear(&var);
+        if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_AudioEndpoint_GUID, &var))) {
+            CLSIDFromString(var.pwszVal, guid);
+        }
+        PropVariantClear(&var);
+        IPropertyStore_Release(props);
+    }
+}
+
+/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
+typedef struct DevIdList
+{
+    WCHAR *str;
+    LPGUID guid;
+    struct DevIdList *next;
+} DevIdList;
+
+static DevIdList *deviceid_list = NULL;
+
+static void
+SDL_IMMDevice_Remove(const SDL_bool iscapture, LPCWSTR devid, SDL_bool useguid)
+{
+    DevIdList *i;
+    DevIdList *next;
+    DevIdList *prev = NULL;
+    for (i = deviceid_list; i; i = next) {
+        next = i->next;
+        if (SDL_wcscmp(i->str, devid) == 0) {
+            if (prev) {
+                prev->next = next;
+            } else {
+                deviceid_list = next;
+            }
+            SDL_RemoveAudioDevice(iscapture, useguid ? ((void *) i->guid) : ((void *) i->str));
+            SDL_free(i->str);
+            SDL_free(i);
+        }
+        prev = i;
+    }
+}
+
+static void
+SDL_IMMDevice_Add(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENSIBLE *fmt, LPCWSTR devid, GUID *dsoundguid, SDL_bool useguid)
+{
+    DevIdList *devidlist;
+    SDL_AudioSpec spec;
+    LPGUID cpyguid;
+
+    /* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever).
+       In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for
+       phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be
+       available and switch automatically. (!!! FIXME...?) */
+
+       /* see if we already have this one. */
+    for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
+        if (SDL_wcscmp(devidlist->str, devid) == 0) {
+            return;  /* we already have this. */
+        }
+    }
+
+    devidlist = (DevIdList *)SDL_malloc(sizeof(*devidlist));
+    if (!devidlist) {
+        return;  /* oh well. */
+    }
+
+    devid = SDL_wcsdup(devid);
+    if (!devid) {
+        SDL_free(devidlist);
+        return;  /* oh well. */
+    }
+
+    cpyguid = (LPGUID)SDL_malloc(sizeof(GUID));
+    if (!cpyguid) {
+        SDL_free(devidlist);
+        return; /* oh well. */
+    }
+    SDL_memcpy(cpyguid, dsoundguid, sizeof(GUID));
+
+    devidlist->str = (WCHAR *)devid;
+    devidlist->guid = cpyguid;
+    devidlist->next = deviceid_list;
+    deviceid_list = devidlist;
+
+    SDL_zero(spec);
+    spec.channels = (Uint8)fmt->Format.nChannels;
+    spec.freq = fmt->Format.nSamplesPerSec;
+    spec.format = WaveFormatToSDLFormat((WAVEFORMATEX *)fmt);
+    SDL_AddAudioDevice(iscapture, devname, &spec, useguid ? ((void *) cpyguid) : ((void *) devid));
+}
+
+/* We need a COM subclass of IMMNotificationClient for hotplug support, which is
+   easy in C++, but we have to tapdance more to make work in C.
+   Thanks to this page for coaching on how to make this work:
+     https://www.codeproject.com/Articles/13601/COM-in-plain-C */
+
+typedef struct SDLMMNotificationClient
+{
+    const IMMNotificationClientVtbl *lpVtbl;
+    SDL_atomic_t refcount;
+    SDL_bool useguid;
+} SDLMMNotificationClient;
+
+static HRESULT STDMETHODCALLTYPE
+SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv)
+{
+    if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient))) {
+        *ppv = this;
+        this->lpVtbl->AddRef(this);
+        return S_OK;
+    }
+
+    *ppv = NULL;
+    return E_NOINTERFACE;
+}
+
+static ULONG STDMETHODCALLTYPE
+SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis)
+{
+    SDLMMNotificationClient *this = (SDLMMNotificationClient *)ithis;
+    return (ULONG)(SDL_AtomicIncRef(&this->refcount) + 1);
+}
+
+static ULONG STDMETHODCALLTYPE
+SDLMMNotificationClient_Release(IMMNotificationClient *ithis)
+{
+    /* this is a static object; we don't ever free it. */
+    SDLMMNotificationClient *this = (SDLMMNotificationClient *)ithis;
+    const ULONG retval = SDL_AtomicDecRef(&this->refcount);
+    if (retval == 0) {
+        SDL_AtomicSet(&this->refcount, 0);  /* uhh... */
+        return 0;
+    }
+    return retval - 1;
+}
+
+/* These are the entry points called when WASAPI device endpoints change. */
+static HRESULT STDMETHODCALLTYPE
+SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId)
+{
+    if (role != SDL_IMMDevice_role) {
+        return S_OK;  /* ignore it. */
+    }
+
+    /* Increment the "generation," so opened devices will pick this up in their threads. */
+    switch (flow) {
+    case eRender:
+        SDL_AtomicAdd(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
+        break;
+
+    case eCapture:
+        SDL_AtomicAdd(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
+        break;
+
+    case eAll:
+        SDL_AtomicAdd(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
+        SDL_AtomicAdd(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
+        break;
+
+    default:
+        SDL_assert(!"uhoh, unexpected OnDefaultDeviceChange flow!");
+        break;
+    }
+
+    return S_OK;
+}
+
+static HRESULT STDMETHODCALLTYPE
+SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
+{
+    /* we ignore this; devices added here then progress to ACTIVE, if appropriate, in
+       OnDeviceStateChange, making that a better place to deal with device adds. More
+       importantly: the first time you plug in a USB audio device, this callback will
+       fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT).
+       Plugging it back in won't fire this callback again. */
+    return S_OK;
+}
+
+static HRESULT STDMETHODCALLTYPE
+SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId)
+{
+    /* See notes in OnDeviceAdded handler about why we ignore this. */
+    return S_OK;
+}
+
+static HRESULT STDMETHODCALLTYPE
+SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState)
+{
+    IMMDevice *device = NULL;
+
+    if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) {
+        IMMEndpoint *endpoint = NULL;
+        if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **)&endpoint))) {
+            EDataFlow flow;
+            if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) {
+                const SDL_bool iscapture = (flow == eCapture);
+                const SDLMMNotificationClient *client = (SDLMMNotificationClient*) ithis;
+                if (dwNewState == DEVICE_STATE_ACTIVE) {
+                    char *utf8dev;
+                    WAVEFORMATEXTENSIBLE fmt;
+                    GUID dsoundguid;
+                    GetMMDeviceInfo(device, &utf8dev, &fmt, &dsoundguid);
+                    if (utf8dev) {
+                        SDL_IMMDevice_Add(iscapture, utf8dev, &fmt, pwstrDeviceId, &dsoundguid, client->useguid);
+                        SDL_free(utf8dev);
+                    }
+                } else {
+                    SDL_IMMDevice_Remove(iscapture, pwstrDeviceId, client->useguid);
+                }
+            }
+            IMMEndpoint_Release(endpoint);
+        }
+        IMMDevice_Release(device);
+    }
+
+    return S_OK;
+}
+
+static HRESULT STDMETHODCALLTYPE
+SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
+{
+    return S_OK;  /* we don't care about these. */
+}
+
+static const IMMNotificationClientVtbl notification_client_vtbl = {
+    SDLMMNotificationClient_QueryInterface,
+    SDLMMNotificationClient_AddRef,
+    SDLMMNotificationClient_Release,
+    SDLMMNotificationClient_OnDeviceStateChanged,
+    SDLMMNotificationClient_OnDeviceAdded,
+    SDLMMNotificationClient_OnDeviceRemoved,
+    SDLMMNotificationClient_OnDefaultDeviceChanged,
+    SDLMMNotificationClient_OnPropertyValueChanged
+};
+
+static SDLMMNotificationClient notification_client = { &notification_client_vtbl, { 1 } };
+
+int
+SDL_IMMDevice_Init(void)
+{
+    HRESULT ret;
+
+    SDL_AtomicSet(&SDL_IMMDevice_DefaultPlaybackGeneration, 1);
+    SDL_AtomicSet(&SDL_IMMDevice_DefaultCaptureGeneration, 1);
+
+    /* just skip the discussion with COM here. */
+    if (!WIN_IsWindowsVistaOrGreater()) {
+        return SDL_SetError("WASAPI support requires Windows Vista or later");
+    }
+
+    if (FAILED(WIN_CoInitialize())) {
+        return SDL_SetError("WASAPI: CoInitialize() failed");
+    }
+
+    ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID *)&enumerator);
+    if (FAILED(ret)) {
+        WIN_CoUninitialize();
+        return WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret);
+    }
+    return 0;
+}
+
+void
+SDL_IMMDevice_Quit(void)
+{
+    if (enumerator) {
+        IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *)&notification_client);
+        IMMDeviceEnumerator_Release(enumerator);
+        enumerator = NULL;
+    }
+}
+
+int
+SDL_IMMDevice_Get(LPCWSTR devid, IMMDevice **device, SDL_bool iscapture)
+{
+    HRESULT ret;
+
+    SDL_assert(device != NULL);
+
+    if (devid == NULL) {
+        const EDataFlow dataflow = iscapture ? eCapture : eRender;
+        ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_IMMDevice_role, device);
+    } else {
+        ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, device);
+    }
+
+    if (FAILED(ret)) {
+        SDL_assert(*device == NULL);
+        return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret);
+    }
+    return 0;
+}
+
+typedef struct
+{
+    LPWSTR devid;
+    char *devname;
+    WAVEFORMATEXTENSIBLE fmt;
+    GUID dsoundguid;
+} EndpointItem;
+
+static int SDLCALL sort_endpoints(const void *_a, const void *_b)
+{
+    LPWSTR a = ((const EndpointItem *)_a)->devid;
+    LPWSTR b = ((const EndpointItem *)_b)->devid;
+    if (!a && b) {
+        return -1;
+    } else if (a && !b) {
+        return 1;
+    }
+
+    while (SDL_TRUE) {
+        if (*a < *b) {
+            return -1;
+        } else if (*a > *b) {
+            return 1;
+        } else if (*a == 0) {
+            break;
+        }
+        a++;
+        b++;
+    }
+
+    return 0;
+}
+
+static void
+EnumerateEndpointsForFlow(const SDL_bool iscapture)
+{
+    IMMDeviceCollection *collection = NULL;
+    EndpointItem *items;
+    UINT i, total;
+
+    /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
+       ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
+
+    if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) {
+        return;
+    }
+
+    if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
+        IMMDeviceCollection_Release(collection);
+        return;
+    }
+
+    items = (EndpointItem *)SDL_calloc(total, sizeof(EndpointItem));
+    if (!items) {
+        return;  /* oh well. */
+    }
+
+    for (i = 0; i < total; i++) {
+        EndpointItem *item = items + i;
+        IMMDevice *device = NULL;
+        if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
+            if (SUCCEEDED(IMMDevice_GetId(device, &item->devid))) {
+                GetMMDeviceInfo(device, &item->devname, &item->fmt, &item->dsoundguid);
+            }
+            IMMDevice_Release(device);
+        }
+    }
+
+    /* sort the list of devices by their guid so list is consistent between runs */
+    SDL_qsort(items, total, sizeof(*items), sort_endpoints);
+
+    /* Send the sorted list on to the SDL's higher level. */
+    for (i = 0; i < total; i++) {
+        EndpointItem *item = items + i;
+        if ((item->devid) && (item->devname)) {
+            SDL_IMMDevice_Add(iscapture, item->devname, &item->fmt, item->devid, &item->dsoundguid, notification_client.useguid);
+        }
+        SDL_free(item->devname);
+        CoTaskMemFree(item->devid);
+    }
+
+    SDL_free(items);
+    IMMDeviceCollection_Release(collection);
+}
+
+void
+SDL_IMMDevice_EnumerateEndpoints(SDL_bool useguid)
+{
+    notification_client.useguid = useguid;
+
+    EnumerateEndpointsForFlow(SDL_FALSE);  /* playback */
+    EnumerateEndpointsForFlow(SDL_TRUE);  /* capture */
+
+    /* if this fails, we just won't get hotplug events. Carry on anyhow. */
+    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *)&notification_client);
+}
+
+SDL_AudioFormat
+WaveFormatToSDLFormat(WAVEFORMATEX *waveformat)
+{
+    if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
+        return AUDIO_F32SYS;
+    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) {
+        return AUDIO_S16SYS;
+    } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) {
+        return AUDIO_S32SYS;
+    } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
+        const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *)waveformat;
+        if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
+            return AUDIO_F32SYS;
+        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 16)) {
+            return AUDIO_S16SYS;
+        } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
+            return AUDIO_S32SYS;
+        }
+    }
+    return 0;
+}
+
+#endif /* (defined(__WIN32__) || defined(__GDK__)) && HAVE_MMDEVICEAPI_H */
diff --git a/src/core/windows/SDL_immdevice.h b/src/core/windows/SDL_immdevice.h
new file mode 100644
index 0000000..cdb87c5
--- /dev/null
+++ b/src/core/windows/SDL_immdevice.h
@@ -0,0 +1,43 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_IMMDEVICE_H
+#define SDL_IMMDEVICE_H
+
+#include "SDL_atomic.h"
+#include "SDL_audio.h"
+
+#define COBJMACROS
+#include <mmdeviceapi.h>
+#include <mmreg.h>
+
+int SDL_IMMDevice_Init(void);
+void SDL_IMMDevice_Quit(void);
+int SDL_IMMDevice_Get(LPCWSTR devid, IMMDevice **device, SDL_bool iscapture);
+void SDL_IMMDevice_EnumerateEndpoints(SDL_bool useguid);
+
+SDL_AudioFormat WaveFormatToSDLFormat(WAVEFORMATEX *waveformat);
+
+/* these increment as default devices change. Opened default devices pick up changes in their threads. */
+extern SDL_atomic_t SDL_IMMDevice_DefaultPlaybackGeneration;
+extern SDL_atomic_t SDL_IMMDevice_DefaultCaptureGeneration;
+
+#endif /* SDL_IMMDEVICE_H */