Show stream.cpp syntax highlighted
/***************************************************************************
stream.cpp - Ogg Vorbis stream
-------------------
begin : Wed Mar 17 2004
copyright : (C) 2004 by Reality Rift Studios
email : mattias@realityrift.com
***************************************************************************
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is the NeoEngine, NeoACVorbis, stream.cpp
The Initial Developer of the Original Code is Mattias Jansson.
Portions created by Mattias Jansson are Copyright (C) 2004
Reality Rift Studios. All Rights Reserved.
***************************************************************************/
#include "stream.h"
using namespace std;
using namespace NeoEngine;
namespace NeoACVorbis
{
Stream::Stream( File *pkFile ) :
SoundStream( pkFile )
{
pkFile->SetBinary( true );
pkFile->Seekg( 0, ios::beg );
ov_callbacks cb;
cb.read_func = read_stream;
cb.seek_func = seek_stream;
cb.close_func = close_stream;
cb.tell_func = tell_stream;
ov_open_callbacks( pkFile, &m_kVorbis, 0, 0, cb );
vorbis_info *pkInfo = ov_info( &m_kVorbis, -1 );
m_uiSize = (unsigned int)ov_pcm_total( &m_kVorbis, -1 ) * 2 * pkInfo->channels; // 2 == 16/8
m_uiNumChannels = pkInfo->channels;
m_uiSamplesPerSecond = pkInfo->rate;
m_uiBitsPerSample = 16;
}
Stream::~Stream()
{
ov_clear( &m_kVorbis );
}
void Stream::Reset()
{
SoundStream::Reset();
ov_pcm_seek( &m_kVorbis, 0 );
}
unsigned int Stream::Decode( unsigned char *pucBuffer, unsigned int uiBytes )
{
char *pucCur = (char*)pucBuffer;
unsigned int uiRead = 0;
int iSection = 0;
bool bEOF = false;
while ( ( uiRead < uiBytes ) && !bEOF )
{
long ret = ov_read( &m_kVorbis, pucCur, uiBytes - uiRead, 0, 2, 1, &iSection );
if( ret <= 0 )
bEOF = true;
else
{
uiRead += ret;
pucCur += ret;
}
}
m_uiCurPos += uiRead;
return uiRead;
}
size_t read_stream( void* ptr, size_t size, size_t nmemb, void *datasource )
{
File* pkFile = (File*)datasource;
int iNumBytes = (int)( size * nmemb );
int iNumAvail = 0;
std::streampos curpos = pkFile->Tellg();
pkFile->Seekg( 0, std::ios::end );
std::streampos endpos = pkFile->Tellg();
pkFile->Seekg( curpos, std::ios::beg );
iNumAvail = (int)( endpos - curpos );
if( iNumAvail < iNumBytes )
iNumBytes = iNumAvail;
pkFile->Read( ptr, iNumBytes );
return iNumBytes;
}
int seek_stream( void *datasource, ogg_int64_t offset, int whence )
{
File *pkFile = (File*)datasource;
std::streamoff offs = (std::streamoff)offset;
switch( whence )
{
case SEEK_SET:
pkFile->Seekg( offs, std::ios::beg );
break;
case SEEK_CUR:
pkFile->Seekg( offs, std::ios::cur );
break;
case SEEK_END:
pkFile->Seekg( offs, std::ios::end );
break;
}
return 0;
}
int close_stream(void *datasource )
{
return 0;
}
long tell_stream( void *datasource )
{
return long( ((File*)datasource)->Tellg() );
}
};
See more files for this project here