Add back audio buffering

This commit is contained in:
GaryOderNichts 2021-07-25 12:02:35 +02:00
parent f348ee2532
commit 4209b88459

View File

@ -679,6 +679,21 @@ public:
#endif #endif
#ifdef AUDIO_OAL_USE_MPG123 #ifdef AUDIO_OAL_USE_MPG123
static ssize_t mpg123_read_replacement(void* handle, void* data, size_t size)
{
return fread(data, 1, size, (FILE*)handle);
}
static off_t mpg123_seek_replacement(void* handle, off_t offset, int whence)
{
fseek((FILE*)handle, offset, whence);
return ftell((FILE*)handle);
}
static void mpg123_close_replacement(void* handle)
{
fclose((FILE*)handle);
}
class CMP3File : public IDecoder class CMP3File : public IDecoder
{ {
@ -689,13 +704,15 @@ protected:
uint32 m_nChannels; uint32 m_nChannels;
const char* m_pPath; const char* m_pPath;
bool m_bFileNotOpenedYet; bool m_bFileNotOpenedYet;
char* m_buffer;
CMP3File() : CMP3File() :
m_pMH(nil), m_pMH(nil),
m_bOpened(false), m_bOpened(false),
m_nRate(0), m_nRate(0),
m_bFileNotOpenedYet(false), m_bFileNotOpenedYet(false),
m_nChannels(0) {} m_nChannels(0),
m_buffer(NULL) {}
public: public:
CMP3File(const char *path) : CMP3File(const char *path) :
m_pMH(nil), m_pMH(nil),
@ -703,7 +720,8 @@ public:
m_nRate(0), m_nRate(0),
m_nChannels(0), m_nChannels(0),
m_pPath(path), m_pPath(path),
m_bFileNotOpenedYet(false) m_bFileNotOpenedYet(false),
m_buffer(NULL)
{ {
m_pMH = mpg123_new(nil, nil); m_pMH = mpg123_new(nil, nil);
if ( m_pMH ) if ( m_pMH )
@ -723,10 +741,16 @@ public:
{ {
if(!m_bFileNotOpenedYet) return; if(!m_bFileNotOpenedYet) return;
FILE* f = fopen(m_pPath, "rb");
m_buffer = (char*) memalign(0x40, IO_BUFFER_SIZE);
setvbuf(f, m_buffer, _IOFBF, IO_BUFFER_SIZE);
long rate = 0; long rate = 0;
int channels = 0; int channels = 0;
int encoding = 0; int encoding = 0;
m_bOpened = mpg123_open(m_pMH, m_pPath) == MPG123_OK m_bOpened = mpg123_replace_reader_handle(m_pMH, mpg123_read_replacement, mpg123_seek_replacement, mpg123_close_replacement) == MPG123_OK
&& mpg123_open_handle(m_pMH, f) == MPG123_OK
&& mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK; && mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK;
m_nRate = rate; m_nRate = rate;
@ -746,6 +770,8 @@ public:
mpg123_close(m_pMH); mpg123_close(m_pMH);
mpg123_delete(m_pMH); mpg123_delete(m_pMH);
free(m_buffer);
m_pMH = nil; m_pMH = nil;
} }
} }
@ -852,6 +878,9 @@ public:
FILE *f = fopen(m_pPath, "rb"); FILE *f = fopen(m_pPath, "rb");
m_buffer = (char*) memalign(0x40, IO_BUFFER_SIZE);
setvbuf(f, m_buffer, _IOFBF, IO_BUFFER_SIZE);
m_bOpened = f && mpg123_replace_reader_handle(m_pMH, r_read, r_seek, r_close) == MPG123_OK m_bOpened = f && mpg123_replace_reader_handle(m_pMH, r_read, r_seek, r_close) == MPG123_OK
&& mpg123_open_handle(m_pMH, f) == MPG123_OK && mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK; && mpg123_open_handle(m_pMH, f) == MPG123_OK && mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK;