usbloadergx/source/libwiigui/gui_sound_decoder_aiff.cpp

159 lines
4.9 KiB
C++
Raw Normal View History

/****************************************************************************
* libwiigui
*
* ardi 2009
*
* gui_sound_plugin_aif.cpp
*
* GUI class definitions
***************************************************************************/
#include <limits.h>
#include <asndlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include "gui_sound_decoder.h"
// ------
// Copyright (C) 1988-1991 Apple Computer, Inc.
#ifndef HUGE_VAL
# define HUGE_VAL HUGE
#endif
# define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
2010-09-24 02:48:03 +02:00
static double ConvertFromIeeeExtended(const u8* bytes)
{
double f;
int expon;
u32 hiMant, loMant;
2010-09-24 02:48:03 +02:00
expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
hiMant = ((unsigned long) (bytes[2] & 0xFF) << 24) | ((unsigned long) (bytes[3] & 0xFF) << 16)
| ((unsigned long) (bytes[4] & 0xFF) << 8) | ((unsigned long) (bytes[5] & 0xFF));
loMant = ((unsigned long) (bytes[6] & 0xFF) << 24) | ((unsigned long) (bytes[7] & 0xFF) << 16)
| ((unsigned long) (bytes[8] & 0xFF) << 8) | ((unsigned long) (bytes[9] & 0xFF));
2010-09-24 02:48:03 +02:00
if (expon == 0 && hiMant == 0 && loMant == 0)
f = 0;
else
{
2010-09-24 02:48:03 +02:00
if (expon == 0x7FFF)
f = HUGE_VAL;
else
{
expon -= 16383;
2010-09-24 02:48:03 +02:00
f = ldexp(UnsignedToFloat( hiMant ), expon -= 31);
f += ldexp(UnsignedToFloat( loMant ), expon -= 32);
}
}
2010-09-24 02:48:03 +02:00
if (bytes[0] & 0x80)
return -f;
2010-09-24 02:48:03 +02:00
else return f;
}
// ------
2010-09-24 02:48:03 +02:00
class GuiSoundDecoderAIFF: public GuiSoundDecoder
{
protected:
2010-09-24 02:48:03 +02:00
GuiSoundDecoderAIFF(const u8 * snd, u32 len, bool snd_is_allocated)
{
2010-09-24 02:48:03 +02:00
sound = snd;
length = len;
is_allocated = snd_is_allocated;
is_running = false;
const u8 *in_ptr = sound;
2010-09-24 02:48:03 +02:00
if (be32inc( in_ptr ) != 0x464F524D /*'FORM'*/) throw("No FORM chunk");
if (be32inc( in_ptr ) + 8 != len) throw("wrong Size");
if (be32inc( in_ptr ) != 0x41494646 /*'AIFF'*/) throw("No AIFF chunk");
2010-09-24 02:48:03 +02:00
while (in_ptr + 8 < sound + len)
{
2010-09-24 02:48:03 +02:00
u32 chunk_id = be32inc( in_ptr );
u32 chunk_size = be32inc( in_ptr );
const u8 *chunk_start = in_ptr;
switch (chunk_id)
{
case 0x434F4D4D /*'COMM'*/:
channelCount = be16inc( in_ptr );
2010-09-24 02:48:03 +02:00
in_ptr += 4; // skip numSampleFrames
bytePerSample = (be16inc( in_ptr ) + 7) / 8;
if (bytePerSample < 1 && bytePerSample > 2) throw("wrong bits per Sample");
sampleRate = ConvertFromIeeeExtended(in_ptr);
break;
case 0x53534E44 /*'SSND'*/:
2010-09-24 02:48:03 +02:00
pcm_start = in_ptr + 8;
pcm_end = chunk_start + chunk_size;
break;
}
in_ptr = chunk_start + chunk_size;
}
currentPos = pcm_start;
}
public:
~GuiSoundDecoderAIFF()
{
2010-09-24 02:48:03 +02:00
while (is_running)
usleep(50);
if (is_allocated) delete[] sound;
}
2010-09-24 02:48:03 +02:00
static GuiSoundDecoder *Create(const u8 * snd, u32 len, bool snd_is_allocated)
{
2010-09-24 02:48:03 +02:00
if (snd && len > 12 && snd[0] == 'F' && snd[1] == 'O' && snd[2] == 'R' && snd[3] == 'M' && snd[8] == 'A'
&& snd[9] == 'I' && snd[10] == 'F' && snd[11] == 'F') return new GuiSoundDecoderAIFF(snd, len,
snd_is_allocated);
return NULL;
}
s32 GetFormat()
{
2010-09-24 02:48:03 +02:00
if (bytePerSample == 2)
return channelCount == 2 ? VOICE_STEREO_16BIT : VOICE_MONO_16BIT;
2010-09-24 02:48:03 +02:00
else return channelCount == 2 ? VOICE_STEREO_8BIT : VOICE_MONO_8BIT;
}
s32 GetSampleRate()
{
return 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-24 02:48:03 +02:00
if (currentPos >= pcm_end) return 0; // EOF
is_running = true;
2010-09-24 02:48:03 +02:00
if (currentPos + buffer_size > pcm_end) buffer_size = pcm_end - currentPos;
memcpy(buffer, currentPos, buffer_size);
currentPos += buffer_size;
is_running = false;
return buffer_size;
}
int Rewind()
{
2010-09-24 02:48:03 +02:00
while (is_running)
usleep(50);
currentPos = pcm_start;
return 0;
}
private:
2010-09-24 02:48:03 +02:00
const u8 *sound;
u32 length;
bool is_allocated;
bool is_running;
2010-09-24 02:48:03 +02:00
u32 sampleRate;
u16 channelCount;
u16 bytePerSample;
const u8 *pcm_start;
const u8 *pcm_end;
const u8 *currentPos;
};
REGISTER_GUI_SOUND_DECODER( GuiSoundDecoderAIFF );