]> camargo.eng.br - camargo/neiasound.git/blob - src/nSoundBuffer.cpp
Initial commit
[camargo/neiasound.git] / src / nSoundBuffer.cpp
1 #include "nSoundBuffer.h"
2 #include "nSoundSystem.h"
3 #include "nSoundBag.h"
4 #include "nSoundStream.h"
5 #include "AL/al.h"
6
7 nSoundBuffer::nSoundBuffer(QString name, nSoundSystem * parent) :
8     QObject(parent)
9 {
10     setObjectName(name);
11
12     alGetError(); //reset error state
13
14     unsigned int hnd;
15     alGenBuffers(1, &hnd);
16     m_handle = hnd;
17
18     ALenum error = alGetError();
19     if(error!=AL_NO_ERROR)
20     {
21         throw QString("nSoundBuffer: alGenBuffers() failed.");
22     }
23 }
24
25 nSoundBuffer::~nSoundBuffer()
26 {
27     alGetError();
28
29     alDeleteBuffers(1, &m_handle);    
30
31     if(alGetError()!=AL_NO_ERROR)
32         throw QString("nSoundBuffer::~nSoundBuffer(): Error destroying openal buffer.");
33 }
34
35 void nSoundBuffer::setData(nSoundBag * bag)
36 {
37     alGetError();
38
39     alBufferData(m_handle, openalFormat(bag->m_format), bag->m_data, bag->m_data_size, bag->m_frequency);
40
41     if(alGetError()!=AL_NO_ERROR)
42         throw QString("nSoundBuffer::setData(): Error loading buffer data.");
43 }
44
45 void nSoundBuffer::setData(nSoundStream * stream)
46 {
47     nSoundBag * tmp = stream->createSoundBag();
48     setData(tmp);
49     delete tmp;
50 }
51
52 int nSoundBuffer::openalFormat(nSoundFormat format)
53 {
54     switch(format)
55     {
56     case SF_8BIT_MONO:
57         return AL_FORMAT_MONO8;
58
59     case SF_8BIT_STEREO:
60         return AL_FORMAT_STEREO8;
61
62     case SF_16BIT_MONO:
63         return AL_FORMAT_MONO16;
64
65     case SF_16BIT_STEREO:
66         return AL_FORMAT_STEREO16;
67     }
68
69     return -1;
70 }