annotate data/fileio/CodedAudioFileReader.h @ 1440:04caefd35391 streaming-csv-writer

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