Skip to content

Commit f87a6db

Browse files
committed
SoundStream / Music Fixes
* Fixed SoundStreamAL buffer re-use * Abstracted private SoundStreamAL functions * Added music to music_list.h * Moved AL_ERROR_CHECK to /thirdparty/OpenAL.h * Added AL_ERROR_SILENCE
1 parent 4465d0a commit f87a6db

File tree

5 files changed

+95
-25
lines changed

5 files changed

+95
-25
lines changed

platforms/openal/SoundStreamAL.cpp

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "SoundStreamAL.hpp"
22
#include <assert.h>
3+
#include "common/Logger.hpp"
34

45
SoundStreamAL::SoundStreamAL()
56
{
67
_decoder = nullptr;
78
_info = stb_vorbis_info();
8-
alGenSources(1, &_source);
9-
alGenBuffers(2, _buffers);
9+
_createSource();
10+
_createBuffers();
1011
_tempPcmBufferSize = 4096 * 8;
1112
_tempPcmBuffer = new int16_t[_tempPcmBufferSize];
1213
_alFormat = AL_NONE;
@@ -20,9 +21,9 @@ SoundStreamAL::SoundStreamAL()
2021

2122
SoundStreamAL::~SoundStreamAL()
2223
{
23-
alDeleteSources(1, &_source);
24-
alDeleteBuffers(2, _buffers);
25-
close();
24+
_deleteSource();
25+
_deleteBuffers();
26+
_deleteDecoder();
2627
delete[] _tempPcmBuffer;
2728
}
2829

@@ -41,18 +42,67 @@ bool SoundStreamAL::_stream(uint32_t buffer)
4142
if (size == 0) return false;
4243

4344
alBufferData(buffer, _alFormat, _tempPcmBuffer, size * sizeof(int16_t), _info.sample_rate);
45+
AL_ERROR_CHECK();
4446
_totalSamplesLeft -= size;
4547

4648
return true;
4749
}
4850

51+
void SoundStreamAL::_deleteSource()
52+
{
53+
alDeleteSources(1, &_source);
54+
AL_ERROR_CHECK();
55+
}
56+
57+
void SoundStreamAL::_createSource()
58+
{
59+
alGenSources(1, &_source);
60+
}
61+
62+
void SoundStreamAL::_resetSource()
63+
{
64+
alSourceStop(_source);
65+
AL_ERROR_CHECK();
66+
alSourceRewind(_source);
67+
AL_ERROR_CHECK();
68+
// Detach all of the buffers from the source
69+
alSourcei(_source, AL_BUFFER, 0);
70+
AL_ERROR_CHECK();
71+
}
72+
73+
void SoundStreamAL::_deleteBuffers()
74+
{
75+
alDeleteBuffers(2, _buffers);
76+
AL_ERROR_CHECK();
77+
}
78+
79+
void SoundStreamAL::_createBuffers()
80+
{
81+
alGenBuffers(2, _buffers);
82+
}
83+
84+
void SoundStreamAL::_resetBuffers()
85+
{
86+
_deleteBuffers();
87+
_createBuffers();
88+
}
89+
90+
void SoundStreamAL::_deleteDecoder()
91+
{
92+
if (_decoder != nullptr)
93+
stb_vorbis_close(_decoder);
94+
_decoder = nullptr;
95+
}
96+
4997
bool SoundStreamAL::open(const std::string& fileName)
5098
{
5199
if (isStreaming())
52100
{
53101
close();
54102
}
55103

104+
_isPaused = false;
105+
56106
_decoder = stb_vorbis_open_filename(fileName.c_str(), NULL, NULL);
57107
if (!_decoder) return false;
58108
// Get file info
@@ -63,7 +113,9 @@ bool SoundStreamAL::open(const std::string& fileName)
63113
if (!_stream(_buffers[0])) return false;
64114
if (!_stream(_buffers[1])) return false;
65115
alSourceQueueBuffers(_source, 2, _buffers);
116+
AL_ERROR_CHECK();
66117
alSourcePlay(_source);
118+
AL_ERROR_CHECK();
67119

68120
_totalSamplesLeft = stb_vorbis_stream_length_in_samples(_decoder) * _info.channels;
69121
_isStreaming = true;
@@ -73,11 +125,9 @@ bool SoundStreamAL::open(const std::string& fileName)
73125

74126
void SoundStreamAL::close()
75127
{
76-
alSourceStop(_source);
77-
alSourceRewind(_source);
78-
if (_decoder != nullptr)
79-
stb_vorbis_close(_decoder);
80-
_decoder = nullptr;
128+
_resetSource();
129+
//_resetBuffers();
130+
_deleteDecoder();
81131
_alFormat = AL_NONE;
82132
_totalSamplesLeft = 0;
83133
_isStreaming = false;
@@ -90,12 +140,14 @@ void SoundStreamAL::update()
90140
int32_t processed = 0;
91141

92142
alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed);
143+
AL_ERROR_CHECK();
93144

94145
while (processed--)
95146
{
96147
uint32_t buffer = 0;
97148

98149
alSourceUnqueueBuffers(_source, 1, &buffer);
150+
AL_ERROR_CHECK();
99151

100152
if (!_stream(buffer))
101153
{
@@ -115,11 +167,12 @@ void SoundStreamAL::update()
115167
}
116168
}
117169
alSourceQueueBuffers(_source, 1, &buffer);
170+
AL_ERROR_CHECK();
118171
}
119172
}
120173

121174
void SoundStreamAL::setVolume(float vol)
122175
{
123176
alSourcef(_source, AL_GAIN, vol);
124-
assert(alGetError() == AL_NO_ERROR);
177+
AL_ERROR_CHECK();
125178
}

platforms/openal/SoundStreamAL.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class SoundStreamAL : public SoundStream
3333
private:
3434
bool _stream(uint32_t buffer);
3535

36+
void _deleteSource();
37+
void _createSource();
38+
void _resetSource();
39+
40+
void _deleteBuffers();
41+
void _createBuffers();
42+
void _resetBuffers();
43+
44+
void _deleteDecoder();
45+
3646
public:
3747
bool isStreaming() const override { return _isStreaming; }
3848
bool isPaused() const override { return _isPaused; }

platforms/openal/SoundSystemAL.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ SoundSystemAL::~SoundSystemAL()
2323
stopEngine();
2424
}
2525

26-
// Error Checking
27-
#define AL_ERROR_CHECK() AL_ERROR_CHECK_MANUAL(alGetError())
28-
#define AL_ERROR_CHECK_MANUAL(val) \
29-
{ \
30-
ALenum __err = val; \
31-
if (__err != AL_NO_ERROR) \
32-
{ \
33-
LOG_E("(%s:%i) OpenAL Error: %s", __FILE__, __LINE__, alGetString(__err)); \
34-
assert(!"An OpenAL error occurred!"); \
35-
} \
36-
}
37-
3826
bool SoundSystemAL::_hasMaxSources() const
3927
{
4028
return _sources.size() + _sources_idle.size() >= SOUND_MAX_SOURCES;

source/client/sound/music_list.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ MUSIC(calm, 2)
33
MUSIC(calm, 3)
44
MUSIC(piano, 1)
55
MUSIC(piano, 2)
6-
MUSIC(piano, 3)
6+
MUSIC(piano, 3)
7+
MUSIC(hal, 1)
8+
MUSIC(hal, 2)
9+
MUSIC(hal, 3)
10+
MUSIC(hal, 4)
11+
MUSIC(nuance, 1)
12+
MUSIC(nuance, 2)

thirdparty/OpenAL.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@
88
#else
99
#include <AL/al.h>
1010
#include <AL/alc.h>
11-
#endif
11+
#endif
12+
13+
// Error Checking
14+
#define AL_ERROR_CHECK() AL_ERROR_CHECK_MANUAL(alGetError())
15+
#define AL_ERROR_CHECK_MANUAL(val) \
16+
{ \
17+
ALenum __err = val; \
18+
if (__err != AL_NO_ERROR) \
19+
{ \
20+
LOG_E("(%s:%i) OpenAL Error: %s", __FILE__, __LINE__, alGetString(__err)); \
21+
assert(!"An OpenAL error occurred!"); \
22+
} \
23+
}
24+
#define AL_ERROR_SILENCE() { while ( alGetError() ); }

0 commit comments

Comments
 (0)