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  {
33  g_runtime->m_listenerPos = pos;
34  g_runtime->configureListener();
35  }
36 }
37 
39 {
40  return g_runtime->m_volume;
41 }
42 
43 void KgAudioScene::setVolume(qreal volume)
44 {
45  if (g_runtime->m_volume != volume)
46  {
47  g_runtime->m_volume = volume;
48  g_runtime->configureListener();
49  }
50 }
51 
53 {
54  return g_runtime->m_error;
55 }
56 
57 //END KgAudioScene
58 //BEGIN KgOpenALRuntime
59 
60 KgOpenALRuntime::KgOpenALRuntime()
61  : m_volume(1)
62  , m_error(false)
63  , m_context(nullptr)
64  , m_device(alcOpenDevice(""))
65 {
66  if (!m_device)
67  {
68  qWarning() << "Failed to create OpenAL device";
69  m_error = true;
70  return;
71  }
72  m_context = alcCreateContext(m_device, nullptr);
73  int error = alcGetError(m_device);
74  if (error != AL_NO_ERROR)
75  {
76  qWarning() << "Failed to create OpenAL context: Error code" << error;
77  m_error = true;
78  return;
79  }
80  alcMakeContextCurrent(m_context);
81  configureListener();
82 }
83 
84 KgOpenALRuntime::~KgOpenALRuntime()
85 {
86  if (m_context == alcGetCurrentContext())
87  {
88  alcMakeContextCurrent(nullptr);
89  }
90  alcDestroyContext(m_context);
91  alcCloseDevice(m_device);
92 }
93 
94 KgOpenALRuntime* KgOpenALRuntime::instance()
95 {
96  return g_runtime;
97 }
98 
99 void KgOpenALRuntime::configureListener()
100 {
101  int error; alGetError(); //clear error cache
102  alListener3f(AL_POSITION, m_listenerPos.x(), m_listenerPos.y(), 0);
103  alListenerf(AL_GAIN, m_volume);
104  if ((error = alGetError()) != AL_NO_ERROR)
105  {
106  qWarning() << "Failed to setup OpenAL listener: Error code" << error;
107  m_error = true;
108  }
109 }
110 
111 //END KgOpenALRuntime
void setListenerPos(const QPointF &pos)
Sets the position of the listener.
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
void setVolume(qreal volume)
Sets the master volume for sounds outputted by TagaroAudio.
This class exposes general properties of the audio playback context.
Playback starts as soon as KgSound::start is called.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.