annotate data/fileio/CodedAudioFileReader.h @ 1861:9219834e39a4

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