1 /// Translated from C to D
2 module soundio.channel_layout;
3 
4 extern(C): @nogc: nothrow: __gshared:
5 
6 import soundio.soundio_private;
7 import soundio.util;
8 import core.stdc.stdio;
9 import core.stdc..string: strlen;
10 
11 package:
12 
13 static SoundIoChannelLayout[26] builtin_channel_layouts = [
14     {
15         "Mono",
16         1,
17         [
18             SoundIoChannelId.FrontCenter,
19         ],
20     },
21     {
22         "Stereo",
23         2,
24         [
25             SoundIoChannelId.FrontLeft,
26             SoundIoChannelId.FrontRight,
27         ],
28     },
29     {
30         "2.1",
31         3,
32         [
33             SoundIoChannelId.FrontLeft,
34             SoundIoChannelId.FrontRight,
35             SoundIoChannelId.Lfe,
36         ],
37     },
38     {
39         "3.0",
40         3,
41         [
42             SoundIoChannelId.FrontLeft,
43             SoundIoChannelId.FrontRight,
44             SoundIoChannelId.FrontCenter,
45         ]
46     },
47     {
48         "3.0 (back)",
49         3,
50         [
51             SoundIoChannelId.FrontLeft,
52             SoundIoChannelId.FrontRight,
53             SoundIoChannelId.BackCenter,
54         ]
55     },
56     {
57         "3.1",
58         4,
59         [
60             SoundIoChannelId.FrontLeft,
61             SoundIoChannelId.FrontRight,
62             SoundIoChannelId.FrontCenter,
63             SoundIoChannelId.Lfe,
64         ]
65     },
66     {
67         "4.0",
68         4,
69         [
70             SoundIoChannelId.FrontLeft,
71             SoundIoChannelId.FrontRight,
72             SoundIoChannelId.FrontCenter,
73             SoundIoChannelId.BackCenter,
74         ]
75     },
76     {
77         "Quad",
78         4,
79         [
80             SoundIoChannelId.FrontLeft,
81             SoundIoChannelId.FrontRight,
82             SoundIoChannelId.BackLeft,
83             SoundIoChannelId.BackRight,
84         ],
85     },
86     {
87         "Quad (side)",
88         4,
89         [
90             SoundIoChannelId.FrontLeft,
91             SoundIoChannelId.FrontRight,
92             SoundIoChannelId.SideLeft,
93             SoundIoChannelId.SideRight,
94         ]
95     },
96     {
97         "4.1",
98         5,
99         [
100             SoundIoChannelId.FrontLeft,
101             SoundIoChannelId.FrontRight,
102             SoundIoChannelId.FrontCenter,
103             SoundIoChannelId.BackCenter,
104             SoundIoChannelId.Lfe,
105         ]
106     },
107     {
108         "5.0 (back)",
109         5,
110         [
111             SoundIoChannelId.FrontLeft,
112             SoundIoChannelId.FrontRight,
113             SoundIoChannelId.FrontCenter,
114             SoundIoChannelId.BackLeft,
115             SoundIoChannelId.BackRight,
116         ]
117     },
118     {
119         "5.0 (side)",
120         5,
121         [
122             SoundIoChannelId.FrontLeft,
123             SoundIoChannelId.FrontRight,
124             SoundIoChannelId.FrontCenter,
125             SoundIoChannelId.SideLeft,
126             SoundIoChannelId.SideRight,
127         ]
128     },
129     {
130         "5.1",
131         6,
132         [
133             SoundIoChannelId.FrontLeft,
134             SoundIoChannelId.FrontRight,
135             SoundIoChannelId.FrontCenter,
136             SoundIoChannelId.SideLeft,
137             SoundIoChannelId.SideRight,
138             SoundIoChannelId.Lfe,
139         ]
140     },
141     {
142         "5.1 (back)",
143         6,
144         [
145             SoundIoChannelId.FrontLeft,
146             SoundIoChannelId.FrontRight,
147             SoundIoChannelId.FrontCenter,
148             SoundIoChannelId.BackLeft,
149             SoundIoChannelId.BackRight,
150             SoundIoChannelId.Lfe,
151         ]
152     },
153     {
154         "6.0 (side)",
155         6,
156         [
157             SoundIoChannelId.FrontLeft,
158             SoundIoChannelId.FrontRight,
159             SoundIoChannelId.FrontCenter,
160             SoundIoChannelId.SideLeft,
161             SoundIoChannelId.SideRight,
162             SoundIoChannelId.BackCenter,
163         ]
164     },
165     {
166         "6.0 (front)",
167         6,
168         [
169             SoundIoChannelId.FrontLeft,
170             SoundIoChannelId.FrontRight,
171             SoundIoChannelId.SideLeft,
172             SoundIoChannelId.SideRight,
173             SoundIoChannelId.FrontLeftCenter,
174             SoundIoChannelId.FrontRightCenter,
175         ]
176     },
177     {
178         "Hexagonal",
179         6,
180         [
181             SoundIoChannelId.FrontLeft,
182             SoundIoChannelId.FrontRight,
183             SoundIoChannelId.FrontCenter,
184             SoundIoChannelId.BackLeft,
185             SoundIoChannelId.BackRight,
186             SoundIoChannelId.BackCenter,
187         ]
188     },
189     {
190         "6.1",
191         7,
192         [
193             SoundIoChannelId.FrontLeft,
194             SoundIoChannelId.FrontRight,
195             SoundIoChannelId.FrontCenter,
196             SoundIoChannelId.SideLeft,
197             SoundIoChannelId.SideRight,
198             SoundIoChannelId.BackCenter,
199             SoundIoChannelId.Lfe,
200         ]
201     },
202     {
203         "6.1 (back)",
204         7,
205         [
206             SoundIoChannelId.FrontLeft,
207             SoundIoChannelId.FrontRight,
208             SoundIoChannelId.FrontCenter,
209             SoundIoChannelId.BackLeft,
210             SoundIoChannelId.BackRight,
211             SoundIoChannelId.BackCenter,
212             SoundIoChannelId.Lfe,
213         ]
214     },
215     {
216         "6.1 (front)",
217         7,
218         [
219             SoundIoChannelId.FrontLeft,
220             SoundIoChannelId.FrontRight,
221             SoundIoChannelId.SideLeft,
222             SoundIoChannelId.SideRight,
223             SoundIoChannelId.FrontLeftCenter,
224             SoundIoChannelId.FrontRightCenter,
225             SoundIoChannelId.Lfe,
226         ]
227     },
228     {
229         "7.0",
230         7,
231         [
232             SoundIoChannelId.FrontLeft,
233             SoundIoChannelId.FrontRight,
234             SoundIoChannelId.FrontCenter,
235             SoundIoChannelId.SideLeft,
236             SoundIoChannelId.SideRight,
237             SoundIoChannelId.BackLeft,
238             SoundIoChannelId.BackRight,
239         ]
240     },
241     {
242         "7.0 (front)",
243         7,
244         [
245             SoundIoChannelId.FrontLeft,
246             SoundIoChannelId.FrontRight,
247             SoundIoChannelId.FrontCenter,
248             SoundIoChannelId.SideLeft,
249             SoundIoChannelId.SideRight,
250             SoundIoChannelId.FrontLeftCenter,
251             SoundIoChannelId.FrontRightCenter,
252         ]
253     },
254     {
255         "7.1",
256         8,
257         [
258             SoundIoChannelId.FrontLeft,
259             SoundIoChannelId.FrontRight,
260             SoundIoChannelId.FrontCenter,
261             SoundIoChannelId.SideLeft,
262             SoundIoChannelId.SideRight,
263             SoundIoChannelId.BackLeft,
264             SoundIoChannelId.BackRight,
265             SoundIoChannelId.Lfe,
266         ]
267     },
268     {
269         "7.1 (wide)",
270         8,
271         [
272             SoundIoChannelId.FrontLeft,
273             SoundIoChannelId.FrontRight,
274             SoundIoChannelId.FrontCenter,
275             SoundIoChannelId.SideLeft,
276             SoundIoChannelId.SideRight,
277             SoundIoChannelId.FrontLeftCenter,
278             SoundIoChannelId.FrontRightCenter,
279             SoundIoChannelId.Lfe,
280         ]
281     },
282     {
283         "7.1 (wide) (back)",
284         8,
285         [
286             SoundIoChannelId.FrontLeft,
287             SoundIoChannelId.FrontRight,
288             SoundIoChannelId.FrontCenter,
289             SoundIoChannelId.BackLeft,
290             SoundIoChannelId.BackRight,
291             SoundIoChannelId.FrontLeftCenter,
292             SoundIoChannelId.FrontRightCenter,
293             SoundIoChannelId.Lfe,
294         ]
295     },
296     {
297         "Octagonal",
298         8,
299         [
300             SoundIoChannelId.FrontLeft,
301             SoundIoChannelId.FrontRight,
302             SoundIoChannelId.FrontCenter,
303             SoundIoChannelId.SideLeft,
304             SoundIoChannelId.SideRight,
305             SoundIoChannelId.BackLeft,
306             SoundIoChannelId.BackRight,
307             SoundIoChannelId.BackCenter,
308         ]
309     },
310 ];
311 
312 enum CHANNEL_NAME_ALIAS_COUNT = 3;
313 alias const(char)*[CHANNEL_NAME_ALIAS_COUNT] channel_names_t;
314 static channel_names_t* channel_names = [
315     ["(Invalid Channel)", null, null],
316     ["Front Left", "FL", "front-left"],
317     ["Front Right", "FR", "front-right"],
318     ["Front Center", "FC", "front-center"],
319     ["LFE", "LFE", "lfe"],
320     ["Back Left", "BL", "rear-left"],
321     ["Back Right", "BR", "rear-right"],
322     ["Front Left Center", "FLC", "front-left-of-center"],
323     ["Front Right Center", "FRC", "front-right-of-center"],
324     ["Back Center", "BC", "rear-center"],
325     ["Side Left", "SL", "side-left"],
326     ["Side Right", "SR", "side-right"],
327     ["Top Center", "TC", "top-center"],
328     ["Top Front Left", "TFL", "top-front-left"],
329     ["Top Front Center", "TFC", "top-front-center"],
330     ["Top Front Right", "TFR", "top-front-right"],
331     ["Top Back Left", "TBL", "top-rear-left"],
332     ["Top Back Center", "TBC", "top-rear-center"],
333     ["Top Back Right", "TBR", "top-rear-right"],
334     ["Back Left Center", null, null],
335     ["Back Right Center", null, null],
336     ["Front Left Wide", null, null],
337     ["Front Right Wide", null, null],
338     ["Front Left High", null, null],
339     ["Front Center High", null, null],
340     ["Front Right High", null, null],
341     ["Top Front Left Center", null, null],
342     ["Top Front Right Center", null, null],
343     ["Top Side Left", null, null],
344     ["Top Side Right", null, null],
345     ["Left LFE", null, null],
346     ["Right LFE", null, null],
347     ["LFE 2", null, null],
348     ["Bottom Center", null, null],
349     ["Bottom Left Center", null, null],
350     ["Bottom Right Center", null, null],
351     ["Mid/Side Mid", null, null],
352     ["Mid/Side Side", null, null],
353     ["Ambisonic W", null, null],
354     ["Ambisonic X", null, null],
355     ["Ambisonic Y", null, null],
356     ["Ambisonic Z", null, null],
357     ["X-Y X", null, null],
358     ["X-Y Y", null, null],
359     ["Headphones Left", null, null],
360     ["Headphones Right", null, null],
361     ["Click Track", null, null],
362     ["Foreign Language", null, null],
363     ["Hearing Impaired", null, null],
364     ["Narration", null, null],
365     ["Haptic", null, null],
366     ["Dialog Centric Mix", null, null],
367     ["Aux", null, null],
368     ["Aux 0", null, null],
369     ["Aux 1", null, null],
370     ["Aux 2", null, null],
371     ["Aux 3", null, null],
372     ["Aux 4", null, null],
373     ["Aux 5", null, null],
374     ["Aux 6", null, null],
375     ["Aux 7", null, null],
376     ["Aux 8", null, null],
377     ["Aux 9", null, null],
378     ["Aux 10", null, null],
379     ["Aux 11", null, null],
380     ["Aux 12", null, null],
381     ["Aux 13", null, null],
382     ["Aux 14", null, null],
383     ["Aux 15", null, null],
384 ];
385 
386 const(char)* soundio_get_channel_name(SoundIoChannelId id) {
387     if (id >= channel_names.length)
388         return "(Invalid Channel)";
389     else
390         return channel_names[id][0];
391 }
392 
393 bool soundio_channel_layout_equal(const(SoundIoChannelLayout)* a, const(SoundIoChannelLayout)* b) {
394     if (a.channel_count != b.channel_count)
395         return false;
396 
397     for (int i = 0; i < a.channel_count; i += 1) {
398         if (a.channels[i] != b.channels[i])
399             return false;
400     }
401 
402     return true;
403 }
404 
405 int soundio_channel_layout_builtin_count() {
406     return builtin_channel_layouts.length;
407 }
408 
409 const(SoundIoChannelLayout)* soundio_channel_layout_get_builtin(int index) {
410     assert(index >= 0);
411     assert(index <= builtin_channel_layouts.length);
412     return &builtin_channel_layouts[index];
413 }
414 
415 int soundio_channel_layout_find_channel(const(SoundIoChannelLayout)* layout, SoundIoChannelId channel) {
416     for (int i = 0; i < layout.channel_count; i += 1) {
417         if (layout.channels[i] == channel)
418             return i;
419     }
420     return -1;
421 }
422 
423 bool soundio_channel_layout_detect_builtin(SoundIoChannelLayout* layout) {
424     for (int i = 0; i < builtin_channel_layouts.length; i += 1) {
425         const(SoundIoChannelLayout)* builtin_layout = &builtin_channel_layouts[i];
426         if (soundio_channel_layout_equal(builtin_layout, layout)) {
427             layout.name = builtin_layout.name;
428             return true;
429         }
430     }
431     layout.name = null;
432     return false;
433 }
434 
435 const(SoundIoChannelLayout)* soundio_channel_layout_get_default(int channel_count) {
436     switch (channel_count) {
437         case 1: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId.Mono);
438         case 2: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId.Stereo);
439         case 3: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId._3Point0);
440         case 4: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId._4Point0);
441         case 5: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId._5Point0Back);
442         case 6: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId._5Point1Back);
443         case 7: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId._6Point1);
444         case 8: return soundio_channel_layout_get_builtin(SoundIoChannelLayoutId._7Point1);
445         default: break;
446     }
447     return null;
448 }
449 
450 SoundIoChannelId soundio_parse_channel_id(const(char)* str, int str_len) {
451     for (int id = 0; id < channel_names.length; id += 1) {
452         for (int i = 0; i < CHANNEL_NAME_ALIAS_COUNT; i += 1) {
453             const(char)* alias_ = channel_names[id][i];
454             if (!alias_)
455                 break;
456             int alias_len = cast(int) strlen(alias_);
457             if (soundio_streql(alias_, alias_len, str, str_len))
458                 return cast(SoundIoChannelId)id;
459         }
460     }
461     return SoundIoChannelId.Invalid;
462 }