summaryrefslogtreecommitdiff
path: root/orkbasecxx/AudioCapture.cpp
blob: e1c64c34cd93bbc1931af601a8ff6d2ee845f445 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

#define _WINSOCKAPI_		// prevents the inclusion of winsock.h

#include "Utils.h"
#include "math.h"
#include "AudioCapture.h"


AudioChunk::AudioChunk()
{
	m_details.Clear();
	m_pBuffer = NULL;
}

AudioChunk::~AudioChunk()
{
	if(m_pBuffer)
	{
		free(m_pBuffer);
	}
}

void* AudioChunk::CreateBuffer(size_t numBytes, AudioChunkDetails& details)
{
	if(m_pBuffer)
	{
		free(m_pBuffer);
		m_pBuffer = NULL;
		m_details.m_numBytes = 0;
	}
	if(numBytes)
	{
		m_pBuffer = calloc(numBytes, 1);
	}
	if (!m_pBuffer)
	{
		CStdString numBytesString = IntToString(numBytes);
		throw("AudioChunk::AudioChunk: could not calloc a buffer of size:" + numBytesString);
	}
	else
	{
		m_details = details;
		m_details.m_numBytes = numBytes;
	}
	return m_pBuffer;
}

void AudioChunk::SetBuffer(void* pBuffer, size_t numBytes, AudioChunkDetails& details)
{
	if(m_pBuffer)
	{
		free(m_pBuffer);
		m_pBuffer = NULL;
		m_details.m_numBytes = 0;
	}
	if(numBytes && pBuffer)
	{
		m_pBuffer = malloc(numBytes);
		if (!m_pBuffer)
		{
			CStdString numBytesString = IntToString(numBytes);
			throw("AudioChunk::AudioChunk: could not malloc a buffer of size:" + numBytesString);
		}
		else
		{
			memcpy(m_pBuffer, pBuffer, numBytes);
			m_details = details;
			m_details.m_numBytes = numBytes;
		}
	}
}

int AudioChunk::GetNumSamples()
{
	switch(m_details.m_encoding)
	{
	case PcmAudio:
		return m_details.m_numBytes/2;
	case AlawAudio: case UlawAudio:
		return m_details.m_numBytes;
	default:
		throw(CStdString("AudioChunk::GetNumSamples: unknown encoding"));
	}
}

double AudioChunk::GetDurationSec()
{
	int i = 0;
	return ((double)GetNumSamples())/((double)m_details.m_sampleRate);
}


double AudioChunk::ComputeRms()
{
	double rmsValue = 0;
	if(m_details.m_encoding == PcmAudio)
	{
		for(int i=0; i<GetNumSamples(); i++)
		{
			rmsValue += ((short *)m_pBuffer)[i] * ((short *)m_pBuffer)[i];
		}
		rmsValue = sqrt(rmsValue/GetNumSamples());
	}
	return rmsValue;
}

double AudioChunk::ComputeRmsDb()
{
	double rmsDbValue = 10 * log10(1.0/32768.0);	// default value, the lowest possible
	if(m_details.m_encoding == PcmAudio)
	{
		rmsDbValue = 10 * log10(ComputeRms()/32768.0);
	}
	return rmsDbValue;
}

AudioEncodingEnum AudioChunk::GetEncoding()
{
	return m_details.m_encoding;
}

int AudioChunk::GetSampleRate()
{
	return m_details.m_sampleRate;
}

AudioChunkDetails* AudioChunk::GetDetails()
{
	return &m_details;
}

void AudioChunk::SetDetails(AudioChunkDetails* details)
{
	m_details = *details;
}


int AudioChunk::GetNumBytes()
{
	return m_details.m_numBytes;
}

//================================
AudioChunkDetails::AudioChunkDetails()
{
	Clear();
}

void AudioChunkDetails::Clear()
{
	m_marker = MEDIA_CHUNK_MARKER;
	m_encoding = UnknownAudio;
	m_timestamp = 0;
	m_arrivalTimestamp = 0;
	m_sequenceNumber = 0;
	m_numBytes = 0;
	m_sampleRate = 8000;
	m_rtpPayloadType = -1;
	m_channel = 0;			// mono by default
}


//=================================

CaptureEvent::CaptureEvent()
{
	m_timestamp = 0;
	m_type = EtUnknown;
}

CStdString CaptureEvent::DirectionToString(int direction)
{
	switch(direction)
	{
	case DirIn:
		return DIR_IN;
	case DirOut:
		return DIR_OUT;
	}
	return DIR_UNKN;
}

int CaptureEvent::DirectionToEnum(CStdString& dir)
{
	if(dir.CompareNoCase(DIR_IN) == 0)
	{
		return DirIn;
	}
	else if(dir.CompareNoCase(DIR_OUT) == 0)
	{
		return DirOut;
	}
	return DirUnkn;
}

CStdString CaptureEvent::EventTypeToString(int eventTypeEnum)
{
	switch(eventTypeEnum)
	{
	case	EtUnknown:
		return ET_UNKNOWN;
	case	EtStart:
		return ET_START;
	case	EtStop:
		return ET_STOP;
	case	EtDirection:
		return ET_DIRECTION;
	case	EtRemoteParty:
		return ET_REMOTEPARTY;
	case	EtLocalParty:
		return ET_LOCALPARTY;
	case	EtLocalEntryPoint:
		return ET_LOCALENTRYPOINT;
	case	EtKeyValue:
		return ET_KEYVALUE;
	}
	return ET_INVALID;
}

int CaptureEvent::EventTypeToEnum(CStdString& eventTypeString)
{
	int eventTypeEnum = EtUnknown;
	if(eventTypeString.CompareNoCase(ET_START) == 0)
	{
		eventTypeEnum = EtStart;
	}
	else if (eventTypeString.CompareNoCase(ET_STOP) == 0)
	{
		eventTypeEnum = EtStop;
	}
	else if (eventTypeString.CompareNoCase(ET_DIRECTION) == 0)
	{
		eventTypeEnum = EtDirection;
	}
	else if (eventTypeString.CompareNoCase(ET_REMOTEPARTY) == 0)
	{
		eventTypeEnum = EtRemoteParty;
	}
	else if (eventTypeString.CompareNoCase(ET_LOCALPARTY) == 0)
	{
		eventTypeEnum = EtLocalParty;
	}
	else if (eventTypeString.CompareNoCase(ET_LOCALENTRYPOINT) == 0)
	{
		eventTypeEnum = EtLocalEntryPoint;
	}
	else if (eventTypeString.CompareNoCase(ET_KEYVALUE) == 0)
	{
		eventTypeEnum = EtKeyValue;
	}
	return eventTypeEnum;
}