CodedAudioFileReader.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_CODED_AUDIO_FILE_READER_H
17 #define SV_CODED_AUDIO_FILE_READER_H
18 
19 #include "AudioFileReader.h"
20 
21 #include <QMutex>
22 #include <QReadWriteLock>
23 
24 #ifdef Q_OS_WIN
25 #include <windows.h>
26 #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
27 #endif
28 
29 #include <sndfile.h>
30 
31 #include <atomic>
32 
33 class WavFileReader;
34 class Serialiser;
35 
36 namespace breakfastquay {
37  class Resampler;
38 }
39 
41 {
42  Q_OBJECT
43 
44 public:
45  virtual ~CodedAudioFileReader();
46 
47  enum CacheMode {
49  CacheInMemory
50  };
51 
52  enum DecodeMode {
53  DecodeAtOnce, // decode the file on construction, with progress
54  DecodeThreaded // decode in a background thread after construction
55  };
56 
57  floatvec_t getInterleavedFrames(sv_frame_t start, sv_frame_t count) const override;
58 
59  sv_samplerate_t getNativeRate() const override { return m_fileRate; }
60 
61  QString getLocalFilename() const override { return m_cacheFileName; }
62 
64  bool isQuicklySeekable() const override { return true; }
65 
66 signals:
67  void progress(int);
68 
69 protected:
71  sv_samplerate_t targetRate,
72  bool normalised);
73 
74  void initialiseDecodeCache(); // samplerate, channels must have been set
75 
76  // compensation for encoder delays:
77  void setFramesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd);
78 
79  // may throw InsufficientDiscSpace:
80  void addSamplesToDecodeCache(float **samples, sv_frame_t nframes);
81  void addSamplesToDecodeCache(float *samplesInterleaved, sv_frame_t nframes);
82  void addSamplesToDecodeCache(const floatvec_t &interleaved);
83 
84  // may throw InsufficientDiscSpace:
85  void finishDecodeCache();
86 
87  bool isDecodeCacheInitialised() const { return m_initialised; }
88 
89  void startSerialised(QString id, const std::atomic<bool> *cancelled);
90  void endSerialised();
91 
92 private:
93  void pushCacheWriteBufferMaybe(bool final);
94 
95  sv_frame_t pushBuffer(float *interleaved, sv_frame_t sz, bool final);
96 
97  // to be called only by pushBuffer
98  void pushBufferResampling(float *interleaved, sv_frame_t sz, double ratio, bool final);
99 
100  // to be called only by pushBuffer and pushBufferResampling
101  void pushBufferNonResampling(float *interleaved, sv_frame_t sz);
102 
103 protected:
104  QMutex m_cacheMutex;
107  mutable QMutex m_dataLock;
111 
116  sv_frame_t m_cacheWriteBufferIndex; // buffer write pointer in samples
117  sv_frame_t m_cacheWriteBufferFrames; // buffer size in frames
118 
119  breakfastquay::Resampler *m_resampler;
123 
125  float m_max;
126  float m_gain;
127 
130 
134 };
135 
136 #endif
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
bool isQuicklySeekable() const override
Intermediate cache means all CodedAudioFileReaders are quickly seekable.
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
Reader for audio files using libsndfile.
Definition: WavFileReader.h:41
std::vector< float, breakfastquay::StlAllocator< float > > floatvec_t
Definition: BaseTypes.h:53
QString getLocalFilename() const override
Return the local file path of the audio data.
sv_samplerate_t getNativeRate() const override
Return the native samplerate of the file.
WavFileReader * m_cacheFileReader
breakfastquay::Resampler * m_resampler
bool isDecodeCacheInitialised() const