ITANetAudioStream.cpp 2.88 KB
Newer Older
1
2
#include <ITANetAudioStream.h>

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
3
4
#include <ITANetAudioStreamingClient.h>

5
6
7
8
9
10
11
12
13
14
15
16
// ITA includes
#include <ITAException.h>

// STL
#include <cmath>

CITANetAudioStream::CITANetAudioStream( int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity )
	: m_sfOutputStreamBuffer( iChannels, iBufferSize, true )
	, m_dSampleRate( dSamplingRate )
	, m_sfRingBuffer( iChannels, iRingBufferCapacity, true )
	
{
17
18
19
20
	if( iBufferSize > iRingBufferCapacity )
		ITA_EXCEPT1( INVALID_PARAMETER, "Ring buffer capacity can not be smaller than buffer size." );

	m_pNetAudioStreamingClient = new CITANetAudioStreamingClient( this );
Anne's avatar
Anne committed
21
22
	m_iReadCursor = 0;
	m_iWriteCursor = 0;
23
24
}

25
26
CITANetAudioStream::~CITANetAudioStream()
{
27
	delete m_pNetAudioStreamingClient;
28
29
}

30
31
bool CITANetAudioStream::Connect( const std::string& sAddress, int iPort )
{
32
	return m_pNetAudioStreamingClient->Connect( sAddress, iPort );
33
34
}

35
bool CITANetAudioStream::GetIsConnected() const
36
{
37
	return m_pNetAudioStreamingClient->GetIsConnected();
38
39
}

40
const float* CITANetAudioStream::GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* )
41
{
Anne's avatar
Anne committed
42
43
44
45
46
	m_sfOutputStreamBuffer[uiChannel].Zero();
	if (this->GetIsConnected())
	{
		// todo restlichen kopieren und dann rein und raus faden
		int iCurrentWritePointer = m_iWriteCursor;
Anne's avatar
Anne committed
47
48
		m_sfRingBuffer[uiChannel].cyclic_read(m_sfOutputStreamBuffer[uiChannel].GetData(),
			m_sfOutputStreamBuffer.GetLength(), m_iReadCursor);
49
	}
50
	return m_sfOutputStreamBuffer[ uiChannel ].GetData();
51
52
}

53
void CITANetAudioStream::IncrementBlockPointer()
54
{
55
56
	// Increment read cursor by one audio block and wrap around if exceeding ring buffer
	m_iReadCursor = ( m_iReadCursor + m_sfOutputStreamBuffer.GetLength() ) % m_sfRingBuffer.GetLength();
Anne's avatar
Anne committed
57
	m_pNetAudioStreamingClient->TriggerBlockIncrement( );
58
59
}

60
int CITANetAudioStream::Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples )
61
{
62
63
	int iCurrentReadCursor = m_iReadCursor;

64
	// write samples in the buffer
65
66
67
	m_sfRingBuffer.cyclic_write(sfNewSamples, iNumSamples, 
		iCurrentReadCursor, m_iWriteCursor);

68
	// set write curser
Anne's avatar
Anne committed
69
	m_iWriteCursor = ( m_iWriteCursor + iNumSamples ) % m_sfRingBuffer.GetLength( );
Anne's avatar
Anne committed
70

71
	// return free BufferSize
72
73
74
75
76
	if (iCurrentReadCursor > m_iWriteCursor) {
		return m_iWriteCursor - iCurrentReadCursor;
	}
	else {
		return m_sfRingBuffer.GetLength() - m_iWriteCursor + iCurrentReadCursor;
Anne's avatar
Anne committed
77
	}	
78
79
}

80
81
int CITANetAudioStream::GetRingbufferFreeSamples()
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
82
83
	int iFreeSamples = ( m_iWriteCursor - m_iReadCursor + GetRingBufferSize() ) % GetRingBufferSize();
	return iFreeSamples;
84
85
}

86
int CITANetAudioStream::GetRingBufferSize() const
87
{
88
	return m_sfRingBuffer.GetLength();
89
90
}

91
unsigned int CITANetAudioStream::GetBlocklength() const
92
{
93
	return  ( unsigned int ) m_sfOutputStreamBuffer.GetLength();
94
95
}

96
unsigned int CITANetAudioStream::GetNumberOfChannels() const
97
{
98
	return ( unsigned int ) m_sfOutputStreamBuffer.channels();
99
100
}

101
double CITANetAudioStream::GetSampleRate() const
102
{
103
	return m_dSampleRate;
104
}