From 4209b88459bce25a7a0a80f28163838dc21595e8 Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Sun, 25 Jul 2021 12:02:35 +0200 Subject: [PATCH] Add back audio buffering --- src/audio/oal/stream.cpp | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/audio/oal/stream.cpp b/src/audio/oal/stream.cpp index 7e68f042..a4f45272 100644 --- a/src/audio/oal/stream.cpp +++ b/src/audio/oal/stream.cpp @@ -679,6 +679,21 @@ public: #endif #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 { @@ -689,13 +704,15 @@ protected: uint32 m_nChannels; const char* m_pPath; bool m_bFileNotOpenedYet; + char* m_buffer; CMP3File() : m_pMH(nil), m_bOpened(false), m_nRate(0), m_bFileNotOpenedYet(false), - m_nChannels(0) {} + m_nChannels(0), + m_buffer(NULL) {} public: CMP3File(const char *path) : m_pMH(nil), @@ -703,7 +720,8 @@ public: m_nRate(0), m_nChannels(0), m_pPath(path), - m_bFileNotOpenedYet(false) + m_bFileNotOpenedYet(false), + m_buffer(NULL) { m_pMH = mpg123_new(nil, nil); if ( m_pMH ) @@ -723,10 +741,16 @@ public: { 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; int channels = 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; m_nRate = rate; @@ -746,6 +770,8 @@ public: mpg123_close(m_pMH); mpg123_delete(m_pMH); + free(m_buffer); + m_pMH = nil; } } @@ -852,6 +878,9 @@ public: 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 && mpg123_open_handle(m_pMH, f) == MPG123_OK && mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK;