-
Notifications
You must be signed in to change notification settings - Fork 8
/
DecoderFlac.h
60 lines (46 loc) · 1.87 KB
/
DecoderFlac.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include "Decoder.h"
#include "FLAC++\all.h"
#include <fstream>
#include <vector>
// FLAC decoder
class DecoderFlac : public Decoder, public FLAC::Decoder::Stream
{
public:
// 'filename' - file name.
// 'context' - context for which the decoder is to be used.
// Throws a std::runtime_error exception if the file could not be loaded.
DecoderFlac( const std::wstring& filename, const Context context );
~DecoderFlac() override;
// Reads sample data.
// 'buffer' - output buffer (floating point format scaled to +/-1.0f).
// 'sampleCount' - number of samples to read.
// Returns the number of samples read, or zero if the stream has ended.
long Read( float* buffer, const long sampleCount ) override;
// Seeks to a 'position' in the stream, in seconds.
// Returns the new position in seconds.
double Seek( const double position ) override;
protected:
// FLAC callbacks
FLAC__StreamDecoderReadStatus read_callback( FLAC__byte [], size_t * ) override;
FLAC__StreamDecoderSeekStatus seek_callback( FLAC__uint64 ) override;
FLAC__StreamDecoderTellStatus tell_callback( FLAC__uint64 * ) override;
FLAC__StreamDecoderLengthStatus length_callback( FLAC__uint64 * ) override;
bool eof_callback() override;
FLAC__StreamDecoderWriteStatus write_callback( const FLAC__Frame *, const FLAC__int32 *const [] ) override;
void metadata_callback( const FLAC__StreamMetadata * ) override;
void error_callback( FLAC__StreamDecoderErrorStatus ) override;
private:
// Calculates the bitrate of the FLAC stream (returns nullopt if the bitrate was not calculated).
std::optional<float> CalculateBitrate();
// Input file stream.
std::ifstream m_FileStream;
// Current FLAC frame.
FLAC__Frame m_FLACFrame;
// Frame buffer.
std::vector<float> m_FrameBuffer;
// Current frame buffer position.
uint32_t m_FramePos;
// Indicates whether this is a valid FLAC stream.
bool m_Valid;
};