radio/lib/wModel/file/audio.cpp

46 lines
1.1 KiB
C++
Raw Normal View History

2018-12-17 17:15:58 +00:00
#include "audio.h"
#include <mad.h>
M::Audio::Audio(W::Blob* p_file, const W::Address& addr, QObject* parent):
File(p_file, addr, parent)
2018-12-17 17:15:58 +00:00
{
}
M::Audio::~Audio()
{}
M::Model::ModelType M::Audio::getType() const
{
return type;
}
void M::Audio::initAdditional(const W::String& p_mime)
{
File::initAdditional(p_mime); //TODO handle other than mp3 formats
mad_stream stream;
mad_header header;
mad_stream_init(&stream);
mad_header_init(&header);
mad_stream_buffer(&stream, file->uchar(), file->size());
uint64_t length = 0;
uint64_t tBits = 0;
uint64_t amount = 0;
2018-12-17 17:15:58 +00:00
while(stream.error != MAD_ERROR_BUFLEN) { //TODO handle other errors;
int success = mad_header_decode(&header, &stream);
if (success == 0) {
amount++;
2018-12-17 17:15:58 +00:00
length += header.duration.seconds * MAD_TIMER_RESOLUTION + header.duration.fraction;
tBits += header.bitrate;
}
}
2019-01-26 18:54:22 +00:00
additional.insert(u"duration", new W::Uint64(length * 1000 / MAD_TIMER_RESOLUTION));
additional.insert(u"bitrate", new W::Uint64(tBits / amount));
2018-12-17 17:15:58 +00:00
}