2009-11-10 00:03:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* libwiigui
|
|
|
|
*
|
|
|
|
* Tantric 2009
|
|
|
|
*
|
|
|
|
* gui_sound_plugin_mpg.cpp
|
|
|
|
*
|
|
|
|
* by ardi 2009
|
|
|
|
*
|
|
|
|
* Decoder for MPEG-Audio Mpeg-1/-2 Layer I,II and III with libmad
|
|
|
|
*
|
|
|
|
* GUI class definitions
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <asndlib.h>
|
|
|
|
#include <mad.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include "gui_sound_decoder.h"
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
static inline s16 FixedToShort(mad_fixed_t Fixed)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Clipping */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (Fixed >= MAD_F_ONE) return (SHRT_MAX);
|
|
|
|
if (Fixed <= -MAD_F_ONE) return (-SHRT_MAX);
|
2009-11-10 00:03:13 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
Fixed = Fixed >> (MAD_F_FRACBITS - 15);
|
|
|
|
return ((s16) Fixed);
|
2009-11-10 00:03:13 +01:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
#define ADMA_BUFFERSIZE (8192)
|
|
|
|
#define DATABUFFER_SIZE (32768)
|
2009-11-10 00:03:13 +01:00
|
|
|
// http://www.fr-an.de/fragen/v06/02_01.htm
|
|
|
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
class GuiSoundDecoderMPG: public GuiSoundDecoder
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
protected:
|
2010-09-24 02:48:03 +02:00
|
|
|
GuiSoundDecoderMPG(const u8 * snd, u32 len, bool snd_is_allocated)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
sound = snd;
|
|
|
|
length = len;
|
|
|
|
is_allocated = snd_is_allocated;
|
2010-09-19 01:16:05 +02:00
|
|
|
// Init mad-structures
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_stream_init(&madStream);
|
|
|
|
mad_stream_buffer(&madStream, sound, length);
|
|
|
|
mad_frame_init(&madFrame);
|
|
|
|
mad_synth_init(&madSynth);
|
2010-09-19 01:16:05 +02:00
|
|
|
madSynthPcmPos = 0;
|
|
|
|
mad_timer_reset( &madTimer );
|
|
|
|
guardBuffer = NULL;
|
|
|
|
is_running = false;
|
2009-11-10 00:03:13 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// decode first Frame
|
2010-09-24 02:48:03 +02:00
|
|
|
if (DecodeFirstFrame())
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
mad_synth_finish( &madSynth );
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_frame_finish(&madFrame);
|
|
|
|
mad_stream_finish(&madStream);
|
|
|
|
throw("Stream Error");
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
~GuiSoundDecoderMPG()
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
while (is_running)
|
|
|
|
usleep(50);
|
2010-09-19 01:16:05 +02:00
|
|
|
mad_synth_finish( &madSynth );
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_frame_finish(&madFrame);
|
|
|
|
mad_stream_finish(&madStream);
|
|
|
|
delete[] guardBuffer;
|
|
|
|
if (is_allocated) delete[] sound;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
static GuiSoundDecoder *Create(const u8 * snd, u32 len, bool snd_is_allocated)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
struct mad_stream madStream;
|
2010-09-19 01:16:05 +02:00
|
|
|
struct mad_header madHeader;
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_stream_init(&madStream);
|
|
|
|
mad_stream_buffer(&madStream, snd, len);
|
|
|
|
mad_header_init(&madHeader);
|
|
|
|
s32 ret = mad_header_decode(&madHeader, &madStream);
|
|
|
|
if (ret == 0 || madStream.error == MAD_ERROR_LOSTSYNC) // LOSTSYNC in first call is ok
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
int i;
|
2010-09-24 02:48:03 +02:00
|
|
|
for (i = 0; i < 4 && mad_header_decode(&madHeader, &madStream) == 0; i++)
|
|
|
|
;
|
|
|
|
if (i == 4)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
mad_header_finish( &madHeader );
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_stream_finish(&madStream);
|
|
|
|
return new GuiSoundDecoderMPG(snd, len, snd_is_allocated);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
} mad_header_finish( &madHeader );
|
|
|
|
mad_stream_finish(&madStream);
|
2010-09-19 01:16:05 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s32 GetFormat()
|
|
|
|
{
|
|
|
|
return MAD_NCHANNELS( &madFrame.header ) == 2 ? VOICE_STEREO_16BIT : VOICE_MONO_16BIT;
|
|
|
|
}
|
|
|
|
s32 GetSampleRate()
|
|
|
|
{
|
|
|
|
return madFrame.header.samplerate;
|
|
|
|
}
|
|
|
|
/* Read reads data from stream to buffer
|
2010-09-24 02:48:03 +02:00
|
|
|
return: >0 = readed bytes;
|
|
|
|
0 = EOF;
|
|
|
|
<0 = Error;
|
|
|
|
*/
|
|
|
|
int Read(u8 * buffer, int buffer_size)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
is_running = true;
|
2010-09-24 02:48:03 +02:00
|
|
|
if (MAD_NCHANNELS( &madFrame.header ) == 2) // stereo
|
2010-09-19 01:16:05 +02:00
|
|
|
buffer_size &= ~0x0003; // make size to a kind of 4
|
2010-09-24 02:48:03 +02:00
|
|
|
else buffer_size &= ~0x0001; // make size to a kind of 2
|
2010-09-19 01:16:05 +02:00
|
|
|
u8 *write_pos = buffer;
|
|
|
|
u8 *write_end = buffer + buffer_size;
|
2009-11-10 00:03:13 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
for (;;)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
for (; madSynthPcmPos < madSynth.pcm.length; ++madSynthPcmPos)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (write_pos >= write_end)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
is_running = false;
|
|
|
|
return write_pos - buffer;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
*((s16*) write_pos) = FixedToShort(madSynth.pcm.samples[0][madSynthPcmPos]);
|
|
|
|
write_pos += 2;
|
|
|
|
if (MAD_NCHANNELS( &madFrame.header ) == 2) // stereo
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
*((s16*) write_pos) = FixedToShort(madSynth.pcm.samples[1][madSynthPcmPos]);
|
|
|
|
write_pos += 2;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
madStream.error = MAD_ERROR_NONE;
|
2010-09-24 02:48:03 +02:00
|
|
|
if (mad_frame_decode(&madFrame, &madStream))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (MAD_RECOVERABLE( madStream.error ))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (madStream.error != MAD_ERROR_LOSTSYNC || !guardBuffer) continue;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
else if (madStream.error == MAD_ERROR_BUFLEN)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!guardBuffer)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
u32 guardLen = (madStream.bufend - madStream.next_frame);
|
|
|
|
guardBuffer = new (std::nothrow) u8[guardLen + MAD_BUFFER_GUARD];
|
|
|
|
if (guardBuffer)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
memcpy(guardBuffer, madStream.next_frame, guardLen);
|
|
|
|
memset(guardBuffer + guardLen, 0, MAD_BUFFER_GUARD);
|
|
|
|
mad_stream_buffer(&madStream, guardBuffer, guardLen + MAD_BUFFER_GUARD);
|
2010-09-19 01:16:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_timer_add(&madTimer, madFrame.header.duration);
|
|
|
|
mad_synth_frame(&madSynth, &madFrame);
|
2010-09-19 01:16:05 +02:00
|
|
|
madSynthPcmPos = 0;
|
|
|
|
}
|
|
|
|
is_running = false;
|
|
|
|
return write_pos - buffer;
|
|
|
|
}
|
|
|
|
int Rewind()
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
while (is_running)
|
|
|
|
usleep(50);
|
|
|
|
delete[] guardBuffer;
|
|
|
|
guardBuffer = NULL;
|
|
|
|
mad_stream_buffer(&madStream, sound, length);
|
2010-09-19 01:16:05 +02:00
|
|
|
mad_synth_finish( &madSynth );
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_synth_init(&madSynth);
|
2010-09-19 01:16:05 +02:00
|
|
|
madSynthPcmPos = 0;
|
|
|
|
mad_timer_reset( &madTimer );
|
|
|
|
// decode first Frame
|
|
|
|
return DecodeFirstFrame();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
int DecodeFirstFrame()
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
for (;;)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
madStream.error = MAD_ERROR_NONE;
|
2010-09-24 02:48:03 +02:00
|
|
|
if (mad_frame_decode(&madFrame, &madStream))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (MAD_RECOVERABLE( madStream.error ))
|
2010-09-19 01:16:05 +02:00
|
|
|
continue;
|
2010-09-24 02:48:03 +02:00
|
|
|
else return -1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
mad_timer_add(&madTimer, madFrame.header.duration);
|
|
|
|
mad_synth_frame(&madSynth, &madFrame);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
const u8 *sound;
|
|
|
|
u32 length;
|
|
|
|
bool is_allocated;
|
|
|
|
struct mad_stream madStream;
|
|
|
|
struct mad_frame madFrame;
|
|
|
|
struct mad_synth madSynth;
|
|
|
|
u16 madSynthPcmPos;
|
|
|
|
mad_timer_t madTimer;
|
|
|
|
u8 *guardBuffer;
|
|
|
|
bool is_running;
|
2009-11-10 00:03:13 +01:00
|
|
|
};
|
2010-09-19 01:16:05 +02:00
|
|
|
REGISTER_GUI_SOUND_DECODER( GuiSoundDecoderMPG );
|