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 #include <QGlobalStatic>
14 
15 Q_GLOBAL_STATIC(KgOpenALRuntime, g_runtime)
16 
17 // BEGIN KgAudioScene
18 
20 {
21  return SupportsLowLatencyPlayback | SupportsPositionalPlayback;
22 }
23 
25 {
26  return g_runtime->m_listenerPos;
27 }
28 
30 {
31  if (g_runtime->m_listenerPos != pos) {
32  g_runtime->m_listenerPos = pos;
33  g_runtime->configureListener();
34  }
35 }
36 
38 {
39  return g_runtime->m_volume;
40 }
41 
42 void KgAudioScene::setVolume(qreal volume)
43 {
44  if (g_runtime->m_volume != volume) {
45  g_runtime->m_volume = volume;
46  g_runtime->configureListener();
47  }
48 }
49 
51 {
52  return g_runtime->m_error;
53 }
54 
55 // END KgAudioScene
56 // BEGIN KgOpenALRuntime
57 
58 KgOpenALRuntime::KgOpenALRuntime()
59  : m_volume(1)
60  , m_error(false)
61  , m_context(nullptr)
62  , m_device(alcOpenDevice(""))
63 {
64  if (!m_device) {
65  qWarning() << "Failed to create OpenAL device";
66  m_error = true;
67  return;
68  }
69  m_context = alcCreateContext(m_device, nullptr);
70  int error = alcGetError(m_device);
71  if (error != AL_NO_ERROR) {
72  qWarning() << "Failed to create OpenAL context: Error code" << error;
73  m_error = true;
74  return;
75  }
76  alcMakeContextCurrent(m_context);
77  configureListener();
78 }
79 
80 KgOpenALRuntime::~KgOpenALRuntime()
81 {
82  if (m_context == alcGetCurrentContext()) {
83  alcMakeContextCurrent(nullptr);
84  }
85  alcDestroyContext(m_context);
86  alcCloseDevice(m_device);
87 }
88 
89 KgOpenALRuntime *KgOpenALRuntime::instance()
90 {
91  return g_runtime;
92 }
93 
94 void KgOpenALRuntime::configureListener()
95 {
96  int error;
97  alGetError(); // clear error cache
98  alListener3f(AL_POSITION, m_listenerPos.x(), m_listenerPos.y(), 0);
99  alListenerf(AL_GAIN, m_volume);
100  if ((error = alGetError()) != AL_NO_ERROR) {
101  qWarning() << "Failed to setup OpenAL listener: Error code" << error;
102  m_error = true;
103  }
104 }
105 
106 // 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.
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
@ SupportsLowLatencyPlayback
Playback starts as soon as KgSound::start is called.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 03:57:35 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.