-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerAudio.cpp
More file actions
303 lines (261 loc) · 7.12 KB
/
PlayerAudio.cpp
File metadata and controls
303 lines (261 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "PlayerAudio.h"
PlayerAudio::PlayerAudio()
: resamplingSource(&transportSource, false, 2),
isPaused(false),
islooping(false),
abLoopEnabled(false),
abLoopPointA(0.0),
abLoopPointB(0.0),
currentPlaylistIndex(-1),
shouldAutoAdvance(false)
{
formatManager.registerBasicFormats();
}
PlayerAudio::~PlayerAudio()
{
transportSource.setSource(nullptr);
}
void PlayerAudio::prepareToPlay(int samplesPerBlockExpected, double sampleRate)
{
transportSource.prepareToPlay(samplesPerBlockExpected, sampleRate);
resamplingSource.prepareToPlay(samplesPerBlockExpected, sampleRate);
}
void PlayerAudio::getNextAudioBlock(const juce::AudioSourceChannelInfo& bufferToFill)
{
resamplingSource.getNextAudioBlock(bufferToFill);
if (abLoopEnabled && readerSource != nullptr)
{
double currentPos = transportSource.getCurrentPosition();
if (currentPos >= abLoopPointB)
{
transportSource.setPosition(abLoopPointA);
}
}
// Auto-advance to next track when current track finishes
if (!abLoopEnabled && !islooping && readerSource != nullptr)
{
double currentPos = transportSource.getCurrentPosition();
double length = getLengthInSeconds();
// Check if track has finished (position at or past end)
if (length > 0.0 && currentPos >= length - 0.1)
{
if (!shouldAutoAdvance && hasNext())
{
shouldAutoAdvance = true;
// Schedule playNext on message thread to avoid audio thread issues
juce::MessageManager::callAsync([this]() {
if (hasNext()) {
playNext();
}
});
}
}
else if (currentPos < length - 0.5)
{
// Reset flag when position is not near the end
shouldAutoAdvance = false;
}
}
}
void PlayerAudio::releaseResources()
{
resamplingSource.releaseResources();
transportSource.releaseResources();
}
void PlayerAudio::loadAudioFile(const juce::File& file)
{
// Clear playlist when loading a single file
playlist.clear();
currentPlaylistIndex = -1;
shouldAutoAdvance = false;
loadFileInternal(file);
}
void PlayerAudio::loadFileInternal(const juce::File& file)
{
if (file.existsAsFile())
{
currentFilename = file.getFileName();
currentMetadata = Metadata();
TagLib::FileRef fileRef(file.getFullPathName().toRawUTF8());
if (!fileRef.isNull() && fileRef.tag())
{
TagLib::Tag* tag = fileRef.tag();
currentMetadata.title = juce::String(tag->title().toCString(true));
currentMetadata.artist = juce::String(tag->artist().toCString(true));
currentMetadata.album = juce::String(tag->album().toCString(true));
currentMetadata.genre = juce::String(tag->genre().toCString(true));
currentMetadata.comment = juce::String(tag->comment().toCString(true));
currentMetadata.year = tag->year();
currentMetadata.track = tag->track();
}
// Load audio file for playback using JUCE
if (auto* reader = formatManager.createReaderFor(file))
{
transportSource.stop();
transportSource.setSource(nullptr);
readerSource.reset();
readerSource = std::make_unique<juce::AudioFormatReaderSource>(reader, true);
transportSource.setSource(readerSource.get(), 0, nullptr, reader->sampleRate);
readerSource->setLooping(islooping);
isPaused = false;
abLoopEnabled = false;
abLoopPointA = 0.0;
abLoopPointB = getLengthInSeconds();
}
}
else
{
// Clear metadata if file doesn't exist
currentMetadata = Metadata();
currentFilename = juce::String();
}
}
void PlayerAudio::play()
{
if (readerSource != nullptr)
{
transportSource.start();
isPaused = false;
}
}
void PlayerAudio::pause()
{
if (transportSource.isPlaying())
{
transportSource.stop();
isPaused = true;
}
}
void PlayerAudio::stop()
{
transportSource.stop();
transportSource.setPosition(0.0);
isPaused = false;
}
void PlayerAudio::goToStart()
{
transportSource.setPosition(0.0);
}
void PlayerAudio::goToEnd()
{
if (readerSource != nullptr)
{
double lengthInSeconds = getLengthInSeconds();
transportSource.setPosition(lengthInSeconds);
}
}
void PlayerAudio::setGain(float gain)
{
transportSource.setGain(gain);
}
void PlayerAudio::setPosition(double positionInSeconds)
{
transportSource.setPosition(positionInSeconds);
}
void PlayerAudio::setLooping(bool shouldLoop)
{
islooping = shouldLoop;
if (readerSource != nullptr)
{
readerSource->setLooping(shouldLoop);
}
}
void PlayerAudio::setSpeed(double speed)
{
resamplingSource.setResamplingRatio(speed);
}
void PlayerAudio::setABLoopPoints(double pointA, double pointB)
{
if (pointA < pointB && pointB <= getLengthInSeconds())
{
abLoopPointA = pointA;
abLoopPointB = pointB;
abLoopEnabled = true;
}
}
void PlayerAudio::clearABLoop()
{
abLoopEnabled = false;
abLoopPointA = 0.0;
abLoopPointB = getLengthInSeconds();
}
bool PlayerAudio::isPlaying() const
{
return transportSource.isPlaying();
}
bool PlayerAudio::isLooping() const
{
return islooping;
}
double PlayerAudio::getPosition() const
{
return transportSource.getCurrentPosition();
}
double PlayerAudio::getLengthInSeconds() const
{
if (readerSource != nullptr)
{
return transportSource.getLengthInSeconds();
}
return 0.0;
}
void PlayerAudio::loadPlaylist(const juce::Array<juce::File>& files)
{
playlist.clear();
currentPlaylistIndex = -1;
shouldAutoAdvance = false;
// Filter out invalid files and add to playlist
for (const auto& file : files)
{
if (file.existsAsFile())
{
playlist.add(file);
}
}
// Load first track if playlist is not empty
if (playlist.size() > 0)
{
setCurrentPlaylistIndex(0);
}
}
void PlayerAudio::playNext()
{
if (hasNext())
{
setCurrentPlaylistIndex(currentPlaylistIndex + 1);
play();
}
}
void PlayerAudio::playPrevious()
{
if (hasPrevious())
{
setCurrentPlaylistIndex(currentPlaylistIndex - 1);
play();
}
}
bool PlayerAudio::hasNext() const
{
return currentPlaylistIndex >= 0 && currentPlaylistIndex < playlist.size() - 1;
}
bool PlayerAudio::hasPrevious() const
{
return currentPlaylistIndex > 0 && playlist.size() > 0;
}
int PlayerAudio::getPlaylistSize() const
{
return playlist.size();
}
int PlayerAudio::getCurrentPlaylistIndex() const
{
return currentPlaylistIndex;
}
void PlayerAudio::setCurrentPlaylistIndex(int index)
{
if (index >= 0 && index < playlist.size())
{
currentPlaylistIndex = index;
shouldAutoAdvance = false;
loadFileInternal(playlist[index]);
}
}