-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiovizmediaplayer.cpp
314 lines (246 loc) · 9.88 KB
/
audiovizmediaplayer.cpp
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
304
305
306
307
308
309
310
311
312
313
314
#include "audiovizmediaplayer.h"
#include "audiovisualizerwidget.h"
#include "complexes.h"
#include <QApplication>
#include <QAudioFormat>
#include <QMediaDevices>
#include <QBuffer>
#include <QFile>
#include <QProcess>
#include <QThread>
#include <QDebug>
AudioVizMediaPlayer::AudioVizMediaPlayer(QMediaPlayer *m_player, AudioVisualizerWidget *vizLeft, AudioVisualizerWidget *vizRight, QObject *parent)
: QObject(parent)
, m_mediaPlayer(m_player)
, m_audioPosition(0)
, m_audioTimer(new QTimer(this))
, m_visualizer_left(vizLeft)
, m_visualizer_right(vizRight)
, m_decodedAudioData(new QByteArray)
, m_framePositions(new QVector<qint64>)
{
// Connect signals and slots
connect(m_audioTimer, &QTimer::timeout, this, &AudioVizMediaPlayer::updateVisualizer);
// Set a default audio format
m_audioFormat.setSampleRate(44100);
m_audioFormat.setChannelCount(2);
m_audioFormat.setSampleFormat(QAudioFormat::Int16);
}
AudioVizMediaPlayer::~AudioVizMediaPlayer()
{
if (m_audioTimer) {
m_audioTimer->stop();
m_audioTimer->deleteLater();
}
m_visualizer_left->clear();
m_visualizer_right->clear();
m_decodedAudioData->detach();
m_decodedAudioData->clear(); // Clear the audio data buffer
m_framePositions->detach();
m_framePositions->clear();
m_decodedAudioData.reset();
m_framePositions.reset();
disconnect(m_audioTimer, &QTimer::timeout, this, &AudioVizMediaPlayer::updateVisualizer);
}
void AudioVizMediaPlayer::mute(bool toggle) {
if ( m_audioTimer )
if ( toggle ) {
m_audioTimer->stop();
is_Mute = true;
m_visualizer_left->mute(true);
m_visualizer_right->mute(true);
} else {
is_Mute = false;
m_visualizer_left->mute(false);
m_visualizer_right->mute(false);
if (m_mediaPlayer->playbackState() == QMediaPlayer::PlayingState)
seek(m_mediaPlayer->position(), false);
}
}
void AudioVizMediaPlayer::setMedia(const QString &source)
{
m_mediaSource = source;
//m_mediaPlayer->setSource(QUrl());
// Reset the audio data
m_decodedAudioData->detach();
m_decodedAudioData->clear(); // Clear the audio buffer
m_framePositions->detach();
m_framePositions->clear(); // Clear frame positions
QString audioFile = extractedPlayback;
extractAudio(source, audioFile);
}
void AudioVizMediaPlayer::play()
{
if (m_mediaPlayer->playbackState() == QMediaPlayer::StoppedState || m_mediaPlayer->playbackState() == QMediaPlayer::PausedState) {
m_mediaPlayer->play();
if (!m_audioTimer->isActive() && !is_Mute ) {
m_audioTimer->start(100); // Start the timer to update the visualizer, if needed
}
}
}
void AudioVizMediaPlayer::pause()
{
m_mediaPlayer->pause();
m_audioTimer->stop();
}
void AudioVizMediaPlayer::stop()
{
m_audioPosition = 0;
m_audioTimer->stop();
if (m_mediaPlayer)
m_mediaPlayer->stop();
if (m_visualizer_left)
m_visualizer_left->clear(); // Clear visualizations
if (m_visualizer_right)
m_visualizer_right->clear(); // Clear visualizations
}
void AudioVizMediaPlayer::seek(qint64 position, bool seekPlayback)
{
if (!m_mediaPlayer) {
qWarning() << "Media player is null";
return;
}
// Ensure position is within valid range
if (position < 0 || position > m_mediaPlayer->duration()) {
qWarning() << "Seek position out of bounds:" << position;
return;
}
// Clear the visualizer before updating the new pos
m_visualizer_left->clear();
m_visualizer_right->clear();
// Calculate the frame position corresponding to the seek position
qint64 targetFrame = position * m_framePositions->size() / m_mediaPlayer->duration();
qint64 closestFramePos = findClosestFramePosition(targetFrame);
// Ensure closestFramePos is within valid range
if (closestFramePos < 0 || closestFramePos >= m_decodedAudioData->size()) {
qWarning() << "Calculated audio position out of bounds:" << closestFramePos;
return;
}
if (!m_audioTimer->isActive() && !is_Mute ) {
m_audioTimer->start(100); // Start the timer to update the visualizer, if needed
}
// Update media player position
if ( seekPlayback )
m_mediaPlayer->setPosition(position);
// Update the audio position to the closest frame position
m_audioPosition = closestFramePos;
// Log the updated information for debugging
qDebug() << "Seeking to media position:" << position
<< "with closest frame position:" << closestFramePos;
// Update visualizer to match the new position
updateVisualizer();
}
qint64 AudioVizMediaPlayer::findClosestFramePosition(qint64 targetFrame)
{
if (m_framePositions->isEmpty()) {
return 0; // Return the start if positions are empty
}
if (targetFrame <= 0) {
return m_framePositions->first(); // Return first frame
}
if (targetFrame >= m_framePositions->size() - 1) {
return m_framePositions->last(); // Return last frame
}
// Locate nearest frames
qint64 frame1 = targetFrame;
qint64 frame2 = frame1 + 1;
if (frame2 >= m_framePositions->size()) {
frame2 = m_framePositions->size() - 1; // Ensure frame2 is within bounds
}
qint64 bytePos1 = m_framePositions->at(frame1);
qint64 bytePos2 = m_framePositions->at(frame2);
// Calculate interpolation for smoother seeking, avoid any divide-by-zero errors
if (frame2 == frame1 || bytePos2 == bytePos1) {
return bytePos1; // Return exact position if frames are equal
}
double ratio = static_cast<double>(targetFrame - frame1) / (frame2 - frame1);
return bytePos1 + static_cast<qint64>(ratio * (bytePos2 - bytePos1));
}
void AudioVizMediaPlayer::updateVisualizer()
{
if (!m_visualizer_left || !m_visualizer_right || m_decodedAudioData->isEmpty()) {
return;
}
// Ensure m_audioPosition is within valid bounds
if (m_audioPosition < 0 || m_audioPosition >= m_decodedAudioData->size()) {
m_audioPosition = 0; // Reset audio position if out of bounds
m_visualizer_left->clear();
m_visualizer_right->clear();
return;
}
const qint64 bytesPerFrame = m_audioFormat.bytesForDuration(100000); // 100ms worth of audio
qint64 bytesToVisualize = qMin(bytesPerFrame, m_decodedAudioData->size() - m_audioPosition);
if (bytesToVisualize > 0) {
QByteArray audioChunk = m_decodedAudioData->mid(m_audioPosition, bytesToVisualize);
// Send data to visualizer
m_visualizer_left->updateVisualization(audioChunk, m_audioFormat);
m_visualizer_right->updateVisualization(audioChunk, m_audioFormat);
m_audioPosition += bytesToVisualize;
// Ensure audioPosition doesn't exceed the audio data size
if (m_audioPosition >= m_decodedAudioData->size()) {
m_audioPosition = 0; // Reset if we reach the end
m_audioTimer->stop();
}
} else {
m_visualizer_left->clear();
m_visualizer_right->clear();
}
}
void AudioVizMediaPlayer::extractAudio(const QString &source, const QString &outputFile)
{
QThread* ffmpegThread = new QThread(this); // Thread owned by the current class
QProcess* ffmpegProcess = new QProcess(); // Process owned by the thread
ffmpegProcess->moveToThread(ffmpegThread);
connect(ffmpegProcess, &QProcess::finished,
this, [ffmpegProcess, outputFile, source, this]() {
QFile file(outputFile);
if (!file.exists()) {
qWarning() << "Audio Visualizer audio file was not created.";
} else {
emit ffmpegExtractionFinished(outputFile, source);
}
ffmpegProcess->deleteLater(); // Clean up the process safely
});
connect(ffmpegProcess, &QProcess::errorOccurred, this, [ffmpegProcess]() {
qWarning() << "FFmpeg process error occurred. Cleaning up.";
ffmpegProcess->deleteLater(); // Cleanup on error
});
connect(ffmpegThread, &QThread::finished, [ffmpegThread]() {
ffmpegThread->deleteLater();
});
connect(this, &AudioVizMediaPlayer::ffmpegExtractionFinished, this, &AudioVizMediaPlayer::loadAudioData, Qt::UniqueConnection);
connect(ffmpegThread, &QThread::started, [ffmpegProcess, source, outputFile]() {
QStringList ffmpegArgs;
ffmpegArgs << "-y" << "-i" << source
<< "-vn" << "-ac" << "2"
<< "-acodec" << "pcm_s16le" << "-ar" << "44100" << outputFile;
ffmpegProcess->start("ffmpeg", ffmpegArgs);
if (!ffmpegProcess->waitForStarted()) {
qWarning() << "Failed to start FFmpeg process for Audio Visualizer.";
}
});
connect(ffmpegProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [ffmpegThread]() {
if (ffmpegThread->isRunning()) {
ffmpegThread->quit();
ffmpegThread->wait();
}
});
ffmpegThread->start();
}
void AudioVizMediaPlayer::loadAudioData(const QString &audioFile, const QString &sourceFile)
{
QFile file(audioFile);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open audio file for reading.";
return;
}
*m_decodedAudioData = file.readAll(); // Read all audio data
file.close();
m_framePositions->clear(); // Clear any previous data
for (int i = 0; i < m_decodedAudioData->size(); i += m_audioFormat.bytesForDuration(100000)) {
m_framePositions->append(i); // Append the byte position for visualization
}
// Now that everything is ready, load the true media player
m_mediaPlayer->setSource(QUrl::fromLocalFile(sourceFile));
}