OSDN Git Service

6850da47f5bf6e45a21142805e3c5e9ef54d0db7
[cinnamon-audio/cinnamon.git] / cin_common.c
1 /*
2  * Copyright (c) 2018 AlaskanEmily
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7
8 #include "cin_common.h"
9 #include "cin_loader.h"
10 #include "cin_driver.h"
11
12 #include <assert.h>
13    
14 #define CIN_COMMON_LOADER_ASSERT_ENUMS_EQUAL(WHAT)\
15     assert((int)Cin_eLoader ## WHAT == (int)Cin_eDriver ## WHAT)
16
17 CIN_PRIVATE(enum Cin_LoaderError) Cin_FormatCompatible(struct Cin_Driver *drv,
18     unsigned sample_rate,
19     unsigned num_channels,
20     enum Cin_Format format){
21     
22     CIN_COMMON_LOADER_ASSERT_ENUMS_EQUAL(UnsupportedFormat);
23     CIN_COMMON_LOADER_ASSERT_ENUMS_EQUAL(InvalidFormat);
24     CIN_COMMON_LOADER_ASSERT_ENUMS_EQUAL(UnsupportedNumChannels);
25     CIN_COMMON_LOADER_ASSERT_ENUMS_EQUAL(UnsupportedSampleRate);
26     
27     {
28         const enum Cin_DriverError drv_err =
29             Cin_DriverSupportsFormat(drv, format, num_channels);
30         if(drv_err != Cin_eDriverSuccess){
31             assert(drv_err == Cin_eDriverUnsupportedFormat ||
32                 drv_err == Cin_eDriverInvalidFormat ||
33                 drv_err == Cin_eDriverUnsupportedNumChannels);
34             return (enum Cin_LoaderError)drv_err;
35         }
36     }
37     
38     {
39         const enum Cin_DriverError drv_err =
40             Cin_DriverSupportsSampleRate(drv, sample_rate);
41         if(drv_err != Cin_eDriverSuccess){
42             assert(drv_err == Cin_eLoaderUnsupportedSampleRate);
43             return (enum Cin_LoaderError)drv_err;
44         }
45     }
46     
47     return Cin_eLoaderSuccess;
48 }
49
50 CIN_PRIVATE_PURE(unsigned) Cin_BytesPerSample(enum Cin_Format format){
51     switch(format){
52         case Cin_eFormatS8:
53             return 1;
54         case Cin_eFormatS16:
55             return 2;
56         case Cin_eFormatS32:
57             return 4;
58         case Cin_eFormatFloat32:
59             return 4;
60         case Cin_eFormatFloat64:
61             return 8;
62         case Cin_eFormatULaw8:
63             return 1;
64         default:
65             return 0;
66     }
67 }