1 // Copyright (c) 2007, Niels Martin Hansen
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of the Aegisub Group nor the names of its contributors
13 // may be used to endorse or promote products derived from this software
14 // without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 // Aegisub Project http://www.aegisub.org/
29
30 /// @file audio_player_openal.cpp
31 /// @brief OpenAL-based audio output
32 /// @ingroup audio_output
33 ///
34
35 #ifdef WITH_OPENAL
36 #include "include/aegisub/audio_player.h"
37
38 #include "audio_controller.h"
39 #include "utils.h"
40
41 #include <libaegisub/audio/provider.h>
42 #include <libaegisub/log.h>
43 #include <libaegisub/make_unique.h>
44
45 #ifdef __WINDOWS__
46 #include <al.h>
47 #include <alc.h>
48 #elif defined(__APPLE__)
49 #include <OpenAL/al.h>
50 #include <OpenAL/alc.h>
51 #else
52 #include <AL/al.h>
53 #include <AL/alc.h>
54 #endif
55
56 #include <vector>
57 #include <wx/timer.h>
58
59 // Auto-link to OpenAL lib for MSVC
60 #ifdef _MSC_VER
61 #pragma comment(lib, "openal32.lib")
62 #endif
63
64 namespace {
65 class OpenALPlayer final : public AudioPlayer, wxTimer {
66 /// Number of OpenAL buffers to use
67 static const ALsizei num_buffers = 8;
68
69 bool playing = false; ///< Is audio currently playing?
70
71 float volume = 1.f; ///< Current audio volume
72 ALsizei samplerate; ///< Sample rate of the audio
73 int bpf; ///< Bytes per frame
74
75 int64_t start_frame = 0; ///< First frame of playbacka
76 int64_t cur_frame = 0; ///< Next frame to write to playback buffers
77 int64_t end_frame = 0; ///< Last frame to play
78
79 ALCdevice *device = nullptr; ///< OpenAL device handle
80 ALCcontext *context = nullptr; ///< OpenAL sound context
81 ALuint buffers[num_buffers]; ///< OpenAL sound buffers
82 ALuint source = 0; ///< OpenAL playback source
83
84 /// Index into buffers, first free (unqueued) buffer to be filled
85 ALsizei buf_first_free = 0;
86
87 /// Index into buffers, first queued (non-free) buffer
88 ALsizei buf_first_queued = 0;
89
90 /// Number of free buffers
91 ALsizei buffers_free = 0;
92
93 /// Number of buffers which have been fully played since playback was last started
94 ALsizei buffers_played = 0;
95
96 wxStopWatch playback_segment_timer;
97
98 /// Buffer to decode audio into
99 std::vector<char> decode_buffer;
100
101 /// Fill count OpenAL buffers
102 void FillBuffers(ALsizei count);
103
104 protected:
105 /// wxTimer override to periodically fill available buffers
106 void Notify() override;
107
108 public:
109 OpenALPlayer(agi::AudioProvider *provider);
110 ~OpenALPlayer();
111
112 void Play(int64_t start,int64_t count) override;
113 void Stop() override;
IsPlaying()114 bool IsPlaying() override { return playing; }
115
GetEndPosition()116 int64_t GetEndPosition() override { return end_frame; }
117 int64_t GetCurrentPosition() override;
118 void SetEndPosition(int64_t pos) override;
119
SetVolume(double vol)120 void SetVolume(double vol) override { volume = vol; }
121 };
122
OpenALPlayer(agi::AudioProvider * provider)123 OpenALPlayer::OpenALPlayer(agi::AudioProvider *provider)
124 : AudioPlayer(provider)
125 , samplerate(provider->GetSampleRate())
126 , bpf(provider->GetChannels() * provider->GetBytesPerSample())
127 {
128 try {
129 // Open device
130 device = alcOpenDevice(nullptr);
131 if (!device) throw AudioPlayerOpenError("Failed opening default OpenAL device");
132
133 // Create context
134 context = alcCreateContext(device, nullptr);
135 if (!context) throw AudioPlayerOpenError("Failed creating OpenAL context");
136 if (!alcMakeContextCurrent(context)) throw AudioPlayerOpenError("Failed selecting OpenAL context");
137
138 // Clear error code
139 alGetError();
140
141 // Generate buffers
142 alGenBuffers(num_buffers, buffers);
143 if (alGetError() != AL_NO_ERROR) throw AudioPlayerOpenError("Error generating OpenAL buffers");
144
145 // Generate source
146 alGenSources(1, &source);
147 if (alGetError() != AL_NO_ERROR) {
148 alDeleteBuffers(num_buffers, buffers);
149 throw AudioPlayerOpenError("Error generating OpenAL source");
150 }
151 }
152 catch (...)
153 {
154 alcDestroyContext(context);
155 alcCloseDevice(device);
156 throw;
157 }
158
159 // Determine buffer length
160 decode_buffer.resize(samplerate * bpf / num_buffers / 2); // buffers for half a second of audio
161 }
162
~OpenALPlayer()163 OpenALPlayer::~OpenALPlayer()
164 {
165 Stop();
166
167 alDeleteSources(1, &source);
168 alDeleteBuffers(num_buffers, buffers);
169 alcDestroyContext(context);
170 alcCloseDevice(device);
171 }
172
Play(int64_t start,int64_t count)173 void OpenALPlayer::Play(int64_t start, int64_t count)
174 {
175 if (playing) {
176 // Quick reset
177 playing = false;
178 alSourceStop(source);
179 alSourcei(source, AL_BUFFER, 0);
180 }
181
182 // Set params
183 start_frame = start;
184 cur_frame = start;
185 end_frame = start + count;
186 playing = true;
187
188 // Prepare buffers
189 buffers_free = num_buffers;
190 buffers_played = 0;
191 buf_first_free = 0;
192 buf_first_queued = 0;
193 FillBuffers(num_buffers);
194
195 // And go!
196 alSourcePlay(source);
197 wxTimer::Start(100);
198 playback_segment_timer.Start();
199 }
200
Stop()201 void OpenALPlayer::Stop()
202 {
203 if (!playing) return;
204
205 // Reset data
206 wxTimer::Stop();
207 playing = false;
208 start_frame = 0;
209 cur_frame = 0;
210 end_frame = 0;
211
212 // Then drop the playback
213 alSourceStop(source);
214 alSourcei(source, AL_BUFFER, 0);
215 }
216
FillBuffers(ALsizei count)217 void OpenALPlayer::FillBuffers(ALsizei count)
218 {
219 // Do the actual filling/queueing
220 for (count = mid(1, count, buffers_free); count > 0; --count) {
221 ALsizei fill_len = mid<ALsizei>(0, decode_buffer.size() / bpf, end_frame - cur_frame);
222
223 if (fill_len > 0)
224 // Get fill_len frames of audio
225 provider->GetAudioWithVolume(&decode_buffer[0], cur_frame, fill_len, volume);
226 if ((size_t)fill_len * bpf < decode_buffer.size())
227 // And zerofill the rest
228 memset(&decode_buffer[fill_len * bpf], 0, decode_buffer.size() - fill_len * bpf);
229
230 cur_frame += fill_len;
231
232 alBufferData(buffers[buf_first_free], AL_FORMAT_MONO16, &decode_buffer[0], decode_buffer.size(), samplerate);
233 alSourceQueueBuffers(source, 1, &buffers[buf_first_free]); // FIXME: collect buffer handles and queue all at once instead of one at a time?
234 buf_first_free = (buf_first_free + 1) % num_buffers;
235 --buffers_free;
236 }
237 }
238
Notify()239 void OpenALPlayer::Notify()
240 {
241 ALsizei newplayed;
242 alGetSourcei(source, AL_BUFFERS_PROCESSED, &newplayed);
243
244 LOG_D("player/audio/openal") << "buffers_played=" << buffers_played << " newplayed=" << newplayed;
245
246 if (newplayed > 0) {
247 // Reclaim buffers
248 ALuint bufs[num_buffers];
249 for (ALsizei i = 0; i < newplayed; ++i) {
250 bufs[i] = buffers[buf_first_queued];
251 buf_first_queued = (buf_first_queued + 1) % num_buffers;
252 }
253 alSourceUnqueueBuffers(source, newplayed, bufs);
254 buffers_free += newplayed;
255
256 // Update
257 buffers_played += newplayed;
258 playback_segment_timer.Start();
259
260 // Fill more buffers
261 FillBuffers(newplayed);
262 }
263
264 LOG_D("player/audio/openal") << "frames played=" << (buffers_played - num_buffers) * decode_buffer.size() / bpf << " num frames=" << end_frame - start_frame;
265 // Check that all of the selected audio plus one full set of buffers has been queued
266 if ((buffers_played - num_buffers) * (int64_t)decode_buffer.size() > (end_frame - start_frame) * bpf) {
267 Stop();
268 }
269 }
270
SetEndPosition(int64_t pos)271 void OpenALPlayer::SetEndPosition(int64_t pos)
272 {
273 end_frame = pos;
274 }
275
GetCurrentPosition()276 int64_t OpenALPlayer::GetCurrentPosition()
277 {
278 // FIXME: this should be based on not duration played but actual sample being heard
279 // (during video playback, cur_frame might get changed to resync)
280 long extra = playback_segment_timer.Time();
281 return buffers_played * decode_buffer.size() / bpf + start_frame + extra * samplerate / 1000;
282 }
283 }
284
CreateOpenALPlayer(agi::AudioProvider * provider,wxWindow *)285 std::unique_ptr<AudioPlayer> CreateOpenALPlayer(agi::AudioProvider *provider, wxWindow *)
286 {
287 return agi::make_unique<OpenALPlayer>(provider);
288 }
289
290 #endif // WITH_OPENAL
291