]> git.tdb.fi Git - libs/al.git/blobdiff - source/format.cpp
Add utility functions for manipulating formats
[libs/al.git] / source / format.cpp
diff --git a/source/format.cpp b/source/format.cpp
new file mode 100644 (file)
index 0000000..1cb5b23
--- /dev/null
@@ -0,0 +1,59 @@
+#include <stdexcept>
+#include "format.h"
+
+using namespace std;
+
+namespace Msp {
+namespace AL {
+
+Format create_format(unsigned sample_size, unsigned n_channels)
+{
+       if(n_channels==1)
+       {
+               if(sample_size==1)
+                       return MONO8;
+               else if(sample_size==2)
+                       return MONO16;
+       }
+       else if(n_channels==2)
+       {
+               if(sample_size==1)
+                       return STEREO8;
+               else if(sample_size==2)
+                       return STEREO16;
+       }
+
+       throw invalid_argument("create_format");
+}
+
+unsigned get_sample_size(Format fmt)
+{
+       switch(fmt)
+       {
+       case MONO8:
+       case STEREO8: return 1;
+       case MONO16:
+       case STEREO16: return 2;
+       default: throw invalid_argument("get_sample_size");
+       }
+}
+
+unsigned get_n_channels(Format fmt)
+{
+       switch(fmt)
+       {
+       case MONO8:
+       case MONO16: return 1;
+       case STEREO8:
+       case STEREO16: return 2;
+       default: throw invalid_argument("get_n_channels");
+       }
+}
+
+unsigned get_unit_size(Format fmt)
+{
+       return get_sample_size(fmt)*get_n_channels(fmt);
+}
+
+} // namespace AL
+} // namespace Msp