1
1
#include " sound.hpp"
2
2
#include " audio_exceptions.hpp"
3
- #include " sound_context.hpp"
4
3
#include < cmath>
5
4
#include < stdexcept>
6
5
@@ -13,83 +12,64 @@ Sound::Sound(SoundDataInterface const& soundData)
13
12
m_format = AL_FORMAT_STEREO_FLOAT32;
14
13
}
15
14
16
- createSource ();
17
-
18
- // Create and fill buffers
19
- createBuffers ();
20
- fillBufferFromStart ();
15
+ initSourceAndBuffers ();
21
16
22
17
auto const errorIfAny = alGetError ();
23
18
if (errorIfAny != AL_NO_ERROR) {
24
- auto const errorMessage
25
- = " Could not create OpenAL soundData, error code: " + std::to_string (errorIfAny);
26
- throw oalpp::AudioException { errorMessage.c_str () };
19
+ throw oalpp::AudioException { " Could not create OpenAL soundData, error code: "
20
+ + std::to_string (errorIfAny) };
27
21
}
28
22
}
29
-
30
- Sound::~Sound ()
31
- {
32
- deleteSource ();
33
- deleteBuffers ();
34
- }
35
-
36
- void Sound::deleteBuffers ()
23
+ void Sound::initSourceAndBuffers ()
37
24
{
38
- alDeleteBuffers (static_cast <ALsizei>(m_bufferIds.size ()), m_bufferIds.data ());
39
- }
25
+ // Create source
26
+ alGenSources (1 , &m_sourceId);
27
+ alSourcef (m_sourceId, AL_PITCH, 1 .0f );
28
+ alSourcef (m_sourceId, AL_GAIN, m_volume);
29
+ alSource3f (m_sourceId, AL_POSITION, 0 .0f , 0 , -1 .0f );
30
+ alSource3f (m_sourceId, AL_VELOCITY, 0 .0f , 0 .0f , 0 .0f );
31
+ alSourcei (m_sourceId, AL_LOOPING, AL_FALSE);
32
+ alSourcef (m_sourceId, AL_ROLLOFF_FACTOR, 0 .0f );
33
+ alSourcei (m_sourceId, AL_SOURCE_RELATIVE, true );
40
34
41
- void Sound::deleteSource () const { alDeleteSources ( 1 , &m_sourceId); }
35
+ alGenBuffers ( static_cast <ALsizei>(m_bufferIds. size ()), m_bufferIds. data ());
42
36
43
- void Sound::fillBufferFromStart ()
44
- {
45
37
m_cursor = 0 ;
46
38
for (auto const & bufferId : m_bufferIds) {
47
39
selectSamplesForBuffer (bufferId);
48
40
}
49
41
}
50
42
51
- void Sound::createBuffers ()
52
- {
53
- alGenBuffers (static_cast <ALsizei>(m_bufferIds.size ()), m_bufferIds.data ());
54
- }
43
+ Sound::~Sound () { deleteSourceAndBuffers (); }
55
44
56
- void Sound::createSource ()
45
+ void Sound::deleteSourceAndBuffers ()
57
46
{
58
- // Create source
59
- alGenSources (1 , &m_sourceId);
60
- alSourcef (m_sourceId, AL_PITCH, 1 .0f );
61
- alSourcef (m_sourceId, AL_GAIN, m_volume);
62
- alSource3f (m_sourceId, AL_POSITION, 0 .0f , 0 , -1 .0f );
63
- alSource3f (m_sourceId, AL_VELOCITY, 0 .0f , 0 .0f , 0 .0f );
64
- alSourcei (m_sourceId, AL_LOOPING, AL_FALSE);
65
- alSourcef (m_sourceId, AL_ROLLOFF_FACTOR, 0 .0f );
66
- alSourcei (m_sourceId, AL_SOURCE_RELATIVE, true );
47
+ alDeleteSources (1 , &m_sourceId);
48
+ alDeleteBuffers (static_cast <ALsizei>(m_bufferIds.size ()), m_bufferIds.data ());
67
49
}
68
50
69
51
void Sound::play ()
70
52
{
71
53
alSourcePlay (m_sourceId);
72
54
auto const errorIfAny = alGetError ();
73
55
if (errorIfAny != AL_NO_ERROR) {
74
- auto const errorMessage = " Could not play sound, error code: " + std::to_string (errorIfAny);
75
- throw oalpp::AudioException { errorMessage. c_str ( ) };
56
+ throw oalpp::AudioException { " Could not play sound, error code: "
57
+ + std::to_string (errorIfAny ) };
76
58
}
77
59
}
78
60
79
61
void Sound::stop ()
80
62
{
81
63
alSourceStop (m_sourceId);
82
64
83
- deleteSource ();
84
- deleteBuffers ();
85
- createSource ();
86
- createBuffers ();
87
- fillBufferFromStart ();
65
+ deleteSourceAndBuffers ();
66
+
67
+ initSourceAndBuffers ();
88
68
89
69
auto const errorIfAny = alGetError ();
90
70
if (errorIfAny != AL_NO_ERROR) {
91
- auto const errorMessage = " Could not stop sound, error code: " + std::to_string (errorIfAny);
92
- throw oalpp::AudioException { errorMessage. c_str ( ) };
71
+ throw oalpp::AudioException { " Could not stop sound, error code: "
72
+ + std::to_string (errorIfAny ) };
93
73
}
94
74
}
95
75
@@ -98,8 +78,8 @@ void Sound::pause()
98
78
alSourcePause (m_sourceId);
99
79
auto const errorIfAny = alGetError ();
100
80
if (errorIfAny != AL_NO_ERROR) {
101
- auto const errorMessage = " Could not stop sound, error code: " + std::to_string (errorIfAny);
102
- throw oalpp::AudioException { errorMessage. c_str ( ) };
81
+ throw oalpp::AudioException { " Could not stop sound, error code: "
82
+ + std::to_string (errorIfAny ) };
103
83
}
104
84
}
105
85
@@ -115,9 +95,7 @@ float Sound::getVolume() const { return m_volume; }
115
95
void Sound::setVolume (float newVolume)
116
96
{
117
97
if (newVolume < 0 || newVolume > 1 .0f ) {
118
- auto const errorMessage
119
- = std::string { " Could not set volume value: " } + std::to_string (newVolume);
120
- throw std::invalid_argument { errorMessage.c_str () };
98
+ throw std::invalid_argument { " Could not set volume value: " + std::to_string (newVolume) };
121
99
}
122
100
m_volume = newVolume;
123
101
alSourcef (m_sourceId, AL_GAIN, newVolume);
@@ -126,35 +104,32 @@ void Sound::setVolume(float newVolume)
126
104
void Sound::setPan (float newPan)
127
105
{
128
106
if (newPan < -1 .0f || newPan > 1 .0f ) {
129
- auto const errorMessage
130
- = std::string { " Could not set pan value: " } + std::to_string (newPan);
131
- throw std::invalid_argument { errorMessage. c_str () };
107
+ throw std::invalid_argument {
108
+ ( " Could not set pan value: " + std::to_string (newPan)). c_str ()
109
+ };
132
110
}
133
111
134
- setPosition (
135
- std::array<float , 3 > { newPan, 0 , -static_cast <float >(sqrt (1 .0f - newPan * newPan)) });
112
+ setPosition (Position { newPan, 0 , -static_cast <float >(sqrt (1 .0f - newPan * newPan)) });
136
113
}
137
114
138
- std::array< float , 3 > Sound::getPosition () const { return m_position; }
115
+ Position Sound::getPosition () const { return m_position; }
139
116
140
- void Sound::setPosition (std::array< float , 3 > const & newPos )
117
+ void Sound::setPosition (Position const & newPosition )
141
118
{
142
119
if (m_format == AL_FORMAT_STEREO_FLOAT32) {
143
120
throw oalpp::AudioException { " Could not set position on non-mono file" };
144
121
}
145
122
146
- m_position = newPos ;
147
- alSource3f (m_sourceId, AL_POSITION, newPos[ 0 ], newPos[ 1 ], newPos[ 2 ] );
123
+ m_position = newPosition ;
124
+ alSource3f (m_sourceId, AL_POSITION, newPosition. x , newPosition. y , newPosition. z );
148
125
}
149
126
150
127
float Sound::getPitch () const { return m_pitch; }
151
128
152
129
void Sound::setPitch (float const newPitch)
153
130
{
154
131
if (newPitch <= 0 .0f ) {
155
- auto const errorMessage
156
- = std::string { " Could not set pitch value: " } + std::to_string (newPitch);
157
- throw std::invalid_argument { errorMessage.c_str () };
132
+ throw std::invalid_argument { " Could not set pitch value: " + std::to_string (newPitch) };
158
133
}
159
134
m_pitch = newPitch;
160
135
alSourcef (m_sourceId, AL_PITCH, newPitch);
@@ -192,10 +167,12 @@ void Sound::update()
192
167
193
168
void Sound::selectSamplesForBuffer (ALuint bufferId)
194
169
{
195
- if (m_cursor >= m_soundData. getSamples (). size ()) {
170
+ if (! hasDataToEnqueue ()) {
196
171
// do not queue any buffer
197
172
return ;
198
- } else if (m_cursor + BUFFER_SIZE <= m_soundData.getSamples ().size ()) {
173
+ }
174
+
175
+ if (hasDataForFullBufferToEnqueue ()) {
199
176
// queue a full buffer
200
177
enqueueSamplesToBuffer (bufferId, BUFFER_SIZE);
201
178
} else {
@@ -210,6 +187,11 @@ void Sound::selectSamplesForBuffer(ALuint bufferId)
210
187
}
211
188
}
212
189
}
190
+ bool Sound::hasDataForFullBufferToEnqueue () const
191
+ {
192
+ return m_cursor + BUFFER_SIZE <= m_soundData.getSamples ().size ();
193
+ }
194
+ bool Sound::hasDataToEnqueue () const { return m_cursor < m_soundData.getSamples ().size (); }
213
195
214
196
bool Sound::getIsLooping () const { return m_isLooping; }
215
197
void Sound::setIsLooping (bool value) { m_isLooping = value; }
0 commit comments