KDEGames

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

KDE's Doxygen guidelines are available online.