]> git.tdb.fi Git - libs/al.git/blob - source/format.cpp
Add utility functions for manipulating formats
[libs/al.git] / source / format.cpp
1 #include <stdexcept>
2 #include "format.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace AL {
8
9 Format create_format(unsigned sample_size, unsigned n_channels)
10 {
11         if(n_channels==1)
12         {
13                 if(sample_size==1)
14                         return MONO8;
15                 else if(sample_size==2)
16                         return MONO16;
17         }
18         else if(n_channels==2)
19         {
20                 if(sample_size==1)
21                         return STEREO8;
22                 else if(sample_size==2)
23                         return STEREO16;
24         }
25
26         throw invalid_argument("create_format");
27 }
28
29 unsigned get_sample_size(Format fmt)
30 {
31         switch(fmt)
32         {
33         case MONO8:
34         case STEREO8: return 1;
35         case MONO16:
36         case STEREO16: return 2;
37         default: throw invalid_argument("get_sample_size");
38         }
39 }
40
41 unsigned get_n_channels(Format fmt)
42 {
43         switch(fmt)
44         {
45         case MONO8:
46         case MONO16: return 1;
47         case STEREO8:
48         case STEREO16: return 2;
49         default: throw invalid_argument("get_n_channels");
50         }
51 }
52
53 unsigned get_unit_size(Format fmt)
54 {
55         return get_sample_size(fmt)*get_n_channels(fmt);
56 }
57
58 } // namespace AL
59 } // namespace Msp