This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/client/sound/s_stream.c

286 lines
6.8 KiB
C
Raw Normal View History

2009-10-02 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2009 <20>
// s_stream.c - sound streaming
//=======================================================================
2010-11-20 22:00:00 +01:00
#include "common.h"
2009-10-02 22:00:00 +02:00
#include "sound.h"
2010-11-20 22:00:00 +01:00
#include "client.h"
2009-10-02 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
portable_samplepair_t s_rawsamples[MAX_RAW_SAMPLES];
static bg_track_t s_bgTrack;
2010-09-30 22:00:00 +02:00
int s_rawend;
2009-10-02 22:00:00 +02:00
2010-11-19 22:00:00 +01:00
void S_CheckLerpingState( void )
{
wavdata_t *info;
s_listener.lerping = false;
if( !s_bgTrack.stream ) return;
info = FS_StreamInfo( s_bgTrack.stream );
if( info && ((float)info->rate / SOUND_DMA_SPEED ) >= 1.0f )
s_listener.lerping = s_lerping->integer;
}
2009-10-02 22:00:00 +02:00
/*
=================
S_StartBackgroundTrack
=================
*/
2010-04-27 22:00:00 +02:00
void S_StartBackgroundTrack( const char *introTrack, const char *mainTrack )
2009-10-02 22:00:00 +02:00
{
S_StopBackgroundTrack();
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
2010-04-27 22:00:00 +02:00
if(( !introTrack || !*introTrack ) && ( !mainTrack || !*mainTrack ))
return;
2010-11-19 22:00:00 +01:00
if( !introTrack ) introTrack = mainTrack;
2010-04-27 22:00:00 +02:00
if( !*introTrack ) return;
2010-11-19 22:00:00 +01:00
if( !mainTrack || !*mainTrack ) s_bgTrack.loopName[0] = '\0';
else com.strncpy( s_bgTrack.loopName, mainTrack, sizeof( s_bgTrack.loopName ));
2010-04-27 22:00:00 +02:00
// open stream
2010-06-27 22:00:00 +02:00
s_bgTrack.stream = FS_OpenStream( va( "media/%s", introTrack ));
2010-11-18 22:00:00 +01:00
2010-11-19 22:00:00 +01:00
S_CheckLerpingState();
2009-10-02 22:00:00 +02:00
}
void S_StopBackgroundTrack( void )
{
2010-11-19 22:00:00 +01:00
s_listener.stream_paused = false;
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
2010-06-27 22:00:00 +02:00
if( !s_bgTrack.stream ) return;
2010-04-27 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
FS_CloseStream( s_bgTrack.stream );
Mem_Set( &s_bgTrack, 0, sizeof( bg_track_t ));
2010-11-18 22:00:00 +01:00
s_listener.lerping = false;
2010-06-27 22:00:00 +02:00
s_rawend = 0;
2010-04-27 22:00:00 +02:00
}
2010-11-19 22:00:00 +01:00
void S_StreamSetPause( int pause )
{
s_listener.stream_paused = pause;
}
2010-04-27 22:00:00 +02:00
/*
=================
S_StreamBackgroundTrack
=================
*/
void S_StreamBackgroundTrack( void )
{
int bufferSamples;
int fileSamples;
byte raw[MAX_RAW_SAMPLES];
2010-06-24 22:00:00 +02:00
int r, fileBytes;
2010-04-27 22:00:00 +02:00
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
2010-06-27 22:00:00 +02:00
if( !s_bgTrack.stream ) return;
2010-09-30 22:00:00 +02:00
if( s_listener.streaming ) return; // we are playing movie or somewhat
2010-04-27 22:00:00 +02:00
// don't bother playing anything if musicvolume is 0
2010-11-19 22:00:00 +01:00
if( !s_musicvolume->value || s_listener.paused || s_listener.stream_paused )
return;
2010-04-27 22:00:00 +02:00
// see how many samples should be copied into the raw buffer
2010-06-27 22:00:00 +02:00
if( s_rawend < soundtime )
s_rawend = soundtime;
2010-04-27 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
while( s_rawend < soundtime + MAX_RAW_SAMPLES )
2010-04-27 22:00:00 +02:00
{
2010-06-27 22:00:00 +02:00
wavdata_t *info = FS_StreamInfo( s_bgTrack.stream );
2010-04-27 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
bufferSamples = MAX_RAW_SAMPLES - (s_rawend - soundtime);
2010-04-27 22:00:00 +02:00
// decide how much data needs to be read from the file
2010-11-18 22:00:00 +01:00
fileSamples = bufferSamples * ((float)info->rate / SOUND_DMA_SPEED );
if( fileSamples <= 0 ) return; // no more samples need
2010-04-27 22:00:00 +02:00
// our max buffer size
fileBytes = fileSamples * ( info->width * info->channels );
if( fileBytes > sizeof( raw ))
{
fileBytes = sizeof( raw );
fileSamples = fileBytes / ( info->width * info->channels );
}
// read
2010-06-27 22:00:00 +02:00
r = FS_ReadStream( s_bgTrack.stream, fileBytes, raw );
2010-04-27 22:00:00 +02:00
if( r < fileBytes )
{
fileBytes = r;
fileSamples = r / ( info->width * info->channels );
}
if( r > 0 )
{
// add to raw buffer
S_StreamRawSamples( fileSamples, info->rate, info->width, info->channels, raw );
}
else
{
// loop
2010-06-27 22:00:00 +02:00
if( s_bgTrack.loopName[0] )
2010-04-27 22:00:00 +02:00
{
2010-06-27 22:00:00 +02:00
FS_CloseStream( s_bgTrack.stream );
2010-11-19 22:00:00 +01:00
s_bgTrack.stream = FS_OpenStream( va( "media/%s", s_bgTrack.loopName ));
2010-06-27 22:00:00 +02:00
if( !s_bgTrack.stream ) return;
2010-11-19 22:00:00 +01:00
S_CheckLerpingState();
2010-04-27 22:00:00 +02:00
}
else
{
S_StopBackgroundTrack();
return;
}
}
}
2009-10-02 22:00:00 +02:00
}
2010-04-27 22:00:00 +02:00
/*
=================
S_StartStreaming
=================
*/
2009-10-02 22:00:00 +02:00
void S_StartStreaming( void )
{
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
2010-09-30 22:00:00 +02:00
// begin streaming movie soundtrack
s_listener.streaming = true;
2010-11-18 22:00:00 +01:00
s_listener.lerping = false;
2009-10-02 22:00:00 +02:00
}
2010-04-27 22:00:00 +02:00
/*
=================
S_StopStreaming
=================
*/
2009-10-02 22:00:00 +02:00
void S_StopStreaming( void )
{
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
2010-09-30 22:00:00 +02:00
s_listener.streaming = false;
2010-11-18 22:00:00 +01:00
s_listener.lerping = false;
2010-09-30 22:00:00 +02:00
s_rawend = 0;
}
/*
=================
S_StreamSoundTrack
=================
*/
void S_StreamSoundTrack( void )
{
int bufferSamples;
int fileSamples;
byte raw[MAX_RAW_SAMPLES];
int r, fileBytes;
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
2010-09-30 22:00:00 +02:00
if( !s_listener.streaming || s_listener.paused ) return;
// see how many samples should be copied into the raw buffer
if( s_rawend < soundtime )
s_rawend = soundtime;
while( s_rawend < soundtime + MAX_RAW_SAMPLES )
{
2010-11-20 22:00:00 +01:00
wavdata_t *info = SCR_GetMovieInfo();
2010-09-30 22:00:00 +02:00
bufferSamples = MAX_RAW_SAMPLES - (s_rawend - soundtime);
// decide how much data needs to be read from the file
fileSamples = bufferSamples * info->rate / SOUND_DMA_SPEED;
// our max buffer size
fileBytes = fileSamples * ( info->width * info->channels );
if( fileBytes > sizeof( raw ))
{
fileBytes = sizeof( raw );
fileSamples = fileBytes / ( info->width * info->channels );
}
// read audio stream
2010-11-20 22:00:00 +01:00
r = SCR_GetAudioChunk( raw, fileBytes );
2010-09-30 22:00:00 +02:00
if( r < fileBytes )
{
fileBytes = r;
fileSamples = r / ( info->width * info->channels );
}
if( r > 0 )
{
// add to raw buffer
S_StreamRawSamples( fileSamples, info->rate, info->width, info->channels, raw );
}
else break; // no more samples for this frame
}
2009-10-02 22:00:00 +02:00
}
/*
============
S_StreamRawSamples
Cinematic streaming and voice over network
============
*/
void S_StreamRawSamples( int samples, int rate, int width, int channels, const byte *data )
{
2010-09-16 22:00:00 +02:00
int i, a, b, src, dst;
2010-09-10 22:00:00 +02:00
int fracstep, samplefrac;
int incount, outcount;
src = 0;
samplefrac = 0;
2010-09-16 22:00:00 +02:00
fracstep = (((double)rate) / (double)SOUND_DMA_SPEED) * 256.0;
outcount = (double)samples * (double)SOUND_DMA_SPEED / (double)rate;
2010-09-10 22:00:00 +02:00
incount = samples * channels;
#define TAKE_SAMPLE( s ) (sizeof(*in) == 1 ? (a = (in[src+(s)]-128)<<8,\
b = (src < incount - channels) ? (in[src+channels+(s)]-128)<<8 : 128) : \
(a = in[src+(s)],\
b = (src < incount - channels) ? (in[src+channels+(s)]) : 0))
2010-09-30 22:00:00 +02:00
// NOTE: disable lerping for cinematic sountracks
2010-11-18 22:00:00 +01:00
#define LERP_SAMPLE s_listener.lerping ? (((((b - a) * (samplefrac & 255)) >> 8) + a)) : a
2010-09-10 22:00:00 +02:00
#define RESAMPLE_RAW \
if( channels == 2 ) { \
for( i = 0; i < outcount; i++, samplefrac += fracstep, src = (samplefrac >> 8) << 1 ) { \
dst = s_rawend++ & (MAX_RAW_SAMPLES - 1); \
TAKE_SAMPLE(0); \
s_rawsamples[dst].left = LERP_SAMPLE; \
TAKE_SAMPLE(1); \
s_rawsamples[dst].right = LERP_SAMPLE; \
} \
} else { \
for( i = 0; i < outcount; i++, samplefrac += fracstep, src = (samplefrac >> 8) << 0 ) { \
dst = s_rawend++ & (MAX_RAW_SAMPLES - 1); \
TAKE_SAMPLE(0); \
s_rawsamples[dst].left = LERP_SAMPLE; \
s_rawsamples[dst].right = s_rawsamples[dst].left; \
} \
}
2010-06-27 22:00:00 +02:00
if( s_rawend < paintedtime )
s_rawend = paintedtime;
2009-10-02 22:00:00 +02:00
2010-09-10 22:00:00 +02:00
if( width == 2 )
2009-10-02 22:00:00 +02:00
{
2010-09-10 22:00:00 +02:00
short *in = (short *)data;
RESAMPLE_RAW
2009-10-02 22:00:00 +02:00
}
2010-09-10 22:00:00 +02:00
else
2009-10-02 22:00:00 +02:00
{
2010-09-10 22:00:00 +02:00
byte *in = (unsigned char *)data;
RESAMPLE_RAW
2009-10-02 22:00:00 +02:00
}
}