Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpenALHelpers.cpp
Go to the documentation of this file.
1 //
2 // C++ Implementation: OpenAL helper functions
3 //
4 
5 #include "OpenALHelpers.h"
6 
7 #include "../../Types.h"
8 #include "../../Exceptions.h"
9 
10 #include "config.h"
11 #include "al.h"
12 
13 #include <string>
14 #include <stdio.h>
15 
16 namespace Audio {
17  namespace __impl {
18  namespace OpenAL {
19 
20  void _checkAlErrorAt(ALenum error, const char *filename, int lineno)
21  throw (Exception)
22  {
23  switch(error) {
24  case AL_NO_ERROR : return;
25  default:
26  {
27  const char* errdesc = (const char *)alGetString(error);
28  char s_lineno[32];
29  char s_errcode_buf[32];
30  const char *s_errcode;
31  sprintf(s_lineno, "%d", lineno);
32 
33  switch (error) {
34  case ALC_INVALID_DEVICE: s_errcode = "(Invalid device)"; break;
35  case ALC_INVALID_CONTEXT:s_errcode = "(Invalid context id)"; break;
36  case ALC_INVALID_ENUM: s_errcode = "(Bad enum)"; break;
37  case ALC_INVALID_VALUE: s_errcode = "(Bad value)"; break;
38  case ALC_OUT_OF_MEMORY: s_errcode = "(Out of memory)"; break;
39  default:
40  sprintf(s_errcode_buf, "(0x%x)", error);
41  s_errcode = s_errcode_buf;
42  };
43 
44  std::string error("OpenAL error: ");
45  error += std::string(errdesc ? errdesc : "unknown") + s_errcode + " at " + filename + ":" + s_lineno;
46  fprintf(stderr, "%s\n", error.c_str());
47  throw Exception(error);
48  }
49  }
50  }
51 
53  throw()
54  {
55  alGetError();
56  }
57 
58  ALenum asALFormat(const Format &format)
59  throw (Exception)
60  {
61  ALenum alformat;
62  switch(format.bitsPerSample) {
63  case 8:
64  switch(format.channels) {
65  case 1: alformat = AL_FORMAT_MONO8; break;
66  case 2: alformat = AL_FORMAT_STEREO8; break;
67  default: throw Exception("internal error - unexpected number of channels");
68  };
69  break;
70  case 16:
71  switch(format.channels) {
72  case 1: alformat = AL_FORMAT_MONO16; break;
73  case 2: alformat = AL_FORMAT_STEREO16; break;
74  default: throw Exception("internal error - unexpected number of channels");
75  };
76  break;
77  default:
78  throw Exception("internal error - unexpected bit depth");
79  };
80 
81  return alformat;
82  }
83 
84  }
85  }
86 }