forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecoderCDDA.cpp
70 lines (64 loc) · 2.04 KB
/
DecoderCDDA.cpp
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
60
61
62
63
64
65
66
67
68
69
70
#include "DecoderCDDA.h"
#include "Utility.h"
DecoderCDDA::DecoderCDDA( const CDDAMedia& cddaMedia, const long track ) :
Decoder(),
m_CDDAMedia( cddaMedia ),
m_SectorStart( m_CDDAMedia.GetStartSector( track ) ),
m_SectorEnd( m_CDDAMedia.GetStartSector( track + 1 ) ),
m_Handle( ( m_SectorEnd > m_SectorStart ) ? m_CDDAMedia.Open() : nullptr ),
m_Buffer(),
m_CurrentSector( m_SectorStart ),
m_CurrentBufPos( 0 )
{
if ( nullptr == m_Handle ) {
throw std::runtime_error( "DecoderCDDA could not open track" );
} else {
SetSampleRate( 44100 );
SetChannels( 2 );
SetBPS( 16 );
SetDuration( static_cast<float>( m_CDDAMedia.GetTrackLength( track ) ) / ( 44100 * 4 ) );
}
}
DecoderCDDA::~DecoderCDDA()
{
m_CDDAMedia.Close( m_Handle );
}
long DecoderCDDA::Read( float* buffer, const long sampleCount )
{
long samplesRead = 0;
long outputBufPos = 0;
while ( samplesRead < sampleCount ) {
if ( m_CurrentBufPos < m_Buffer.size() ) {
buffer[ outputBufPos++ ] = Signed16ToFloat( m_Buffer[ m_CurrentBufPos++ ] );
buffer[ outputBufPos++ ] = Signed16ToFloat( m_Buffer[ m_CurrentBufPos++ ] );
++samplesRead;
} else {
if ( ( m_CurrentSector < m_SectorEnd ) && ( m_CDDAMedia.Read( m_Handle, m_CurrentSector++, true /*useCache*/, m_Buffer ) ) ) {
m_CurrentBufPos = 0;
} else {
break;
}
}
}
return samplesRead;
}
float DecoderCDDA::Seek( const float position )
{
float seekPosition = 0;
const float duration = GetDuration();
if ( duration > 0 ) {
const long seekSector = m_SectorStart + static_cast<long>( ( m_SectorEnd - m_SectorStart ) * ( position / duration ) );
if ( ( seekSector >= m_SectorStart ) && ( seekSector < m_SectorEnd ) ) {
m_CurrentSector = seekSector;
m_CurrentBufPos = 0;
m_Buffer.clear();
seekPosition = position;
}
}
return seekPosition;
}
std::optional<float> DecoderCDDA::CalculateTrackGain( CanContinue canContinue, const float secondsLimit )
{
// Reading a CD is too slow to provide an estimate.
return ( secondsLimit > 0 ) ? std::nullopt : Decoder::CalculateTrackGain( canContinue, secondsLimit );
}