KDEGames

kgameaudioscene-openal.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#include "kgameaudioscene.h"
8
9// own
10#include "kgameopenalruntime_p.h"
11#include <kdegames_audio_logging.h>
12
13Q_GLOBAL_STATIC(KGameOpenALRuntime, g_runtime)
14
15// BEGIN KGameAudioScene
16
18{
19 return SupportsLowLatencyPlayback | SupportsPositionalPlayback;
20}
21
23{
24 return g_runtime->m_listenerPos;
25}
26
28{
29 if (g_runtime->m_listenerPos != pos) {
30 g_runtime->m_listenerPos = pos;
31 g_runtime->configureListener();
32 }
33}
34
36{
37 return g_runtime->m_volume;
38}
39
41{
42 if (g_runtime->m_volume != volume) {
43 g_runtime->m_volume = volume;
44 g_runtime->configureListener();
45 }
46}
47
49{
50 return g_runtime->m_error;
51}
52
53// END KGameAudioScene
54// BEGIN KGameOpenALRuntime
55
56KGameOpenALRuntime::KGameOpenALRuntime()
57 : m_volume(1)
58 , m_error(false)
59 , m_context(nullptr)
60 , m_device(alcOpenDevice(""))
61{
62 if (!m_device) {
63 qCWarning(KDEGAMES_AUDIO_LOG) << "Failed to create OpenAL device";
64 m_error = true;
65 return;
66 }
67 m_context = alcCreateContext(m_device, nullptr);
68 int error = alcGetError(m_device);
69 if (error != AL_NO_ERROR) {
70 qCWarning(KDEGAMES_AUDIO_LOG) << "Failed to create OpenAL context: Error code" << error;
71 m_error = true;
72 return;
73 }
74 alcMakeContextCurrent(m_context);
75 configureListener();
76}
77
78KGameOpenALRuntime::~KGameOpenALRuntime()
79{
80 if (m_context == alcGetCurrentContext()) {
81 alcMakeContextCurrent(nullptr);
82 }
83 alcDestroyContext(m_context);
84 alcCloseDevice(m_device);
85}
86
87KGameOpenALRuntime *KGameOpenALRuntime::instance()
88{
89 return g_runtime;
90}
91
92void KGameOpenALRuntime::configureListener()
93{
94 int error;
95 alGetError(); // clear error cache
96 alListener3f(AL_POSITION, m_listenerPos.x(), m_listenerPos.y(), 0);
97 alListenerf(AL_GAIN, m_volume);
98 if ((error = alGetError()) != AL_NO_ERROR) {
99 qCWarning(KDEGAMES_AUDIO_LOG) << "Failed to setup OpenAL listener: Error code" << error;
100 m_error = true;
101 }
102}
103
104// END KGameOpenALRuntime
This class exposes general properties of the audio playback context.
@ SupportsLowLatencyPlayback
Playback starts as soon as KGameSound::start is called.
void setListenerPos(QPointF pos)
Sets the position of the listener.
void setVolume(qreal volume)
Sets the master volume for sounds outputted by TagaroAudio.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:16 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.