2020-05-22 15:00:18 +00:00
|
|
|
#include "common.h"
|
2020-05-04 17:33:48 +00:00
|
|
|
|
|
|
|
#ifdef AUDIO_OAL
|
2020-05-22 15:00:18 +00:00
|
|
|
#include "stream.h"
|
2020-05-07 06:26:16 +00:00
|
|
|
#include "sampman.h"
|
|
|
|
|
2020-05-22 14:40:19 +00:00
|
|
|
#ifdef AUDIO_OPUS
|
|
|
|
#include <opusfile.h>
|
|
|
|
#else
|
2020-05-11 02:55:57 +00:00
|
|
|
#ifdef _WIN32
|
2020-05-07 06:26:16 +00:00
|
|
|
#pragma comment( lib, "libsndfile-1.lib" )
|
2020-08-03 14:04:00 +00:00
|
|
|
#pragma comment( lib, "libmpg123-0.lib" )
|
2020-05-22 19:49:54 +00:00
|
|
|
#else
|
|
|
|
#include "crossplatform.h"
|
2020-05-11 02:55:57 +00:00
|
|
|
#endif
|
2020-05-22 15:00:18 +00:00
|
|
|
#include <sndfile.h>
|
|
|
|
#include <mpg123.h>
|
2020-05-22 14:40:19 +00:00
|
|
|
#endif
|
2020-05-07 06:26:16 +00:00
|
|
|
|
2020-05-22 14:40:19 +00:00
|
|
|
#ifndef AUDIO_OPUS
|
2020-05-07 06:26:16 +00:00
|
|
|
class CSndFile : public IDecoder
|
|
|
|
{
|
|
|
|
SNDFILE *m_pfSound;
|
|
|
|
SF_INFO m_soundInfo;
|
|
|
|
public:
|
|
|
|
CSndFile(const char *path) :
|
|
|
|
m_pfSound(nil)
|
|
|
|
{
|
|
|
|
memset(&m_soundInfo, 0, sizeof(m_soundInfo));
|
|
|
|
m_pfSound = sf_open(path, SFM_READ, &m_soundInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CSndFile()
|
|
|
|
{
|
|
|
|
if ( m_pfSound )
|
|
|
|
{
|
|
|
|
sf_close(m_pfSound);
|
|
|
|
m_pfSound = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOpened()
|
|
|
|
{
|
|
|
|
return m_pfSound != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleSize()
|
|
|
|
{
|
|
|
|
return sizeof(uint16);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleCount()
|
|
|
|
{
|
|
|
|
return m_soundInfo.frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleRate()
|
|
|
|
{
|
|
|
|
return m_soundInfo.samplerate;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetChannels()
|
|
|
|
{
|
|
|
|
return m_soundInfo.channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Seek(uint32 milliseconds)
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return;
|
|
|
|
sf_seek(m_pfSound, ms2samples(milliseconds), SF_SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Tell()
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
|
|
|
return samples2ms(sf_seek(m_pfSound, 0, SF_SEEK_CUR));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Decode(void *buffer)
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
|
|
|
return sf_read_short(m_pfSound, (short *)buffer, GetBufferSamples()) * GetSampleSize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CMP3File : public IDecoder
|
|
|
|
{
|
|
|
|
mpg123_handle *m_pMH;
|
|
|
|
bool m_bOpened;
|
|
|
|
uint32 m_nRate;
|
|
|
|
uint32 m_nChannels;
|
|
|
|
public:
|
|
|
|
CMP3File(const char *path) :
|
|
|
|
m_pMH(nil),
|
|
|
|
m_bOpened(false),
|
|
|
|
m_nRate(0),
|
|
|
|
m_nChannels(0)
|
|
|
|
{
|
|
|
|
m_pMH = mpg123_new(nil, nil);
|
|
|
|
if ( m_pMH )
|
|
|
|
{
|
|
|
|
long rate = 0;
|
|
|
|
int channels = 0;
|
|
|
|
int encoding = 0;
|
|
|
|
|
|
|
|
m_bOpened = mpg123_open(m_pMH, path) == MPG123_OK
|
|
|
|
&& mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK;
|
|
|
|
m_nRate = rate;
|
|
|
|
m_nChannels = channels;
|
|
|
|
|
|
|
|
if ( IsOpened() )
|
|
|
|
{
|
|
|
|
mpg123_format_none(m_pMH);
|
|
|
|
mpg123_format(m_pMH, rate, channels, encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~CMP3File()
|
|
|
|
{
|
|
|
|
if ( m_pMH )
|
|
|
|
{
|
|
|
|
mpg123_close(m_pMH);
|
|
|
|
mpg123_delete(m_pMH);
|
|
|
|
m_pMH = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOpened()
|
|
|
|
{
|
|
|
|
return m_bOpened;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleSize()
|
|
|
|
{
|
|
|
|
return sizeof(uint16);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleCount()
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
|
|
|
return mpg123_length(m_pMH);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleRate()
|
|
|
|
{
|
|
|
|
return m_nRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetChannels()
|
|
|
|
{
|
|
|
|
return m_nChannels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Seek(uint32 milliseconds)
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return;
|
2020-10-05 10:59:40 +00:00
|
|
|
mpg123_seek(m_pMH, ms2samples(milliseconds), SEEK_SET);
|
2020-05-07 06:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Tell()
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
2020-10-05 10:59:40 +00:00
|
|
|
return samples2ms(mpg123_tell(m_pMH));
|
2020-05-07 06:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Decode(void *buffer)
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
|
|
|
|
|
|
|
size_t size;
|
|
|
|
int err = mpg123_read(m_pMH, (unsigned char *)buffer, GetBufferSize(), &size);
|
2020-07-22 11:56:28 +00:00
|
|
|
#if defined(__LP64__) || defined(_WIN64)
|
|
|
|
assert("We can't handle audio files more then 2 GB yet :shrug:" && (size < UINT32_MAX));
|
|
|
|
#endif
|
2020-05-07 06:26:16 +00:00
|
|
|
if (err != MPG123_OK && err != MPG123_DONE) return 0;
|
2020-07-22 11:56:28 +00:00
|
|
|
return (uint32)size;
|
2020-05-07 06:26:16 +00:00
|
|
|
}
|
|
|
|
};
|
2020-05-22 14:40:19 +00:00
|
|
|
#else
|
|
|
|
class COpusFile : public IDecoder
|
|
|
|
{
|
|
|
|
OggOpusFile *m_FileH;
|
|
|
|
bool m_bOpened;
|
|
|
|
uint32 m_nRate;
|
|
|
|
uint32 m_nChannels;
|
|
|
|
public:
|
2020-05-24 15:43:50 +00:00
|
|
|
COpusFile(const char *path) : m_FileH(nil),
|
2020-05-22 14:40:19 +00:00
|
|
|
m_bOpened(false),
|
|
|
|
m_nRate(0),
|
|
|
|
m_nChannels(0)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
m_FileH = op_open_file(path, &ret);
|
|
|
|
|
|
|
|
if (m_FileH) {
|
|
|
|
m_nChannels = op_head(m_FileH, 0)->channel_count;
|
2020-10-24 13:37:14 +00:00
|
|
|
m_nRate = 48000;
|
2020-05-24 15:43:50 +00:00
|
|
|
const OpusTags *tags = op_tags(m_FileH, 0);
|
2020-05-22 14:40:19 +00:00
|
|
|
for (int i = 0; i < tags->comments; i++) {
|
|
|
|
if (strncmp(tags->user_comments[i], "SAMPLERATE", sizeof("SAMPLERATE")-1) == 0)
|
|
|
|
{
|
|
|
|
sscanf(tags->user_comments[i], "SAMPLERATE=%i", &m_nRate);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bOpened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~COpusFile()
|
|
|
|
{
|
|
|
|
if (m_FileH)
|
|
|
|
{
|
|
|
|
op_free(m_FileH);
|
|
|
|
m_FileH = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOpened()
|
|
|
|
{
|
|
|
|
return m_bOpened;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleSize()
|
|
|
|
{
|
|
|
|
return sizeof(uint16);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleCount()
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
|
|
|
return op_pcm_total(m_FileH, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetSampleRate()
|
|
|
|
{
|
|
|
|
return m_nRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GetChannels()
|
|
|
|
{
|
|
|
|
return m_nChannels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Seek(uint32 milliseconds)
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return;
|
2020-10-05 10:59:40 +00:00
|
|
|
op_pcm_seek(m_FileH, ms2samples(milliseconds) / GetChannels());
|
2020-05-22 14:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Tell()
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
2020-10-05 10:59:40 +00:00
|
|
|
return samples2ms(op_pcm_tell(m_FileH) * GetChannels());
|
2020-05-22 14:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 Decode(void *buffer)
|
|
|
|
{
|
|
|
|
if ( !IsOpened() ) return 0;
|
|
|
|
|
|
|
|
int size = op_read(m_FileH, (opus_int16 *)buffer, GetBufferSamples(), NULL);
|
|
|
|
|
|
|
|
if (size < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return size * m_nChannels * GetSampleSize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
2020-05-04 17:33:48 +00:00
|
|
|
|
|
|
|
void CStream::Initialise()
|
|
|
|
{
|
2020-05-22 14:40:19 +00:00
|
|
|
#ifndef AUDIO_OPUS
|
2020-05-07 06:26:16 +00:00
|
|
|
mpg123_init();
|
2020-05-22 14:40:19 +00:00
|
|
|
#endif
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::Terminate()
|
|
|
|
{
|
2020-05-22 14:40:19 +00:00
|
|
|
#ifndef AUDIO_OPUS
|
2020-05-07 06:26:16 +00:00
|
|
|
mpg123_exit();
|
2020-05-22 14:40:19 +00:00
|
|
|
#endif
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CStream::CStream(char *filename, ALuint &source, ALuint (&buffers)[NUM_STREAMBUFFERS]) :
|
|
|
|
m_alSource(source),
|
|
|
|
m_alBuffers(buffers),
|
2020-05-07 06:26:16 +00:00
|
|
|
m_pBuffer(nil),
|
|
|
|
m_bPaused(false),
|
|
|
|
m_bActive(false),
|
|
|
|
m_pSoundFile(nil),
|
|
|
|
m_bReset(false),
|
|
|
|
m_nVolume(0),
|
|
|
|
m_nPan(0),
|
|
|
|
m_nPosBeforeReset(0)
|
2020-05-04 17:33:48 +00:00
|
|
|
|
|
|
|
{
|
2020-05-11 02:55:57 +00:00
|
|
|
// Be case-insensitive on linux (from https://github.com/OneSadCookie/fcaseopen/)
|
|
|
|
#if !defined(_WIN32)
|
2020-07-26 17:59:58 +00:00
|
|
|
char *real = casepath(filename);
|
|
|
|
if (real) {
|
|
|
|
strcpy(m_aFilename, real);
|
|
|
|
free(real);
|
2020-05-11 02:55:57 +00:00
|
|
|
} else {
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
strcpy(m_aFilename, filename);
|
|
|
|
}
|
2020-05-04 17:33:48 +00:00
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
DEV("Stream %s\n", m_aFilename);
|
|
|
|
|
2020-05-22 14:40:19 +00:00
|
|
|
#ifndef AUDIO_OPUS
|
2020-05-07 06:26:16 +00:00
|
|
|
if (!strcasecmp(&m_aFilename[strlen(m_aFilename) - strlen(".mp3")], ".mp3"))
|
|
|
|
m_pSoundFile = new CMP3File(m_aFilename);
|
|
|
|
else if (!strcasecmp(&m_aFilename[strlen(m_aFilename) - strlen(".wav")], ".wav"))
|
|
|
|
m_pSoundFile = new CSndFile(m_aFilename);
|
2020-05-22 14:40:19 +00:00
|
|
|
#else
|
|
|
|
if (!strcasecmp(&m_aFilename[strlen(m_aFilename) - strlen(".opus")], ".opus"))
|
|
|
|
m_pSoundFile = new COpusFile(m_aFilename);
|
|
|
|
#endif
|
2020-05-07 06:26:16 +00:00
|
|
|
else
|
|
|
|
m_pSoundFile = nil;
|
2020-10-18 16:16:37 +00:00
|
|
|
|
|
|
|
if ( IsOpened() )
|
2020-05-07 06:26:16 +00:00
|
|
|
{
|
|
|
|
m_pBuffer = malloc(m_pSoundFile->GetBufferSize());
|
|
|
|
ASSERT(m_pBuffer!=nil);
|
|
|
|
|
|
|
|
DEV("AvgSamplesPerSec: %d\n", m_pSoundFile->GetAvgSamplesPerSec());
|
|
|
|
DEV("SampleCount: %d\n", m_pSoundFile->GetSampleCount());
|
|
|
|
DEV("SampleRate: %d\n", m_pSoundFile->GetSampleRate());
|
|
|
|
DEV("Channels: %d\n", m_pSoundFile->GetChannels());
|
|
|
|
DEV("Buffer Samples: %d\n", m_pSoundFile->GetBufferSamples());
|
|
|
|
DEV("Buffer sec: %f\n", (float(m_pSoundFile->GetBufferSamples()) / float(m_pSoundFile->GetChannels())/ float(m_pSoundFile->GetSampleRate())));
|
|
|
|
DEV("Length MS: %02d:%02d\n", (m_pSoundFile->GetLength() / 1000) / 60, (m_pSoundFile->GetLength() / 1000) % 60);
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
return;
|
2020-05-07 06:26:16 +00:00
|
|
|
}
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CStream::~CStream()
|
|
|
|
{
|
|
|
|
Delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::Delete()
|
2020-05-07 06:26:16 +00:00
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
ClearBuffers();
|
|
|
|
|
|
|
|
if ( m_pSoundFile )
|
|
|
|
{
|
|
|
|
delete m_pSoundFile;
|
|
|
|
m_pSoundFile = nil;
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
if ( m_pBuffer )
|
|
|
|
{
|
|
|
|
free(m_pBuffer);
|
2020-05-07 06:26:16 +00:00
|
|
|
m_pBuffer = nil;
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
bool CStream::HasSource()
|
|
|
|
{
|
|
|
|
return m_alSource != AL_NONE;
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
bool CStream::IsOpened()
|
|
|
|
{
|
2020-10-18 16:16:37 +00:00
|
|
|
return m_pSoundFile && m_pSoundFile->IsOpened();
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CStream::IsPlaying()
|
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
if ( !HasSource() || !IsOpened() ) return false;
|
|
|
|
|
2020-10-18 16:16:37 +00:00
|
|
|
if ( !m_bPaused )
|
2020-05-07 06:26:16 +00:00
|
|
|
{
|
|
|
|
ALint sourceState;
|
|
|
|
alGetSourcei(m_alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
if ( m_bActive || sourceState == AL_PLAYING )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
void CStream::Pause()
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
ALint sourceState = AL_PAUSED;
|
|
|
|
alGetSourcei(m_alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
if (sourceState != AL_PAUSED )
|
|
|
|
alSourcePause(m_alSource);
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
void CStream::SetPause(bool bPause)
|
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
if ( !HasSource() ) return;
|
|
|
|
if ( bPause )
|
|
|
|
{
|
|
|
|
Pause();
|
|
|
|
m_bPaused = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_bPaused)
|
|
|
|
SetPlay(true);
|
|
|
|
m_bPaused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::SetPitch(float pitch)
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
alSourcef(m_alSource, AL_PITCH, pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::SetGain(float gain)
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
alSourcef(m_alSource, AL_GAIN, gain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::SetPosition(float x, float y, float z)
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
alSource3f(m_alSource, AL_POSITION, x, y, z);
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::SetVolume(uint32 nVol)
|
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
m_nVolume = nVol;
|
|
|
|
SetGain(ALfloat(nVol) / MAX_VOLUME);
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::SetPan(uint8 nPan)
|
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
m_nPan = nPan;
|
|
|
|
SetPosition((nPan - 63)/64.0f, 0.0f, Sqrt(1.0f-SQR((nPan-63)/64.0f)));
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
void CStream::SetPosMS(uint32 nPos)
|
2020-05-04 17:33:48 +00:00
|
|
|
{
|
2020-10-18 16:16:37 +00:00
|
|
|
if ( !IsOpened() ) return;
|
2020-05-07 06:26:16 +00:00
|
|
|
m_pSoundFile->Seek(nPos);
|
|
|
|
ClearBuffers();
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
uint32 CStream::GetPosMS()
|
2020-05-04 17:33:48 +00:00
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
if ( !HasSource() ) return 0;
|
2020-10-18 16:16:37 +00:00
|
|
|
if ( !IsOpened() ) return 0;
|
2020-05-07 06:26:16 +00:00
|
|
|
|
|
|
|
ALint offset;
|
|
|
|
//alGetSourcei(m_alSource, AL_SAMPLE_OFFSET, &offset);
|
|
|
|
alGetSourcei(m_alSource, AL_BYTE_OFFSET, &offset);
|
|
|
|
|
|
|
|
return m_pSoundFile->Tell()
|
2020-10-05 10:59:40 +00:00
|
|
|
- m_pSoundFile->samples2ms(m_pSoundFile->GetBufferSamples() * (NUM_STREAMBUFFERS-1)) / m_pSoundFile->GetChannels()
|
|
|
|
+ m_pSoundFile->samples2ms(offset/m_pSoundFile->GetSampleSize()) / m_pSoundFile->GetChannels();
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
uint32 CStream::GetLengthMS()
|
2020-05-04 17:33:48 +00:00
|
|
|
{
|
2020-10-18 16:16:37 +00:00
|
|
|
if ( !IsOpened() ) return 0;
|
2020-05-07 06:26:16 +00:00
|
|
|
return m_pSoundFile->GetLength();
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
bool CStream::FillBuffer(ALuint alBuffer)
|
2020-05-04 17:33:48 +00:00
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
if ( !HasSource() )
|
2020-05-04 17:33:48 +00:00
|
|
|
return false;
|
2020-10-18 16:16:37 +00:00
|
|
|
if ( !IsOpened() )
|
2020-05-07 06:26:16 +00:00
|
|
|
return false;
|
|
|
|
if ( !(alBuffer != AL_NONE && alIsBuffer(alBuffer)) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32 size = m_pSoundFile->Decode(m_pBuffer);
|
|
|
|
if( size == 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
alBufferData(alBuffer, m_pSoundFile->GetChannels() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16,
|
|
|
|
m_pBuffer, size, m_pSoundFile->GetSampleRate());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 CStream::FillBuffers()
|
|
|
|
{
|
|
|
|
int32 i = 0;
|
|
|
|
for ( i = 0; i < NUM_STREAMBUFFERS; i++ )
|
|
|
|
{
|
|
|
|
if ( !FillBuffer(m_alBuffers[i]) )
|
|
|
|
break;
|
|
|
|
alSourceQueueBuffers(m_alSource, 1, &m_alBuffers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::ClearBuffers()
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
|
|
|
|
ALint buffersQueued;
|
|
|
|
alGetSourcei(m_alSource, AL_BUFFERS_QUEUED, &buffersQueued);
|
|
|
|
|
|
|
|
ALuint value;
|
|
|
|
while (buffersQueued--)
|
|
|
|
alSourceUnqueueBuffers(m_alSource, 1, &value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CStream::Setup()
|
|
|
|
{
|
2020-10-18 16:16:37 +00:00
|
|
|
if ( IsOpened() )
|
2020-05-07 06:26:16 +00:00
|
|
|
{
|
|
|
|
m_pSoundFile->Seek(0);
|
|
|
|
alSourcei(m_alSource, AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
//SetPosition(0.0f, 0.0f, 0.0f);
|
|
|
|
SetPitch(1.0f);
|
|
|
|
//SetPan(m_nPan);
|
|
|
|
//SetVolume(100);
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
return IsOpened();
|
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
void CStream::SetPlay(bool state)
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
if ( state )
|
|
|
|
{
|
|
|
|
ALint sourceState = AL_PLAYING;
|
|
|
|
alGetSourcei(m_alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
if (sourceState != AL_PLAYING )
|
|
|
|
alSourcePlay(m_alSource);
|
|
|
|
m_bActive = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ALint sourceState = AL_STOPPED;
|
|
|
|
alGetSourcei(m_alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
if (sourceState != AL_STOPPED )
|
|
|
|
alSourceStop(m_alSource);
|
|
|
|
m_bActive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:33:48 +00:00
|
|
|
void CStream::Start()
|
|
|
|
{
|
2020-05-07 06:26:16 +00:00
|
|
|
if ( !HasSource() ) return;
|
|
|
|
if ( FillBuffers() != 0 )
|
|
|
|
SetPlay(true);
|
|
|
|
}
|
2020-05-04 17:33:48 +00:00
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
void CStream::Stop()
|
|
|
|
{
|
|
|
|
if ( !HasSource() ) return;
|
|
|
|
SetPlay(false);
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::Update()
|
|
|
|
{
|
|
|
|
if ( !IsOpened() )
|
|
|
|
return;
|
2020-05-07 06:26:16 +00:00
|
|
|
|
|
|
|
if ( !HasSource() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( m_bReset )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( !m_bPaused )
|
|
|
|
{
|
|
|
|
ALint sourceState;
|
|
|
|
ALint buffersProcessed = 0;
|
|
|
|
|
|
|
|
alGetSourcei(m_alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
alGetSourcei(m_alSource, AL_BUFFERS_PROCESSED, &buffersProcessed);
|
|
|
|
|
|
|
|
ALint looping = AL_FALSE;
|
|
|
|
alGetSourcei(m_alSource, AL_LOOPING, &looping);
|
|
|
|
|
|
|
|
if ( looping == AL_TRUE )
|
|
|
|
{
|
|
|
|
TRACE("stream set looping");
|
|
|
|
alSourcei(m_alSource, AL_LOOPING, AL_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while( buffersProcessed-- )
|
|
|
|
{
|
|
|
|
ALuint buffer;
|
|
|
|
|
|
|
|
alSourceUnqueueBuffers(m_alSource, 1, &buffer);
|
|
|
|
|
|
|
|
if ( m_bActive && FillBuffer(buffer) )
|
|
|
|
alSourceQueueBuffers(m_alSource, 1, &buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sourceState != AL_PLAYING )
|
|
|
|
{
|
|
|
|
alGetSourcei(m_alSource, AL_BUFFERS_PROCESSED, &buffersProcessed);
|
|
|
|
SetPlay(buffersProcessed!=0);
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 06:26:16 +00:00
|
|
|
void CStream::ProviderInit()
|
|
|
|
{
|
|
|
|
if ( m_bReset )
|
|
|
|
{
|
|
|
|
if ( Setup() )
|
|
|
|
{
|
|
|
|
SetPan(m_nPan);
|
|
|
|
SetVolume(m_nVolume);
|
|
|
|
SetPosMS(m_nPosBeforeReset);
|
|
|
|
if (m_bActive)
|
|
|
|
FillBuffers();
|
|
|
|
SetPlay(m_bActive);
|
|
|
|
if ( m_bPaused )
|
|
|
|
Pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bReset = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStream::ProviderTerm()
|
|
|
|
{
|
|
|
|
m_bReset = true;
|
|
|
|
m_nPosBeforeReset = GetPosMS();
|
|
|
|
|
|
|
|
ClearBuffers();
|
|
|
|
}
|
|
|
|
|
2020-05-11 02:55:57 +00:00
|
|
|
#endif
|