2016-10-10 13:29:41 -04:00
|
|
|
#include "aud_file.h"
|
|
|
|
|
|
|
|
AudFile::AudFile(QObject *parent) : QFile(parent)
|
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
offset = 0;
|
|
|
|
buffRate = 0;
|
|
|
|
init = true;
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudFile::~AudFile()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudFile::openFile(const QString &path)
|
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
close();
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
init = true;
|
|
|
|
buffRate = 0;
|
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
setFileName(path);
|
|
|
|
|
|
|
|
bool ret = open(QFile::ReadOnly);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
offset = 0;
|
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
if (peek(3) == "ID3")
|
|
|
|
{
|
|
|
|
QByteArray header = read(10);
|
|
|
|
QByteArray intBytes = header.mid(6);
|
|
|
|
quint64 num = 0;
|
|
|
|
quint64 bit = 1;
|
|
|
|
|
|
|
|
offset += 10;
|
|
|
|
|
|
|
|
if (header[5] & 16) //Footer flag check
|
|
|
|
{
|
|
|
|
offset += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = intBytes.size() - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
int byte = intBytes[i];
|
|
|
|
|
|
|
|
for (int inBit = 1; inBit <= 64; inBit *= 2, bit *= 2)
|
|
|
|
{
|
|
|
|
if ((byte & inBit) != 0) num |= bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += num;
|
|
|
|
}
|
2016-10-16 17:04:21 -04:00
|
|
|
|
|
|
|
seek(0);
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:04:21 -04:00
|
|
|
bool AudFile::seek(qint64 off)
|
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
qint64 newPos = offset + off;
|
|
|
|
|
|
|
|
if (init)
|
|
|
|
{
|
|
|
|
if ((off) && (!buffRate))
|
|
|
|
{
|
|
|
|
buffRate = off;
|
|
|
|
|
|
|
|
emit duration(getDuration());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPos >= size())
|
|
|
|
{
|
|
|
|
init = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit posChanged(pos());
|
|
|
|
|
|
|
|
if (newPos >= size())
|
|
|
|
{
|
|
|
|
QTimer::singleShot(2000, this, SLOT(delayFinished()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QFile::seek(newPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudFile::getOffset()
|
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudFile::getDuration()
|
|
|
|
{
|
|
|
|
return ((size() - offset) / buffRate) * 1000;
|
2016-10-16 17:04:21 -04:00
|
|
|
}
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
void AudFile::delayFinished()
|
2016-10-10 13:29:41 -04:00
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
emit endOfPlayback();
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|