changeset 386:e6d11871e4c9

* Fix bug that was causing decoded audio files (mp3s, oggs) to come up some of the time with zero sample rate
author Chris Cannam
date Sat, 01 Mar 2008 16:17:44 +0000
parents 9b35a1731c3d
children 7aa1de571880
files data/fileio/AudioFileReaderFactory.cpp data/fileio/CodedAudioFileReader.cpp data/fileio/MP3FileReader.cpp data/fileio/OggVorbisFileReader.cpp
diffstat 4 files changed, 177 insertions(+), 173 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/AudioFileReaderFactory.cpp	Wed Feb 27 18:04:10 2008 +0000
+++ b/data/fileio/AudioFileReaderFactory.cpp	Sat Mar 01 16:17:44 2008 +0000
@@ -1,171 +1,171 @@
-/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
-
-/*
-    Sonic Visualiser
-    An audio file viewer and annotation editor.
-    Centre for Digital Music, Queen Mary, University of London.
-    This file copyright 2006 Chris Cannam and QMUL.
-    
-    This program is free software; you can redistribute it and/or
-    modify it under the terms of the GNU General Public License as
-    published by the Free Software Foundation; either version 2 of the
-    License, or (at your option) any later version.  See the file
-    COPYING included with this distribution for more information.
-*/
-
-#include "AudioFileReaderFactory.h"
-
-#include "WavFileReader.h"
-#include "ResamplingWavFileReader.h"
-#include "OggVorbisFileReader.h"
-#include "MP3FileReader.h"
-#include "QuickTimeFileReader.h"
-
-#include <QString>
-#include <QFileInfo>
-#include <iostream>
-
-QString
-AudioFileReaderFactory::getKnownExtensions()
-{
-    std::set<QString> extensions;
-
-    WavFileReader::getSupportedExtensions(extensions);
-#ifdef HAVE_MAD
-    MP3FileReader::getSupportedExtensions(extensions);
-#endif
-#ifdef HAVE_OGGZ
-#ifdef HAVE_FISHSOUND
-    OggVorbisFileReader::getSupportedExtensions(extensions);
-#endif
-#endif
-#ifdef HAVE_QUICKTIME
-    QuickTimeFileReader::getSupportedExtensions(extensions);
-#endif
-
-    QString rv;
-    for (std::set<QString>::const_iterator i = extensions.begin();
-         i != extensions.end(); ++i) {
-        if (i != extensions.begin()) rv += " ";
-        rv += "*." + *i;
-    }
-
-    return rv;
-}
-
-AudioFileReader *
-AudioFileReaderFactory::createReader(FileSource source, size_t targetRate)
-{
-    return create(source, targetRate, false);
-}
-
-AudioFileReader *
-AudioFileReaderFactory::createThreadingReader(FileSource source, size_t targetRate)
-{
-    return create(source, targetRate, true);
-}
-
-AudioFileReader *
-AudioFileReaderFactory::create(FileSource source, size_t targetRate, bool threading)
-{
-    QString err;
-
-//    std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
-
-    if (!source.isOK() || !source.isAvailable()) {
-        std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\": Source unavailable" << std::endl;
-        return 0;
-    }
-
-    AudioFileReader *reader = 0;
-
-    // Try to construct a preferred reader based on the extension or
-    // MIME type.
-
-    if (WavFileReader::supports(source)) {
-
-        reader = new WavFileReader(source);
-
-        if (targetRate != 0 &&
-            reader->isOK() &&
-            reader->getSampleRate() != targetRate) {
-
-            std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
-
-            delete reader;
-            reader = new ResamplingWavFileReader
-                (source,
-                 threading ?
-                 ResamplingWavFileReader::ResampleThreaded :
-                 ResamplingWavFileReader::ResampleAtOnce,
-                 ResamplingWavFileReader::CacheInTemporaryFile,
-                 targetRate);
-        }
-    }
-    
-#ifdef HAVE_OGGZ
-#ifdef HAVE_FISHSOUND
-    if (!reader) {
-        if (OggVorbisFileReader::supports(source)) {
-            reader = new OggVorbisFileReader
-                (source,
-                 threading ?
-                 OggVorbisFileReader::DecodeThreaded :
-                 OggVorbisFileReader::DecodeAtOnce,
-                 OggVorbisFileReader::CacheInTemporaryFile,
-                 targetRate);
-        }
-    }
-#endif
-#endif
-
-#ifdef HAVE_MAD
-    if (!reader) {
-        if (MP3FileReader::supports(source)) {
-            reader = new MP3FileReader
-                (source,
-                 threading ?
-                 MP3FileReader::DecodeThreaded :
-                 MP3FileReader::DecodeAtOnce,
-                 MP3FileReader::CacheInTemporaryFile,
-                 targetRate);
-        }
-    }
-#endif
-
-#ifdef HAVE_QUICKTIME
-    if (!reader) {
-        if (QuickTimeFileReader::supports(source)) {
-            reader = new QuickTimeFileReader
-                (source,
-                 threading ?
-                 QuickTimeFileReader::DecodeThreaded : 
-                 QuickTimeFileReader::DecodeAtOnce,
-                 QuickTimeFileReader::CacheInTemporaryFile,
-                 targetRate);
-        }
-    }
-#endif
-
-    if (reader) {
-        if (reader->isOK()) {
-//            std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl;
-            return reader;
-        }
-        std::cerr << "AudioFileReaderFactory: Preferred reader for "
-                  << "url \"" << source.getLocation().toStdString()
-                  << "\" (content type \""
-                  << source.getContentType().toStdString() << "\") failed";
-
-        if (reader->getError() != "") {
-            std::cerr << ": \"" << reader->getError().toStdString() << "\"";
-        }
-        std::cerr << std::endl;
-        delete reader;
-        reader = 0;
-    }
-
-    std::cerr << "AudioFileReaderFactory: No reader" << std::endl;
-    return reader;
-}
-
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    This file copyright 2006 Chris Cannam and QMUL.
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "AudioFileReaderFactory.h"
+
+#include "WavFileReader.h"
+#include "ResamplingWavFileReader.h"
+#include "OggVorbisFileReader.h"
+#include "MP3FileReader.h"
+#include "QuickTimeFileReader.h"
+
+#include <QString>
+#include <QFileInfo>
+#include <iostream>
+
+QString
+AudioFileReaderFactory::getKnownExtensions()
+{
+    std::set<QString> extensions;
+
+    WavFileReader::getSupportedExtensions(extensions);
+#ifdef HAVE_MAD
+    MP3FileReader::getSupportedExtensions(extensions);
+#endif
+#ifdef HAVE_OGGZ
+#ifdef HAVE_FISHSOUND
+    OggVorbisFileReader::getSupportedExtensions(extensions);
+#endif
+#endif
+#ifdef HAVE_QUICKTIME
+    QuickTimeFileReader::getSupportedExtensions(extensions);
+#endif
+
+    QString rv;
+    for (std::set<QString>::const_iterator i = extensions.begin();
+         i != extensions.end(); ++i) {
+        if (i != extensions.begin()) rv += " ";
+        rv += "*." + *i;
+    }
+
+    return rv;
+}
+
+AudioFileReader *
+AudioFileReaderFactory::createReader(FileSource source, size_t targetRate)
+{
+    return create(source, targetRate, false);
+}
+
+AudioFileReader *
+AudioFileReaderFactory::createThreadingReader(FileSource source, size_t targetRate)
+{
+    return create(source, targetRate, true);
+}
+
+AudioFileReader *
+AudioFileReaderFactory::create(FileSource source, size_t targetRate, bool threading)
+{
+    QString err;
+
+//    std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
+
+    if (!source.isOK() || !source.isAvailable()) {
+        std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\": Source unavailable" << std::endl;
+        return 0;
+    }
+
+    AudioFileReader *reader = 0;
+
+    // Try to construct a preferred reader based on the extension or
+    // MIME type.
+
+    if (WavFileReader::supports(source)) {
+
+        reader = new WavFileReader(source);
+
+        if (targetRate != 0 &&
+            reader->isOK() &&
+            reader->getSampleRate() != targetRate) {
+
+            std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
+
+            delete reader;
+            reader = new ResamplingWavFileReader
+                (source,
+                 threading ?
+                 ResamplingWavFileReader::ResampleThreaded :
+                 ResamplingWavFileReader::ResampleAtOnce,
+                 ResamplingWavFileReader::CacheInTemporaryFile,
+                 targetRate);
+        }
+    }
+    
+#ifdef HAVE_OGGZ
+#ifdef HAVE_FISHSOUND
+    if (!reader) {
+        if (OggVorbisFileReader::supports(source)) {
+            reader = new OggVorbisFileReader
+                (source,
+                 threading ?
+                 OggVorbisFileReader::DecodeThreaded :
+                 OggVorbisFileReader::DecodeAtOnce,
+                 OggVorbisFileReader::CacheInTemporaryFile,
+                 targetRate);
+        }
+    }
+#endif
+#endif
+
+#ifdef HAVE_MAD
+    if (!reader) {
+        if (MP3FileReader::supports(source)) {
+            reader = new MP3FileReader
+                (source,
+                 threading ?
+                 MP3FileReader::DecodeThreaded :
+                 MP3FileReader::DecodeAtOnce,
+                 MP3FileReader::CacheInTemporaryFile,
+                 targetRate);
+        }
+    }
+#endif
+
+#ifdef HAVE_QUICKTIME
+    if (!reader) {
+        if (QuickTimeFileReader::supports(source)) {
+            reader = new QuickTimeFileReader
+                (source,
+                 threading ?
+                 QuickTimeFileReader::DecodeThreaded : 
+                 QuickTimeFileReader::DecodeAtOnce,
+                 QuickTimeFileReader::CacheInTemporaryFile,
+                 targetRate);
+        }
+    }
+#endif
+
+    if (reader) {
+        if (reader->isOK()) {
+//            std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl;
+            return reader;
+        }
+        std::cerr << "AudioFileReaderFactory: Preferred reader for "
+                  << "url \"" << source.getLocation().toStdString()
+                  << "\" (content type \""
+                  << source.getContentType().toStdString() << "\") failed";
+
+        if (reader->getError() != "") {
+            std::cerr << ": \"" << reader->getError().toStdString() << "\"";
+        }
+        std::cerr << std::endl;
+        delete reader;
+        reader = 0;
+    }
+
+    std::cerr << "AudioFileReaderFactory: No reader" << std::endl;
+    return reader;
+}
+
--- a/data/fileio/CodedAudioFileReader.cpp	Wed Feb 27 18:04:10 2008 +0000
+++ b/data/fileio/CodedAudioFileReader.cpp	Sat Mar 01 16:17:44 2008 +0000
@@ -98,6 +98,7 @@
     }
     if (m_sampleRate == 0) {
         m_sampleRate = m_fileRate;
+        std::cerr << "CodedAudioFileReader::initialiseDecodeCache: rate (from file) = " << m_fileRate << std::endl;
     }
     if (m_fileRate != m_sampleRate) {
         std::cerr << "CodedAudioFileReader: resampling " << m_fileRate << " -> " <<  m_sampleRate << std::endl;
--- a/data/fileio/MP3FileReader.cpp	Wed Feb 27 18:04:10 2008 +0000
+++ b/data/fileio/MP3FileReader.cpp	Sat Mar 01 16:17:44 2008 +0000
@@ -135,9 +135,12 @@
         m_decodeThread = new DecodeThread(this);
         m_decodeThread->start();
 
-        while ((m_channelCount == 0 || m_fileRate == 0) && !m_done) {
+        while ((m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0)
+               && !m_done) {
             usleep(10);
         }
+        
+        std::cerr << "MP3FileReader ctor: exiting with file rate = " << m_fileRate << std::endl;
     }
 }
 
--- a/data/fileio/OggVorbisFileReader.cpp	Wed Feb 27 18:04:10 2008 +0000
+++ b/data/fileio/OggVorbisFileReader.cpp	Sat Mar 01 16:17:44 2008 +0000
@@ -97,7 +97,7 @@
     } else {
 
         while (oggz_read(m_oggz, 1024) > 0 &&
-               m_channelCount == 0);
+               (m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0));
 
         if (m_channelCount > 0) {
             m_decodeThread = new DecodeThread(this);