From 7e1d63dd9fd149e4934bf77095c8610fac786b04 Mon Sep 17 00:00:00 2001 From: Henri Herscher Date: Thu, 20 Oct 2005 13:40:58 +0000 Subject: First checkin git-svn-id: https://oreka.svn.sourceforge.net/svnroot/oreka/trunk@2 09dcff7a-b715-0410-9601-b79a96267cd0 --- orkbasecxx/AudioCapture.cpp | 201 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 orkbasecxx/AudioCapture.cpp (limited to 'orkbasecxx/AudioCapture.cpp') diff --git a/orkbasecxx/AudioCapture.cpp b/orkbasecxx/AudioCapture.cpp new file mode 100644 index 0000000..3178f2e --- /dev/null +++ b/orkbasecxx/AudioCapture.cpp @@ -0,0 +1,201 @@ +/* + * 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_encoding = UnknownAudio; + m_timestamp = 0; + m_sequenceNumber = 0; + m_numBytes = 0; + m_pBuffer = NULL; + m_sampleRate = 8000; +} + +AudioChunk::~AudioChunk() +{ + if(m_pBuffer) + { + free(m_pBuffer); + } +} + +void AudioChunk::SetBuffer(void* pBuffer, size_t numBytes, AudioEncodingEnum encoding, unsigned int timestamp, unsigned int sequenceNumber) +{ + if(m_pBuffer) + { + free(m_pBuffer); + 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 + { + m_numBytes = numBytes; + memcpy(m_pBuffer, pBuffer, numBytes); + m_encoding = encoding; + m_timestamp = timestamp; + m_sequenceNumber = sequenceNumber; + } + } +} + +int AudioChunk::GetNumSamples() +{ + switch(m_encoding) + { + case PcmAudio: + return m_numBytes/2; + case AlawAudio: case UlawAudio: + return m_numBytes; + default: + throw(CStdString("AudioChunk::GetNumSamples: unknown encoding")); + } +} + +double AudioChunk::GetDurationSec() +{ + int i = 0; + return ((double)GetNumSamples())/((double)m_sampleRate); +} + + +double AudioChunk::ComputeRms() +{ + double rmsValue = 0; + if(m_encoding == PcmAudio) + { + for(int i=0; i