#include <MP3FileReader.h>

Inheritance diagram for MP3FileReader:
Inheritance graph
Collaboration diagram for MP3FileReader:
Collaboration graph

Classes

struct  DecoderData
 
class  DecodeThread
 

Public Types

enum  GaplessMode { GaplessMode::Gapless, GaplessMode::Gappy }
 How the MP3FileReader should handle leading and trailing gaps. More...
 
enum  CacheMode { CacheInTemporaryFile, CacheInMemory }
 
enum  DecodeMode { DecodeAtOnce, DecodeThreaded }
 
typedef std::map< QString, QString > TagMap
 Return any tag pairs picked up from the audio file. More...
 

Public Slots

void cancelled ()
 

Signals

void progress (int)
 
void frameCountChanged ()
 

Public Member Functions

 MP3FileReader (FileSource source, DecodeMode decodeMode, CacheMode cacheMode, GaplessMode gaplessMode, sv_samplerate_t targetRate=0, bool normalised=false, ProgressReporter *reporter=0)
 
virtual ~MP3FileReader ()
 
QString getError () const override
 If isOK() is false, return an error string. More...
 
QString getLocation () const override
 Return the location of the audio data in the reader (as passed in to the FileSource constructor, for example). More...
 
QString getTitle () const override
 Return the title of the work in the audio file, if known. More...
 
QString getMaker () const override
 Return the "maker" of the work in the audio file, if known. More...
 
TagMap getTags () const override
 
int getDecodeCompletion () const override
 Return a percentage value indicating how far through decoding the audio file we are. More...
 
bool isUpdating () const override
 Return true if decoding is still in progress and the frame count may change. More...
 
floatvec_t getInterleavedFrames (sv_frame_t start, sv_frame_t count) const override
 Return interleaved samples for count frames from index start. More...
 
sv_samplerate_t getNativeRate () const override
 Return the native samplerate of the file. More...
 
QString getLocalFilename () const override
 Return the local file path of the audio data. More...
 
bool isQuicklySeekable () const override
 Intermediate cache means all CodedAudioFileReaders are quickly seekable. More...
 
bool isOK () const
 Return true if the file was opened successfully and no error has subsequently occurred. More...
 
sv_frame_t getFrameCount () const
 Return the number of audio sample frames (i.e. More...
 
int getChannelCount () const
 Return the number of channels in the file. More...
 
sv_samplerate_t getSampleRate () const
 Return the samplerate at which the file is being read. More...
 
virtual std::vector< floatvec_tgetDeInterleavedFrames (sv_frame_t start, sv_frame_t count) const
 Return de-interleaved samples for count frames from index start. More...
 

Static Public Member Functions

static void getSupportedExtensions (std::set< QString > &extensions)
 
static bool supportsExtension (QString ext)
 
static bool supportsContentType (QString type)
 
static bool supports (FileSource &source)
 

Protected Member Functions

bool decode (void *mm, sv_frame_t sz)
 
enum mad_flow filter (struct mad_stream const *, struct mad_frame *)
 
enum mad_flow accept (struct mad_header const *, struct mad_pcm *)
 
void loadTags (int fd)
 
QString loadTag (void *vtag, const char *name)
 
void initialiseDecodeCache ()
 
void setFramesToTrim (sv_frame_t fromStart, sv_frame_t fromEnd)
 
void addSamplesToDecodeCache (float **samples, sv_frame_t nframes)
 
void addSamplesToDecodeCache (float *samplesInterleaved, sv_frame_t nframes)
 
void addSamplesToDecodeCache (const floatvec_t &interleaved)
 
void finishDecodeCache ()
 
bool isDecodeCacheInitialised () const
 
void startSerialised (QString id, const std::atomic< bool > *cancelled)
 
void endSerialised ()
 

Static Protected Member Functions

static enum mad_flow input_callback (void *, struct mad_stream *)
 
static enum mad_flow output_callback (void *, struct mad_header const *, struct mad_pcm *)
 
static enum mad_flow filter_callback (void *, struct mad_stream const *, struct mad_frame *)
 
static enum mad_flow error_callback (void *, struct mad_stream *, struct mad_frame *)
 

Protected Attributes

FileSource m_source
 
QString m_path
 
QString m_error
 
QString m_title
 
QString m_maker
 
TagMap m_tags
 
GaplessMode m_gaplessMode
 
sv_frame_t m_fileSize
 
double m_bitrateNum
 
int m_bitrateDenom
 
int m_mp3FrameCount
 
int m_completion
 
bool m_done
 
unsigned char * m_fileBuffer
 
size_t m_fileBufferSize
 
float ** m_sampleBuffer
 
size_t m_sampleBufferSize
 
ProgressReporterm_reporter
 
std::atomic< bool > m_cancelled
 
bool m_decodeErrorShown
 
DecodeThreadm_decodeThread
 
QMutex m_cacheMutex
 
CacheMode m_cacheMode
 
floatvec_t m_data
 
QMutex m_dataLock
 
bool m_initialised
 
Serialiserm_serialiser
 
sv_samplerate_t m_fileRate
 
QString m_cacheFileName
 
SNDFILE * m_cacheFileWritePtr
 
WavFileReaderm_cacheFileReader
 
float * m_cacheWriteBuffer
 
sv_frame_t m_cacheWriteBufferIndex
 
sv_frame_t m_cacheWriteBufferFrames
 
breakfastquay::Resampler * m_resampler
 
float * m_resampleBuffer
 
int m_resampleBufferFrames
 
sv_frame_t m_fileFrameCount
 
bool m_normalised
 
float m_max
 
float m_gain
 
sv_frame_t m_trimFromStart
 
sv_frame_t m_trimFromEnd
 
sv_frame_t m_clippedCount
 
sv_frame_t m_firstNonzero
 
sv_frame_t m_lastNonzero
 
sv_frame_t m_frameCount
 
int m_channelCount
 
sv_samplerate_t m_sampleRate
 

Detailed Description

Definition at line 31 of file MP3FileReader.h.

Member Typedef Documentation

typedef std::map<QString, QString> AudioFileReader::TagMap
inherited

Return any tag pairs picked up from the audio file.

See also getTitle and getMaker, and note that a reader which does not implement getTags may still return values from those.

Definition at line 117 of file AudioFileReader.h.

Member Enumeration Documentation

How the MP3FileReader should handle leading and trailing gaps.

See http://lame.sourceforge.net/tech-FAQ.txt for a technical explanation of the numbers here.

Enumerator
Gapless 

Trim unwanted samples from the start and end of the decoded audio.

From the start, trim a number of samples equal to the decoder delay (a fixed 529 samples) plus any encoder delay that may be specified in Xing/LAME metadata. From the end, trim any padding specified in Xing/LAME metadata, less the fixed decoder delay. This usually results in "gapless" audio, i.e. with no spurious zero padding at either end.

Gappy 

Do not trim any samples.

Also do not suppress any frames from being passed to the mp3 decoder, even Xing/LAME metadata frames. This will result in the audio being padded with zeros at either end: at the start, typically 529+576+1152 = 2257 samples for LAME-encoded mp3s; at the end an unknown number depending on the fill ratio of the final coded frame, but typically less than 1152-529 = 623.

This mode produces the same output as produced by older versions of this code before the gapless option was added, and is present mostly for backward compatibility.

Definition at line 41 of file MP3FileReader.h.

Enumerator
CacheInTemporaryFile 
CacheInMemory 

Definition at line 47 of file CodedAudioFileReader.h.

Enumerator
DecodeAtOnce 
DecodeThreaded 

Definition at line 52 of file CodedAudioFileReader.h.

Constructor & Destructor Documentation

MP3FileReader::~MP3FileReader ( )
virtual

Definition at line 175 of file MP3FileReader.cpp.

References m_cancelled, and m_decodeThread.

Member Function Documentation

QString MP3FileReader::getError ( ) const
inlineoverridevirtual

If isOK() is false, return an error string.

Reimplemented from AudioFileReader.

Definition at line 78 of file MP3FileReader.h.

References m_error.

QString MP3FileReader::getLocation ( ) const
inlineoverridevirtual

Return the location of the audio data in the reader (as passed in to the FileSource constructor, for example).

This might be a remote URL.

See also getLocalFilename().

Implements AudioFileReader.

Definition at line 80 of file MP3FileReader.h.

References FileSource::getLocation(), and m_source.

QString MP3FileReader::getTitle ( ) const
inlineoverridevirtual

Return the title of the work in the audio file, if known.

This may be implemented by subclasses that support file tagging. This is not the same thing as the file name.

Implements AudioFileReader.

Definition at line 81 of file MP3FileReader.h.

References m_title.

QString MP3FileReader::getMaker ( ) const
inlineoverridevirtual

Return the "maker" of the work in the audio file, if known.

This could represent almost anything (band, composer, conductor, artist etc).

Implements AudioFileReader.

Definition at line 82 of file MP3FileReader.h.

References m_maker.

TagMap MP3FileReader::getTags ( ) const
inlineoverridevirtual

Reimplemented from AudioFileReader.

Definition at line 83 of file MP3FileReader.h.

References getSupportedExtensions(), m_tags, supports(), supportsContentType(), and supportsExtension().

void MP3FileReader::getSupportedExtensions ( std::set< QString > &  extensions)
static
bool MP3FileReader::supportsExtension ( QString  ext)
static

Definition at line 620 of file MP3FileReader.cpp.

References getSupportedExtensions().

Referenced by getTags(), and supports().

bool MP3FileReader::supportsContentType ( QString  type)
static

Definition at line 628 of file MP3FileReader.cpp.

Referenced by getTags(), and supports().

int MP3FileReader::getDecodeCompletion ( ) const
inlineoverridevirtual

Return a percentage value indicating how far through decoding the audio file we are.

This should be implemented by subclasses that will not know exactly how long the audio file is (in sample frames) until it has been completely decoded. A reader that initialises the frame count directly within its constructor should always return 100 from this.

Reimplemented from AudioFileReader.

Definition at line 90 of file MP3FileReader.h.

References m_completion.

bool MP3FileReader::isUpdating ( ) const
inlineoverridevirtual

Return true if decoding is still in progress and the frame count may change.

Reimplemented from AudioFileReader.

Definition at line 92 of file MP3FileReader.h.

References cancelled(), and m_decodeThread.

void MP3FileReader::cancelled ( )
slot

Definition at line 187 of file MP3FileReader.cpp.

References m_cancelled.

Referenced by isUpdating(), and MP3FileReader().

enum mad_flow MP3FileReader::filter ( struct mad_stream const *  stream,
struct mad_frame *   
)
protected
enum mad_flow MP3FileReader::input_callback ( void *  dp,
struct mad_stream *  stream 
)
staticprotected
enum mad_flow MP3FileReader::output_callback ( void *  dp,
struct mad_header const *  header,
struct mad_pcm *  pcm 
)
staticprotected

Definition at line 478 of file MP3FileReader.cpp.

References accept(), and MP3FileReader::DecoderData::reader.

Referenced by decode().

enum mad_flow MP3FileReader::filter_callback ( void *  dp,
struct mad_stream const *  stream,
struct mad_frame *  frame 
)
staticprotected

Definition at line 380 of file MP3FileReader.cpp.

References filter(), and MP3FileReader::DecoderData::reader.

Referenced by decode().

enum mad_flow MP3FileReader::error_callback ( void *  dp,
struct mad_stream *  stream,
struct mad_frame *   
)
staticprotected
void MP3FileReader::loadTags ( int  fd)
protected

Definition at line 193 of file MP3FileReader.cpp.

References loadTag(), m_maker, m_tags, m_title, and SVDEBUG.

Referenced by MP3FileReader().

QString MP3FileReader::loadTag ( void *  vtag,
const char *  name 
)
protected

Definition at line 243 of file MP3FileReader.cpp.

References SVDEBUG.

Referenced by loadTags().

floatvec_t CodedAudioFileReader::getInterleavedFrames ( sv_frame_t  start,
sv_frame_t  count 
) const
overridevirtualinherited

Return interleaved samples for count frames from index start.

The resulting vector will contain count * getChannelCount() samples (or fewer if end of file is reached).

The subclass implementations of this function must be thread-safe – that is, safe to call from multiple threads with different arguments on the same object at the same time.

Implements AudioFileReader.

Definition at line 550 of file CodedAudioFileReader.cpp.

References CodedAudioFileReader::CacheInMemory, CodedAudioFileReader::CacheInTemporaryFile, WavFileReader::getInterleavedFrames(), AudioFileReader::isOK(), CodedAudioFileReader::m_cacheFileReader, CodedAudioFileReader::m_cacheMode, AudioFileReader::m_channelCount, CodedAudioFileReader::m_data, CodedAudioFileReader::m_dataLock, CodedAudioFileReader::m_gain, CodedAudioFileReader::m_initialised, CodedAudioFileReader::m_normalised, and SVDEBUG.

sv_samplerate_t CodedAudioFileReader::getNativeRate ( ) const
inlineoverridevirtualinherited

Return the native samplerate of the file.

This will differ from getSampleRate() if the file is being resampled because it was requested to open at a different rate from native.

Reimplemented from AudioFileReader.

Definition at line 59 of file CodedAudioFileReader.h.

QString CodedAudioFileReader::getLocalFilename ( ) const
inlineoverridevirtualinherited

Return the local file path of the audio data.

This is the filesystem location most likely to contain readable audio data, but it may be in a different place or format from the originally specified location - for example, if the file has been retrieved and decoded, then it will be the (possibly temporary) decode target file.

This returns a non-empty value only if there is some local filename that contains exactly the audio data being provided by this reader. In some cases this may not exist, for example when a file has been resampled or normalised directly into a memory buffer. In this case, return an empty string.

See also getLocation().

Implements AudioFileReader.

Definition at line 61 of file CodedAudioFileReader.h.

bool CodedAudioFileReader::isQuicklySeekable ( ) const
inlineoverridevirtualinherited

Intermediate cache means all CodedAudioFileReaders are quickly seekable.

Implements AudioFileReader.

Definition at line 64 of file CodedAudioFileReader.h.

void CodedAudioFileReader::progress ( int  )
signalinherited
void CodedAudioFileReader::setFramesToTrim ( sv_frame_t  fromStart,
sv_frame_t  fromEnd 
)
protectedinherited
bool CodedAudioFileReader::isDecodeCacheInitialised ( ) const
inlineprotectedinherited
void CodedAudioFileReader::startSerialised ( QString  id,
const std::atomic< bool > *  cancelled 
)
protectedinherited

Definition at line 108 of file CodedAudioFileReader.cpp.

References CodedAudioFileReader::m_serialiser.

Referenced by accept().

void CodedAudioFileReader::endSerialised ( )
protectedinherited
sv_frame_t AudioFileReader::getFrameCount ( ) const
inlineinherited
int AudioFileReader::getChannelCount ( ) const
inlineinherited
sv_samplerate_t AudioFileReader::getSampleRate ( ) const
inlineinherited

Return the samplerate at which the file is being read.

This is the rate requested when the file was opened, which may differ from the native rate of the file (in which case the file will be resampled as it is read).

Definition at line 62 of file AudioFileReader.h.

References AudioFileReader::m_sampleRate.

Referenced by AudioFileReaderFactory::createReader(), DecodingWavFileReader::DecodingWavFileReader(), AudioFileSizeEstimator::estimate(), ReadOnlyWaveFileModel::getSampleRate(), and ReadOnlyWaveFileModel::ReadOnlyWaveFileModel().

vector< floatvec_t > AudioFileReader::getDeInterleavedFrames ( sv_frame_t  start,
sv_frame_t  count 
) const
virtualinherited

Return de-interleaved samples for count frames from index start.

Implemented in this class (it calls getInterleavedFrames and de-interleaves). The resulting vector will contain getChannelCount() sample blocks of count samples each (or fewer if end of file is reached).

Definition at line 21 of file AudioFileReader.cpp.

References AudioFileReader::getChannelCount(), and AudioFileReader::getInterleavedFrames().

Referenced by AudioFileReader::isUpdating().

void AudioFileReader::frameCountChanged ( )
signalinherited

Member Data Documentation

FileSource MP3FileReader::m_source
protected

Definition at line 100 of file MP3FileReader.h.

Referenced by getLocation().

QString MP3FileReader::m_path
protected

Definition at line 101 of file MP3FileReader.h.

Referenced by error_callback(), and MP3FileReader().

QString MP3FileReader::m_error
protected

Definition at line 102 of file MP3FileReader.h.

Referenced by getError(), and MP3FileReader().

QString MP3FileReader::m_title
protected

Definition at line 103 of file MP3FileReader.h.

Referenced by getTitle(), and loadTags().

QString MP3FileReader::m_maker
protected

Definition at line 104 of file MP3FileReader.h.

Referenced by getMaker(), and loadTags().

TagMap MP3FileReader::m_tags
protected

Definition at line 105 of file MP3FileReader.h.

Referenced by getTags(), and loadTags().

GaplessMode MP3FileReader::m_gaplessMode
protected

Definition at line 106 of file MP3FileReader.h.

Referenced by filter(), and MP3FileReader().

sv_frame_t MP3FileReader::m_fileSize
protected

Definition at line 107 of file MP3FileReader.h.

Referenced by accept(), and MP3FileReader().

double MP3FileReader::m_bitrateNum
protected

Definition at line 108 of file MP3FileReader.h.

Referenced by accept(), and MP3FileReader().

int MP3FileReader::m_bitrateDenom
protected

Definition at line 109 of file MP3FileReader.h.

Referenced by accept(), and MP3FileReader().

int MP3FileReader::m_mp3FrameCount
protected

Definition at line 110 of file MP3FileReader.h.

Referenced by accept(), decode(), filter(), and MP3FileReader().

int MP3FileReader::m_completion
protected

Definition at line 111 of file MP3FileReader.h.

Referenced by accept(), getDecodeCompletion(), and MP3FileReader().

bool MP3FileReader::m_done
protected

Definition at line 112 of file MP3FileReader.h.

Referenced by decode(), and MP3FileReader().

unsigned char* MP3FileReader::m_fileBuffer
protected

Definition at line 114 of file MP3FileReader.h.

Referenced by MP3FileReader().

size_t MP3FileReader::m_fileBufferSize
protected

Definition at line 115 of file MP3FileReader.h.

Referenced by MP3FileReader().

float** MP3FileReader::m_sampleBuffer
protected

Definition at line 117 of file MP3FileReader.h.

Referenced by accept(), and MP3FileReader().

size_t MP3FileReader::m_sampleBufferSize
protected

Definition at line 118 of file MP3FileReader.h.

Referenced by accept(), and MP3FileReader().

ProgressReporter* MP3FileReader::m_reporter
protected

Definition at line 120 of file MP3FileReader.h.

Referenced by accept(), and MP3FileReader().

std::atomic<bool> MP3FileReader::m_cancelled
protected

Definition at line 121 of file MP3FileReader.h.

Referenced by accept(), cancelled(), MP3FileReader(), and ~MP3FileReader().

bool MP3FileReader::m_decodeErrorShown
protected

Definition at line 123 of file MP3FileReader.h.

Referenced by error_callback().

DecodeThread* MP3FileReader::m_decodeThread
protected

Definition at line 154 of file MP3FileReader.h.

Referenced by isUpdating(), MP3FileReader(), and ~MP3FileReader().

QMutex CodedAudioFileReader::m_dataLock
mutableprotectedinherited
Serialiser* CodedAudioFileReader::m_serialiser
protectedinherited
QString CodedAudioFileReader::m_cacheFileName
protectedinherited
sv_frame_t CodedAudioFileReader::m_cacheWriteBufferIndex
protectedinherited
sv_frame_t CodedAudioFileReader::m_cacheWriteBufferFrames
protectedinherited
int CodedAudioFileReader::m_resampleBufferFrames
protectedinherited
sv_frame_t CodedAudioFileReader::m_fileFrameCount
protectedinherited
bool CodedAudioFileReader::m_normalised
protectedinherited
float CodedAudioFileReader::m_max
protectedinherited
float CodedAudioFileReader::m_gain
protectedinherited
sv_frame_t CodedAudioFileReader::m_trimFromStart
protectedinherited
sv_frame_t CodedAudioFileReader::m_clippedCount
protectedinherited
sv_frame_t CodedAudioFileReader::m_firstNonzero
protectedinherited
sv_frame_t CodedAudioFileReader::m_lastNonzero
protectedinherited

The documentation for this class was generated from the following files: