annotate data/fileio/CodedAudioFileReader.h @ 1316:a421e752d22c 3.0-integration

Add Apple Lossless files
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 02 Dec 2016 09:14:11 +0000
parents c84629395040
children f830a10bfbd6
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@148 7 This file copyright 2006 Chris Cannam.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #ifndef _CODED_AUDIO_FILE_READER_H_
Chris@148 17 #define _CODED_AUDIO_FILE_READER_H_
Chris@148 18
Chris@148 19 #include "AudioFileReader.h"
Chris@148 20
Chris@148 21 #include <sndfile.h>
Chris@263 22 #include <QMutex>
Chris@543 23 #include <QReadWriteLock>
Chris@148 24
Chris@148 25 class WavFileReader;
Chris@297 26 class Serialiser;
Chris@297 27 class Resampler;
Chris@148 28
Chris@148 29 class CodedAudioFileReader : public AudioFileReader
Chris@148 30 {
Chris@357 31 Q_OBJECT
Chris@357 32
Chris@148 33 public:
Chris@148 34 virtual ~CodedAudioFileReader();
Chris@148 35
Chris@148 36 enum CacheMode {
Chris@148 37 CacheInTemporaryFile,
Chris@148 38 CacheInMemory
Chris@148 39 };
Chris@148 40
Chris@1097 41 enum DecodeMode {
Chris@1097 42 DecodeAtOnce, // decode the file on construction, with progress
Chris@1097 43 DecodeThreaded // decode in a background thread after construction
Chris@1097 44 };
Chris@1097 45
Chris@1096 46 virtual std::vector<float> getInterleavedFrames(sv_frame_t start, sv_frame_t count) const;
Chris@148 47
Chris@1040 48 virtual sv_samplerate_t getNativeRate() const { return m_fileRate; }
Chris@297 49
Chris@1010 50 virtual QString getLocalFilename() const { return m_cacheFileName; }
Chris@1010 51
Chris@823 52 /// Intermediate cache means all CodedAudioFileReaders are quickly seekable
Chris@823 53 virtual bool isQuicklySeekable() const { return true; }
Chris@823 54
Chris@357 55 signals:
Chris@357 56 void progress(int);
Chris@357 57
Chris@148 58 protected:
Chris@933 59 CodedAudioFileReader(CacheMode cacheMode,
Chris@1040 60 sv_samplerate_t targetRate,
Chris@920 61 bool normalised);
Chris@148 62
Chris@148 63 void initialiseDecodeCache(); // samplerate, channels must have been set
Chris@544 64
Chris@1305 65 // compensation for encoder delays:
Chris@1307 66 void setFramesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd);
Chris@1305 67
Chris@544 68 // may throw InsufficientDiscSpace:
Chris@1038 69 void addSamplesToDecodeCache(float **samples, sv_frame_t nframes);
Chris@1038 70 void addSamplesToDecodeCache(float *samplesInterleaved, sv_frame_t nframes);
Chris@1096 71 void addSamplesToDecodeCache(const std::vector<float> &interleaved);
Chris@544 72
Chris@544 73 // may throw InsufficientDiscSpace:
Chris@148 74 void finishDecodeCache();
Chris@544 75
Chris@148 76 bool isDecodeCacheInitialised() const { return m_initialised; }
Chris@148 77
Chris@297 78 void startSerialised(QString id);
Chris@297 79 void endSerialised();
Chris@297 80
Chris@297 81 private:
Chris@1306 82 void pushCacheWriteBufferMaybe(bool final);
Chris@1306 83
Chris@1306 84 sv_frame_t pushBuffer(float *interleaved, sv_frame_t sz, bool final);
Chris@1306 85
Chris@1306 86 // to be called only by pushBuffer
Chris@1038 87 void pushBufferResampling(float *interleaved, sv_frame_t sz, double ratio, bool final);
Chris@1306 88
Chris@1306 89 // to be called only by pushBuffer and pushBufferResampling
Chris@1038 90 void pushBufferNonResampling(float *interleaved, sv_frame_t sz);
Chris@297 91
Chris@297 92 protected:
Chris@263 93 QMutex m_cacheMutex;
Chris@148 94 CacheMode m_cacheMode;
Chris@1096 95 std::vector<float> m_data;
Chris@1100 96 mutable QMutex m_dataLock;
Chris@148 97 bool m_initialised;
Chris@297 98 Serialiser *m_serialiser;
Chris@1040 99 sv_samplerate_t m_fileRate;
Chris@148 100
Chris@290 101 QString m_cacheFileName;
Chris@148 102 SNDFILE *m_cacheFileWritePtr;
Chris@148 103 WavFileReader *m_cacheFileReader;
Chris@148 104 float *m_cacheWriteBuffer;
Chris@1307 105 sv_frame_t m_cacheWriteBufferIndex; // samples
Chris@1038 106 sv_frame_t m_cacheWriteBufferSize; // frames
Chris@297 107
Chris@297 108 Resampler *m_resampler;
Chris@297 109 float *m_resampleBuffer;
Chris@1038 110 sv_frame_t m_fileFrameCount;
Chris@920 111
Chris@920 112 bool m_normalised;
Chris@920 113 float m_max;
Chris@920 114 float m_gain;
Chris@1285 115
Chris@1305 116 sv_frame_t m_trimFromStart;
Chris@1305 117 sv_frame_t m_trimFromEnd;
Chris@1305 118
Chris@1285 119 sv_frame_t m_clippedCount;
Chris@1285 120 sv_frame_t m_firstNonzero;
Chris@1286 121 sv_frame_t m_lastNonzero;
Chris@148 122 };
Chris@148 123
Chris@148 124 #endif