annotate data/fileio/CodedAudioFileReader.h @ 1337:3dcd83595bc8 3.0-integration

Make writable model updating explicit rather than essentially an arbitrary hidden accident
author Chris Cannam
date Wed, 04 Jan 2017 14:22:39 +0000
parents 54af1e21705c
children 1c9bbbb6116a
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@1318 27
Chris@1318 28 namespace breakfastquay {
Chris@1318 29 class Resampler;
Chris@1318 30 }
Chris@148 31
Chris@148 32 class CodedAudioFileReader : public AudioFileReader
Chris@148 33 {
Chris@357 34 Q_OBJECT
Chris@357 35
Chris@148 36 public:
Chris@148 37 virtual ~CodedAudioFileReader();
Chris@148 38
Chris@148 39 enum CacheMode {
Chris@148 40 CacheInTemporaryFile,
Chris@148 41 CacheInMemory
Chris@148 42 };
Chris@148 43
Chris@1097 44 enum DecodeMode {
Chris@1097 45 DecodeAtOnce, // decode the file on construction, with progress
Chris@1097 46 DecodeThreaded // decode in a background thread after construction
Chris@1097 47 };
Chris@1097 48
Chris@1326 49 virtual floatvec_t getInterleavedFrames(sv_frame_t start, sv_frame_t count) const;
Chris@148 50
Chris@1040 51 virtual sv_samplerate_t getNativeRate() const { return m_fileRate; }
Chris@297 52
Chris@1010 53 virtual QString getLocalFilename() const { return m_cacheFileName; }
Chris@1010 54
Chris@823 55 /// Intermediate cache means all CodedAudioFileReaders are quickly seekable
Chris@823 56 virtual bool isQuicklySeekable() const { return true; }
Chris@823 57
Chris@357 58 signals:
Chris@357 59 void progress(int);
Chris@357 60
Chris@148 61 protected:
Chris@933 62 CodedAudioFileReader(CacheMode cacheMode,
Chris@1040 63 sv_samplerate_t targetRate,
Chris@920 64 bool normalised);
Chris@148 65
Chris@148 66 void initialiseDecodeCache(); // samplerate, channels must have been set
Chris@544 67
Chris@1305 68 // compensation for encoder delays:
Chris@1307 69 void setFramesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd);
Chris@1305 70
Chris@544 71 // may throw InsufficientDiscSpace:
Chris@1038 72 void addSamplesToDecodeCache(float **samples, sv_frame_t nframes);
Chris@1038 73 void addSamplesToDecodeCache(float *samplesInterleaved, sv_frame_t nframes);
Chris@1326 74 void addSamplesToDecodeCache(const floatvec_t &interleaved);
Chris@544 75
Chris@544 76 // may throw InsufficientDiscSpace:
Chris@148 77 void finishDecodeCache();
Chris@544 78
Chris@148 79 bool isDecodeCacheInitialised() const { return m_initialised; }
Chris@148 80
Chris@297 81 void startSerialised(QString id);
Chris@297 82 void endSerialised();
Chris@297 83
Chris@297 84 private:
Chris@1306 85 void pushCacheWriteBufferMaybe(bool final);
Chris@1306 86
Chris@1306 87 sv_frame_t pushBuffer(float *interleaved, sv_frame_t sz, bool final);
Chris@1306 88
Chris@1306 89 // to be called only by pushBuffer
Chris@1038 90 void pushBufferResampling(float *interleaved, sv_frame_t sz, double ratio, bool final);
Chris@1306 91
Chris@1306 92 // to be called only by pushBuffer and pushBufferResampling
Chris@1038 93 void pushBufferNonResampling(float *interleaved, sv_frame_t sz);
Chris@297 94
Chris@297 95 protected:
Chris@263 96 QMutex m_cacheMutex;
Chris@148 97 CacheMode m_cacheMode;
Chris@1326 98 floatvec_t m_data;
Chris@1100 99 mutable QMutex m_dataLock;
Chris@148 100 bool m_initialised;
Chris@297 101 Serialiser *m_serialiser;
Chris@1040 102 sv_samplerate_t m_fileRate;
Chris@148 103
Chris@290 104 QString m_cacheFileName;
Chris@148 105 SNDFILE *m_cacheFileWritePtr;
Chris@148 106 WavFileReader *m_cacheFileReader;
Chris@148 107 float *m_cacheWriteBuffer;
Chris@1320 108 sv_frame_t m_cacheWriteBufferIndex; // buffer write pointer in samples
Chris@1320 109 sv_frame_t m_cacheWriteBufferFrames; // buffer size in frames
Chris@297 110
Chris@1318 111 breakfastquay::Resampler *m_resampler;
Chris@297 112 float *m_resampleBuffer;
Chris@1320 113 int m_resampleBufferFrames;
Chris@1038 114 sv_frame_t m_fileFrameCount;
Chris@920 115
Chris@920 116 bool m_normalised;
Chris@920 117 float m_max;
Chris@920 118 float m_gain;
Chris@1285 119
Chris@1305 120 sv_frame_t m_trimFromStart;
Chris@1305 121 sv_frame_t m_trimFromEnd;
Chris@1305 122
Chris@1285 123 sv_frame_t m_clippedCount;
Chris@1285 124 sv_frame_t m_firstNonzero;
Chris@1286 125 sv_frame_t m_lastNonzero;
Chris@148 126 };
Chris@148 127
Chris@148 128 #endif