summaryrefslogtreecommitdiffstats
path: root/src/qalcontext.cpp
blob: 37a9c3f69bd251d6b388a23df87b7f0df6bb112d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/******************************************************************************
 * This file is part of the QtOpenAL project
 * Copyright (c) 2011 Laszlo Papp <lpapp@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "qalcontext.h"

#include "qalbufferqueue.h"

#include <QtCore/QDebug>

class QALContext::Private
{
    public:
        Private(const QALAttributes &attributes)
            : alcDevice(0)
            , alcContext(0)
            , requestedAttributes(attributes)
        {
        }

        ~Private()
        {
        }

        ALCdevice *alcDevice;
        ALCcontext *alcContext;
        QALAttributes requestedAttributes;
        QHash<QString, ALuint> loadedBuffers;
};

QALContext::QALContext(const QALAttributes &attributes)
    : d(new Private(attributes))
{
}

QALContext::~QALContext()
{
}

bool
QALContext::create()
{
    // Open the default device
    if ((d->alcDevice = alcOpenDevice(NULL)) == false)
        return false;

    if ((d->alcContext = alcCreateContext(d->alcDevice, NULL)) == 0)
    {
        alcCloseDevice(d->alcDevice);
        d->alcDevice = 0;
        return false;
    }

    return true;
}

QALAttributes
QALContext::requestedAttributes() const
{
    return d->requestedAttributes;
}

void
QALContext::setRequestedAttributes(const QALAttributes &attributes)
{
    d->requestedAttributes = attributes;
}

QALAttributes
QALContext::attributes() const
{
    ALCenum error;

    if ((error = alcGetError(d->alcDevice)) != ALC_NO_ERROR) {
        qWarning() << "Error before trying to create attributes:" << alcGetString(d->alcDevice, error);
    };

    QALAttributes attributes = QALAttributes::defaultAttributes();

    if (isValid() == false)
        return attributes;

    attributes.setDeviceSpecifier(alcGetString(d->alcDevice, ALC_DEVICE_SPECIFIER));

    ALCint tmp;

    alcGetIntegerv(d->alcDevice, ALC_FREQUENCY, 1, &tmp);
    attributes.setFrequency(tmp);

    alcGetIntegerv(d->alcDevice, ALC_REFRESH, 1, &tmp);
    attributes.setRefresh(tmp);

    alcGetIntegerv(d->alcDevice, ALC_SYNC, 1, &tmp);
    attributes.setSync(tmp);

    alcGetIntegerv(d->alcDevice, ALC_MONO_SOURCES, 1, &tmp);
    attributes.setMonoSources(tmp);

    alcGetIntegerv(d->alcDevice, ALC_STEREO_SOURCES, 1, &tmp);
    attributes.setStereoSources(tmp);

    return attributes;
}

bool
QALContext::isValid() const
{
    return alcGetContextsDevice(d->alcContext) != 0;
}

bool
QALContext::reset()
{
    ALCenum error;
    if ((error = alcGetError(d->alcDevice)) != ALC_NO_ERROR) {
        qWarning() << "Error before trying to destroy the context:" << alcGetString(d->alcDevice, error);
    };

    alcDestroyContext(d->alcContext);
    if ((error = alcGetError(d->alcDevice)) != ALC_NO_ERROR) {
        qWarning() << "Failed to destroy the context:" << alcGetString(d->alcDevice, error);
        return false;
    };

    d->alcContext = 0;

    alcCloseDevice(d->alcDevice);
    d->alcDevice = 0;

    return true;
}

bool
QALContext::makeCurrent()
{
    if (isValid() == false) {
        qWarning() << Q_FUNC_INFO << "Cannot make invalid context current";
        return false;
    }

    return alcMakeContextCurrent(d->alcContext);
}

bool
QALContext::doneCurrent()
{
    return alcMakeContextCurrent(0);
}

ALuint
QALContext::cacheBuffer(const QString& filename)
{
    ALuint buffer = d->loadedBuffers.value(filename, 0);
    if (!buffer) {
        // buffer = loadFromFile(filename);
        d->loadedBuffers.insert(filename, buffer);
    }

    return buffer;
}

bool
QALContext::deleteBuffer(ALuint bufferId)
{
    ALenum error;
    if ((error = alGetError()) != ALC_NO_ERROR) {
        qWarning() << "Error before trying to delete the buffer:" << alGetString(error);
    };

    alDeleteBuffers(1, &bufferId);

    if ((error = alGetError()) != AL_NO_ERROR) {
        qWarning() << "Failed to delete to buffer:" << alGetString(error);
        return false;
    };

    d->loadedBuffers.remove(d->loadedBuffers.key(bufferId));

    return true;
}

bool
QALContext::deleteBuffers()
{
    ALenum error;
    if ((error = alGetError()) != ALC_NO_ERROR) {
        qWarning() << "Error before trying to delete the buffers:" << alGetString(error);
    };

    QList<ALuint> identifiers = d->loadedBuffers.values();
    alDeleteBuffers(identifiers.count(), identifiers.toVector().data());

    if ((error = alGetError()) != AL_NO_ERROR) {
        qWarning() << "Failed to delete to buffers:" << alGetString(error);
        return false;
    };

    d->loadedBuffers.clear();

    return true;
}

void
QALContext::deleteBufferQueue(QALBufferQueue &bufferQueue)
{
}

void*
QALContext::getProcAddress(const QString &proc) const
{
    return alGetProcAddress(proc.toAscii().constData());
}