Debian Codegraph @

src/player.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2023 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <player.h>
14 #include <config.h>
15 #include <utilities.h>
16 #include <freac.h>
17
18 #include <engine/locking.h>
19
20 #include <engine/decoder.h>
21 #include <engine/processor.h>
22
23 using namespace BoCA;
24 using namespace BoCA::AS;
25
26 freac::Player::Player()
27 {
28 playing = False;
29 paused = False;
30
31 stop = False;
32
33 output = NIL;
34
35 newPosition = -1;
36 }
37
38 freac::Player::~Player()
39 {
40 }
41
42 Void freac::Player::Play(const Track &iTrack)
43 {
44 /* Resume playback if it is paused.
45 */
46 if (playing && paused && track.GetTrackID() == iTrack.GetTrackID())
47 {
48 Resume();
49
50 return;
51 }
52
53 /* Stop playback if already playing.
54 */
55 if (playing) Stop();
56
57 /* Lock device in case this is a CD track.
58 */
59 if (!Locking::LockDeviceForTrack(iTrack))
60 {
61 BoCA::Utilities::ErrorMessage("Cannot play a CD track while ripping from the same drive!");
62
63 return;
64 }
65
66 /* Play selected track.
67 */
68 track = iTrack;
69
70 playing = True;
71 paused = False;
72
73 stop = False;
74
75 NonBlocking0<>(&Player::PlayThread, this).Call();
76 }
77
78 Int freac::Player::PlayThread()
79 {
80 BoCA::Config *config = BoCA::Config::Copy();
81
82 /* Get config values.
83 */
84 Bool processPlayback = config->GetIntValue(Config::CategoryProcessingID, Config::ProcessingProcessPlaybackID, Config::ProcessingProcessPlaybackDefault);
85
86 /* Create decoder.
87 */
88 Decoder *decoder = new Decoder(config);
89
90 if (!decoder->Create(track.fileName, track))
91 {
92 delete decoder;
93
94 Locking::UnlockDeviceForTrack(track);
95
96 BoCA::Config::Free(config);
97
98 playing = False;
99
100 return Error();
101 }
102
103 /* Create processor.
104 */
105 Track trackToPlay = track;
106 Processor *processor = new Processor(config);
107
108 if (processPlayback)
109 {
110 if (!processor->Create(track))
111 {
112 delete decoder;
113 delete processor;
114
115 Locking::UnlockDeviceForTrack(track);
116
117 BoCA::Config::Free(config);
118
119 playing = False;
120
121 return Error();
122 }
123
124 trackToPlay.SetFormat(processor->GetFormatInfo());
125 }
126
127 /* Notify application about track playback.
128 */
129 onPlay.Emit(track);
130
131 /* Create output component.
132 */
133 Int error = Error();
134 Registry &boca = Registry::Get();
135
136 for (Int i = 0; i < boca.GetNumberOfComponents(); i++)
137 {
138 if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_OUTPUT) continue;
139
140 output = (OutputComponent *) boca.CreateComponentByID(boca.GetComponentID(i));
141
142 if (output == NIL) continue;
143
144 output->SetConfiguration(config);
145 output->SetAudioTrackInfo(trackToPlay);
146
147 if (output->Activate())
148 {
149 /* Enter playback loop.
150 */
151 if (!output->GetErrorState()) Loop(decoder, processor);
152
153 /* Clean up.
154 */
155 output->Deactivate();
156
157 error = Success();
158 }
159
160 boca.DeleteComponent(output);
161
162 break;
163 }
164
165 delete decoder;
166 delete processor;
167
168 Locking::UnlockDeviceForTrack(track);
169
170 BoCA::Config::Free(config);
171
172 stop = True;
173 playing = False;
174
175 /* Notify application about finished playback.
176 */
177 onFinish.Emit(track);
178
179 return error;
180 }
181
182 Void freac::Player::Loop(Decoder *decoder, Processor *processor)
183 {
184 BoCA::I18n *i18n = BoCA::I18n::Get();
185
186 /* Compute format specs and create buffer.
187 */
188 const Format &format = track.GetFormat();
189
190 Int64 position = 0;
191 UnsignedLong samplesSize = format.rate / 8;
192
193 Int bytesPerSample = format.bits / 8 * format.channels;
194 Buffer<UnsignedByte> buffer(samplesSize * bytesPerSample);
195
196 /* Enter playback loop.
197 */
198 while (!stop)
199 {
200 /* Seek if requested.
201 */
202 if (newPosition >= 0)
203 {
204 if (track.length >= 0) position = track.length / 1000 * newPosition;
205 else if (track.approxLength >= 0) position = track.approxLength / 1000 * newPosition;
206 else position = (Int64(240) * format.rate) / 1000 * newPosition;
207
208 decoder->Seek(position);
209
210 newPosition = -1;
211 }
212
213 /* Find step size.
214 */
215 Int step = samplesSize;
216
217 if (track.length >= 0)
218 {
219 if (position >= track.length) break;
220
221 if (position + step > track.length) step = track.length - position;
222 }
223
224 buffer.Resize(step * bytesPerSample);
225
226 /* Read samples from decoder.
227 */
228 Int bytes = decoder->Read(buffer);
229
230 if (bytes == 0) break;
231
232 /* Transform samples using processor.
233 */
234 if (processor != NIL) processor->Transform(buffer);
235
236 /* Update position and write data.
237 */
238 position += (bytes / bytesPerSample);
239
240 if (track.length >= 0) onProgress.Emit(i18n->IsActiveLanguageRightToLeft() ? 1000 - 1000.0 / track.length * position : 1000.0 / track.length * position);
241 else if (track.approxLength >= 0) onProgress.Emit(i18n->IsActiveLanguageRightToLeft() ? 1000 - 1000.0 / track.approxLength * position : 1000.0 / track.approxLength * position);
242 else onProgress.Emit(i18n->IsActiveLanguageRightToLeft() ? 1000 - 1000.0 / (240 * format.rate) * position : 1000.0 / (240 * format.rate) * position);
243
244 Write(buffer);
245 }
246
247 /* Finish sample transformations.
248 */
249 buffer.Resize(0);
250
251 if (processor != NIL) processor->Finish(buffer);
252
253 /* Pass remaining samples to output.
254 */
255 Write(buffer);
256
257 if (!stop) output->Finish();
258
259 while (!stop && output->IsPlaying()) S::System::System::Sleep(20);
260 }
261
262 Void freac::Player::Write(Buffer<UnsignedByte> &buffer)
263 {
264 while (buffer.Size() > 0)
265 {
266 while (!stop && (paused || output->CanWrite() == 0)) S::System::System::Sleep(10);
267
268 if (stop) break;
269
270 Buffer<UnsignedByte> outputBuffer(Math::Min(buffer.Size(), output->CanWrite()));
271
272 memcpy(outputBuffer, buffer, outputBuffer.Size());
273 memmove(buffer, buffer + outputBuffer.Size(), buffer.Size() - outputBuffer.Size());
274
275 buffer.Resize(buffer.Size() - outputBuffer.Size());
276
277 output->WriteData(outputBuffer);
278 }
279 }
280
281 Void freac::Player::Pause()
282 {
283 if (!playing) return;
284
285 output->SetPause(True);
286
287 paused = True;
288 }
289
290 Void freac::Player::Resume()
291 {
292 if (!playing) return;
293
294 output->SetPause(False);
295
296 paused = False;
297 }
298
299 Void freac::Player::SetPosition(Int position)
300 {
301 if (!playing) return;
302
303 newPosition = position;
304 }
305
306 Void freac::Player::Stop()
307 {
308 if (!playing || stop) return;
309
310 /* Request stop of playback.
311 */
312 stop = True;
313
314 while (playing)
315 {
316 Int suspendCount = GUI::Application::Lock::SuspendLock();
317
318 S::System::System::Sleep(10);
319
320 GUI::Application::Lock::ResumeLock(suspendCount);
321 }
322 }