forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBuffer.cpp
193 lines (162 loc) · 5.51 KB
/
CBuffer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//--------------------------------------------------------------------------------------
// CBuffer.cpp
//
// Circular buffer
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "CBuffer.h"
//
// CBuffer()
//
CBuffer::CBuffer(uint32_t size)
{
ZeroMemory( &m_sourceFormat, sizeof( WAVEFORMATEX ) );
ZeroMemory( &m_renderFormat, sizeof( WAVEFORMATEX ) );
AllocateSize(size);
m_front = 0;
m_back = 0;
m_sourceSampleSize = 0;
m_renderSampleSize = 0;
if (!InitializeCriticalSectionEx(&m_critSec, 0, 0))
{
DX::ThrowIfFailed(HRESULT_FROM_WIN32(GetLastError()));
}
}
//
// ~CBuffer()
//
CBuffer::~CBuffer()
{
ZeroMemory( &m_buffer, sizeof(m_size ) );
ZeroMemory( &m_sourceFormat, sizeof( WAVEFORMATEX ) );
ZeroMemory( &m_renderFormat, sizeof( WAVEFORMATEX ) );
DeleteCriticalSection( &m_critSec );
}
//--------------------------------------------------------------------------------------
// Name: AllocateSize
// Desc: Allocates buffer
//--------------------------------------------------------------------------------------
void CBuffer::AllocateSize(uint32_t inSize)
{
m_buffer = new BYTE[inSize];
ZeroMemory(m_buffer, inSize);
m_size = inSize;
m_free = inSize;
}
//--------------------------------------------------------------------------------------
// Name: SetSourceFormat
// Desc: Sets the format of the data entering the buffering
//--------------------------------------------------------------------------------------
void CBuffer::SetSourceFormat(WAVEFORMATEX* sourceWfx)
{
CopyMemory(&m_sourceFormat, sourceWfx, sizeof( WAVEFORMATEX));
SetFormatCalculations();
}
//--------------------------------------------------------------------------------------
// Name: SetRenderFormat
// Desc: Sets the format of the data stored in the buffer
//--------------------------------------------------------------------------------------
void CBuffer::SetRenderFormat(WAVEFORMATEX* renderWfx)
{
CopyMemory(&m_renderFormat, renderWfx, sizeof( WAVEFORMATEX));
SetFormatCalculations();
//We can't trust the format in the buffer, so empty it
m_front = 0;
m_back = 0;
}
//--------------------------------------------------------------------------------------
// Name: SetFormatCalculations
// Desc: Sets the format of the data stored in the buffer
//--------------------------------------------------------------------------------------
void CBuffer::SetFormatCalculations()
{
if(m_sourceFormat.cbSize != 0 && m_renderFormat.cbSize != 0)
{
if(m_sourceFormat.wBitsPerSample != 32 || m_renderFormat.wBitsPerSample != 32)
{
//We only support 32bit
m_sourceSampleSize = 0;
m_renderSampleSize = 0;
}
else
{
m_sourceSampleSize = m_sourceFormat.nChannels * m_sourceFormat.nBlockAlign;
m_renderSampleSize = m_renderFormat.nChannels * m_renderFormat.nBlockAlign;
}
}
}
//--------------------------------------------------------------------------------------
// Name: GetCaptureBuffer
// Desc: Gets data from the buffer
//--------------------------------------------------------------------------------------
void CBuffer::GetCaptureBuffer(uint32_t numBytesRequested, byte *data)
{
//Fills the data buffer with data from circular buffer and silent the rest
uint32_t numBytesWritten = 0;
if(numBytesRequested > GetCurrentUsage() || m_sourceSampleSize == 0 || m_renderSampleSize == 0)
{
return;
}
EnterCriticalSection( &m_critSec );
if(numBytesRequested > m_size - m_front)
{
//We need to circle
CopyMemory(data, m_buffer + m_front, m_size - m_front);
numBytesWritten = m_size - m_front;
m_front = 0;
}
CopyMemory(data + numBytesWritten, m_buffer + m_front, numBytesRequested - numBytesWritten);
m_front += numBytesRequested - numBytesWritten;
m_free += numBytesRequested;
if(m_front >= m_size)
{
m_front -= m_size;
}
LeaveCriticalSection( &m_critSec );
}
//--------------------------------------------------------------------------------------
// Name: SetCaptureBuffer
// Desc: Add data to buffer
//--------------------------------------------------------------------------------------
void CBuffer::SetCaptureBuffer(uint32_t numBytesGiven, byte *data)
{
if(m_sourceSampleSize == 0 || m_renderSampleSize == 0)
{
return;
}
EnterCriticalSection( &m_critSec );
uint32_t numSamplesGiven = numBytesGiven/m_sourceSampleSize;
for( uint32_t sample = 0; sample < numSamplesGiven; sample++ )
{
//Add mono sample
CopyMemory(m_buffer + m_back, data + (sample*m_sourceSampleSize), m_sourceSampleSize);
m_back += m_sourceSampleSize;
if(m_back >= m_size)
{
m_back -= m_size;
}
//Add blank space for any additional channels
for(DWORD channels = 1; channels < m_renderFormat.nChannels; channels++)
{
ZeroMemory(m_buffer + m_back, m_sourceSampleSize);
m_back += m_sourceSampleSize;
if(m_back >= m_size)
{
m_back -= m_size;
}
}
}
if(m_renderSampleSize/m_sourceSampleSize*numBytesGiven > m_free)
{
m_front = m_back;
m_free = 0;
}
else
{
m_free -= numBytesGiven * m_renderSampleSize/m_sourceSampleSize;
}
LeaveCriticalSection( &m_critSec );
}