fix for ogg clicks and segfaults from JF

git-svn-id: https://svn.eduke32.com/eduke32@1523 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2009-10-03 14:34:05 +00:00
parent b9579e9db0
commit 1a587a73a5
3 changed files with 107 additions and 195 deletions

View file

@ -278,12 +278,10 @@ static void MV_Mix
uint32_t rate;
uint32_t FixedPointBufferSize;
// cheap fix for a crash under 64-bit linux --\
// v
if ((voice->length == 0) && ((voice->GetSound == NULL) || (voice->GetSound(voice) != KeepPlaying)))
{
/* cheap fix for a crash under 64-bit linux --\
v */
if (voice->length == 0 && (!voice->GetSound || voice->GetSound(voice) != KeepPlaying))
return;
}
length = MixBufferSize;
FixedPointBufferSize = voice->FixedPointBufferSize;
@ -341,7 +339,7 @@ static void MV_Mix
return;
}
if (length > 0)
if (length > (voice->channels - 1))
{
// Get the position of the last sample in the buffer
FixedPointBufferSize = voice->RateScale * (length - voice->channels);

View file

@ -29,65 +29,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**********************************************************************/
#include <stdlib.h>
//#include <math.h>
#include <math.h>
#include "pitch.h"
#define MAXDETUNE 25
#define MAXDETUNE 100
static uint32_t PitchTable[ 12 ][ MAXDETUNE ] =
{
{ 0x10000, 0x10097, 0x1012f, 0x101c7, 0x10260, 0x102f9, 0x10392, 0x1042c,
0x104c6, 0x10561, 0x105fb, 0x10696, 0x10732, 0x107ce, 0x1086a, 0x10907,
0x109a4, 0x10a41, 0x10adf, 0x10b7d, 0x10c1b, 0x10cba, 0x10d59, 0x10df8,
0x10e98 },
{ 0x10f38, 0x10fd9, 0x1107a, 0x1111b, 0x111bd, 0x1125f, 0x11302, 0x113a5,
0x11448, 0x114eb, 0x1158f, 0x11634, 0x116d8, 0x1177e, 0x11823, 0x118c9,
0x1196f, 0x11a16, 0x11abd, 0x11b64, 0x11c0c, 0x11cb4, 0x11d5d, 0x11e06,
0x11eaf },
{ 0x11f59, 0x12003, 0x120ae, 0x12159, 0x12204, 0x122b0, 0x1235c, 0x12409,
0x124b6, 0x12563, 0x12611, 0x126bf, 0x1276d, 0x1281c, 0x128cc, 0x1297b,
0x12a2b, 0x12adc, 0x12b8d, 0x12c3e, 0x12cf0, 0x12da2, 0x12e55, 0x12f08,
0x12fbc },
{ 0x1306f, 0x13124, 0x131d8, 0x1328d, 0x13343, 0x133f9, 0x134af, 0x13566,
0x1361d, 0x136d5, 0x1378d, 0x13846, 0x138fe, 0x139b8, 0x13a72, 0x13b2c,
0x13be6, 0x13ca1, 0x13d5d, 0x13e19, 0x13ed5, 0x13f92, 0x1404f, 0x1410d,
0x141cb },
{ 0x1428a, 0x14349, 0x14408, 0x144c8, 0x14588, 0x14649, 0x1470a, 0x147cc,
0x1488e, 0x14951, 0x14a14, 0x14ad7, 0x14b9b, 0x14c5f, 0x14d24, 0x14dea,
0x14eaf, 0x14f75, 0x1503c, 0x15103, 0x151cb, 0x15293, 0x1535b, 0x15424,
0x154ee },
{ 0x155b8, 0x15682, 0x1574d, 0x15818, 0x158e4, 0x159b0, 0x15a7d, 0x15b4a,
0x15c18, 0x15ce6, 0x15db4, 0x15e83, 0x15f53, 0x16023, 0x160f4, 0x161c5,
0x16296, 0x16368, 0x1643a, 0x1650d, 0x165e1, 0x166b5, 0x16789, 0x1685e,
0x16934 },
{ 0x16a09, 0x16ae0, 0x16bb7, 0x16c8e, 0x16d66, 0x16e3e, 0x16f17, 0x16ff1,
0x170ca, 0x171a5, 0x17280, 0x1735b, 0x17437, 0x17513, 0x175f0, 0x176ce,
0x177ac, 0x1788a, 0x17969, 0x17a49, 0x17b29, 0x17c09, 0x17cea, 0x17dcc,
0x17eae },
{ 0x17f91, 0x18074, 0x18157, 0x1823c, 0x18320, 0x18406, 0x184eb, 0x185d2,
0x186b8, 0x187a0, 0x18888, 0x18970, 0x18a59, 0x18b43, 0x18c2d, 0x18d17,
0x18e02, 0x18eee, 0x18fda, 0x190c7, 0x191b5, 0x192a2, 0x19391, 0x19480,
0x1956f },
{ 0x1965f, 0x19750, 0x19841, 0x19933, 0x19a25, 0x19b18, 0x19c0c, 0x19d00,
0x19df4, 0x19ee9, 0x19fdf, 0x1a0d5, 0x1a1cc, 0x1a2c4, 0x1a3bc, 0x1a4b4,
0x1a5ad, 0x1a6a7, 0x1a7a1, 0x1a89c, 0x1a998, 0x1aa94, 0x1ab90, 0x1ac8d,
0x1ad8b },
{ 0x1ae89, 0x1af88, 0x1b088, 0x1b188, 0x1b289, 0x1b38a, 0x1b48c, 0x1b58f,
0x1b692, 0x1b795, 0x1b89a, 0x1b99f, 0x1baa4, 0x1bbaa, 0x1bcb1, 0x1bdb8,
0x1bec0, 0x1bfc9, 0x1c0d2, 0x1c1dc, 0x1c2e6, 0x1c3f1, 0x1c4fd, 0x1c609,
0x1c716 },
{ 0x1c823, 0x1c931, 0x1ca40, 0x1cb50, 0x1cc60, 0x1cd70, 0x1ce81, 0x1cf93,
0x1d0a6, 0x1d1b9, 0x1d2cd, 0x1d3e1, 0x1d4f6, 0x1d60c, 0x1d722, 0x1d839,
0x1d951, 0x1da69, 0x1db82, 0x1dc9c, 0x1ddb6, 0x1ded1, 0x1dfec, 0x1e109,
0x1e225 },
{ 0x1e343, 0x1e461, 0x1e580, 0x1e6a0, 0x1e7c0, 0x1e8e0, 0x1ea02, 0x1eb24,
0x1ec47, 0x1ed6b, 0x1ee8f, 0x1efb4, 0x1f0d9, 0x1f1ff, 0x1f326, 0x1f44e,
0x1f576, 0x1f69f, 0x1f7c9, 0x1f8f3, 0x1fa1e, 0x1fb4a, 0x1fc76, 0x1fda3,
0x1fed1 }
};
//static int32_t PITCH_Installed = FALSE;
static uint32_t PitchTable[ 12 ][ MAXDETUNE ];
static int32_t PITCH_Installed = 0;
/*---------------------------------------------------------------------
@ -95,31 +43,27 @@ static uint32_t PitchTable[ 12 ][ MAXDETUNE ] =
Initializes pitch table.
---------------------------------------------------------------------*/
/*
void PITCH_Init
(
void
)
{
int32_t note;
int32_t detune;
if ( !PITCH_Installed )
{
for( note = 0; note < 12; note++ )
{
for( detune = 0; detune < MAXDETUNE; detune++ )
void PITCH_Init(void)
{
int32_t note;
int32_t detune;
if (!PITCH_Installed)
{
for (note = 0; note < 12; note++)
{
for (detune = 0; detune < MAXDETUNE; detune++)
{
PitchTable[ note ][ detune ] = 0x10000 *
pow( 2, ( note * MAXDETUNE + detune ) / ( 12.0 * MAXDETUNE ) );
PitchTable[ note ][ detune ] = 0x10000 *
pow(2, (note * MAXDETUNE + detune) / (12.0 * MAXDETUNE));
}
}
}
PITCH_Installed = TRUE;
}
}
*/
PITCH_Installed = 1;
}
}
/*---------------------------------------------------------------------
@ -128,60 +72,41 @@ void PITCH_Init
Returns a fixed-point value to scale number the specified amount.
---------------------------------------------------------------------*/
uint32_t PITCH_GetScale
(
int32_t pitchoffset
)
uint32_t PITCH_GetScale(int32_t pitchoffset)
{
uint32_t scale;
int32_t octaveshift;
int32_t noteshift;
int32_t note;
int32_t detune;
{
uint32_t scale;
int32_t octaveshift;
int32_t noteshift;
int32_t note;
int32_t detune;
if ( !PITCH_Installed )
PITCH_Init();
// if ( !PITCH_Installed )
// {
// PITCH_Init();
// }
if (pitchoffset == 0)
return(PitchTable[ 0 ][ 0 ]);
if ( pitchoffset == 0 )
{
return( PitchTable[ 0 ][ 0 ] );
}
noteshift = pitchoffset % 1200;
if (noteshift < 0)
noteshift += 1200;
noteshift = pitchoffset % 1200;
if ( noteshift < 0 )
{
noteshift += 1200;
}
note = noteshift / 100;
detune = (noteshift % 100) / (100 / MAXDETUNE);
octaveshift = (pitchoffset - noteshift) / 1200;
note = noteshift / 100;
detune = ( noteshift % 100 ) / ( 100 / MAXDETUNE );
octaveshift = ( pitchoffset - noteshift ) / 1200;
if (detune < 0)
{
detune += (100 / MAXDETUNE);
note--;
if (note < 0)
{
note += 12;
octaveshift--;
}
}
if ( detune < 0 )
{
detune += ( 100 / MAXDETUNE );
note--;
if ( note < 0 )
{
note += 12;
octaveshift--;
}
}
scale = PitchTable[ note ][ detune ];
scale = PitchTable[ note ][ detune ];
if ( octaveshift < 0 )
{
scale >>= -octaveshift;
}
else
{
scale <<= octaveshift;
}
return( scale );
}
return (octaveshift < 0) ? (scale >> -octaveshift) : (scale <<= octaveshift);
}

View file

@ -22,6 +22,8 @@
* OggVorbis source support for MultiVoc
*/
#ifdef HAVE_VORBIS
#ifdef __APPLE__
# include <vorbis/vorbisfile.h>
#else
@ -31,18 +33,22 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// #include <unistd.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <errno.h>
#include "pitch.h"
#include "multivoc.h"
#include "_multivc.h"
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
#define BLOCKSIZE 0x8000
typedef struct {
void * ptr;
@ -51,11 +57,11 @@ typedef struct {
OggVorbis_File vf;
char block[0x8000];
int32_t blockused;
char block[BLOCKSIZE];
int32_t lastbitstream;
} vorbis_data;
static size_t read_vorbis(void * ptr, size_t size, size_t nmemb, void * datasource)
{
vorbis_data * vorb = (vorbis_data *) datasource;
@ -74,9 +80,9 @@ static size_t read_vorbis(void * ptr, size_t size, size_t nmemb, void * datasour
bytes = size;
}
memcpy(ptr, (uint8_t *)vorb->ptr + vorb->pos, bytes);
vorb->pos += bytes;
ptr = (uint8_t *)ptr + bytes;
memcpy(ptr, (uint8_t *)vorb->ptr + vorb->pos, bytes);
vorb->pos += bytes;
ptr = (uint8_t *)ptr + bytes;
if (vorb->length == vorb->pos) {
nread++;
@ -87,6 +93,7 @@ static size_t read_vorbis(void * ptr, size_t size, size_t nmemb, void * datasour
return nread;
}
static int32_t seek_vorbis(void * datasource, ogg_int64_t offset, int32_t whence)
{
vorbis_data * vorb = (vorbis_data *) datasource;
@ -98,9 +105,8 @@ static int32_t seek_vorbis(void * datasource, ogg_int64_t offset, int32_t whence
}
vorb->pos += offset;
if ((int32_t)vorb->pos < 0) {
vorb->pos = 0;
} else if (vorb->pos > vorb->length) {
if (vorb->pos > vorb->length) {
vorb->pos = vorb->length;
}
@ -133,7 +139,7 @@ Function: MV_GetNextVorbisBlock
Controls playback of OggVorbis data
---------------------------------------------------------------------*/
playbackstatus MV_GetNextVorbisBlock
static playbackstatus MV_GetNextVorbisBlock
(
VoiceNode *voice
)
@ -145,41 +151,29 @@ playbackstatus MV_GetNextVorbisBlock
voice->Playing = TRUE;
if ( voice->BlockLength > 0 )
{
voice->position -= voice->length;
voice->sound += voice->length >> 16;
voice->length = min( voice->BlockLength, 0x8000 );
voice->BlockLength -= voice->length;
voice->length <<= 16;
return( KeepPlaying );
}
bytesread = 0;
do {
bytes = ov_read(&vd->vf, vd->block + bytesread, sizeof(vd->block) - bytesread, 0, 2, 1, &bitstream);
//fprintf(stderr, "ov_read = %d\n", bytes);
if (bytes == OV_HOLE) continue;
if (bytes == 0) {
if (voice->LoopStart) {
err = ov_pcm_seek_page(&vd->vf, 0);
if (err != 0) {
fprintf(stderr, "MV_GetNextVorbisBlock ov_pcm_seek_page_lap: err %d\n", err);
} else {
continue;
}
} else {
break;
}
} else if (bytes < 0) {
fprintf(stderr, "MV_GetNextVorbisBlock ov_read: err %d\n", bytes);
voice->Playing = FALSE;
return NoMoreData;
}
bytesread += bytes;
} while (bytesread < (signed)sizeof(vd->block));
bytes = ov_read(&vd->vf, vd->block + bytesread, BLOCKSIZE - bytesread, 0, 2, 1, &bitstream);
//fprintf(stderr, "ov_read = %d\n", bytes);
if (bytes > 0) { bytesread += bytes; continue; }
else if (bytes == OV_HOLE) continue;
else if (bytes == 0) {
if (voice->LoopStart) {
err = ov_pcm_seek_page(&vd->vf, 0);
if (err != 0) {
fprintf(stderr, "MV_GetNextVorbisBlock ov_pcm_seek_page_lap: err %d\n", err);
} else {
continue;
}
} else {
break;
}
} else if (bytes < 0) {
fprintf(stderr, "MV_GetNextVorbisBlock ov_read: err %d\n", bytes);
voice->Playing = FALSE;
return NoMoreData;
}
} while (bytesread < BLOCKSIZE);
if (bytesread == 0) {
voice->Playing = FALSE;
@ -190,12 +184,7 @@ playbackstatus MV_GetNextVorbisBlock
vorbis_info * vi = 0;
vi = ov_info(&vd->vf, -1);
if (!vi) {
voice->Playing = FALSE;
return NoMoreData;
}
if (vi->channels != 1 && vi->channels != 2) {
if (!vi || (vi->channels != 1 && vi->channels != 2)) {
voice->Playing = FALSE;
return NoMoreData;
}
@ -203,19 +192,17 @@ playbackstatus MV_GetNextVorbisBlock
voice->channels = vi->channels;
voice->SamplingRate = vi->rate;
voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) / MV_MixRate;
voice->FixedPointBufferSize = ( voice->RateScale * MixBufferSize ) - voice->RateScale;
MV_SetVoiceMixMode( voice );
vd->lastbitstream = bitstream;
}
vd->lastbitstream = bitstream;
vd->blockused = bytesread;
bytesread /= 2 * voice->channels;
voice->position = 0;
voice->sound = vd->block;
voice->BlockLength = bytesread;
voice->length = min( voice->BlockLength, 0x8000 );
voice->BlockLength -= voice->length;
voice->length <<= 16;
voice->BlockLength = 0;
voice->length = bytesread << 16;
return( KeepPlaying );
}
@ -346,7 +333,6 @@ int32_t MV_PlayLoopedVorbis
vd->ptr = ptr;
vd->pos = 0;
vd->length = ptrlength;
vd->blockused = 0;
vd->lastbitstream = -1;
status = ov_open_callbacks((void *) vd, &vd->vf, 0, 0, vorbis_callbacks);
@ -399,11 +385,13 @@ int32_t MV_PlayLoopedVorbis
voice->LoopStart = (char *) (loopstart >= 0 ? TRUE : FALSE);
voice->LoopEnd = 0;
voice->LoopSize = 0;
voice->Playing = TRUE;
voice->Paused = FALSE;
voice->Playing = TRUE;
voice->Paused = FALSE;
voice->SamplingRate = vi->rate;
voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) / MV_MixRate;
voice->FixedPointBufferSize = ( voice->RateScale * MixBufferSize ) -
voice->RateScale;
MV_SetVoiceMixMode( voice );
MV_SetVoiceVolume( voice, vol, left, right );
@ -427,3 +415,4 @@ void MV_ReleaseVorbisVoice( VoiceNode * voice )
voice->extra = 0;
}
#endif //HAVE_VORBIS