Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #ifdef HAVE_MAD Chris@148: Chris@148: #include "MP3FileReader.h" Chris@392: #include "base/ProgressReporter.h" Chris@1858: #include "base/Profiler.h" Chris@357: Chris@150: #include "system/System.h" Chris@148: Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: Chris@148: #include Chris@148: Chris@405: #include Chris@405: Chris@271: #ifdef HAVE_ID3TAG Chris@271: #include Chris@271: #endif Chris@1168: Chris@1364: #ifdef _WIN32 Chris@1364: #include Chris@1364: #include Chris@1364: #else Chris@1364: #include Chris@1364: #include Chris@1364: #endif Chris@1364: Chris@186: #include Chris@148: Chris@1346: #include Chris@1346: Chris@1305: using std::string; Chris@1305: Chris@1305: static sv_frame_t DEFAULT_DECODER_DELAY = 529; Chris@1305: Chris@317: MP3FileReader::MP3FileReader(FileSource source, DecodeMode decodeMode, Chris@1305: CacheMode mode, GaplessMode gaplessMode, Chris@1305: sv_samplerate_t targetRate, Chris@920: bool normalised, Chris@392: ProgressReporter *reporter) : Chris@920: CodedAudioFileReader(mode, targetRate, normalised), Chris@316: m_source(source), Chris@316: m_path(source.getLocalFilename()), Chris@1305: m_gaplessMode(gaplessMode), Chris@1284: m_decodeErrorShown(false), Chris@1582: m_decodeThread(nullptr) Chris@148: { Chris@1279: SVDEBUG << "MP3FileReader: local path: \"" << m_path Chris@1279: << "\", decode mode: " << decodeMode << " (" Chris@1279: << (decodeMode == DecodeAtOnce ? "DecodeAtOnce" : "DecodeThreaded") Chris@1279: << ")" << endl; Chris@1279: Chris@148: m_channelCount = 0; Chris@297: m_fileRate = 0; Chris@148: m_fileSize = 0; Chris@148: m_bitrateNum = 0; Chris@148: m_bitrateDenom = 0; Chris@148: m_cancelled = false; Chris@1288: m_mp3FrameCount = 0; Chris@265: m_completion = 0; Chris@263: m_done = false; Chris@392: m_reporter = reporter; Chris@148: Chris@1313: if (m_gaplessMode == GaplessMode::Gapless) { Chris@1307: CodedAudioFileReader::setFramesToTrim(DEFAULT_DECODER_DELAY, 0); Chris@1305: } Chris@1305: Chris@1348: m_fileSize = 0; Chris@148: Chris@1582: m_fileBuffer = nullptr; Chris@1290: m_fileBufferSize = 0; Chris@1290: Chris@1582: m_sampleBuffer = nullptr; Chris@1290: m_sampleBufferSize = 0; Chris@977: Chris@1348: QFile qfile(m_path); Chris@1348: if (!qfile.open(QIODevice::ReadOnly)) { Chris@1342: m_error = QString("Failed to open file %1 for reading.").arg(m_path); Chris@1342: SVDEBUG << "MP3FileReader: " << m_error << endl; Chris@1342: return; Chris@1343: } Chris@148: Chris@1348: m_fileSize = qfile.size(); Chris@1348: Chris@148: try { Chris@1290: // We need a mysterious MAD_BUFFER_GUARD (== 8) zero bytes at Chris@1290: // end of input, to ensure libmad decodes the last frame Chris@1290: // correctly. Otherwise the decoded audio is truncated. Chris@1401: SVDEBUG << "file size = " << m_fileSize << ", buffer guard = " << MAD_BUFFER_GUARD << endl; Chris@1290: m_fileBufferSize = m_fileSize + MAD_BUFFER_GUARD; Chris@1290: m_fileBuffer = new unsigned char[m_fileBufferSize]; Chris@1290: memset(m_fileBuffer + m_fileSize, 0, MAD_BUFFER_GUARD); Chris@148: } catch (...) { Chris@290: m_error = QString("Out of memory"); Chris@1342: SVDEBUG << "MP3FileReader: " << m_error << endl; Chris@1342: return; Chris@148: } Chris@1348: Chris@1348: auto amountRead = qfile.read(reinterpret_cast(m_fileBuffer), Chris@1348: m_fileSize); Chris@1348: Chris@1348: if (amountRead < m_fileSize) { Chris@1348: SVCERR << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes") Chris@1348: .arg(amountRead).arg(m_fileSize) << endl; Chris@1348: memset(m_fileBuffer + amountRead, 0, m_fileSize - amountRead); Chris@1348: m_fileSize = amountRead; Chris@148: } Chris@1348: Chris@1348: loadTags(qfile.handle()); Chris@148: Chris@1348: qfile.close(); Chris@271: Chris@263: if (decodeMode == DecodeAtOnce) { Chris@263: Chris@392: if (m_reporter) { Chris@392: connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled())); Chris@392: m_reporter->setMessage Chris@392: (tr("Decoding %1...").arg(QFileInfo(m_path).fileName())); Chris@327: } Chris@148: Chris@1290: if (!decode(m_fileBuffer, m_fileBufferSize)) { Chris@316: m_error = QString("Failed to decode file %1.").arg(m_path); Chris@263: } Chris@1402: Chris@1402: if (m_sampleBuffer) { Chris@1402: for (int c = 0; c < m_channelCount; ++c) { Chris@1402: delete[] m_sampleBuffer[c]; Chris@1402: } Chris@1402: delete[] m_sampleBuffer; Chris@1582: m_sampleBuffer = nullptr; Chris@1402: } Chris@263: Chris@1290: delete[] m_fileBuffer; Chris@1582: m_fileBuffer = nullptr; Chris@148: Chris@263: if (isDecodeCacheInitialised()) finishDecodeCache(); Chris@398: endSerialised(); Chris@263: Chris@392: } else { Chris@263: Chris@392: if (m_reporter) m_reporter->setProgress(100); Chris@263: Chris@263: m_decodeThread = new DecodeThread(this); Chris@263: m_decodeThread->start(); Chris@263: Chris@386: while ((m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0) Chris@386: && !m_done) { Chris@263: usleep(10); Chris@263: } Chris@386: Chris@1288: SVDEBUG << "MP3FileReader: decoding startup complete, file rate = " << m_fileRate << endl; Chris@148: } Chris@499: Chris@499: if (m_error != "") { Chris@1279: SVDEBUG << "MP3FileReader::MP3FileReader(\"" << m_path << "\"): ERROR: " << m_error << endl; Chris@499: } Chris@148: } Chris@148: Chris@148: MP3FileReader::~MP3FileReader() Chris@148: { Chris@1858: Profiler profiler("MP3FileReader::~MP3FileReader"); Chris@1858: Chris@263: if (m_decodeThread) { Chris@265: m_cancelled = true; Chris@263: m_decodeThread->wait(); Chris@263: delete m_decodeThread; Chris@263: } Chris@148: } Chris@148: Chris@263: void Chris@392: MP3FileReader::cancelled() Chris@392: { Chris@392: m_cancelled = true; Chris@392: } Chris@392: Chris@392: void Chris@1348: MP3FileReader::loadTags(int fd) Chris@271: { Chris@271: m_title = ""; Chris@271: Chris@271: #ifdef HAVE_ID3TAG Chris@271: Chris@1364: #ifdef _WIN32 Chris@1364: int id3fd = _dup(fd); Chris@1364: #else Chris@1364: int id3fd = dup(fd); Chris@1364: #endif Chris@1364: Chris@1364: id3_file *file = id3_file_fdopen(id3fd, ID3_FILE_MODE_READONLY); Chris@271: if (!file) return; Chris@271: Chris@273: // We can do this a lot more elegantly, but we'll leave that for Chris@273: // when we implement support for more than just the one tag! Chris@273: Chris@271: id3_tag *tag = id3_file_tag(file); Chris@273: if (!tag) { Chris@1279: SVDEBUG << "MP3FileReader::loadTags: No ID3 tag found" << endl; Chris@1364: id3_file_close(file); // also closes our dup'd fd Chris@273: return; Chris@271: } Chris@271: Chris@333: m_title = loadTag(tag, "TIT2"); // work title Chris@333: if (m_title == "") m_title = loadTag(tag, "TIT1"); Chris@1287: if (m_title == "") SVDEBUG << "MP3FileReader::loadTags: No title found" << endl; Chris@273: Chris@333: m_maker = loadTag(tag, "TPE1"); // "lead artist" Chris@333: if (m_maker == "") m_maker = loadTag(tag, "TPE2"); Chris@1287: if (m_maker == "") SVDEBUG << "MP3FileReader::loadTags: No artist/maker found" << endl; Chris@273: Chris@632: for (unsigned int i = 0; i < tag->nframes; ++i) { Chris@632: if (tag->frames[i]) { Chris@632: QString value = loadTag(tag, tag->frames[i]->id); Chris@1287: if (value != "") { Chris@1287: m_tags[tag->frames[i]->id] = value; Chris@1287: } Chris@632: } Chris@632: } Chris@632: Chris@1364: id3_file_close(file); // also closes our dup'd fd Chris@273: Chris@273: #else Chris@1287: SVDEBUG << "MP3FileReader::loadTags: ID3 tag support not compiled in" << endl; Chris@273: #endif Chris@333: } Chris@273: Chris@333: QString Chris@333: MP3FileReader::loadTag(void *vtag, const char *name) Chris@333: { Chris@333: #ifdef HAVE_ID3TAG Chris@333: id3_tag *tag = (id3_tag *)vtag; Chris@333: Chris@333: id3_frame *frame = id3_tag_findframe(tag, name, 0); Chris@333: if (!frame) { Chris@1287: SVDEBUG << "MP3FileReader::loadTag: No \"" << name << "\" frame found in ID3 tag" << endl; Chris@333: return ""; Chris@333: } Chris@333: Chris@333: if (frame->nfields < 2) { Chris@1287: cerr << "MP3FileReader::loadTag: WARNING: Not enough fields (" << frame->nfields << ") for \"" << name << "\" in ID3 tag" << endl; Chris@333: return ""; Chris@333: } Chris@333: Chris@333: unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]); Chris@333: if (nstrings == 0) { Chris@1287: SVDEBUG << "MP3FileReader::loadTag: No strings for \"" << name << "\" in ID3 tag" << endl; Chris@333: return ""; Chris@333: } Chris@333: Chris@333: id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0); Chris@333: if (!ustr) { Chris@1287: SVDEBUG << "MP3FileReader::loadTag: Invalid or absent data for \"" << name << "\" in ID3 tag" << endl; Chris@333: return ""; Chris@333: } Chris@333: Chris@333: id3_utf8_t *u8str = id3_ucs4_utf8duplicate(ustr); Chris@333: if (!u8str) { Chris@1287: SVDEBUG << "MP3FileReader::loadTag: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 tag" << endl; Chris@333: return ""; Chris@333: } Chris@333: Chris@333: QString rv = QString::fromUtf8((const char *)u8str); Chris@333: free(u8str); Chris@334: Chris@1287: SVDEBUG << "MP3FileReader::loadTag: Tag \"" << name << "\" -> \"" Chris@1287: << rv << "\"" << endl; Chris@334: Chris@333: return rv; Chris@333: Chris@333: #else Chris@333: return ""; Chris@333: #endif Chris@271: } Chris@271: Chris@271: void Chris@263: MP3FileReader::DecodeThread::run() Chris@263: { Chris@1290: if (!m_reader->decode(m_reader->m_fileBuffer, m_reader->m_fileBufferSize)) { Chris@290: m_reader->m_error = QString("Failed to decode file %1.").arg(m_reader->m_path); Chris@263: } Chris@263: Chris@1290: delete[] m_reader->m_fileBuffer; Chris@1582: m_reader->m_fileBuffer = nullptr; Chris@297: Chris@1290: if (m_reader->m_sampleBuffer) { Chris@929: for (int c = 0; c < m_reader->m_channelCount; ++c) { Chris@1290: delete[] m_reader->m_sampleBuffer[c]; Chris@297: } Chris@1290: delete[] m_reader->m_sampleBuffer; Chris@1582: m_reader->m_sampleBuffer = nullptr; Chris@297: } Chris@297: Chris@1858: if (m_reader->isDecodeCacheInitialised()) { Chris@1858: m_reader->finishDecodeCache(); Chris@1858: } Chris@263: Chris@263: m_reader->m_done = true; Chris@265: m_reader->m_completion = 100; Chris@297: Chris@297: m_reader->endSerialised(); Chris@263: } Chris@263: Chris@148: bool Chris@1038: MP3FileReader::decode(void *mm, sv_frame_t sz) Chris@148: { Chris@148: DecoderData data; Chris@148: struct mad_decoder decoder; Chris@148: Chris@148: data.start = (unsigned char const *)mm; Chris@1288: data.length = sz; Chris@1310: data.finished = false; Chris@148: data.reader = this; Chris@148: Chris@1288: mad_decoder_init(&decoder, // decoder to initialise Chris@1288: &data, // our own data block for callbacks Chris@1288: input_callback, // provides (entire) input to mad Chris@1582: nullptr, // checks header Chris@1288: filter_callback, // filters frame before decoding Chris@1288: output_callback, // receives decoded output Chris@1288: error_callback, // handles decode errors Chris@1582: nullptr); // "message_func" Chris@1288: Chris@148: mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC); Chris@148: mad_decoder_finish(&decoder); Chris@148: Chris@1288: SVDEBUG << "MP3FileReader: Decoding complete, decoded " << m_mp3FrameCount Chris@1288: << " mp3 frames" << endl; Chris@1288: Chris@263: m_done = true; Chris@148: return true; Chris@148: } Chris@148: Chris@148: enum mad_flow Chris@1288: MP3FileReader::input_callback(void *dp, struct mad_stream *stream) Chris@148: { Chris@148: DecoderData *data = (DecoderData *)dp; Chris@148: Chris@1310: if (!data->length) { Chris@1310: data->finished = true; Chris@1310: return MAD_FLOW_STOP; Chris@1310: } Chris@348: Chris@348: unsigned char const *start = data->start; Chris@1288: sv_frame_t length = data->length; Chris@348: Chris@348: #ifdef HAVE_ID3TAG Chris@1288: while (length > ID3_TAG_QUERYSIZE) { Chris@1038: ssize_t taglen = id3_tag_query(start, ID3_TAG_QUERYSIZE); Chris@1288: if (taglen <= 0) { Chris@1288: break; Chris@348: } Chris@1288: SVDEBUG << "MP3FileReader: ID3 tag length to skip: " << taglen << endl; Chris@1288: start += taglen; Chris@1288: length -= taglen; Chris@348: } Chris@348: #endif Chris@348: Chris@348: mad_stream_buffer(stream, start, length); Chris@148: data->length = 0; Chris@148: Chris@148: return MAD_FLOW_CONTINUE; Chris@148: } Chris@148: Chris@148: enum mad_flow Chris@1288: MP3FileReader::filter_callback(void *dp, Chris@1288: struct mad_stream const *stream, Chris@1288: struct mad_frame *frame) Chris@1288: { Chris@1288: DecoderData *data = (DecoderData *)dp; Chris@1288: return data->reader->filter(stream, frame); Chris@1288: } Chris@1288: Chris@1307: static string toMagic(unsigned long fourcc) Chris@1305: { Chris@1305: string magic("...."); Chris@1305: for (int i = 0; i < 4; ++i) { Chris@1305: magic[3-i] = char((fourcc >> (8*i)) & 0xff); Chris@1305: } Chris@1305: return magic; Chris@1305: } Chris@1305: Chris@1288: enum mad_flow Chris@1288: MP3FileReader::filter(struct mad_stream const *stream, Chris@1288: struct mad_frame *) Chris@1288: { Chris@1289: if (m_mp3FrameCount > 0) { Chris@1305: // only handle info frame if it appears as first mp3 frame Chris@1289: return MAD_FLOW_CONTINUE; Chris@1305: } Chris@1305: Chris@1313: if (m_gaplessMode == GaplessMode::Gappy) { Chris@1305: // Our non-gapless mode does not even filter out the Xing/LAME Chris@1305: // frame. That's because the main reason non-gapless mode Chris@1305: // exists is for backward compatibility with MP3FileReader Chris@1305: // behaviour before the gapless support was added, so we even Chris@1305: // need to keep the spurious 1152 samples resulting from Chris@1305: // feeding Xing/LAME frame to the decoder as otherwise we'd Chris@1305: // have different output from before. Chris@1305: SVDEBUG << "MP3FileReader: Not gapless mode, not checking Xing/LAME frame" Chris@1305: << endl; Chris@1305: return MAD_FLOW_CONTINUE; Chris@1305: } Chris@1305: Chris@1305: struct mad_bitptr ptr = stream->anc_ptr; Chris@1305: string magic = toMagic(mad_bit_read(&ptr, 32)); Chris@1305: Chris@1305: if (magic == "Xing" || magic == "Info") { Chris@1305: Chris@1305: SVDEBUG << "MP3FileReader: Found Xing/LAME metadata frame (magic = \"" Chris@1305: << magic << "\")" << endl; Chris@1305: Chris@1305: // All we want at this point is the LAME encoder delay and Chris@1305: // padding values. We expect to see the Xing/Info magic (which Chris@1305: // we've already read), then 116 bytes of Xing data, then LAME Chris@1305: // magic, 5 byte version string, 12 bytes of LAME data that we Chris@1305: // aren't currently interested in, then the delays encoded as Chris@1305: // two 12-bit numbers into three bytes. Chris@1305: // Chris@1305: // (See gabriel.mp3-tech.org/mp3infotag.html) Chris@1305: Chris@1305: for (int skip = 0; skip < 116; ++skip) { Chris@1305: (void)mad_bit_read(&ptr, 8); Chris@1305: } Chris@1305: Chris@1305: magic = toMagic(mad_bit_read(&ptr, 32)); Chris@1305: Chris@1305: if (magic == "LAME") { Chris@1305: Chris@1305: SVDEBUG << "MP3FileReader: Found LAME-specific metadata" << endl; Chris@1305: Chris@1305: for (int skip = 0; skip < 5 + 12; ++skip) { Chris@1305: (void)mad_bit_read(&ptr, 8); Chris@1305: } Chris@1305: Chris@1307: auto delay = mad_bit_read(&ptr, 12); Chris@1307: auto padding = mad_bit_read(&ptr, 12); Chris@1305: Chris@1305: sv_frame_t delayToDrop = DEFAULT_DECODER_DELAY + delay; Chris@1305: sv_frame_t paddingToDrop = padding - DEFAULT_DECODER_DELAY; Chris@1305: if (paddingToDrop < 0) paddingToDrop = 0; Chris@1305: Chris@1305: SVDEBUG << "MP3FileReader: LAME encoder delay = " << delay Chris@1305: << ", padding = " << padding << endl; Chris@1305: Chris@1305: SVDEBUG << "MP3FileReader: Will be trimming " << delayToDrop Chris@1306: << " samples from start and " << paddingToDrop Chris@1306: << " from end" << endl; Chris@1305: Chris@1307: CodedAudioFileReader::setFramesToTrim(delayToDrop, paddingToDrop); Chris@1305: Chris@1305: } else { Chris@1305: SVDEBUG << "MP3FileReader: Xing frame has no LAME metadata" << endl; Chris@1305: } Chris@1305: Chris@1305: return MAD_FLOW_IGNORE; Chris@1305: Chris@1288: } else { Chris@1305: return MAD_FLOW_CONTINUE; Chris@1288: } Chris@1288: } Chris@1288: Chris@1288: enum mad_flow Chris@1288: MP3FileReader::output_callback(void *dp, Chris@1288: struct mad_header const *header, Chris@1288: struct mad_pcm *pcm) Chris@148: { Chris@148: DecoderData *data = (DecoderData *)dp; Chris@148: return data->reader->accept(header, pcm); Chris@148: } Chris@148: Chris@148: enum mad_flow Chris@148: MP3FileReader::accept(struct mad_header const *header, Chris@1343: struct mad_pcm *pcm) Chris@148: { Chris@148: int channels = pcm->channels; Chris@148: int frames = pcm->length; Chris@1288: Chris@148: if (header) { Chris@1038: m_bitrateNum = m_bitrateNum + double(header->bitrate); Chris@148: m_bitrateDenom ++; Chris@148: } Chris@148: Chris@148: if (frames < 1) return MAD_FLOW_CONTINUE; Chris@148: Chris@148: if (m_channelCount == 0) { Chris@297: Chris@297: m_fileRate = pcm->samplerate; Chris@148: m_channelCount = channels; Chris@297: Chris@1354: SVDEBUG << "MP3FileReader::accept: file rate = " << pcm->samplerate Chris@1354: << ", channel count = " << channels << ", about to init " Chris@1354: << "decode cache" << endl; Chris@1354: Chris@297: initialiseDecodeCache(); Chris@297: Chris@297: if (m_cacheMode == CacheInTemporaryFile) { Chris@690: // SVDEBUG << "MP3FileReader::accept: channel count " << m_channelCount << ", file rate " << m_fileRate << ", about to start serialised section" << endl; Chris@1858: startSerialised("MP3FileReader::Decode", &m_cancelled); Chris@1858: if (m_cancelled) { Chris@1858: return MAD_FLOW_STOP; Chris@1858: } Chris@297: } Chris@148: } Chris@148: Chris@148: if (m_bitrateDenom > 0) { Chris@148: double bitrate = m_bitrateNum / m_bitrateDenom; Chris@148: double duration = double(m_fileSize * 8) / bitrate; Chris@148: double elapsed = double(m_frameCount) / m_sampleRate; Chris@375: double percent = 100; Chris@375: if (duration > 0.0) percent = ((elapsed * 100.0) / duration); Chris@357: int p = int(percent); Chris@357: if (p < 1) p = 1; Chris@357: if (p > 99) p = 99; Chris@392: if (m_completion != p && m_reporter) { Chris@357: m_completion = p; Chris@392: m_reporter->setProgress(m_completion); Chris@148: } Chris@148: } Chris@148: Chris@1290: if (m_cancelled) { Chris@1290: SVDEBUG << "MP3FileReader: Decoding cancelled" << endl; Chris@1290: return MAD_FLOW_STOP; Chris@1290: } Chris@148: Chris@148: if (!isDecodeCacheInitialised()) { Chris@1354: SVDEBUG << "MP3FileReader::accept: fallback case: file rate = " << pcm->samplerate Chris@1354: << ", channel count = " << channels << ", about to init " Chris@1354: << "decode cache" << endl; Chris@148: initialiseDecodeCache(); Chris@148: } Chris@148: Chris@1290: if (m_sampleBufferSize < size_t(frames)) { Chris@1290: if (!m_sampleBuffer) { Chris@1290: m_sampleBuffer = new float *[channels]; Chris@297: for (int c = 0; c < channels; ++c) { Chris@1582: m_sampleBuffer[c] = nullptr; Chris@297: } Chris@297: } Chris@297: for (int c = 0; c < channels; ++c) { Chris@1290: delete[] m_sampleBuffer[c]; Chris@1290: m_sampleBuffer[c] = new float[frames]; Chris@297: } Chris@1290: m_sampleBufferSize = frames; Chris@297: } Chris@148: Chris@297: int activeChannels = int(sizeof(pcm->samples) / sizeof(pcm->samples[0])); Chris@297: Chris@297: for (int ch = 0; ch < channels; ++ch) { Chris@297: Chris@297: for (int i = 0; i < frames; ++i) { Chris@297: Chris@1343: mad_fixed_t sample = 0; Chris@1343: if (ch < activeChannels) { Chris@1343: sample = pcm->samples[ch][i]; Chris@1343: } Chris@1343: float fsample = float(sample) / float(MAD_F_ONE); Chris@297: Chris@1290: m_sampleBuffer[ch][i] = fsample; Chris@1343: } Chris@148: } Chris@148: Chris@1290: addSamplesToDecodeCache(m_sampleBuffer, frames); Chris@148: Chris@1288: ++m_mp3FrameCount; Chris@1288: Chris@148: return MAD_FLOW_CONTINUE; Chris@148: } Chris@148: Chris@148: enum mad_flow Chris@1288: MP3FileReader::error_callback(void *dp, Chris@1288: struct mad_stream *stream, Chris@1288: struct mad_frame *) Chris@148: { Chris@1284: DecoderData *data = (DecoderData *)dp; Chris@1290: Chris@1312: sv_frame_t ix = stream->this_frame - data->start; Chris@1312: Chris@1290: if (stream->error == MAD_ERROR_LOSTSYNC && Chris@1312: (data->finished || ix >= data->length)) { Chris@1310: // We are at end of file, losing sync is expected behaviour, Chris@1310: // don't report it Chris@1290: return MAD_FLOW_CONTINUE; Chris@1290: } Chris@1290: Chris@1284: if (!data->reader->m_decodeErrorShown) { Chris@1284: char buffer[256]; Chris@1284: snprintf(buffer, 255, cannam@1334: "MP3 decoding error 0x%04x (%s) at byte offset %lld", Chris@1335: stream->error, mad_stream_errorstr(stream), (long long int)ix); Chris@1284: SVCERR << "Warning: in file \"" << data->reader->m_path << "\": " Chris@1284: << buffer << " (continuing; will not report any further decode errors for this file)" << endl; Chris@1284: data->reader->m_decodeErrorShown = true; Chris@1279: } Chris@148: Chris@148: return MAD_FLOW_CONTINUE; Chris@148: } Chris@148: Chris@157: void Chris@290: MP3FileReader::getSupportedExtensions(std::set &extensions) Chris@157: { Chris@157: extensions.insert("mp3"); Chris@157: } Chris@157: Chris@316: bool Chris@316: MP3FileReader::supportsExtension(QString extension) Chris@316: { Chris@316: std::set extensions; Chris@316: getSupportedExtensions(extensions); Chris@316: return (extensions.find(extension.toLower()) != extensions.end()); Chris@316: } Chris@316: Chris@316: bool Chris@316: MP3FileReader::supportsContentType(QString type) Chris@316: { Chris@316: return (type == "audio/mpeg"); Chris@316: } Chris@316: Chris@316: bool Chris@317: MP3FileReader::supports(FileSource &source) Chris@316: { Chris@316: return (supportsExtension(source.getExtension()) || Chris@316: supportsContentType(source.getContentType())); Chris@316: } Chris@316: Chris@316: Chris@148: #endif