KDEGames

kgaudioscene-openal.cpp
1 /***************************************************************************
2  * Copyright 2010 Stefan Majewsky <[email protected]> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Library General Public License *
6  * version 2 as published by the Free Software Foundation *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU Library General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU Library General Public *
14  * License along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
17  ***************************************************************************/
18 
19 #include "kgaudioscene.h"
20 #include "kgopenalruntime_p.h"
21 
22 #include <QDebug>
23 #include <QGlobalStatic>
24 
25 Q_GLOBAL_STATIC(KgOpenALRuntime, g_runtime)
26 
27 //BEGIN KgAudioScene
28 
30 {
31  return SupportsLowLatencyPlayback | SupportsPositionalPlayback;
32 }
33 
35 {
36  return g_runtime->m_listenerPos;
37 }
38 
40 {
41  if (g_runtime->m_listenerPos != pos)
42  {
43  g_runtime->m_listenerPos = pos;
44  g_runtime->configureListener();
45  }
46 }
47 
49 {
50  return g_runtime->m_volume;
51 }
52 
53 void KgAudioScene::setVolume(qreal volume)
54 {
55  if (g_runtime->m_volume != volume)
56  {
57  g_runtime->m_volume = volume;
58  g_runtime->configureListener();
59  }
60 }
61 
63 {
64  return g_runtime->m_error;
65 }
66 
67 //END KgAudioScene
68 //BEGIN KgOpenALRuntime
69 
70 KgOpenALRuntime::KgOpenALRuntime()
71  : m_volume(1)
72  , m_error(false)
73  , m_context(nullptr)
74  , m_device(alcOpenDevice(""))
75 {
76  if (!m_device)
77  {
78  qWarning() << "Failed to create OpenAL device";
79  m_error = true;
80  return;
81  }
82  m_context = alcCreateContext(m_device, nullptr);
83  int error = alcGetError(m_device);
84  if (error != AL_NO_ERROR)
85  {
86  qWarning() << "Failed to create OpenAL context: Error code" << error;
87  m_error = true;
88  return;
89  }
90  alcMakeContextCurrent(m_context);
91  configureListener();
92 }
93 
94 KgOpenALRuntime::~KgOpenALRuntime()
95 {
96  if (m_context == alcGetCurrentContext())
97  {
98  alcMakeContextCurrent(nullptr);
99  }
100  alcDestroyContext(m_context);
101  alcCloseDevice(m_device);
102 }
103 
104 KgOpenALRuntime* KgOpenALRuntime::instance()
105 {
106  return g_runtime;
107 }
108 
109 void KgOpenALRuntime::configureListener()
110 {
111  int error; alGetError(); //clear error cache
112  alListener3f(AL_POSITION, m_listenerPos.x(), m_listenerPos.y(), 0);
113  alListenerf(AL_GAIN, m_volume);
114  if ((error = alGetError()) != AL_NO_ERROR)
115  {
116  qWarning() << "Failed to setup OpenAL listener: Error code" << error;
117  m_error = true;
118  }
119 }
120 
121 //END KgOpenALRuntime
KDEGAMES_EXPORT QPointF listenerPos()
KDEGAMES_EXPORT qreal volume()
KDEGAMES_EXPORT void setListenerPos(const QPointF &pos)
Sets the position of the listener.
KDEGAMES_EXPORT 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.
KDEGAMES_EXPORT bool hasError()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:36:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.