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_OGGZ Chris@148: #ifdef HAVE_FISHSOUND Chris@148: Chris@148: #include "OggVorbisFileReader.h" Chris@357: Chris@392: #include "base/ProgressReporter.h" Chris@148: #include "base/Profiler.h" Chris@150: #include "system/System.h" Chris@148: Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: Chris@148: #include Chris@148: Chris@524: //static int instances = 0; Chris@148: Chris@317: OggVorbisFileReader::OggVorbisFileReader(FileSource source, Chris@263: DecodeMode decodeMode, Chris@297: CacheMode mode, Chris@392: size_t targetRate, Chris@392: ProgressReporter *reporter) : Chris@297: CodedAudioFileReader(mode, targetRate), Chris@316: m_source(source), Chris@316: m_path(source.getLocalFilename()), Chris@392: m_reporter(reporter), Chris@148: m_fileSize(0), Chris@148: m_bytesRead(0), Chris@271: m_commentsRead(false), Chris@263: m_cancelled(false), Chris@265: m_completion(0), Chris@263: m_decodeThread(0) Chris@148: { Chris@148: m_channelCount = 0; Chris@297: m_fileRate = 0; Chris@148: Chris@845: // SVDEBUG << "OggVorbisFileReader::OggVorbisFileReader(" << m_path << "): now have " << (++instances) << " instances" << endl; Chris@148: Chris@148: Profiler profiler("OggVorbisFileReader::OggVorbisFileReader", true); Chris@148: Chris@316: QFileInfo info(m_path); Chris@148: m_fileSize = info.size(); Chris@148: Chris@316: if (!(m_oggz = oggz_open(m_path.toLocal8Bit().data(), OGGZ_READ))) { Chris@316: m_error = QString("File %1 is not an OGG file.").arg(m_path); Chris@148: return; Chris@148: } Chris@148: Chris@148: FishSoundInfo fsinfo; Chris@148: m_fishSound = fish_sound_new(FISH_SOUND_DECODE, &fsinfo); Chris@148: Chris@148: fish_sound_set_decoded_callback(m_fishSound, acceptFrames, this); Chris@620: oggz_set_read_callback(m_oggz, -1, (OggzReadPacket)readPacket, this); Chris@148: 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@263: while (oggz_read(m_oggz, 1024) > 0); Chris@263: Chris@263: fish_sound_delete(m_fishSound); Chris@263: m_fishSound = 0; Chris@263: oggz_close(m_oggz); Chris@263: m_oggz = 0; Chris@148: Chris@263: if (isDecodeCacheInitialised()) finishDecodeCache(); Chris@398: endSerialised(); Chris@148: Chris@392: } else { Chris@148: Chris@392: if (m_reporter) m_reporter->setProgress(100); Chris@263: Chris@263: while (oggz_read(m_oggz, 1024) > 0 && Chris@386: (m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0)); Chris@263: Chris@263: if (m_channelCount > 0) { Chris@263: m_decodeThread = new DecodeThread(this); Chris@263: m_decodeThread->start(); Chris@263: } Chris@148: } Chris@148: } Chris@148: Chris@148: OggVorbisFileReader::~OggVorbisFileReader() Chris@148: { Chris@845: // SVDEBUG << "OggVorbisFileReader::~OggVorbisFileReader(" << m_path << "): now have " << (--instances) << " instances" << endl; 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: OggVorbisFileReader::cancelled() Chris@392: { Chris@392: m_cancelled = true; Chris@392: } Chris@392: Chris@392: void Chris@263: OggVorbisFileReader::DecodeThread::run() Chris@263: { Chris@297: if (m_reader->m_cacheMode == CacheInTemporaryFile) { Chris@297: m_reader->m_completion = 1; Chris@297: m_reader->startSerialised("OggVorbisFileReader::Decode"); Chris@297: } Chris@297: Chris@263: while (oggz_read(m_reader->m_oggz, 1024) > 0); Chris@263: Chris@263: fish_sound_delete(m_reader->m_fishSound); Chris@263: m_reader->m_fishSound = 0; Chris@263: oggz_close(m_reader->m_oggz); Chris@263: m_reader->m_oggz = 0; Chris@263: Chris@263: if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache(); Chris@265: m_reader->m_completion = 100; Chris@297: Chris@297: m_reader->endSerialised(); Chris@263: } Chris@263: Chris@148: int Chris@620: OggVorbisFileReader::readPacket(OGGZ *, ogg_packet *packet, long, void *data) Chris@148: { Chris@148: OggVorbisFileReader *reader = (OggVorbisFileReader *)data; Chris@148: FishSound *fs = reader->m_fishSound; Chris@148: Chris@620: fish_sound_prepare_truncation(fs, packet->granulepos, packet->e_o_s); Chris@620: fish_sound_decode(fs, packet->packet, packet->bytes); Chris@148: Chris@620: reader->m_bytesRead += packet->bytes; Chris@265: Chris@265: // The number of bytes read by this function is smaller than Chris@265: // the file size because of the packet headers Chris@357: int p = lrint(double(reader->m_bytesRead) * 114 / Chris@357: double(reader->m_fileSize)); Chris@357: if (p > 99) p = 99; Chris@357: reader->m_completion = p; Chris@357: reader->progress(p); Chris@357: Chris@392: if (reader->m_fileSize > 0 && reader->m_reporter) { Chris@392: reader->m_reporter->setProgress(p); Chris@265: } Chris@148: Chris@148: if (reader->m_cancelled) return 1; Chris@148: return 0; Chris@148: } Chris@148: Chris@148: int Chris@148: OggVorbisFileReader::acceptFrames(FishSound *fs, float **frames, long nframes, Chris@148: void *data) Chris@148: { Chris@148: OggVorbisFileReader *reader = (OggVorbisFileReader *)data; Chris@148: Chris@271: if (!reader->m_commentsRead) { Chris@633: const FishSoundComment *comment; Chris@633: comment = fish_sound_comment_first_byname(fs, "TITLE"); Chris@633: if (comment && comment->value) { Chris@633: reader->m_title = QString::fromUtf8(comment->value); Chris@333: } Chris@633: comment = fish_sound_comment_first_byname(fs, "ARTIST"); Chris@633: if (comment && comment->value) { Chris@633: reader->m_maker = QString::fromUtf8(comment->value); Chris@633: } Chris@633: comment = fish_sound_comment_first(fs); Chris@633: while (comment) { Chris@634: reader->m_tags[QString::fromUtf8(comment->name).toUpper()] = Chris@633: QString::fromUtf8(comment->value); Chris@633: comment = fish_sound_comment_next(fs, comment); Chris@271: } Chris@271: reader->m_commentsRead = true; Chris@271: } Chris@271: Chris@148: if (reader->m_channelCount == 0) { Chris@148: FishSoundInfo fsinfo; Chris@148: fish_sound_command(fs, FISH_SOUND_GET_INFO, Chris@148: &fsinfo, sizeof(FishSoundInfo)); Chris@297: reader->m_fileRate = fsinfo.samplerate; Chris@148: reader->m_channelCount = fsinfo.channels; Chris@148: reader->initialiseDecodeCache(); Chris@148: } Chris@148: Chris@148: if (nframes > 0) { Chris@297: reader->addSamplesToDecodeCache(frames, nframes); Chris@148: } Chris@148: Chris@148: if (reader->m_cancelled) return 1; Chris@148: return 0; Chris@148: } Chris@148: Chris@157: void Chris@290: OggVorbisFileReader::getSupportedExtensions(std::set &extensions) Chris@157: { Chris@157: extensions.insert("ogg"); Chris@571: extensions.insert("oga"); Chris@157: } Chris@157: Chris@316: bool Chris@316: OggVorbisFileReader::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: OggVorbisFileReader::supportsContentType(QString type) Chris@316: { Chris@316: return (type == "application/ogg"); Chris@316: } Chris@316: Chris@316: bool Chris@317: OggVorbisFileReader::supports(FileSource &source) Chris@316: { Chris@316: return (supportsExtension(source.getExtension()) || Chris@316: supportsContentType(source.getContentType())); Chris@316: } Chris@316: Chris@148: #endif Chris@148: #endif