Index: apps/codecs.c
===================================================================
--- apps/codecs.c	(revision 29139)
+++ apps/codecs.c	(working copy)
@@ -123,7 +123,9 @@
     /* strings and memory */
     strcpy,
     strlen,
+    strrchr,
     strcmp,
+    strcasecmp,
     strcat,
     memset,
     memcpy,
Index: apps/codecs.h
===================================================================
--- apps/codecs.h	(revision 29139)
+++ apps/codecs.h	(working copy)
@@ -183,7 +183,9 @@
     /* strings and memory */
     char* (*strcpy)(char *dst, const char *src);
     size_t (*strlen)(const char *str);
+    char * (*strrchr)(const char *s, int c);
     int (*strcmp)(const char *, const char *);
+    int (*strcasecmp)(const char *, const char *);
     char *(*strcat)(char *s1, const char *s2);
     void* (*memset)(void *dst, int c, size_t length);
     void* (*memcpy)(void *out, const void *in, size_t n);
Index: apps/metadata/gbs.c
===================================================================
--- apps/metadata/gbs.c	(revision 0)
+++ apps/metadata/gbs.c	(revision 0)
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <inttypes.h>
+
+#include "system.h"
+#include "metadata.h"
+#include "metadata_common.h"
+#include "metadata_parsers.h"
+#include "rbunicode.h"
+
+bool get_gbs_metadata(int fd, struct mp3entry* id3)
+{
+    /* Use the trackname part of the id3 structure as a temporary buffer */
+    unsigned char* buf = (unsigned char *)id3->path;
+    int read_bytes;
+    char *p;
+
+    if ((lseek(fd, 0, SEEK_SET) < 0) 
+         || ((read_bytes = read(fd, buf, 112)) < 112))
+    {
+        return false;
+    }
+
+    id3->length = 120*1000;
+    id3->vbr = false;
+    id3->filesize = filesize(fd);
+  
+    if (memcmp(buf,"GBS",3) != 0) /* GBR not supported */
+    {
+        return false;
+    }
+
+    p = id3->id3v2buf;
+
+    /* Title */
+    memcpy(p, &buf[16], 32);
+    id3->title = p;
+    p += strlen(p)+1;
+
+    /* Artist */
+    memcpy(p, &buf[48], 32);
+    id3->artist = p;
+    p += strlen(p)+1;
+
+    /* Copyright (per codec) */
+    memcpy(p, &buf[80], 32);
+    id3->album = p;
+    p += strlen(p)+1;
+        
+    return true;
+}
Index: apps/metadata/metadata_parsers.h
===================================================================
--- apps/metadata/metadata_parsers.h	(revision 29139)
+++ apps/metadata/metadata_parsers.h	(working copy)
@@ -47,3 +47,4 @@
 bool get_vox_metadata(int fd, struct mp3entry* id3);
 bool get_wave64_metadata(int fd, struct mp3entry* id3);
 bool get_tta_metadata(int fd, struct mp3entry* id3);
+bool get_gbs_metadata(int fd, struct mp3entry* id3);
Index: apps/metadata.c
===================================================================
--- apps/metadata.c	(revision 29139)
+++ apps/metadata.c	(working copy)
@@ -203,6 +203,9 @@
     /* WMA Voice in ASF */
     [AFMT_WMAVOICE] =
         AFMT_ENTRY("WMAVoice","wmavoice",NULL,  NULL,               "wma\0wmv\0"),
+    /* GBS (Game Boy Sound Format) */
+    [AFMT_GBS] =
+        AFMT_ENTRY("GBS",   "gbs",  NULL,       get_gbs_metadata,   "gbs\0"),
 #endif
 };
 
Index: apps/metadata.h
===================================================================
--- apps/metadata.h	(revision 29139)
+++ apps/metadata.h	(working copy)
@@ -85,6 +85,7 @@
     AFMT_WAVE64,       /* Wave64 */
     AFMT_TTA,          /* True Audio */
     AFMT_WMAVOICE,     /* WMA Voice in ASF */
+    AFMT_GBS,          /* GBS (Game Boy Sound Format) */
 #endif
 
     /* add new formats at any index above this line to have a sensible order -
Index: apps/playback.c
===================================================================
--- apps/playback.c	(revision 29139)
+++ apps/playback.c	(working copy)
@@ -1395,6 +1395,7 @@
     case AFMT_NSF:
     case AFMT_SPC:
     case AFMT_SID:
+    case AFMT_GBS:
         logf("Loading atomic %d",track_id3->codectype);
         type = TYPE_ATOMIC_AUDIO;
         break;
Index: apps/SOURCES
===================================================================
--- apps/SOURCES	(revision 29139)
+++ apps/SOURCES	(working copy)
@@ -220,6 +220,7 @@
 metadata/au.c
 metadata/vox.c
 metadata/tta.c
+metadata/gbs.c
 #endif
 #ifdef HAVE_TAGCACHE
 tagcache.c
Index: apps/filetypes.c
===================================================================
--- apps/filetypes.c	(revision 29139)
+++ apps/filetypes.c	(working copy)
@@ -112,6 +112,7 @@
     { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
     { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
     { "tta", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
+    { "gbs", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
 #endif
     { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
     { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
Index: apps/codecs/gbs.c
===================================================================
--- apps/codecs/gbs.c	(revision 0)
+++ apps/codecs/gbs.c	(revision 0)
@@ -0,0 +1,117 @@
+
+/* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
+
+#include <codecs/lib/codeclib.h>
+#include "libgbs/gbs_emu.h" 
+
+CODEC_HEADER
+
+/* Maximum number of bytes to process in one iteration */
+#define CHUNK_SIZE (1024*2)
+
+static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
+static struct Gbs_Emu gbs_emu IDATA_ATTR CACHEALIGN_ATTR;
+
+/****************** rockbox interface ******************/
+
+static void set_codec_track(int t) {
+    Gbs_start_track(&gbs_emu, t); 
+
+    /* for REPEAT_ONE we disable track limits */
+    if (ci->global_settings->repeat_mode != REPEAT_ONE) {
+        Track_set_fade(&gbs_emu, Track_get_length( &gbs_emu, t ), 4000);
+    }
+    ci->id3->elapsed = t*1000; /* t is track no to display */
+}
+
+/* this is the codec entry point */
+enum codec_status codec_main(void)
+{
+    blargg_err_t err;
+    uint8_t *buf;
+    size_t n;
+    int track;
+
+    /* we only render 16 bits */
+    ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
+
+    /* 44 Khz, Interleaved stereo */
+    ci->configure(DSP_SET_FREQUENCY, 44100);
+    ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
+
+    Gbs_init(&gbs_emu);
+    Gbs_set_sample_rate(&gbs_emu, 44100);
+
+next_track:
+    track = 0;
+
+    DEBUGF("GBS: next_track\n");
+    if (codec_init()) {
+        return CODEC_ERROR;
+    }  
+
+    /* wait for track info to load */
+    while (!*ci->taginfo_ready && !ci->stop_codec)
+        ci->sleep(1);
+
+    codec_set_replaygain(ci->id3);
+        
+    /* Read the entire file */
+    DEBUGF("GBS: request file\n");
+    ci->seek_buffer(0);
+    buf = ci->request_buffer(&n, ci->filesize);
+    if (!buf || n < (size_t)ci->filesize) {
+        DEBUGF("GBS: file load failed\n");
+        return CODEC_ERROR;
+    }
+   
+    if ((err = Gbs_load(&gbs_emu, buf, ci->filesize))) {
+        DEBUGF("GBS: Gbs_load failed (%s)\n", err);
+        return CODEC_ERROR;
+    }
+
+    /* Load m3u playlist */
+    strcpy(ci->id3->id3v2buf, ci->id3->path);
+    char *p = strrchr(ci->id3->id3v2buf, '.');
+    if (p) {
+        strcpy(p, ".m3u");
+        Gbs_load_m3u(&gbs_emu, ci->id3->id3v2buf);
+    }
+
+    ci->id3->title = gbs_emu.header.game;
+    ci->id3->artist = gbs_emu.header.author;
+    ci->id3->album = gbs_emu.header.copyright;
+    ci->id3->length = gbs_emu.track_count*1000;
+
+next_tune:
+    set_codec_track(track);
+
+    /* The main decoder loop */
+    while (1) {
+        ci->yield();
+        if (ci->stop_codec || ci->new_track)
+            break;
+
+        if (ci->seek_time) {
+            track = ci->seek_time/1000;
+            ci->seek_complete();
+            if (track >= gbs_emu.track_count) break;
+            goto next_tune;
+        }
+
+        /* Generate audio buffer */
+        err = Gbs_play(&gbs_emu, CHUNK_SIZE, samples);
+        if (err || gbs_emu.track_ended) {
+            track++;
+            if (track >= gbs_emu.track_count) break;
+            goto next_tune;
+        }
+
+        ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
+    }
+    
+    if (ci->request_next_track())
+        goto next_track; /* when we fall through here we'll reload the file */
+
+    return CODEC_OK;
+}
Index: apps/codecs/libgbs/gb_cpu.c
===================================================================
--- apps/codecs/libgbs/gb_cpu.c	(revision 0)
+++ apps/codecs/libgbs/gb_cpu.c	(revision 0)
@@ -0,0 +1,1077 @@
+// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
+
+#include "gb_cpu.h"
+
+#include <string.h>
+
+//#include "gb_cpu_log.h"
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "gb_cpu_io.h"
+
+#include "blargg_source.h"
+
+struct state_t* state; // points to state_ or a local copy within run()
+struct state_t state_;
+
+// Base address for RST vectors (normally 0)
+gb_addr_t rst_base;
+
+void Cpu_init()
+{
+	 rst_base = 0;
+	 state = &state_;
+}
+
+// Common instructions:
+//
+// 365880   FA      LD  A,IND16
+// 355863   20      JR  NZ
+// 313655   21      LD  HL,IMM
+// 274580   28      JR  Z
+// 252878   FE      CMP IMM
+// 230541   7E      LD  A,(HL)
+// 226209   2A      LD A,(HL+)
+// 217467   CD      CALL
+// 212034   C9      RET
+// 208376   CB      CB prefix
+//
+//  27486   CB 7E   BIT 7,(HL)
+//  15925   CB 76   BIT 6,(HL)
+//  13035   CB 19   RR  C
+//  11557   CB 7F   BIT 7,A
+//  10898   CB 37   SWAP A
+//  10208   CB 66   BIT 4,(HL)
+
+#if defined(BLARGG_NONPORTABLE)
+	#define PAGE_OFFSET( addr ) (addr)
+#else
+	#define PAGE_OFFSET( addr ) ((addr) & (page_size - 1))
+#endif
+
+static inline void Cpu_set_code_page( int i, uint8_t* p )
+{
+	state->code_map [i] = p - PAGE_OFFSET( i * (blargg_long) page_size );
+}
+
+void Cpu_reset( void* unmapped )
+{
+	check( state == &state_ );
+	state = &state_;
+	
+	state_.remain = 0;
+	
+	int i;
+	for ( i = 0; i < page_count + 1; i++ )
+		Cpu_set_code_page( i, (uint8_t*) unmapped );
+	
+	//interrupts_enabled = false;
+	
+	blargg_verify_byte_order();
+}
+
+void Cpu_map_code( gb_addr_t start, unsigned size, void* data )
+{
+	// address range must begin and end on page boundaries
+	require( start % page_size == 0 );
+	require( size % page_size == 0 );
+	
+	unsigned first_page = start / page_size;
+	unsigned i;
+	for ( i = size / page_size; i--; )
+		Cpu_set_code_page( first_page + i, (uint8_t*) data + i * page_size );
+}
+
+#define READ( addr )            CPU_READ( this, (addr), s.remain )
+#define WRITE( addr, data )     {CPU_WRITE( this, (addr), (data), s.remain );}
+#define READ_FAST( addr, out )  CPU_READ_FAST( this, (addr), s.remain, out )
+#define READ_PROG( addr )       (s.code_map [(addr) >> page_shift] [PAGE_OFFSET( addr )])
+
+unsigned const z_flag = 0x80;
+unsigned const n_flag = 0x40;
+unsigned const h_flag = 0x20;
+unsigned const c_flag = 0x10;
+
+bool Cpu_run( struct Gbs_Emu* this, blargg_long cycle_count )
+{
+	state_.remain = (blargg_ulong) (cycle_count + clocks_per_instr) / clocks_per_instr;
+	struct state_t s;
+	state = &s;
+	memcpy( &s, &state_, sizeof s );
+	
+#if defined(BLARGG_BIG_ENDIAN)
+	#define R8( n ) (reg.r8_ [n]) 
+#elif defined(BLARGG_LITTLE_ENDIAN)
+	#define R8( n ) (reg.r8_ [(n) ^ 1]) 
+#else
+	#error "Byte order of CPU must be known"
+#endif
+	
+	union {
+		struct core_regs_t rg; // individual registers
+		
+		struct {
+			uint16_t bc, de, hl, unused; // pairs
+		} rp;
+		
+		uint8_t r8_ [8]; // indexed registers (use R8 macro due to endian dependence)
+		uint16_t r16 [4]; // indexed pairs
+	} reg;
+	BOOST_STATIC_ASSERT( sizeof reg.rg == 8 && sizeof reg.rp == 8 );
+	
+	struct registers_t* r = &this->r;
+	
+	reg.rg = r->rg;
+	unsigned pc = r->pc;
+	unsigned sp = r->sp;
+	unsigned flags = r->rg.flags;
+	
+loop:
+	
+	check( (unsigned long) pc < 0x10000 );
+	check( (unsigned long) sp < 0x10000 );
+	check( (flags & ~0xF0) == 0 );
+	
+	uint8_t const* instr = s.code_map [pc >> page_shift];
+	unsigned op;
+	
+	// TODO: eliminate this special case
+	#if defined(BLARGG_NONPORTABLE)
+		op = instr [pc];
+		pc++;
+		instr += pc;
+	#else
+		instr += PAGE_OFFSET( pc );
+		op = *instr++;
+		pc++;
+	#endif
+	
+#define GET_ADDR()  GET_LE16( instr )
+	
+	if ( !--s.remain )
+		goto stop;
+	
+	unsigned data;
+	data = *instr;
+	
+	#ifdef GB_CPU_LOG_H
+		gb_cpu_log( "new", pc - 1, op, data, instr [1] );
+	#endif
+	
+	switch ( op )
+	{
+
+// TODO: more efficient way to handle negative branch that wraps PC around
+#define BRANCH( cond )\
+{\
+	pc++;\
+	int offset = (int8_t) data;\
+	if ( !(cond) ) goto loop;\
+	pc = (uint16_t) (pc + offset);\
+	goto loop;\
+}
+
+// Most Common
+
+	case 0x20: // JR NZ
+		BRANCH( !(flags & z_flag) )
+	
+	case 0x21: // LD HL,IMM (common)
+		reg.rp.hl = GET_ADDR();
+		pc += 2;
+		goto loop;
+	
+	case 0x28: // JR Z
+		BRANCH( flags & z_flag )
+	
+	{
+		unsigned temp;
+	case 0xF0: // LD A,(0xFF00+imm)
+		temp = data | 0xFF00;
+		pc++;
+		goto ld_a_ind_comm;
+	
+	case 0xF2: // LD A,(0xFF00+C)
+		temp = reg.rg.c | 0xFF00;
+		goto ld_a_ind_comm;
+	
+	case 0x0A: // LD A,(BC)
+		temp = reg.rp.bc;
+		goto ld_a_ind_comm;
+	
+	case 0x3A: // LD A,(HL-)
+		temp = reg.rp.hl;
+		reg.rp.hl = temp - 1;
+		goto ld_a_ind_comm;
+	
+	case 0x1A: // LD A,(DE)
+		temp = reg.rp.de;
+		goto ld_a_ind_comm;
+	
+	case 0x2A: // LD A,(HL+) (common)
+		temp = reg.rp.hl;
+		reg.rp.hl = temp + 1;
+		goto ld_a_ind_comm;
+		
+	case 0xFA: // LD A,IND16 (common)
+		temp = GET_ADDR();
+		pc += 2;
+	ld_a_ind_comm:
+		READ_FAST( temp, reg.rg.a );
+		goto loop;
+	}
+	
+	case 0xBE: // CMP (HL)
+		data = READ( reg.rp.hl );
+		goto cmp_comm;
+	
+	case 0xB8: // CMP B
+	case 0xB9: // CMP C
+	case 0xBA: // CMP D
+	case 0xBB: // CMP E
+	case 0xBC: // CMP H
+	case 0xBD: // CMP L
+		data = R8( op & 7 );
+		goto cmp_comm;
+	
+	case 0xFE: // CMP IMM
+		pc++;
+	cmp_comm:
+		op = reg.rg.a;
+		data = op - data;
+	sub_set_flags:
+		flags = ((op & 15) - (data & 15)) & h_flag;
+		flags |= (data >> 4) & c_flag;
+		flags |= n_flag;
+		if ( data & 0xFF )
+			goto loop;
+		flags |= z_flag;
+		goto loop;
+
+	case 0x46: // LD B,(HL)
+	case 0x4E: // LD C,(HL)
+	case 0x56: // LD D,(HL)
+	case 0x5E: // LD E,(HL)
+	case 0x66: // LD H,(HL)
+	case 0x6E: // LD L,(HL)
+	case 0x7E:{// LD A,(HL)
+		unsigned addr = reg.rp.hl;
+		READ_FAST( addr, R8( (op >> 3) & 7 ) );
+		goto loop;
+	}
+	
+	case 0xC4: // CNZ (next-most-common)
+		pc += 2;
+		if ( flags & z_flag )
+			goto loop;
+	call:
+		pc -= 2;
+	case 0xCD: // CALL (most-common)
+		data = pc + 2;
+		pc = GET_ADDR();
+	push:
+		sp = (sp - 1) & 0xFFFF;
+		WRITE( sp, data >> 8 );
+		sp = (sp - 1) & 0xFFFF;
+		WRITE( sp, data & 0xFF );
+		goto loop;
+	
+	case 0xC8: // RNZ (next-most-common)
+		if ( !(flags & z_flag) )
+			goto loop;
+	case 0xC9: // RET (most common)
+	ret:
+		pc = READ( sp );
+		pc += 0x100 * READ( sp + 1 );
+		sp = (sp + 2) & 0xFFFF;
+		goto loop;
+	
+	case 0x00: // NOP
+	case 0x40: // LD B,B
+	case 0x49: // LD C,C
+	case 0x52: // LD D,D
+	case 0x5B: // LD E,E
+	case 0x64: // LD H,H
+	case 0x6D: // LD L,L
+	case 0x7F: // LD A,A
+		goto loop;
+	
+// CB Instructions
+
+	case 0xCB:
+		pc++;
+		// now data is the opcode
+		switch ( data ) {
+			
+		{
+			int temp;
+		case 0x46: // BIT b,(HL)
+		case 0x4E:
+		case 0x56:
+		case 0x5E:
+		case 0x66:
+		case 0x6E:
+		case 0x76:
+		case 0x7E:
+			{
+				unsigned addr = reg.rp.hl;
+				READ_FAST( addr, temp );
+				goto bit_comm;
+			}
+		
+		case 0x40: case 0x41: case 0x42: case 0x43: // BIT b,r
+		case 0x44: case 0x45: case 0x47: case 0x48:
+		case 0x49: case 0x4A: case 0x4B: case 0x4C:
+		case 0x4D: case 0x4F: case 0x50: case 0x51:
+		case 0x52: case 0x53: case 0x54: case 0x55:
+		case 0x57: case 0x58: case 0x59: case 0x5A:
+		case 0x5B: case 0x5C: case 0x5D: case 0x5F:
+		case 0x60: case 0x61: case 0x62: case 0x63:
+		case 0x64: case 0x65: case 0x67: case 0x68:
+		case 0x69: case 0x6A: case 0x6B: case 0x6C:
+		case 0x6D: case 0x6F: case 0x70: case 0x71:
+		case 0x72: case 0x73: case 0x74: case 0x75:
+		case 0x77: case 0x78: case 0x79: case 0x7A:
+		case 0x7B: case 0x7C: case 0x7D: case 0x7F:
+			temp = R8( data & 7 );
+		bit_comm:
+			{
+				int bit = (~data >> 3) & 7;
+				flags &= ~n_flag;
+				flags |= h_flag | z_flag;
+				flags ^= (temp << bit) & z_flag;
+				goto loop;
+			}
+		}
+		
+		case 0x86: // RES b,(HL)
+		case 0x8E:
+		case 0x96:
+		case 0x9E:
+		case 0xA6:
+		case 0xAE:
+		case 0xB6:
+		case 0xBE:
+		case 0xC6: // SET b,(HL)
+		case 0xCE:
+		case 0xD6:
+		case 0xDE:
+		case 0xE6:
+		case 0xEE:
+		case 0xF6:
+		case 0xFE: {
+			int temp = READ( reg.rp.hl );
+			int bit = 1 << ((data >> 3) & 7);
+			temp &= ~bit;
+			if ( !(data & 0x40) )
+				bit = 0;
+			WRITE( reg.rp.hl, temp | bit );
+			goto loop;
+		}
+		
+		case 0xC0: case 0xC1: case 0xC2: case 0xC3: // SET b,r
+		case 0xC4: case 0xC5: case 0xC7: case 0xC8:
+		case 0xC9: case 0xCA: case 0xCB: case 0xCC:
+		case 0xCD: case 0xCF: case 0xD0: case 0xD1:
+		case 0xD2: case 0xD3: case 0xD4: case 0xD5:
+		case 0xD7: case 0xD8: case 0xD9: case 0xDA:
+		case 0xDB: case 0xDC: case 0xDD: case 0xDF:
+		case 0xE0: case 0xE1: case 0xE2: case 0xE3:
+		case 0xE4: case 0xE5: case 0xE7: case 0xE8:
+		case 0xE9: case 0xEA: case 0xEB: case 0xEC:
+		case 0xED: case 0xEF: case 0xF0: case 0xF1:
+		case 0xF2: case 0xF3: case 0xF4: case 0xF5:
+		case 0xF7: case 0xF8: case 0xF9: case 0xFA:
+		case 0xFB: case 0xFC: case 0xFD: case 0xFF:
+			R8( data & 7 ) |= 1 << ((data >> 3) & 7);
+			goto loop;
+
+		case 0x80: case 0x81: case 0x82: case 0x83: // RES b,r
+		case 0x84: case 0x85: case 0x87: case 0x88:
+		case 0x89: case 0x8A: case 0x8B: case 0x8C:
+		case 0x8D: case 0x8F: case 0x90: case 0x91:
+		case 0x92: case 0x93: case 0x94: case 0x95:
+		case 0x97: case 0x98: case 0x99: case 0x9A:
+		case 0x9B: case 0x9C: case 0x9D: case 0x9F:
+		case 0xA0: case 0xA1: case 0xA2: case 0xA3:
+		case 0xA4: case 0xA5: case 0xA7: case 0xA8:
+		case 0xA9: case 0xAA: case 0xAB: case 0xAC:
+		case 0xAD: case 0xAF: case 0xB0: case 0xB1:
+		case 0xB2: case 0xB3: case 0xB4: case 0xB5:
+		case 0xB7: case 0xB8: case 0xB9: case 0xBA:
+		case 0xBB: case 0xBC: case 0xBD: case 0xBF:
+			R8( data & 7 ) &= ~(1 << ((data >> 3) & 7));
+			goto loop;
+		
+		{
+			int temp;
+		case 0x36: // SWAP (HL)
+			temp = READ( reg.rp.hl );
+			goto swap_comm;
+		
+		case 0x30: // SWAP B
+		case 0x31: // SWAP C
+		case 0x32: // SWAP D
+		case 0x33: // SWAP E
+		case 0x34: // SWAP H
+		case 0x35: // SWAP L
+		case 0x37: // SWAP A
+			temp = R8( data & 7 );
+		swap_comm:
+			op = (temp >> 4) | (temp << 4);
+			flags = 0;
+			goto shift_comm;
+		}
+		
+// Shift/Rotate
+
+		case 0x06: // RLC (HL)
+		case 0x16: // RL (HL)
+		case 0x26: // SLA (HL)
+			op = READ( reg.rp.hl );
+			goto rl_comm;
+		
+		case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x27: // SLA A
+		case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x07: // RLC A
+		case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x17: // RL A
+			op = R8( data & 7 );
+			goto rl_comm;
+		
+		case 0x3E: // SRL (HL)
+			data += 0x10; // bump up to 0x4n to avoid preserving sign bit
+		case 0x1E: // RR (HL)
+		case 0x0E: // RRC (HL)
+		case 0x2E: // SRA (HL)
+			op = READ( reg.rp.hl );
+			goto rr_comm;
+		
+		case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3F: // SRL A
+			data += 0x10; // bump up to 0x4n
+		case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1F: // RR A
+		case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0F: // RRC A
+		case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2F: // SRA A
+			op = R8( data & 7 );
+			goto rr_comm;
+		
+	} // CB op
+	assert( false ); // unhandled CB op
+
+	case 0x07: // RLCA
+	case 0x17: // RLA
+		data = op;
+		op = reg.rg.a;
+	rl_comm:
+		op <<= 1;
+		op |= ((data & flags) >> 4) & 1; // RL and carry is set
+		flags = (op >> 4) & c_flag; // C = bit shifted out
+		if ( data < 0x10 ) // RLC
+			op |= op >> 8;
+		// SLA doesn't fill lower bit
+		goto shift_comm;
+	
+	case 0x0F: // RRCA
+	case 0x1F: // RRA
+		data = op;
+		op = reg.rg.a;
+	rr_comm:
+		op |= (data & flags) << 4; // RR and carry is set
+		flags = (op << 4) & c_flag; // C = bit shifted out
+		if ( data < 0x10 ) // RRC
+			op |= op << 8;
+		op >>= 1;
+		if ( data & 0x20 ) // SRA propagates sign bit
+			op |= (op << 1) & 0x80;
+	shift_comm:
+		data &= 7;
+		if ( !(op & 0xFF) )
+			flags |= z_flag;
+		if ( data == 6 )
+			goto write_hl_op_ff;
+		R8( data ) = op;
+		goto loop;
+
+// Load
+
+	case 0x70: // LD (HL),B
+	case 0x71: // LD (HL),C
+	case 0x72: // LD (HL),D
+	case 0x73: // LD (HL),E
+	case 0x74: // LD (HL),H
+	case 0x75: // LD (HL),L
+	case 0x77: // LD (HL),A
+		op = R8( op & 7 );
+	write_hl_op_ff:
+		WRITE( reg.rp.hl, op & 0xFF );
+		goto loop;
+
+	case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x47: // LD r,r
+	case 0x48: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4F:
+	case 0x50: case 0x51: case 0x53: case 0x54: case 0x55: case 0x57:
+	case 0x58: case 0x59: case 0x5A: case 0x5C: case 0x5D: case 0x5F:
+	case 0x60: case 0x61: case 0x62: case 0x63: case 0x65: case 0x67:
+	case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6F:
+	case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D:
+		R8( (op >> 3) & 7 ) = R8( op & 7 );
+		goto loop;
+
+	case 0x08: // LD IND16,SP
+		data = GET_ADDR();
+		pc += 2;
+		WRITE( data, sp&0xFF );
+		data++;
+		WRITE( data, sp >> 8 );
+		goto loop;
+	
+	case 0xF9: // LD SP,HL
+		sp = reg.rp.hl;
+		goto loop;
+
+	case 0x31: // LD SP,IMM
+		sp = GET_ADDR();
+		pc += 2;
+		goto loop;
+	
+	case 0x01: // LD BC,IMM
+	case 0x11: // LD DE,IMM
+		reg.r16 [op >> 4] = GET_ADDR();
+		pc += 2;
+		goto loop;
+	
+	{
+		unsigned temp;
+	case 0xE0: // LD (0xFF00+imm),A
+		temp = data | 0xFF00;
+		pc++;
+		goto write_data_rg_a;
+	
+	case 0xE2: // LD (0xFF00+C),A
+		temp = reg.rg.c | 0xFF00;
+		goto write_data_rg_a;
+
+	case 0x32: // LD (HL-),A
+		temp = reg.rp.hl;
+		reg.rp.hl = temp - 1;
+		goto write_data_rg_a;
+	
+	case 0x02: // LD (BC),A
+		temp = reg.rp.bc;
+		goto write_data_rg_a;
+	
+	case 0x12: // LD (DE),A
+		temp = reg.rp.de;
+		goto write_data_rg_a;
+	
+	case 0x22: // LD (HL+),A
+		temp = reg.rp.hl;
+		reg.rp.hl = temp + 1;
+		goto write_data_rg_a;
+		
+	case 0xEA: // LD IND16,A (common)
+		temp = GET_ADDR();
+		pc += 2;
+	write_data_rg_a:
+		WRITE( temp, reg.rg.a );
+		goto loop;
+	}
+	
+	case 0x06: // LD B,IMM
+		reg.rg.b = data;
+		pc++;
+		goto loop;
+	
+	case 0x0E: // LD C,IMM
+		reg.rg.c = data;
+		pc++;
+		goto loop;
+	
+	case 0x16: // LD D,IMM
+		reg.rg.d = data;
+		pc++;
+		goto loop;
+	
+	case 0x1E: // LD E,IMM
+		reg.rg.e = data;
+		pc++;
+		goto loop;
+	
+	case 0x26: // LD H,IMM
+		reg.rg.h = data;
+		pc++;
+		goto loop;
+	
+	case 0x2E: // LD L,IMM
+		reg.rg.l = data;
+		pc++;
+		goto loop;
+	
+	case 0x36: // LD (HL),IMM
+		WRITE( reg.rp.hl, data );
+		pc++;
+		goto loop;
+	
+	case 0x3E: // LD A,IMM
+		reg.rg.a = data;
+		pc++;
+		goto loop;
+
+// Increment/Decrement
+
+	case 0x03: // INC BC
+	case 0x13: // INC DE
+	case 0x23: // INC HL
+		reg.r16 [op >> 4]++;
+		goto loop;
+	
+	case 0x33: // INC SP
+		sp = (sp + 1) & 0xFFFF;
+		goto loop;
+
+	case 0x0B: // DEC BC
+	case 0x1B: // DEC DE
+	case 0x2B: // DEC HL
+		reg.r16 [op >> 4]--;
+		goto loop;
+	
+	case 0x3B: // DEC SP
+		sp = (sp - 1) & 0xFFFF;
+		goto loop;
+	
+	case 0x34: // INC (HL)
+		op = reg.rp.hl;
+		data = READ( op );
+		data++;
+		WRITE( op, data & 0xFF );
+		goto inc_comm;
+	
+	case 0x04: // INC B
+	case 0x0C: // INC C (common)
+	case 0x14: // INC D
+	case 0x1C: // INC E
+	case 0x24: // INC H
+	case 0x2C: // INC L
+	case 0x3C: // INC A
+		op = (op >> 3) & 7;
+		R8( op ) = data = R8( op ) + 1;
+	inc_comm:
+		flags = (flags & c_flag) | (((data & 15) - 1) & h_flag) | ((data >> 1) & z_flag);
+		goto loop;
+	
+	case 0x35: // DEC (HL)
+		op = reg.rp.hl;
+		data = READ( op );
+		data--;
+		WRITE( op, data & 0xFF );
+		goto dec_comm;
+	
+	case 0x05: // DEC B
+	case 0x0D: // DEC C
+	case 0x15: // DEC D
+	case 0x1D: // DEC E
+	case 0x25: // DEC H
+	case 0x2D: // DEC L
+	case 0x3D: // DEC A
+		op = (op >> 3) & 7;
+		data = R8( op ) - 1;
+		R8( op ) = data;
+	dec_comm:
+		flags = (flags & c_flag) | n_flag | (((data & 15) + 0x31) & h_flag);
+		if ( data & 0xFF )
+			goto loop;
+		flags |= z_flag;
+		goto loop;
+
+// Add 16-bit
+
+	{
+		blargg_ulong temp; // need more than 16 bits for carry
+		unsigned prev;
+		
+	case 0xF8: // LD HL,SP+imm
+		temp = (int8_t) (data); // sign-extend to 16 bits
+		pc++;
+		flags = 0;
+		temp += sp;
+		prev = sp;
+		goto add_16_hl;
+	
+	case 0xE8: // ADD SP,IMM
+		temp = (int8_t) (data); // sign-extend to 16 bits
+		pc++;
+		flags = 0;
+		temp += sp;
+		prev = sp;
+		sp = temp & 0xFFFF;
+		goto add_16_comm;
+
+	case 0x39: // ADD HL,SP
+		temp = sp;
+		goto add_hl_comm;
+	
+	case 0x09: // ADD HL,BC
+	case 0x19: // ADD HL,DE
+	case 0x29: // ADD HL,HL
+		temp = reg.r16 [op >> 4];
+	add_hl_comm:
+		prev = reg.rp.hl;
+		temp += prev;
+		flags &= z_flag;
+	add_16_hl:
+		reg.rp.hl = temp;
+	add_16_comm:
+		flags |= (temp >> 12) & c_flag;
+		flags |= (((temp & 0x0FFF) - (prev & 0x0FFF)) >> 7) & h_flag;
+		goto loop;
+	}
+	
+	case 0x86: // ADD (HL)
+		data = READ( reg.rp.hl );
+		goto add_comm;
+	
+	case 0x80: // ADD B
+	case 0x81: // ADD C
+	case 0x82: // ADD D
+	case 0x83: // ADD E
+	case 0x84: // ADD H
+	case 0x85: // ADD L
+	case 0x87: // ADD A
+		data = R8( op & 7 );
+		goto add_comm;
+	
+	case 0xC6: // ADD IMM
+		pc++;
+	add_comm:
+		flags = reg.rg.a;
+		data += flags;
+		flags = ((data & 15) - (flags & 15)) & h_flag;
+		flags |= (data >> 4) & c_flag;
+		reg.rg.a = data;
+		if ( data & 0xFF )
+			goto loop;
+		flags |= z_flag;
+		goto loop;
+
+// Add/Subtract
+
+	case 0x8E: // ADC (HL)
+		data = READ( reg.rp.hl );
+		goto adc_comm;
+	
+	case 0x88: // ADC B
+	case 0x89: // ADC C
+	case 0x8A: // ADC D
+	case 0x8B: // ADC E
+	case 0x8C: // ADC H
+	case 0x8D: // ADC L
+	case 0x8F: // ADC A
+		data = R8( op & 7 );
+		goto adc_comm;
+	
+	case 0xCE: // ADC IMM
+		pc++;
+	adc_comm:
+		data += (flags >> 4) & 1;
+		data &= 0xFF; // to do: does carry get set when sum + carry = 0x100?
+		goto add_comm;
+
+	case 0x96: // SUB (HL)
+		data = READ( reg.rp.hl );
+		goto sub_comm;
+	
+	case 0x90: // SUB B
+	case 0x91: // SUB C
+	case 0x92: // SUB D
+	case 0x93: // SUB E
+	case 0x94: // SUB H
+	case 0x95: // SUB L
+	case 0x97: // SUB A
+		data = R8( op & 7 );
+		goto sub_comm;
+	
+	case 0xD6: // SUB IMM
+		pc++;
+	sub_comm:
+		op = reg.rg.a;
+		data = op - data;
+		reg.rg.a = data;
+		goto sub_set_flags;
+
+	case 0x9E: // SBC (HL)
+		data = READ( reg.rp.hl );
+		goto sbc_comm;
+	
+	case 0x98: // SBC B
+	case 0x99: // SBC C
+	case 0x9A: // SBC D
+	case 0x9B: // SBC E
+	case 0x9C: // SBC H
+	case 0x9D: // SBC L
+	case 0x9F: // SBC A
+		data = R8( op & 7 );
+		goto sbc_comm;
+	
+	case 0xDE: // SBC IMM
+		pc++;
+	sbc_comm:
+		data += (flags >> 4) & 1;
+		data &= 0xFF; // to do: does carry get set when sum + carry = 0x100?
+		goto sub_comm;
+
+// Logical
+
+	case 0xA0: // AND B
+	case 0xA1: // AND C
+	case 0xA2: // AND D
+	case 0xA3: // AND E
+	case 0xA4: // AND H
+	case 0xA5: // AND L
+		data = R8( op & 7 );
+		goto and_comm;
+	
+	case 0xA6: // AND (HL)
+		data = READ( reg.rp.hl );
+		pc--;
+	case 0xE6: // AND IMM
+		pc++;
+	and_comm:
+		reg.rg.a &= data;
+	case 0xA7: // AND A
+		flags = h_flag | (((reg.rg.a - 1) >> 1) & z_flag);
+		goto loop;
+
+	case 0xB0: // OR B
+	case 0xB1: // OR C
+	case 0xB2: // OR D
+	case 0xB3: // OR E
+	case 0xB4: // OR H
+	case 0xB5: // OR L
+		data = R8( op & 7 );
+		goto or_comm;
+	
+	case 0xB6: // OR (HL)
+		data = READ( reg.rp.hl );
+		pc--;
+	case 0xF6: // OR IMM
+		pc++;
+	or_comm:
+		reg.rg.a |= data;
+	case 0xB7: // OR A
+		flags = ((reg.rg.a - 1) >> 1) & z_flag;
+		goto loop;
+
+	case 0xA8: // XOR B
+	case 0xA9: // XOR C
+	case 0xAA: // XOR D
+	case 0xAB: // XOR E
+	case 0xAC: // XOR H
+	case 0xAD: // XOR L
+		data = R8( op & 7 );
+		goto xor_comm;
+	
+	case 0xAE: // XOR (HL)
+		data = READ( reg.rp.hl );
+		pc--;
+	case 0xEE: // XOR IMM
+		pc++;
+	xor_comm:
+		data ^= reg.rg.a;
+		reg.rg.a = data;
+		data--;
+		flags = (data >> 1) & z_flag;
+		goto loop;
+	
+	case 0xAF: // XOR A
+		reg.rg.a = 0;
+		flags = z_flag;
+		goto loop;
+
+// Stack
+
+	case 0xF1: // POP FA
+	case 0xC1: // POP BC
+	case 0xD1: // POP DE
+	case 0xE1: // POP HL (common)
+		data = READ( sp );
+		reg.r16 [(op >> 4) & 3] = data + 0x100 * READ( sp + 1 );
+		sp = (sp + 2) & 0xFFFF;
+		if ( op != 0xF1 )
+			goto loop;
+		flags = reg.rg.flags & 0xF0;
+		goto loop;
+	
+	case 0xC5: // PUSH BC
+		data = reg.rp.bc;
+		goto push;
+	
+	case 0xD5: // PUSH DE
+		data = reg.rp.de;
+		goto push;
+	
+	case 0xE5: // PUSH HL
+		data = reg.rp.hl;
+		goto push;
+	
+	case 0xF5: // PUSH FA
+		data = (flags << 8) | reg.rg.a;
+		goto push;
+
+// Flow control
+	
+	case 0xFF:
+		if ( pc == idle_addr + 1 )
+			goto stop;
+	case 0xC7: case 0xCF: case 0xD7: case 0xDF:  // RST
+	case 0xE7: case 0xEF: case 0xF7:
+		data = pc;
+		pc = (op & 0x38) + rst_base;
+		goto push;
+	
+	case 0xCC: // CZ
+		pc += 2;
+		if ( flags & z_flag )
+			goto call;
+		goto loop;
+	
+	case 0xD4: // CNC
+		pc += 2;
+		if ( !(flags & c_flag) )
+			goto call;
+		goto loop;
+	
+	case 0xDC: // CC
+		pc += 2;
+		if ( flags & c_flag )
+			goto call;
+		goto loop;
+
+	case 0xD9: // RETI
+		//interrupts_enabled = 1;
+		goto ret;
+	
+	case 0xC0: // RZ
+		if ( !(flags & z_flag) )
+			goto ret;
+		goto loop;
+	
+	case 0xD0: // RNC
+		if ( !(flags & c_flag) )
+			goto ret;
+		goto loop;
+	
+	case 0xD8: // RC
+		if ( flags & c_flag )
+			goto ret;
+		goto loop;
+
+	case 0x18: // JR
+		BRANCH( true )
+	
+	case 0x30: // JR NC
+		BRANCH( !(flags & c_flag) )
+	
+	case 0x38: // JR C
+		BRANCH( flags & c_flag )
+	
+	case 0xE9: // JP_HL
+		pc = reg.rp.hl;
+		goto loop;
+
+	case 0xC3: // JP (next-most-common)
+		pc = GET_ADDR();
+		goto loop;
+	
+	case 0xC2: // JP NZ
+		pc += 2;
+		if ( !(flags & z_flag) )
+			goto jp_taken;
+		goto loop;
+	
+	case 0xCA: // JP Z (most common)
+		pc += 2;
+		if ( !(flags & z_flag) )
+			goto loop;
+	jp_taken:
+		pc -= 2;
+		pc = GET_ADDR();
+		goto loop;
+	
+	case 0xD2: // JP NC
+		pc += 2;
+		if ( !(flags & c_flag) )
+			goto jp_taken;
+		goto loop;
+	
+	case 0xDA: // JP C
+		pc += 2;
+		if ( flags & c_flag )
+			goto jp_taken;
+		goto loop;
+
+// Flags
+
+	case 0x2F: // CPL
+		reg.rg.a = ~reg.rg.a;
+		flags |= n_flag | h_flag;
+		goto loop;
+
+	case 0x3F: // CCF
+		flags = (flags ^ c_flag) & ~(n_flag | h_flag);
+		goto loop;
+
+	case 0x37: // SCF
+		flags = (flags | c_flag) & ~(n_flag | h_flag);
+		goto loop;
+
+	case 0xF3: // DI
+		//interrupts_enabled = 0;
+		goto loop;
+
+	case 0xFB: // EI
+		//interrupts_enabled = 1;
+		goto loop;
+
+// Special
+
+	case 0xDD: case 0xD3: case 0xDB: case 0xE3: case 0xE4: // ?
+	case 0xEB: case 0xEC: case 0xF4: case 0xFD: case 0xFC:
+	case 0x10: // STOP
+	case 0x27: // DAA (I'll have to implement this eventually...)
+	case 0xBF:
+	case 0xED: // Z80 prefix
+	case 0x76: // HALT
+		s.remain++;
+		goto stop;
+	}
+	
+	// If this fails then the case above is missing an opcode
+	assert( false );
+	
+stop:
+	pc--;
+	
+	// copy state back
+	r->rg = reg.rg;
+	r->pc = pc;
+	r->sp = sp;
+	r->rg.flags = flags;
+	
+	state = &state_;
+	memcpy( &state_, &s, sizeof state_ );
+	
+	return s.remain > 0;
+}
+
+blargg_long Cpu_remain( void )
+{
+	return state->remain * clocks_per_instr;
+}
+
Index: apps/codecs/libgbs/SOURCES
===================================================================
--- apps/codecs/libgbs/SOURCES	(revision 0)
+++ apps/codecs/libgbs/SOURCES	(revision 0)
@@ -0,0 +1,8 @@
+blip_buffer.c
+gb_apu.c
+gb_cpu.c
+gb_oscs.c
+gbs_emu.c
+multi_buffer.c
+rom_data.c
+m3u_playlist.c
Index: apps/codecs/libgbs/gb_apu.h
===================================================================
--- apps/codecs/libgbs/gb_apu.h	(revision 0)
+++ apps/codecs/libgbs/gb_apu.h	(revision 0)
@@ -0,0 +1,75 @@
+// Nintendo Game Boy PAPU sound chip emulator
+
+// Gb_Snd_Emu 0.1.5
+#ifndef GB_APU_H
+#define GB_APU_H
+
+#include "gb_oscs.h"
+
+enum { osc_count = 4 };
+enum { start_addr = 0xFF10 };
+enum { end_addr   = 0xFF3F };
+enum { register_count = end_addr - start_addr + 1 };
+
+struct Gb_Apu {	
+	struct Gb_Osc*     oscs [osc_count];
+	blip_time_t   next_frame_time;
+	blip_time_t   last_time;
+	blip_time_t frame_period;
+	double      volume_unit;
+	int         frame_count;
+	
+	struct Gb_Square   square1;
+	struct Gb_Square   square2;
+	struct Gb_Wave     wave;
+	struct Gb_Noise    noise;
+	uint8_t regs [register_count];
+	Synth square_synth; // used by squares;
+	Synth other_synth; // used by wave and noise
+};
+
+// Initializes Blip_Buffer structure
+void Apu_init( struct Gb_Apu* this );
+
+// Stops (clear) Blip_Buffer structure
+void Apu_stop( struct Gb_Apu* this );
+	
+// Outputs can be assigned to a single buffer for mono output, or to three
+// buffers for stereo output (using Stereo_Buffer to do the mixing).
+	
+// Assign all oscillator outputs to specified buffer(s). If buffer
+// is NULL, silences all oscillators.
+void Apu_output( struct Gb_Apu* this, struct Blip_Buffer* center, struct Blip_Buffer* left, struct Blip_Buffer* right ); ICODE_ATTR
+	
+// Assign single oscillator output to buffer(s). Valid indicies are 0 to 3,
+// which refer to Square 1, Square 2, Wave, and Noise. If buffer is NULL,
+// silences oscillator.
+
+void Apu_osc_output( struct Gb_Apu* this, int index, struct Blip_Buffer* center, struct Blip_Buffer* left, struct Blip_Buffer* right ); ICODE_ATTR
+	
+// Reset oscillators and internal state
+void Apu_reset( struct Gb_Apu* this );
+	
+// Reads and writes at addr must satisfy start_addr <= addr <= end_addr
+	
+// Write 'data' to address at specified time
+void Apu_write_register( struct Gb_Apu* this, blip_time_t, unsigned addr, int data ); ICODE_ATTR
+	
+// Read from address at specified time
+int Apu_read_register( struct Gb_Apu* this, blip_time_t, unsigned addr ); ICODE_ATTR
+	
+// Run all oscillators up to specified time, end current time frame, then
+// start a new frame at time 0.
+void Apu_end_frame( struct Gb_Apu* this, blip_time_t ); ICODE_ATTR
+void Apu_set_tempo( struct Gb_Apu* this, double ); ICODE_ATTR
+void Apu_update_volume( struct Gb_Apu* this ); ICODE_ATTR
+void Apu_run_until( struct Gb_Apu* this, blip_time_t ); ICODE_ATTR
+void Apu_write_osc( struct Gb_Apu* this, int index, int reg, int data ); ICODE_ATTR
+
+// Set overall volume of all oscillators, where 1.0 is full volume
+static inline void Apu_volume( struct Gb_Apu* this, double vol )
+{
+	this->volume_unit = 0.60 / osc_count / 15 /*steps*/ / 2 /*?*/ / 8 /*master vol range*/ * vol;
+	Apu_update_volume( this );
+}
+#endif
Index: apps/codecs/libgbs/gb_cpu.h
===================================================================
--- apps/codecs/libgbs/gb_cpu.h	(revision 0)
+++ apps/codecs/libgbs/gb_cpu.h	(revision 0)
@@ -0,0 +1,79 @@
+// Nintendo Game Boy CPU emulator
+// Treats every instruction as taking 4 cycles
+
+// Game_Music_Emu 0.5.2
+#ifndef GB_CPU_H
+#define GB_CPU_H
+
+#include "blargg_common.h"
+#include "blargg_endian.h"
+
+struct Gbs_Emu;
+
+typedef unsigned gb_addr_t; // 16-bit CPU address
+
+enum { clocks_per_instr = 4 };
+enum { page_size = 0x2000 };
+enum { page_shift = 13 };
+enum { page_count = 0x10000 >> page_shift };
+
+// If CPU executes opcode 0xFF at this address, it treats as illegal instruction
+enum { idle_addr = 0xF00D };
+
+// Can read this many bytes past end of a page
+enum { cpu_padding = 8 };
+
+// Game Boy Z80 registers. *Not* kept updated during a call to run().
+struct core_regs_t {
+#ifdef BLARGG_BIG_ENDIAN
+	uint8_t b, c, d, e, h, l, flags, a;
+#else
+	uint8_t c, b, e, d, l, h, a, flags;
+#endif
+};
+
+struct registers_t {
+	long pc; // more than 16 bits to allow overflow detection
+	uint16_t sp;
+
+	struct core_regs_t rg;
+};
+
+struct state_t {
+	uint8_t* code_map [page_count + 1];
+	blargg_long remain;
+};
+
+// Initializes cpu stuff
+void Cpu_init( void );
+
+// Clear registers and map all pages to unmapped
+void Cpu_reset( void* unmapped );
+
+// Map code memory (memory accessed via the program counter). Start and size
+// must be multiple of page_size.
+
+void Cpu_map_code( gb_addr_t start, unsigned size, void* code ); ICODE_ATTR
+
+// Push a byte on the stack
+void Cpu_push_byte( int ); ICODE_ATTR
+
+// Run CPU for at least 'count' cycles and return false, or return true if
+// illegal instruction is encountered.
+bool Cpu_run( struct Gbs_Emu* this, blargg_long count ); ICODE_ATTR
+
+// Number of clock cycles remaining for most recent run() call
+blargg_long Cpu_remain( void ); ICODE_ATTR
+
+/* void Cpu_set_code_page( int, uint8_t* ); */
+static inline uint8_t* Cpu_get_code( gb_addr_t addr )
+{
+	extern struct state_t* state;
+	return state->code_map [addr >> page_shift] + addr
+	#if !defined(BLARGG_NONPORTABLE)
+		% (unsigned) page_size
+	#endif
+	;
+}
+
+#endif
Index: apps/codecs/libgbs/blargg_common.h
===================================================================
--- apps/codecs/libgbs/blargg_common.h	(revision 0)
+++ apps/codecs/libgbs/blargg_common.h	(revision 0)
@@ -0,0 +1,150 @@
+// Sets up common environment for Shay Green's libraries.
+// To change configuration options, modify blargg_config.h, not this file.
+
+#ifndef BLARGG_COMMON_H
+#define BLARGG_COMMON_H
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+
+#undef BLARGG_COMMON_H
+// allow blargg_config.h to #include blargg_common.h
+#include "blargg_config.h"
+#ifndef BLARGG_COMMON_H
+#define BLARGG_COMMON_H
+
+#if defined(CPU_ARM) || !defined(ROCKBOX)
+#if (CONFIG_CPU != PP5002) || defined(WIN32)
+    #undef  ICODE_ATTR
+    #define ICODE_ATTR
+
+    #undef  IDATA_ATTR
+    #define IDATA_ATTR
+
+    #undef  ICONST_ATTR
+    #define ICONST_ATTR
+
+    #undef  IBSS_ATTR
+    #define IBSS_ATTR
+#endif
+#endif
+
+// STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
+#ifndef STATIC_CAST
+	#define STATIC_CAST(T,expr) ((T) (expr))
+#endif
+
+// blargg_err_t (0 on success, otherwise error string)
+#ifndef blargg_err_t
+	typedef const char* blargg_err_t;
+#endif
+
+#define BLARGG_4CHAR( a, b, c, d ) \
+	((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))
+
+// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
+#ifndef BOOST_STATIC_ASSERT
+	#ifdef _MSC_VER
+		// MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
+		#define BOOST_STATIC_ASSERT( expr ) \
+			void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
+	#else
+		// Some other compilers fail when declaring same function multiple times in class,
+		// so differentiate them by line
+		#define BOOST_STATIC_ASSERT( expr ) \
+			void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
+	#endif
+#endif
+
+// BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,
+// compiler is assumed to support bool. If undefined, availability is determined.
+#ifndef BLARGG_COMPILER_HAS_BOOL
+	#if defined (__MWERKS__)
+		#if !__option(bool)
+			#define BLARGG_COMPILER_HAS_BOOL 0
+		#endif
+	#elif defined (_MSC_VER)
+		#if _MSC_VER < 1100
+			#define BLARGG_COMPILER_HAS_BOOL 0
+		#endif
+	#elif defined (__GNUC__)
+		// supports bool
+	#elif __cplusplus < 199711
+		#define BLARGG_COMPILER_HAS_BOOL 0
+	#endif
+#endif
+#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
+	// If you get errors here, modify your blargg_config.h file
+	typedef int bool;
+	static bool true  = 1;
+	static bool false = 0;
+#endif
+
+// blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
+#include <limits.h>
+
+#if INT_MAX >= 0x7FFFFFFF
+	typedef int blargg_long;
+#else
+	typedef long blargg_long;
+#endif
+
+#if UINT_MAX >= 0xFFFFFFFF
+	typedef unsigned blargg_ulong;
+#else
+	typedef unsigned long blargg_ulong;
+#endif
+
+// int8_t etc.
+
+
+// ROCKBOX: If defined, use <codeclib.h> for int_8_t etc
+#if defined (ROCKBOX)
+	#include <codecs/lib/codeclib.h>
+// HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.
+#elif defined (HAVE_STDINT_H)
+	#include <stdint.h>
+	#define BOOST
+
+// HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
+#elif defined (HAVE_INTTYPES_H)
+	#include <inttypes.h>
+	#define BOOST
+
+#else
+	#if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
+		typedef signed char     int8_t;
+		typedef unsigned char   uint8_t;
+	#else
+		// No suitable 8-bit type available
+		typedef struct see_blargg_common_h int8_t;
+		typedef struct see_blargg_common_h uint8_t;
+	#endif
+		
+	#if USHRT_MAX == 0xFFFF
+		typedef short           int16_t;
+		typedef unsigned short  uint16_t;
+	#else
+		// No suitable 16-bit type available
+		typedef struct see_blargg_common_h int16_t;
+		typedef struct see_blargg_common_h uint16_t;
+	#endif
+		
+	#if ULONG_MAX == 0xFFFFFFFF
+		typedef long            int32_t;
+		typedef unsigned long   uint32_t;
+	#elif UINT_MAX == 0xFFFFFFFF
+		typedef int             int32_t;
+		typedef unsigned int    uint32_t;
+	#else
+		// No suitable 32-bit type available
+		typedef struct see_blargg_common_h int32_t;
+		typedef struct see_blargg_common_h uint32_t;
+	#endif
+#endif
+
+#endif
+#endif
Index: apps/codecs/libgbs/gme.h
===================================================================
--- apps/codecs/libgbs/gme.h	(revision 0)
+++ apps/codecs/libgbs/gme.h	(revision 0)
@@ -0,0 +1,18 @@
+/* Game music emulator library C interface (also usable from C++) */
+
+/* Game_Music_Emu 0.5.2 */
+#ifndef GME_H
+#define GME_H
+
+#ifdef __cplusplus
+	extern "C" {
+#endif
+
+/* Error string returned by library functions, or NULL if no error (success) */
+typedef const char* gme_err_t;
+
+#ifdef __cplusplus
+	}
+#endif
+
+#endif
Index: apps/codecs/libgbs/blip_buffer.c
===================================================================
--- apps/codecs/libgbs/blip_buffer.c	(revision 0)
+++ apps/codecs/libgbs/blip_buffer.c	(revision 0)
@@ -0,0 +1,282 @@
+// Blip_Buffer 0.4.1. http://www.slack.net/~ant/
+
+#include "blip_buffer.h"
+
+#include <assert.h>
+#include <limits.h>
+#include <string.h>
+#include <stdlib.h>
+#include <math.h>
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#ifdef BLARGG_ENABLE_OPTIMIZER
+	#include BLARGG_ENABLE_OPTIMIZER
+#endif
+
+int const silent_buf_size = 1; // size used for Silent_Blip_Buffer
+
+void Blip_init( struct Blip_Buffer* this_ )
+{
+	this_->factor_       = LONG_MAX;
+	this_->offset_       = 0;
+	this_->buffer_size_  = 0;
+	this_->sample_rate_  = 0;
+	this_->reader_accum_ = 0;
+	this_->bass_shift_   = 0;
+	this_->clock_rate_   = 0;
+	this_->bass_freq_    = 16;
+	this_->length_       = 0;
+	
+	// assumptions code makes about implementation-defined features
+	#ifndef NDEBUG
+		// right shift of negative value preserves sign
+		buf_t_ i = -0x7FFFFFFE;
+		assert( (i >> 1) == -0x3FFFFFFF );
+		
+		// casting to short truncates to 16 bits and sign-extends
+		i = 0x18000;
+		assert( (short) i == -0x8000 );
+	#endif
+}
+
+void Blip_stop( struct Blip_Buffer* this_ )
+{
+	if ( this_->buffer_size_ != silent_buf_size )
+		free( this_->buffer_ );
+}
+
+void Blip_clear( struct Blip_Buffer* this_, int entire_buffer )
+{
+	this_->offset_      = 0;
+	this_->reader_accum_ = 0;
+	this_->modified_    = 0;
+	if ( this_->buffer_ )
+	{
+		long count = (entire_buffer ? this_->buffer_size_ : Blip_samples_avail( this_ ));
+		memset( this_->buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) );
+	}
+}
+
+blargg_err_t Blip_set_sample_rate( struct Blip_Buffer* this_, long new_rate, int msec )
+{
+	if ( this_->buffer_size_ == silent_buf_size )
+	{
+		assert( 0 );
+		return "Internal (tried to resize Silent_Blip_Buffer)";
+	}
+	
+	// start with maximum length that resampled time can represent
+	long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
+	if ( msec != blip_max_length )
+	{
+		long s = (new_rate * (msec + 1) + 999) / 1000;
+		if ( s < new_size )
+			new_size = s;
+		else
+			assert( 0 ); // fails if requested buffer length exceeds limit
+	}
+	
+	if ( new_size > blip_buffer_max )
+		return "Out of memory";
+	
+	this_->buffer_size_ = new_size;
+	assert( this_->buffer_size_ != silent_buf_size );
+	
+	// update things based on the sample rate
+	this_->sample_rate_ = new_rate;
+	this_->length_ = new_size * 1000 / new_rate - 1;
+	if ( msec )
+		assert( this_->length_ == msec ); // ensure length is same as that passed in
+	if ( this_->clock_rate_ )
+		Blip_set_clock_rate( this_, this_->clock_rate_ );
+	Blip_bass_freq( this_, this_->bass_freq_ );
+	
+	Blip_clear( this_, 1 );
+	
+	return 0; // success
+}
+
+/* Not sure if this_ affects sound quality */
+#if defined(ROCKBOX)
+  #define floor
+#endif
+
+blip_resampled_time_t Blip_clock_rate_factor( struct Blip_Buffer* this_, long rate )
+{
+	double ratio = (double) this_->sample_rate_ / rate;
+	blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 );
+	assert( factor > 0 || !this_->sample_rate_ ); // fails if clock/output ratio is too large
+	return (blip_resampled_time_t) factor;
+}
+
+void Blip_bass_freq( struct Blip_Buffer* this_, int freq )
+{
+	this_->bass_freq_ = freq;
+	int shift = 31;
+	if ( freq > 0 )
+	{
+		shift = 13;
+		long f = (freq << 16) / this_->sample_rate_;
+		while ( (f >>= 1) && --shift ) { }
+	}
+	this_->bass_shift_ = shift;
+}
+
+void Blip_end_frame( struct Blip_Buffer* this_, blip_time_t t )
+{
+	this_->offset_ += t * this_->factor_;
+	assert( Blip_samples_avail( this_ ) <= (long) this_->buffer_size_ ); // time outside buffer length
+}
+
+void Blip_remove_silence( struct Blip_Buffer* this_, long count )
+{
+	assert( count <= Blip_samples_avail( this_ ) ); // tried to remove more samples than available
+	this_->offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
+}
+
+long Blip_count_samples( struct Blip_Buffer* this_, blip_time_t t )
+{
+	unsigned long last_sample  = Blip_resampled_time( this_, t ) >> BLIP_BUFFER_ACCURACY;
+	unsigned long first_sample = this_->offset_ >> BLIP_BUFFER_ACCURACY;
+	return (long) (last_sample - first_sample);
+}
+
+blip_time_t Blip_count_clocks( struct Blip_Buffer* this_, long count )
+{
+	if ( !this_->factor_ )
+	{
+		assert( 0 ); // sample rate and clock rates must be set first
+		return 0;
+	}
+	
+	if ( count > this_->buffer_size_ )
+		count = this_->buffer_size_;
+	blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
+	return (blip_time_t) ((time - this_->offset_ + this_->factor_ - 1) / this_->factor_);
+}
+
+void Blip_remove_samples( struct Blip_Buffer* this_, long count )
+{
+	if ( count )
+	{
+		Blip_remove_silence( this_, count );
+		
+		// copy remaining samples to beginning and clear old samples
+		long remain = Blip_samples_avail( this_ ) + blip_buffer_extra_;
+		memmove( this_->buffer_, this_->buffer_ + count, remain * sizeof *this_->buffer_ );
+		memset( this_->buffer_ + remain, 0, count * sizeof *this_->buffer_ );
+	}
+}
+
+long Blip_read_samples( struct Blip_Buffer* this_, blip_sample_t* BLIP_RESTRICT out, long max_samples, int stereo )
+{
+	long count = Blip_samples_avail( this_ );
+	if ( count > max_samples )
+		count = max_samples;
+	
+	if ( count )
+	{
+		int const bass = BLIP_READER_BASS( *this_ );
+		BLIP_READER_BEGIN( reader, *this_ );
+		
+		if ( !stereo )
+		{
+			blip_long n;
+			for ( n = count; n; --n )
+			{
+				blip_long s = BLIP_READER_READ( reader );
+				if ( (blip_sample_t) s != s )
+					s = 0x7FFF - (s >> 24);
+				*out++ = (blip_sample_t) s;
+				BLIP_READER_NEXT( reader, bass );
+			}
+		}
+		else
+		{
+			blip_long n;
+			for ( n = count; n; --n )
+			{
+				blip_long s = BLIP_READER_READ( reader );
+				if ( (blip_sample_t) s != s )
+					s = 0x7FFF - (s >> 24);
+				*out = (blip_sample_t) s;
+				out += 2;
+				BLIP_READER_NEXT( reader, bass );
+			}
+		}
+		BLIP_READER_END( reader, *this_ );
+		
+		Blip_remove_samples( this_, count );
+	}
+	return count;
+}
+
+void Blip_mix_samples( struct Blip_Buffer* this_,  blip_sample_t const* in, long count )
+{
+	if ( this_->buffer_size_ == silent_buf_size )
+	{
+		assert( 0 );
+		return;
+	}
+	
+	buf_t_* out = this_->buffer_ + (this_->offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2;
+	
+	int const sample_shift = blip_sample_bits - 16;
+	int prev = 0;
+	while ( count-- )
+	{
+		blip_long s = (blip_long) *in++ << sample_shift;
+		*out += s - prev;
+		prev = s;
+		++out;
+	}
+	*out -= prev;
+}
+
+void Blip_set_modified( struct Blip_Buffer* this_ ) 
+{ 
+	this_->modified_ = 1; 
+}
+
+int Blip_clear_modified( struct Blip_Buffer* this_ )
+{ 
+	int b = this_->modified_;
+	this_->modified_ = 0;
+	return b; 
+}
+
+blip_resampled_time_t Blip_resampled_duration( struct Blip_Buffer* this_, int t )
+{
+	return t * this_->factor_;
+}
+
+blip_resampled_time_t Blip_resampled_time( struct Blip_Buffer* this_, blip_time_t t )
+{
+	return t * this_->factor_ + this_->offset_;
+}
+
+
+// Blip_Synth
+
+void Synth_init( struct Blip_Synth_* this_ )
+{
+	this_->buf = 0;
+	this_->last_amp = 0;
+	this_->delta_factor = 0;
+}
+
+// Set overall volume of waveform
+void Synth_volume( struct Blip_Synth_* this_, double v )
+{
+	this_->delta_factor = (int) (v * (1L << blip_sample_bits) + 0.5);
+}
Index: apps/codecs/libgbs/gb_oscs.c
===================================================================
--- apps/codecs/libgbs/gb_oscs.c	(revision 0)
+++ apps/codecs/libgbs/gb_oscs.c	(revision 0)
@@ -0,0 +1,365 @@
+// Gb_Snd_Emu 0.1.5. http://www.slack.net/~ant/
+
+#include "gb_apu.h"
+
+#include <string.h>
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "blargg_source.h"
+
+// Gb_Osc
+
+void Osc_reset( struct Gb_Osc* this )
+{
+	this->delay = 0;
+	this->last_amp = 0;
+	this->length = 0;
+	this->output_select = 3;
+	this->output = this->outputs [this->output_select];
+}
+
+void Osc_clock_length( struct Gb_Osc* this )
+{
+	if ( (this->regs [4] & len_enabled_mask) && this->length )
+		this->length--;
+}
+
+
+// Gb_Square
+
+void Square_reset( struct Gb_Square* this )
+{
+	this->phase = 0;
+	this->sweep_freq = 0;
+	this->sweep_delay = 0;
+	
+	this->env_delay = 0;
+	Osc_reset( &this->osc );
+	
+	this->osc.delay = 0x40000000; // TODO: something less hacky (never clocked until first trigger)
+}
+
+void Square_clock_envelope( struct Gb_Square* this )
+{
+	if ( this->env_delay && !--this->env_delay )
+	{
+		this->env_delay = this->osc.regs [2] & 7;
+		int v = this->osc.volume - 1 + (this->osc.regs [2] >> 2 & 2);
+		if ( (unsigned) v < 15 )
+			this->osc.volume = v;
+	}
+}
+
+bool Square_write_register( struct Gb_Square* this, int reg, int data )
+{
+	switch ( reg )
+	{
+	case 1:
+		this->osc.length = 64 - (this->osc.regs [1] & 0x3F);
+		break;
+	
+	case 2:
+		if ( !(data >> 4) )
+			this->osc.enabled = false;
+		break;
+	
+	case 4:
+		if ( data & trigger )
+		{
+			this->env_delay = this->osc.regs [2] & 7;
+			this->osc.volume = this->osc.regs [2] >> 4;
+			this->osc.enabled = true;
+			if ( this->osc.length == 0 )
+				this->osc.length = 64;
+			return true;
+		}
+	}
+	return false;
+}
+
+void Square_clock_sweep( struct Gb_Square* this )
+{
+	int sweep_period = (this->osc.regs [0] & period_mask) >> 4;
+	if ( sweep_period && this->sweep_delay && !--this->sweep_delay )
+	{
+		this->sweep_delay = sweep_period;
+		this->osc.regs [3] = this->sweep_freq & 0xFF;
+		this->osc.regs [4] = (this->osc.regs [4] & ~0x07) | (this->sweep_freq >> 8 & 0x07);
+		
+		int offset = this->sweep_freq >> (this->osc.regs [0] & shift_mask);
+		if ( this->osc.regs [0] & 0x08 )
+			offset = -offset;
+		this->sweep_freq += offset;
+		
+		if ( this->sweep_freq < 0 )
+		{
+			this->sweep_freq = 0;
+		}
+		else if ( this->sweep_freq >= 2048 )
+		{
+			this->sweep_delay = 0; // don't modify channel frequency any further
+			this->sweep_freq = 2048; // silence sound immediately
+		}
+	}
+}
+
+void Square_run( struct Gb_Square* this, blip_time_t time, blip_time_t end_time, int playing )
+{
+	if ( this->sweep_freq == 2048 )
+		playing = false;
+	
+	static unsigned char const table [4] ICONST_ATTR = { 1, 2, 4, 6 };
+	int const duty = table [this->osc.regs [1] >> 6];
+	int amp = this->osc.volume & playing;
+	if ( this->phase >= duty )
+		amp = -amp;
+	
+	int frequency = Osc_frequency( &this->osc );
+	if ( (unsigned) (frequency - 1) > 2040 ) // frequency < 1 || frequency > 2041
+	{
+		// really high frequency results in DC at half volume
+		amp = this->osc.volume >> 1;
+		playing = false;
+	}
+	
+	{
+		int delta = amp - this->osc.last_amp;
+		if ( delta )
+		{
+			this->osc.last_amp = amp;
+            Synth_offset( this->synth, time, delta, this->osc.output );
+		}
+	}
+	
+	time += this->osc.delay;
+	if ( !playing )
+		time = end_time;
+	
+	if ( time < end_time )
+	{
+		int const period = (2048 - frequency) * 4;
+		struct Blip_Buffer* const output = this->osc.output;
+		int phase = this->phase;
+		int delta = amp * 2;
+		do
+		{
+			phase = (phase + 1) & 7;
+			if ( phase == 0 || phase == duty )
+			{
+				delta = -delta;
+				Synth_offset_inline( this->synth, time, delta, output );
+			}
+			time += period;
+		}
+		while ( time < end_time );
+		
+		this->phase = phase;
+		this->osc.last_amp = delta >> 1;
+	}
+	this->osc.delay = time - end_time;
+}
+
+
+// Gb_Noise
+
+void Noise_reset( struct Gb_Noise* this )
+{
+	this->bits = 0;	
+	
+	this->env_delay = 0;
+	Osc_reset( &this->osc );
+}
+
+void Noise_clock_envelope( struct Gb_Noise* this )
+{
+	if ( this->env_delay && !--this->env_delay )
+	{
+		this->env_delay = this->osc.regs [2] & 7;
+		int v = this->osc.volume - 1 + (this->osc.regs [2] >> 2 & 2);
+		if ( (unsigned) v < 15 )
+			this->osc.volume = v;
+	}
+}
+
+bool Noise_write_register( struct Gb_Noise* this, int reg, int data )
+{
+	switch ( reg )
+	{
+	case 1:
+		this->osc.length = 64 - (this->osc.regs [1] & 0x3F);
+		break;
+	
+	case 2:
+		if ( !(data >> 4) )
+			this->osc.enabled = false;
+		break;
+	
+	case 4:
+		if ( data & trigger )
+		{
+			this->env_delay = this->osc.regs [2] & 7;
+			this->osc.volume = this->osc.regs [2] >> 4;
+			this->osc.enabled = true;
+			if ( this->osc.length == 0 )
+				this->osc.length = 64;
+			return true;
+		}
+	}
+	return false;
+}
+
+void Noise_run( struct Gb_Noise* this, blip_time_t time, blip_time_t end_time, int playing )
+{
+	int amp = this->osc.volume & playing;
+	int tap = 13 - (this->osc.regs [3] & 8);
+	if ( this->bits >> tap & 2 )
+		amp = -amp;
+	
+	{
+		int delta = amp - this->osc.last_amp;
+		if ( delta )
+		{
+			this->osc.last_amp = amp;
+			Synth_offset( this->synth, time, delta, this->osc.output );
+		}
+	}
+	
+	time += this->osc.delay;
+	if ( !playing )
+		time = end_time;
+	
+	if ( time < end_time )
+	{
+		static unsigned char const table [8] ICONST_ATTR = { 8, 16, 32, 48, 64, 80, 96, 112 };
+		int period = table [this->osc.regs [3] & 7] << (this->osc.regs [3] >> 4);
+		
+		// keep parallel resampled time to eliminate time conversion in the loop
+		struct Blip_Buffer* const output = this->osc.output;
+		const blip_resampled_time_t resampled_period =
+				Blip_resampled_duration( output, period );
+				
+		blip_resampled_time_t resampled_time = Blip_resampled_time( output, time );
+		unsigned bits = this->bits;
+		int delta = amp * 2;
+		
+		do
+		{
+			unsigned changed = (bits >> tap) + 1;
+			time += period;
+			bits <<= 1;
+			if ( changed & 2 )
+			{
+				delta = -delta;
+				bits |= 1;
+				Synth_offset_resampled( this->synth, resampled_time, delta, output );
+			}
+			resampled_time += resampled_period;
+		}
+		while ( time < end_time );
+		
+		this->bits = bits;
+		this->osc.last_amp = delta >> 1;
+	}
+	this->osc.delay = time - end_time;
+}
+
+// Gb_Wave
+
+void Wave_reset( struct Gb_Wave* this )
+{
+	this->wave_pos = 0;
+	Osc_reset( &this->osc );
+}
+
+void Wave_run( struct Gb_Wave* this, blip_time_t time, blip_time_t end_time, int playing )
+{
+	int volume_shift = (this->osc.volume - 1) & 7; // volume = 0 causes shift = 7
+	int frequency;
+	{
+		int amp = (this->wave [this->wave_pos] >> volume_shift & playing) * 2;
+		
+		frequency = Osc_frequency( &this->osc );
+		if ( (unsigned) (frequency - 1) > 2044 ) // frequency < 1 || frequency > 2045
+		{
+			amp = 30 >> volume_shift & playing;
+			playing = false;
+		}
+		
+		int delta = amp - this->osc.last_amp;
+		if ( delta )
+		{
+			this->osc.last_amp = amp;
+			Synth_offset( this->synth, time, delta, this->osc.output );
+		}
+	}
+	
+	time += this->osc.delay;
+	if ( !playing )
+		time = end_time;
+	
+	if ( time < end_time )
+	{
+		struct Blip_Buffer* const output = this->osc.output;
+		int const period = (2048 - frequency) * 2;
+	 	int wave_pos = (this->wave_pos + 1) & (wave_size - 1);
+	 	
+		do
+		{
+			int amp = (this->wave [wave_pos] >> volume_shift) * 2;
+			wave_pos = (wave_pos + 1) & (wave_size - 1);
+			int delta = amp - this->osc.last_amp;
+			if ( delta )
+			{
+				this->osc.last_amp = amp;
+				Synth_offset( this->synth, time, delta, output );
+			}
+			time += period;
+		}
+		while ( time < end_time );
+		
+		this->wave_pos = (wave_pos - 1) & (wave_size - 1);
+	}
+	this->osc.delay = time - end_time;
+}
+
+// Apu_write_osc
+
+void Apu_write_osc( struct Gb_Apu* this, int index, int reg, int data )
+{
+	reg -= index * 5;
+	struct Gb_Square* sq = &this->square2;
+	switch ( index )
+	{
+	case 0:
+		sq = &this->square1;
+	case 1:
+		if ( Square_write_register( sq, reg, data ) && index == 0 )
+		{
+			this->square1.sweep_freq = Osc_frequency( &this->square1.osc );
+			if ( (this->regs [0] & period_mask) && (this->regs [0] & shift_mask) )
+			{
+				this->square1.sweep_delay = 1; // cause sweep to recalculate now
+				Square_clock_sweep( &this->square1 );
+			}
+		}
+		break;
+	
+	case 2:
+		Wave_write_register( &this->wave, reg, data );
+		break;
+	
+	case 3:
+		if ( Noise_write_register( &this->noise, reg, data ) )
+			this->noise.bits = 0x7FFF;
+	}
+}
Index: apps/codecs/libgbs/blargg_endian.h
===================================================================
--- apps/codecs/libgbs/blargg_endian.h	(revision 0)
+++ apps/codecs/libgbs/blargg_endian.h	(revision 0)
@@ -0,0 +1,147 @@
+// CPU Byte Order Utilities
+
+// Game_Music_Emu 0.5.2
+#ifndef BLARGG_ENDIAN
+#define BLARGG_ENDIAN
+
+#include "blargg_common.h"
+
+// BLARGG_CPU_CISC: Defined if CPU has very few general-purpose registers (< 16)
+#if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \
+		defined (__x86_64__) || defined (__ia64__) || defined (__i386__)
+	#define BLARGG_CPU_X86 1
+	#define BLARGG_CPU_CISC 1
+#endif
+
+#if defined (__powerpc__) || defined (__ppc__) || defined (__POWERPC__) || defined (__powerc)
+	#define BLARGG_CPU_POWERPC 1
+#endif
+
+// BLARGG_BIG_ENDIAN, BLARGG_LITTLE_ENDIAN: Determined automatically, otherwise only
+// one may be #defined to 1. Only needed if something actually depends on byte order.
+#if !defined (BLARGG_BIG_ENDIAN) && !defined (BLARGG_LITTLE_ENDIAN)
+#ifdef __GLIBC__
+	// GCC handles this for us
+	#include <endian.h>
+	#if __BYTE_ORDER == __LITTLE_ENDIAN
+		#define BLARGG_LITTLE_ENDIAN 1
+	#elif __BYTE_ORDER == __BIG_ENDIAN
+		#define BLARGG_BIG_ENDIAN 1
+	#endif
+#else
+
+#if defined (LSB_FIRST) || defined (__LITTLE_ENDIAN__) || defined (BLARGG_CPU_X86) || \
+		(defined (LITTLE_ENDIAN) && LITTLE_ENDIAN+0 != 1234)
+	#define BLARGG_LITTLE_ENDIAN 1
+#endif
+
+#if defined (MSB_FIRST)     || defined (__BIG_ENDIAN__) || defined (WORDS_BIGENDIAN) || \
+	defined (__mips__)      || defined (__sparc__)      ||  defined (BLARGG_CPU_POWERPC) || \
+	(defined (BIG_ENDIAN) && BIG_ENDIAN+0 != 4321)
+	#define BLARGG_BIG_ENDIAN 1
+#else
+	// No endian specified; assume little-endian, since it's most common
+	#define BLARGG_LITTLE_ENDIAN 1
+#endif
+#endif
+#endif
+
+#if defined (BLARGG_LITTLE_ENDIAN) && defined(BLARGG_BIG_ENDIAN)
+	#undef BLARGG_LITTLE_ENDIAN
+	#undef BLARGG_BIG_ENDIAN
+#endif
+
+static inline void blargg_verify_byte_order( void )
+{
+	#ifndef NDEBUG
+		#if BLARGG_BIG_ENDIAN
+			volatile int i = 1;
+			assert( *(volatile char*) &i == 0 );
+		#elif BLARGG_LITTLE_ENDIAN
+			volatile int i = 1;
+			assert( *(volatile char*) &i != 0 );
+		#endif
+	#endif
+}
+
+static inline unsigned get_le16( void const* p ) {
+	return  ((unsigned char const*) p) [1] * 0x100u +
+			((unsigned char const*) p) [0];
+}
+static inline unsigned get_be16( void const* p ) {
+	return  ((unsigned char const*) p) [0] * 0x100u +
+			((unsigned char const*) p) [1];
+}
+static inline blargg_ulong get_le32( void const* p ) {
+	return  ((unsigned char const*) p) [3] * 0x01000000u +
+			((unsigned char const*) p) [2] * 0x00010000u +
+			((unsigned char const*) p) [1] * 0x00000100u +
+			((unsigned char const*) p) [0];
+}
+static inline blargg_ulong get_be32( void const* p ) {
+	return  ((unsigned char const*) p) [0] * 0x01000000u +
+			((unsigned char const*) p) [1] * 0x00010000u +
+			((unsigned char const*) p) [2] * 0x00000100u +
+			((unsigned char const*) p) [3];
+}
+static inline void set_le16( void* p, unsigned n ) {
+	((unsigned char*) p) [1] = (unsigned char) (n >> 8);
+	((unsigned char*) p) [0] = (unsigned char) n;
+}
+static inline void set_be16( void* p, unsigned n ) {
+	((unsigned char*) p) [0] = (unsigned char) (n >> 8);
+	((unsigned char*) p) [1] = (unsigned char) n;
+}
+static inline void set_le32( void* p, blargg_ulong n ) {
+	((unsigned char*) p) [3] = (unsigned char) (n >> 24);
+	((unsigned char*) p) [2] = (unsigned char) (n >> 16);
+	((unsigned char*) p) [1] = (unsigned char) (n >> 8);
+	((unsigned char*) p) [0] = (unsigned char) n;
+}
+static inline void set_be32( void* p, blargg_ulong n ) {
+	((unsigned char*) p) [0] = (unsigned char) (n >> 24);
+	((unsigned char*) p) [1] = (unsigned char) (n >> 16);
+	((unsigned char*) p) [2] = (unsigned char) (n >> 8);
+	((unsigned char*) p) [3] = (unsigned char) n;
+}
+
+#if defined(BLARGG_NONPORTABLE)
+	// Optimized implementation if byte order is known
+	#if defined(BLARGG_LITTLE_ENDIAN)
+		#define GET_LE16( addr )        (*(BOOST::uint16_t*) (addr))
+		#define GET_LE32( addr )        (*(BOOST::uint32_t*) (addr))
+		#define SET_LE16( addr, data )  (void) (*(BOOST::uint16_t*) (addr) = (data))
+		#define SET_LE32( addr, data )  (void) (*(BOOST::uint32_t*) (addr) = (data))
+	#elif defined(BLARGG_BIG_ENDIAN)
+		#define GET_BE16( addr )        (*(BOOST::uint16_t*) (addr))
+		#define GET_BE32( addr )        (*(BOOST::uint32_t*) (addr))
+		#define SET_BE16( addr, data )  (void) (*(BOOST::uint16_t*) (addr) = (data))
+		#define SET_BE32( addr, data )  (void) (*(BOOST::uint32_t*) (addr) = (data))
+	#endif
+	
+	#if defined(BLARGG_CPU_POWERPC) && defined (__MWERKS__)
+		// PowerPC has special byte-reversed instructions
+		// to do: assumes that PowerPC is running in big-endian mode
+		// to do: implement for other compilers which don't support these macros
+		#define GET_LE16( addr )        (__lhbrx( (addr), 0 ))
+		#define GET_LE32( addr )        (__lwbrx( (addr), 0 ))
+		#define SET_LE16( addr, data )  (__sthbrx( (data), (addr), 0 ))
+		#define SET_LE32( addr, data )  (__stwbrx( (data), (addr), 0 ))
+	#endif
+#endif
+
+#ifndef GET_LE16
+	#define GET_LE16( addr )        get_le16( addr )
+	#define GET_LE32( addr )        get_le32( addr )
+	#define SET_LE16( addr, data )  set_le16( addr, data )
+	#define SET_LE32( addr, data )  set_le32( addr, data )
+#endif
+
+#ifndef GET_BE16
+	#define GET_BE16( addr )        get_be16( addr )
+	#define GET_BE32( addr )        get_be32( addr )
+	#define SET_BE16( addr, data )  set_be16( addr, data )
+	#define SET_BE32( addr, data )  set_be32( addr, data )
+#endif
+
+#endif
Index: apps/codecs/libgbs/gbs_emu.c
===================================================================
--- apps/codecs/libgbs/gbs_emu.c	(revision 0)
+++ apps/codecs/libgbs/gbs_emu.c	(revision 0)
@@ -0,0 +1,628 @@
+// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
+
+#include "gbs_emu.h"
+
+#include "blargg_endian.h"
+#include "blargg_source.h"
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+
+const char gme_wrong_file_type [] ICONST_ATTR = "Wrong file type for this emulator";
+extern gb_addr_t rst_base;
+
+int const stereo = 2; // number of channels for stereo
+int const silence_max = 6; // seconds
+int const silence_threshold = 0x10;
+long const fade_block_size = 512;
+int const fade_shift = 8; // fade ends with gain at 1.0 / (1 << fade_shift)
+
+void clear_track_vars( struct Gbs_Emu* this )
+{
+	this->current_track_   = -1;
+	this->out_time         = 0;
+	this->emu_time         = 0;
+	this->emu_track_ended_ = true;
+	this->track_ended     = true;
+	this->fade_start       = LONG_MAX / 2 + 1;
+	this->fade_step        = 1;
+	this->silence_time     = 0;
+	this->silence_count    = 0;
+	this->buf_remain       = 0;
+}
+
+void Gbs_init( struct Gbs_Emu* this )
+{	
+	this->sample_rate_ = 0;
+	this->mute_mask_   = 0;
+	this->tempo_       = 1.0;
+	this->gain_        = 1.0;
+	
+	// defaults
+	this->max_initial_silence = 2;
+	this->silence_lookahead   = 3;
+	this->ignore_silence     = false;
+	
+	// Unload
+	clear_track_vars( this );
+
+	this->silence_lookahead = 6;
+	this->max_initial_silence = 21;
+	Sound_set_gain( this, 0.75 );
+	
+	Apu_init( &this->apu );
+	Cpu_init();
+	
+	// Clear m3u playlist
+	M3u_clear( &this->m3u );
+}
+
+static blargg_err_t check_gbs_header( void const* header )
+{
+	if ( memcmp( header, "GBS", 3 ) )
+		return gme_wrong_file_type;
+	return 0;
+}
+
+// Setup
+
+blargg_err_t Gbs_load( struct Gbs_Emu* this, void* data, long size )
+{
+	// Unload
+	this->voice_count_ = 0;
+	clear_track_vars( this );
+
+	assert( offsetof (struct header_t,copyright [32]) == header_size );
+	RETURN_ERR( Rom_load( &this->rom, data, size, header_size, &this->header, 0 ) );
+	
+	RETURN_ERR( check_gbs_header( &this->header ) );
+	
+	/* Ignore warnings? */
+	/*if ( header_.vers != 1 )
+		warning( "Unknown file version" );
+	
+	if ( header_.timer_mode & 0x78 )
+		warning( "Invalid timer mode" ); */
+	
+	/* unsigned load_addr = get_le16( this->header.load_addr ); */
+	/* if ( (header_.load_addr [1] | header_.init_addr [1] | header_.play_addr [1]) > 0x7F ||
+			load_addr < 0x400 )
+		warning( "Invalid load/init/play address" ); */
+	
+	this->voice_count_ = osc_count;
+	Apu_volume( &this->apu, this->gain_ );
+	
+	// Change clock rate & setup buffer
+	this->clock_rate_ = 4194304;
+	Buffer_clock_rate( &this->buf, 4194304 );
+	this->buf_changed_count = Buffer_channels_changed_count( &this->buf );
+	
+	// Post load
+	Sound_set_tempo( this, this->tempo_ );
+	
+	// Remute voices
+	Sound_mute_voices( this, this->mute_mask_ );
+	
+	// Reset track count
+	this->track_count = this->header.track_count;
+	
+	// Clear m3u playlist
+	M3u_clear( &this->m3u );
+	return 0;
+}
+
+// Emulation
+
+// see gb_cpu_io.h for read/write functions
+
+void Set_bank( struct Gbs_Emu* this, int n )
+{
+	blargg_long addr = mask_addr( n * (blargg_long) bank_size, this->rom.mask );
+	if ( addr == 0 && this->rom.size > bank_size )
+	{
+		// TODO: what is the correct behavior? Current Game & Watch Gallery
+		// rip requires that this have no effect or set to bank 1.
+		//dprintf( "Selected ROM bank 0\n" );
+		return;
+		//n = 1;
+	}
+	Cpu_map_code( bank_size, bank_size, Rom_at_addr( &this->rom, addr ) );
+}
+
+void Update_timer( struct Gbs_Emu* this )
+{
+	if ( this->header.timer_mode & 0x04 )
+	{
+		static byte const rates [4] = { 10, 4, 6, 8 };
+		int shift = rates [this->ram [hi_page + 7] & 3] - (this->header.timer_mode >> 7);
+		this->play_period = (256L - this->ram [hi_page + 6]) << shift;
+	}
+	else
+	{
+		this->play_period = 70224; // 59.73 Hz
+	}
+	if ( this->tempo_ != 1.0 )
+		this->play_period = (blip_time_t) (this->play_period / this->tempo_);
+}
+
+static uint8_t const sound_data [register_count] ICONST_ATTR = {
+	0x80, 0xBF, 0x00, 0x00, 0xBF, // square 1
+	0x00, 0x3F, 0x00, 0x00, 0xBF, // square 2
+	0x7F, 0xFF, 0x9F, 0x00, 0xBF, // wave
+	0x00, 0xFF, 0x00, 0x00, 0xBF, // noise
+	0x77, 0xF3, 0xF1, // vin/volume, status, power mode
+	0, 0, 0, 0, 0, 0, 0, 0, 0, // unused
+	0xAC, 0xDD, 0xDA, 0x48, 0x36, 0x02, 0xCF, 0x16, // waveform data
+	0x2C, 0x04, 0xE5, 0x2C, 0xAC, 0xDD, 0xDA, 0x48
+};
+
+void Cpu_jsr( struct Gbs_Emu* this, gb_addr_t addr )
+{
+	check( this->r.sp == get_le16( this->header_.stack_ptr ) );
+	this->r.pc = addr;
+	Cpu_write( this, --this->r.sp, idle_addr >> 8 );
+	Cpu_write( this, --this->r.sp, idle_addr&0xFF );
+}
+
+blargg_err_t Emu_run_clocks( struct Gbs_Emu* this, blip_time_t* duration )
+{
+	this->cpu_time = 0;
+	while ( this->cpu_time < *duration )
+	{
+		long count = *duration - this->cpu_time;
+		this->cpu_time = *duration;
+		bool result = Cpu_run( this, count );
+		this->cpu_time -= Cpu_remain();
+		
+		if ( result )
+		{
+			if ( this->r.pc == idle_addr )
+			{
+				if ( this->next_play > *duration )
+				{
+					this->cpu_time = *duration;
+					break;
+				}
+				
+				if ( this->cpu_time < this->next_play )
+					this->cpu_time = this->next_play;
+				this->next_play += this->play_period;
+				Cpu_jsr( this, get_le16( this->header.play_addr ) );
+				GME_FRAME_HOOK( this );
+				// TODO: handle timer rates different than 60 Hz
+			}
+			else if ( this->r.pc > 0xFFFF )
+			{
+				dprintf( "PC wrapped around\n" );
+				this->r.pc &= 0xFFFF;
+			}
+			else
+			{
+				/* warning( "Emulation error (illegal/unsupported instruction)" ); */
+				dprintf( "Bad opcode $%.2x at $%.4x\n",
+						(int) *Cpu_get_code( this->r.pc ), (int) this->r.pc );
+				this->r.pc = (this->r.pc + 1) & 0xFFFF;
+				this->cpu_time += 6;
+			}
+		}
+	}
+	
+	*duration = this->cpu_time;
+	this->next_play -= this->cpu_time;
+	if ( this->next_play < 0 ) // could go negative if routine is taking too long to return
+		this->next_play = 0;
+	Apu_end_frame( &this->apu, this->cpu_time );
+	
+	return 0;
+}
+
+blargg_err_t play_( struct Gbs_Emu* this, long count, sample_t* out )
+{
+	long remain = count;
+	while ( remain )
+	{
+		remain -= Buffer_read_samples( &this->buf, &out [count - remain], remain );
+		if ( remain )
+		{
+			if ( this->buf_changed_count != Buffer_channels_changed_count( &this->buf ) )
+			{
+				this->buf_changed_count = Buffer_channels_changed_count( &this->buf );
+				
+				// Remute voices
+				Sound_mute_voices( this, this->mute_mask_ );
+			}
+			int msec = Buffer_length( &this->buf );
+			blip_time_t clocks_emulated = (blargg_long) msec * this->clock_rate_ / 1000;
+			RETURN_ERR( Emu_run_clocks( this, &clocks_emulated ) );
+			assert( clocks_emulated );
+			Buffer_end_frame( &this->buf, clocks_emulated );
+		}
+	}
+	return 0;
+}
+
+blargg_err_t Gbs_set_sample_rate( struct Gbs_Emu* this, long rate )
+{
+	require( !this->sample_rate_ ); // sample rate can't be changed once set
+	Buffer_init( &this->buf );
+	RETURN_ERR( Buffer_set_sample_rate( &this->buf, rate, 1000 / 20 ) );
+	
+	// Set bass frequency
+	Buffer_bass_freq( &this->buf, 300 );
+	
+	this->sample_rate_ = rate;
+	return 0;
+}
+
+
+// Sound
+
+void Sound_mute_voice( struct Gbs_Emu* this, int index, bool mute )
+{
+	require( (unsigned) index < (unsigned) this->voice_count_ );
+	int bit = 1 << index;
+	int mask = this->mute_mask_ | bit;
+	if ( !mute )
+		mask ^= bit;
+	Sound_mute_voices( this, mask );
+}
+
+void Sound_mute_voices( struct Gbs_Emu* this, int mask )
+{
+	require( this->sample_rate_ ); // sample rate must be set first
+	this->mute_mask_ = mask;
+	
+	int i;
+	for ( i = this->voice_count_; i--; )
+	{
+		if ( mask & (1 << i) )
+		{
+			Apu_osc_output( &this->apu, i, 0, 0, 0 );
+		}
+		else
+		{
+			struct channel_t ch = Buffer_channel( &this->buf );
+			assert( (ch.center && ch.left && ch.right) ||
+					(!ch.center && !ch.left && !ch.right) ); // all or nothing
+			Apu_osc_output( &this->apu, i, ch.center, ch.left, ch.right );
+		}
+	}
+}
+
+void Sound_set_tempo( struct Gbs_Emu* this, double t )
+{
+	require( this->sample_rate_ ); // sample rate must be set first
+	double const min = 0.02;
+	double const max = 4.00;
+	if ( t < min ) t = min;
+	if ( t > max ) t = max;
+	this->tempo_ = t;
+		
+	Apu_set_tempo( &this->apu, t );
+	Update_timer( this );
+}
+
+void fill_buf( struct Gbs_Emu* this );
+blargg_err_t Gbs_start_track( struct Gbs_Emu* this, int track )
+{
+	clear_track_vars( this );
+	
+	// Remap track if playlist available
+	if ( this->m3u.size > 0 ) {
+		struct entry_t* e = &this->m3u.entries[track];
+		if ( e->track >= 0 )
+		{
+			track = e->track;
+			/* TODO: Is this necessary?*/
+			/* if ( !(this_->m3u.type_->flags_ & 0x02) )
+				track -= e.decimal_track; */
+		}
+		
+		if ( track >= this->header.track_count )
+			track = 0;
+	}
+	
+	this->current_track_ = track;
+	
+	Buffer_clear( &this->buf );
+	
+	memset( this->ram, 0, 0x4000 );
+	memset( this->ram + 0x4000, 0xFF, 0x1F80 );
+	memset( this->ram + 0x5F80, 0, sizeof this->ram - 0x5F80 );
+	this->ram [hi_page] = 0; // joypad reads back as 0
+	
+	Apu_reset( &this->apu );
+	int i;
+	for ( i = 0; i < (int) sizeof sound_data; i++ )
+		Apu_write_register( &this->apu, 0, i + start_addr, sound_data [i] );
+	
+	Cpu_reset( this->rom.unmapped );
+	memset( &this->r, 0, sizeof this->r );
+	
+	unsigned load_addr = get_le16( this->header.load_addr );
+	rst_base = load_addr;
+	Rom_set_addr( &this->rom, load_addr );
+	
+	Cpu_map_code( ram_addr, 0x10000 - ram_addr, this->ram );
+	Cpu_map_code( 0, bank_size, Rom_at_addr( &this->rom, 0 ) );
+	Set_bank( this, this->rom.size > bank_size );
+	
+	this->ram [hi_page + 6] = this->header.timer_modulo;
+	this->ram [hi_page + 7] = this->header.timer_mode;
+	Update_timer( this );
+	this->next_play = this->play_period;
+	
+	this->r.rg.a  = track;
+	this->r.pc = idle_addr;
+	this->r.sp = get_le16( this->header.stack_ptr );
+	this->cpu_time  = 0;
+	Cpu_jsr( this, get_le16( this->header.init_addr ) );
+	
+	this->emu_track_ended_ = false;
+	this->track_ended     = false;
+	
+	if ( !this->ignore_silence )
+	{
+		// play until non-silence or end of track
+		long end;
+		for ( end = this->max_initial_silence * stereo * this->sample_rate_; this->emu_time < end; )
+		{
+			fill_buf( this );
+			if ( this->buf_remain | (int) this->emu_track_ended_ )
+				break;
+		}
+		
+		this->emu_time      = this->buf_remain;
+		this->out_time      = 0;
+		this->silence_time  = 0;
+		this->silence_count = 0;
+	}
+	/* return track_ended() ? warning() : 0; */
+	return 0;
+}
+
+
+// Track
+
+blargg_long msec_to_samples( blargg_long msec, long sample_rate )
+{
+	blargg_long sec = msec / 1000;
+	msec -= sec * 1000;
+	return (sec * sample_rate + msec * sample_rate / 1000) * stereo;
+}
+
+long Track_tell( struct Gbs_Emu* this ) 
+{
+	blargg_long rate = this->sample_rate_ * stereo;
+	blargg_long sec = this->out_time / rate;
+	return sec * 1000 + (this->out_time - sec * rate) * 1000 / rate;
+}
+
+blargg_err_t Track_seek( struct Gbs_Emu* this, long msec )
+{
+	blargg_long time = msec_to_samples( msec, this->sample_rate_ );
+	if ( time < this->out_time )
+		RETURN_ERR( Gbs_start_track( this, this->current_track_ ) );
+	return Track_skip( this, time - this->out_time );
+}
+
+blargg_err_t skip_( struct Gbs_Emu* this, long count )
+{
+	// for long skip, mute sound
+	const long threshold = 30000;
+	if ( count > threshold )
+	{
+		int saved_mute = this->mute_mask_;
+		Sound_mute_voices( this, ~0 );
+		
+		while ( count > threshold / 2 && !this->emu_track_ended_ )
+		{
+			RETURN_ERR( play_( this, buf_size, this->buf_ ) );
+			count -= buf_size;
+		}
+		
+		Sound_mute_voices( this, saved_mute );
+	}
+	
+	while ( count && !this->emu_track_ended_ )
+	{
+		long n = buf_size;
+		if ( n > count )
+			n = count;
+		count -= n;
+		RETURN_ERR( play_( this, n, this->buf_ ) );
+	}
+	return 0;
+}
+
+blargg_err_t Track_skip( struct Gbs_Emu* this, long count )
+{
+	require( this->current_track_ >= 0 ); // start_track() must have been called already
+	this->out_time += count;
+	
+	// remove from silence and buf first
+	{
+		long n = min( count, this->silence_count );
+		this->silence_count -= n;
+		count -= n;
+		
+		n = min( count, this->buf_remain );
+		this->buf_remain -= n;
+		count -= n;
+	}
+		
+	if ( count && !this->emu_track_ended_ )
+	{
+		this->emu_time += count;
+		// End track if error
+		if ( skip_( this, count ) )
+			this->emu_track_ended_ = true;
+	}
+	
+	if ( !(this->silence_count | this->buf_remain) ) // caught up to emulator, so update track ended
+		this->track_ended |= this->emu_track_ended_;
+	
+	return 0;
+}
+
+// Fading
+
+void Track_set_fade( struct Gbs_Emu* this, long start_msec, long length_msec )
+{
+	this->fade_step = this->sample_rate_ * length_msec / (fade_block_size * fade_shift * 1000 / stereo);
+	this->fade_start = msec_to_samples( start_msec, this->sample_rate_ );
+}
+
+// unit / pow( 2.0, (double) x / step )
+static int int_log( blargg_long x, int step, int unit )
+{
+	int shift = x / step;
+	int fraction = (x - shift * step) * unit / step;
+	return ((unit - fraction) + (fraction >> 1)) >> shift;
+}
+
+void handle_fade( struct Gbs_Emu* this, long out_count, sample_t* out )
+{
+	int i;
+	for ( i = 0; i < out_count; i += fade_block_size )
+	{
+		int const shift = 14;
+		int const unit = 1 << shift;
+		int gain = int_log( (this->out_time + i - this->fade_start) / fade_block_size,
+				this->fade_step, unit );
+		if ( gain < (unit >> fade_shift) )
+			this->track_ended = this->emu_track_ended_ = true;
+		
+		sample_t* io = &out [i];
+		int count;
+		for ( count = min( fade_block_size, out_count - i ); count; --count )
+		{
+			*io = (sample_t) ((*io * gain) >> shift);
+			++io;
+		}
+	}
+}
+
+// Silence detection
+
+void emu_play( struct Gbs_Emu* this, long count, sample_t* out )
+{
+	check( current_track_ >= 0 );
+	this->emu_time += count;
+	if ( this->current_track_ >= 0 && !this->emu_track_ended_ ) {
+		// End track if error
+		if ( play_( this, count, out ) ) this->emu_track_ended_ = true; 
+	}
+	else
+		memset( out, 0, count * sizeof *out );
+}
+
+// number of consecutive silent samples at end
+static long count_silence( sample_t* begin, long size )
+{
+	sample_t first = *begin;
+	*begin = silence_threshold; // sentinel
+	sample_t* p = begin + size;
+	while ( (unsigned) (*--p + silence_threshold / 2) <= (unsigned) silence_threshold ) { }
+	*begin = first;
+	return size - (p - begin);
+}
+
+// fill internal buffer and check it for silence
+void fill_buf( struct Gbs_Emu* this )
+{
+	assert( !this->buf_remain );
+	if ( !this->emu_track_ended_ )
+	{
+		emu_play( this, buf_size, this->buf_ );
+		long silence = count_silence( this->buf_, buf_size );
+		if ( silence < buf_size )
+		{
+			this->silence_time = this->emu_time - silence;
+			this->buf_remain   = buf_size;
+			return;
+		}
+	}
+	this->silence_count += buf_size;
+}
+
+blargg_err_t Gbs_play( struct Gbs_Emu* this, long out_count, sample_t* out )
+{
+	if ( this->track_ended )
+	{
+		memset( out, 0, out_count * sizeof *out );
+	}
+	else
+	{
+		require( this->current_track_ >= 0 );
+		require( out_count % stereo == 0 );
+		
+		assert( this->emu_time >= this->out_time );
+		
+		// prints nifty graph of how far ahead we are when searching for silence
+		//dprintf( "%*s \n", int ((emu_time - out_time) * 7 / sample_rate()), "*" );
+		
+		long pos = 0;
+		if ( this->silence_count )
+		{
+			// during a run of silence, run emulator at >=2x speed so it gets ahead
+			long ahead_time = this->silence_lookahead * (this->out_time + out_count - this->silence_time) + this->silence_time;
+			while ( this->emu_time < ahead_time && !(this->buf_remain | this->emu_track_ended_) )
+				fill_buf( this );
+			
+			// fill with silence
+			pos = min( this->silence_count, out_count );
+			memset( out, 0, pos * sizeof *out );
+			this->silence_count -= pos;
+			
+			if ( this->emu_time - this->silence_time > silence_max * stereo * this->sample_rate_ )
+			{
+				this->track_ended  = this->emu_track_ended_ = true;
+				this->silence_count = 0;
+				this->buf_remain    = 0;
+			}
+		}
+		
+		if ( this->buf_remain )
+		{
+			// empty silence buf
+			long n = min( this->buf_remain, out_count - pos );
+			memcpy( &out [pos], this->buf_ + (buf_size - this->buf_remain), n * sizeof *out );
+			this->buf_remain -= n;
+			pos += n;
+		}
+		
+		// generate remaining samples normally
+		long remain = out_count - pos;
+		if ( remain )
+		{
+			emu_play( this, remain, out + pos );
+			this->track_ended |= this->emu_track_ended_;
+			
+			if ( !this->ignore_silence || this->out_time > this->fade_start )
+			{
+				// check end for a new run of silence
+				long silence = count_silence( out + pos, remain );
+				if ( silence < remain )
+					this->silence_time = this->emu_time - silence;
+				
+				if ( this->emu_time - this->silence_time >= buf_size )
+					fill_buf( this ); // cause silence detection on next play()
+			}
+		}
+		
+		if ( this->out_time > this->fade_start )
+			handle_fade( this, out_count, out );
+	}
+	this->out_time += out_count;
+	return 0;
+}
Index: apps/codecs/libgbs/multi_buffer.c
===================================================================
--- apps/codecs/libgbs/multi_buffer.c	(revision 0)
+++ apps/codecs/libgbs/multi_buffer.c	(revision 0)
@@ -0,0 +1,226 @@
+// Blip_Buffer 0.4.1. http://www.slack.net/~ant/
+
+#include "multi_buffer.h"
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this_ module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "blargg_source.h"
+
+#ifdef BLARGG_ENABLE_OPTIMIZER
+	#include BLARGG_ENABLE_OPTIMIZER
+#endif
+
+// Stereo_Buffer
+ 
+void Buffer_init( struct Stereo_Buffer* this_ )
+{
+	Blip_init( &this_->bufs [0] );
+	Blip_init( &this_->bufs [1] );
+	Blip_init( &this_->bufs [2] );
+			
+	this_->chan.center = &this_->bufs [0];
+	this_->chan.left = &this_->bufs [1];
+	this_->chan.right = &this_->bufs [2];
+	
+	this_->length_ = 0;
+	this_->sample_rate_ = 0;
+	this_->channels_changed_count_ = 1;
+	this_->samples_per_frame_ = 2;
+}
+
+blargg_err_t Buffer_set_sample_rate( struct Stereo_Buffer* this_, long rate, int msec )
+{
+	int i;
+	for ( i = 0; i < buf_count; i++ )
+		RETURN_ERR( Blip_set_sample_rate( &this_->bufs[i], rate, msec ) );
+		
+	this_->sample_rate_ = Blip_sample_rate( &this_->bufs [0] );
+	this_->length_ = Blip_length( &this_->bufs [0] );
+	return 0;
+}
+
+void Buffer_clock_rate( struct Stereo_Buffer* this_, long rate )
+{
+	int i;
+	for ( i = 0; i < buf_count; i++ )
+		Blip_set_clock_rate( &this_->bufs [i], rate );
+}
+
+void Buffer_bass_freq( struct Stereo_Buffer* this_, int bass )
+{
+	unsigned i;
+	for ( i = 0; i < buf_count; i++ )
+		Blip_bass_freq( &this_->bufs [i], bass );
+}
+
+struct channel_t Buffer_channel( struct Stereo_Buffer* this_ )
+{
+	return this_->chan;
+}
+
+void Buffer_clear( struct Stereo_Buffer* this_ )
+{
+	this_->stereo_added = 0;
+	this_->was_stereo   = false;
+	int i;
+	for ( i = 0; i < buf_count; i++ )
+		Blip_clear( &this_->bufs [i], 1 );
+}
+
+void Buffer_end_frame( struct Stereo_Buffer* this_, blip_time_t clock_count )
+{
+	this_->stereo_added = 0;
+	unsigned i;
+	for ( i = 0; i < buf_count; i++ )
+	{
+		this_->stereo_added |= Blip_clear_modified( &this_->bufs [i] ) << i;
+		Blip_end_frame( &this_->bufs [i], clock_count );
+	}
+}
+
+long Buffer_read_samples( struct Stereo_Buffer* this_, blip_sample_t* out, long count )
+{
+	require( !(count & 1) ); // count must be even
+	count = (unsigned) count / 2;
+	
+	long avail = Blip_samples_avail( &this_->bufs [0] );
+	if ( count > avail )
+		count = avail;
+	if ( count )
+	{
+		int bufs_used = this_->stereo_added | this_->was_stereo;
+		//dprintf( "%X\n", bufs_used );
+		if ( bufs_used <= 1 )
+		{
+			Buffer_mix_mono( this_, out, count );
+			Blip_remove_samples( &this_->bufs [0], count );
+			Blip_remove_silence( &this_->bufs [1], count );
+			Blip_remove_silence( &this_->bufs [2], count );
+		}
+		else if ( bufs_used & 1 )
+		{
+			Buffer_mix_stereo( this_, out, count );
+			Blip_remove_samples( &this_->bufs [0], count );
+			Blip_remove_samples( &this_->bufs [1], count );
+			Blip_remove_samples( &this_->bufs [2], count );
+		}
+		else
+		{
+			Buffer_mix_stereo_no_center( this_, out, count );
+			Blip_remove_silence( &this_->bufs [0], count );
+			Blip_remove_samples( &this_->bufs [1], count );
+			Blip_remove_samples( &this_->bufs [2], count );
+		}
+		
+		// to do: this_ might miss opportunities for optimization
+		if ( !Blip_samples_avail( &this_->bufs [0] ) )
+		{
+			this_->was_stereo   = this_->stereo_added;
+			this_->stereo_added = 0;
+		}
+	}
+	
+	return count * 2;
+}
+
+unsigned Buffer_channels_changed_count( struct Stereo_Buffer* this_ )
+{
+	return this_->channels_changed_count_;
+}
+
+void Buffer_channels_changed( struct Stereo_Buffer* this_ )
+{
+	this_->channels_changed_count_++;
+}
+
+void Buffer_mix_stereo( struct Stereo_Buffer* this_, blip_sample_t* out_, blargg_long count )
+{
+	blip_sample_t* BLIP_RESTRICT out = out_;
+	int const bass = BLIP_READER_BASS( this_->bufs [1] );
+	BLIP_READER_BEGIN( left, this_->bufs [1] );
+	BLIP_READER_BEGIN( right, this_->bufs [2] );
+	BLIP_READER_BEGIN( center, this_->bufs [0] );
+	
+	for ( ; count; --count )
+	{
+		int c = BLIP_READER_READ( center );
+		blargg_long l = c + BLIP_READER_READ( left );
+		blargg_long r = c + BLIP_READER_READ( right );
+		if ( (int16_t) l != l )
+			l = 0x7FFF - (l >> 24);
+		
+		BLIP_READER_NEXT( center, bass );
+		if ( (int16_t) r != r )
+			r = 0x7FFF - (r >> 24);
+		
+		BLIP_READER_NEXT( left, bass );
+		BLIP_READER_NEXT( right, bass );
+		
+		out [0] = l;
+		out [1] = r;
+		out += 2;
+	}
+	
+	BLIP_READER_END( center, this_->bufs [0] );
+	BLIP_READER_END( right, this_->bufs [2] );
+	BLIP_READER_END( left, this_->bufs [1] );
+}
+
+void Buffer_mix_stereo_no_center( struct Stereo_Buffer* this_, blip_sample_t* out_, blargg_long count )
+{
+	blip_sample_t* BLIP_RESTRICT out = out_;
+	int const bass = BLIP_READER_BASS( this_->bufs [1] );
+	BLIP_READER_BEGIN( left, this_->bufs [1] );
+	BLIP_READER_BEGIN( right, this_->bufs [2] );
+	
+	for ( ; count; --count )
+	{
+		blargg_long l = BLIP_READER_READ( left );
+		if ( (int16_t) l != l )
+			l = 0x7FFF - (l >> 24);
+		
+		blargg_long r = BLIP_READER_READ( right );
+		if ( (int16_t) r != r )
+			r = 0x7FFF - (r >> 24);
+		
+		BLIP_READER_NEXT( left, bass );
+		BLIP_READER_NEXT( right, bass );
+		
+		out [0] = l;
+		out [1] = r;
+		out += 2;
+	}
+	
+	BLIP_READER_END( right, this_->bufs [2] );
+	BLIP_READER_END( left, this_->bufs [1] );
+}
+
+void Buffer_mix_mono( struct Stereo_Buffer* this_, blip_sample_t* out_, blargg_long count )
+{
+	blip_sample_t* BLIP_RESTRICT out = out_;
+	int const bass = BLIP_READER_BASS( this_->bufs [0] );
+	BLIP_READER_BEGIN( center, this_->bufs [0] );
+	
+	for ( ; count; --count )
+	{
+		blargg_long s = BLIP_READER_READ( center );
+		if ( (int16_t) s != s )
+			s = 0x7FFF - (s >> 24);
+		
+		BLIP_READER_NEXT( center, bass );
+		out [0] = s;
+		out [1] = s;
+		out += 2;
+	}
+	
+	BLIP_READER_END( center, this_->bufs [0] );
+}
Index: apps/codecs/libgbs/blargg_source.h
===================================================================
--- apps/codecs/libgbs/blargg_source.h	(revision 0)
+++ apps/codecs/libgbs/blargg_source.h	(revision 0)
@@ -0,0 +1,66 @@
+// Included at the beginning of library source files, after all other #include lines
+#ifndef BLARGG_SOURCE_H
+#define BLARGG_SOURCE_H
+
+// If debugging is enabled, abort program if expr is false. Meant for checking
+// internal state and consistency. A failed assertion indicates a bug in the module.
+// void assert( bool expr );
+#include <assert.h>
+
+// If debugging is enabled and expr is false, abort program. Meant for checking
+// caller-supplied parameters and operations that are outside the control of the
+// module. A failed requirement indicates a bug outside the module.
+// void require( bool expr );
+#undef require
+#define require( expr ) assert( expr )
+
+// Like printf() except output goes to debug log file. Might be defined to do
+// nothing (not even evaluate its arguments).
+// void dprintf( const char* format, ... );
+#if defined(ROCKBOX)
+#define dprintf DEBUGF
+#else
+static inline void blargg_dprintf_( const char* fmt, ... ) { }
+#undef dprintf
+#define dprintf (1) ? (void) 0 : blargg_dprintf_
+#endif
+
+// If enabled, evaluate expr and if false, make debug log entry with source file
+// and line. Meant for finding situations that should be examined further, but that
+// don't indicate a problem. In all cases, execution continues normally.
+#undef check
+#define check( expr ) ((void) 0)
+
+// If expr yields error string, return it from current function, otherwise continue.
+#undef RETURN_ERR
+#define RETURN_ERR( expr ) do {                         \
+		blargg_err_t blargg_return_err_ = (expr);               \
+		if ( blargg_return_err_ ) return blargg_return_err_;    \
+	} while ( 0 )
+
+// If ptr is 0, return out of memory error string.
+#undef CHECK_ALLOC
+#define CHECK_ALLOC( ptr ) do { if ( (ptr) == 0 ) return "Out of memory"; } while ( 0 )
+
+#ifndef max
+	#define max(a,b) (((a) > (b)) ? (a) : (b))
+#endif
+#ifndef min
+	#define min(a,b) (((a) < (b)) ? (a) : (b))
+#endif
+
+// TODO: good idea? bad idea?
+#undef byte
+#define byte byte_
+typedef unsigned char byte;
+
+// deprecated
+#define BLARGG_CHECK_ALLOC CHECK_ALLOC
+#define BLARGG_RETURN_ERR RETURN_ERR
+
+// BLARGG_SOURCE_BEGIN: If defined, #included, allowing redefition of dprintf and check
+#ifdef BLARGG_SOURCE_BEGIN
+	#include BLARGG_SOURCE_BEGIN
+#endif
+
+#endif
Index: apps/codecs/libgbs/blip_buffer.h
===================================================================
--- apps/codecs/libgbs/blip_buffer.h	(revision 0)
+++ apps/codecs/libgbs/blip_buffer.h	(revision 0)
@@ -0,0 +1,270 @@
+// Band-limited sound synthesis buffer
+
+// Blip_Buffer 0.4.1
+#ifndef BLIP_BUFFER_H
+#define BLIP_BUFFER_H
+
+#include <assert.h>
+
+	// internal
+	#include "blargg_common.h"
+	#if INT_MAX >= 0x7FFFFFFF
+		typedef int blip_long;
+		typedef unsigned blip_ulong;
+	#else
+		typedef long blip_long;
+		typedef unsigned long blip_ulong;
+	#endif
+
+// Time unit at source clock rate
+typedef blip_long blip_time_t;
+
+// Number of bits in resample ratio fraction. Higher values give a more accurate ratio
+// but reduce maximum buffer size.
+#ifndef BLIP_BUFFER_ACCURACY
+	#define BLIP_BUFFER_ACCURACY 16
+#endif
+
+// Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in
+// noticeable broadband noise when synthesizing high frequency square waves.
+// Affects size of Blip_Synth objects since they store the waveform directly.
+#ifndef BLIP_PHASE_BITS
+	#define BLIP_PHASE_BITS 8
+#endif
+
+// Output samples are 16-bit signed, with a range of -32768 to 32767
+typedef short blip_sample_t;
+enum { blip_sample_max = 32767 };
+enum { blip_widest_impulse_ = 16 };
+enum { blip_buffer_extra_ = blip_widest_impulse_ + 2 };
+enum { blip_res = 1 << BLIP_PHASE_BITS };
+enum { blip_max_length = 0 };
+enum { blip_default_length = 250 };
+
+// Maximun buffer size (48Khz, 50 ms)
+enum { blip_buffer_max = 2466 };
+enum { blip_sample_bits = 30 };
+
+typedef blip_time_t buf_t_;
+/* typedef const char* blargg_err_t; */
+typedef blip_ulong blip_resampled_time_t;
+
+struct Blip_Buffer {
+	blip_ulong factor_;
+	blip_resampled_time_t offset_;
+	buf_t_ buffer_ [blip_buffer_max];
+	blip_long buffer_size_;
+	blip_long reader_accum_;
+	int bass_shift_;
+
+	long sample_rate_;
+	long clock_rate_;
+	int bass_freq_;
+	int length_;
+	int modified_;
+};
+
+// not documented yet
+void Blip_set_modified( struct Blip_Buffer* this_ ); ICODE_ATTR
+int Blip_clear_modified( struct Blip_Buffer* this__ ); ICODE_ATTR
+void Blip_remove_silence( struct Blip_Buffer* this__, long count ); ICODE_ATTR
+blip_resampled_time_t Blip_resampled_duration( struct Blip_Buffer* this__, int t ); ICODE_ATTR
+blip_resampled_time_t Blip_resampled_time( struct Blip_Buffer* this__, blip_time_t t ); ICODE_ATTR
+blip_resampled_time_t Blip_clock_rate_factor( struct Blip_Buffer* this__, long clock_rate ); ICODE_ATTR
+
+// Initializes Blip_Buffer structure
+void Blip_init( struct Blip_Buffer* this__ );
+
+// Stops (clear) Blip_Buffer structure
+void Blip_stop( struct Blip_Buffer* this__ );
+
+// Set output sample rate and buffer length in milliseconds (1/1000 sec, defaults
+// to 1/4 second), then clear buffer. Returns NULL on success, otherwise if there
+// isn't enough memory, returns error without affecting current buffer setup.
+blargg_err_t Blip_set_sample_rate( struct Blip_Buffer* this__, long samples_per_sec, int msec_length );
+
+// Set number of source time units per second
+static inline void Blip_set_clock_rate( struct Blip_Buffer* this__, long cps )
+{
+	this__->factor_ = Blip_clock_rate_factor( this__, this__->clock_rate_ = cps );
+}
+
+// End current time frame of specified duration and make its samples available
+// (along with any still-unread samples) for reading with read_samples(). Begins
+// a new time frame at the end of the current frame.
+void Blip_end_frame( struct Blip_Buffer* this__, blip_time_t time ); ICODE_ATTR
+
+// Read at most 'max_samples' out of buffer into 'dest', removing them from from
+// the buffer. Returns number of samples actually read and removed. If stereo is
+// true, increments 'dest' one extra time after writing each sample, to allow
+// easy interleving of two channels into a stereo output buffer.
+long Blip_read_samples( struct Blip_Buffer* this__, blip_sample_t* dest, long max_samples, int stereo ); ICODE_ATTR
+
+// Additional optional features
+
+// Current output sample rate
+static inline long Blip_sample_rate( struct Blip_Buffer* this__ )
+{
+	return this__->sample_rate_;
+}
+
+// Length of buffer, in milliseconds
+static inline int  Blip_length( struct Blip_Buffer* this__ )
+{
+	return this__->length_;
+}
+
+// Number of source time units per second
+static inline long Blip_clock_rate( struct Blip_Buffer* this__ )
+{
+	return this__->clock_rate_;
+}
+
+
+// Set frequency high-pass filter frequency, where higher values reduce bass more
+void Blip_bass_freq( struct Blip_Buffer* this__, int frequency );
+
+// Number of samples delay from synthesis to samples read out
+static inline int  Blip_output_latency( void )
+{ 
+	return blip_widest_impulse_ / 2;
+}
+
+// Remove all available samples and clear buffer to silence. If 'entire_buffer' is
+// false, just clears out any samples waiting rather than the entire buffer.
+void Blip_clear( struct Blip_Buffer* this__, int entire_buffer );
+
+// Number of samples available for reading with read_samples()
+static inline long Blip_samples_avail( struct Blip_Buffer* this__ )
+{ 
+	return (long) (this__->offset_ >> BLIP_BUFFER_ACCURACY);
+}
+
+// Remove 'count' samples from those waiting to be read
+void Blip_remove_samples( struct Blip_Buffer* this__, long count ); ICODE_ATTR
+
+// Experimental features
+
+// Count number of clocks needed until 'count' samples will be available.
+// If buffer can't even hold 'count' samples, returns number of clocks until
+// buffer becomes full.
+blip_time_t Blip_count_clocks( struct Blip_Buffer* this__, long count ); ICODE_ATTR
+
+// Number of raw samples that can be mixed within frame of specified duration.
+long Blip_count_samples( struct Blip_Buffer* this__, blip_time_t duration ); ICODE_ATTR
+
+// Mix 'count' samples from 'buf' into buffer.
+void Blip_mix_samples( struct Blip_Buffer* this__, blip_sample_t const* buf, long count ); ICODE_ATTR
+
+// Range specifies the greatest expected change in amplitude. Calculate it
+// by finding the difference between the maximum and minimum expected
+// amplitudes (max - min).
+
+struct Blip_Synth_ {
+	struct Blip_Buffer* buf;
+	int last_amp;
+	int delta_factor;
+};
+
+// Initializes Blip_Synth structure
+void Synth_init( struct Blip_Synth_* this__ );
+
+// Set overall volume of waveform
+void Synth_volume( struct Blip_Synth_* this__, double v ); ICODE_ATTR
+/* In case we want to port the Nes emu */
+/* void Synth_volume( struct Blip_Synth_ *this__, double v, int range ); */
+
+// Get/set Blip_Buffer used for output
+const struct Blip_Buffer* Synth_output( struct Blip_Synth_* this__ ); ICODE_ATTR
+
+// Low-level interface
+
+	#if defined (__GNUC__) || _MSC_VER >= 1100
+		#define BLIP_RESTRICT __restrict
+	#else
+		#define BLIP_RESTRICT
+	#endif
+
+// Works directly in terms of fractional output samples. Contact author for more info.
+static inline void Synth_offset_resampled( struct Blip_Synth_* this__, blip_resampled_time_t time,
+	int delta, struct Blip_Buffer* blip_buf )
+{
+	// Fails if time is beyond end of Blip_Buffer, due to a bug in caller code or the
+	// need for a longer buffer as set by set_sample_rate().
+	assert( (blip_long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ );
+	delta *= this__->delta_factor;
+	blip_long* BLIP_RESTRICT buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY);
+	int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1));
+
+	blip_long left = buf [0] + delta;
+
+	// Kind of crappy, but doing shift after multiply results in overflow.
+	// Alternate way of delaying multiply by delta_factor results in worse
+	// sub-sample resolution.
+	blip_long right = (delta >> BLIP_PHASE_BITS) * phase;
+	left  -= right;
+	right += buf [1];
+
+	buf [0] = left;
+	buf [1] = right;
+}
+
+// Update amplitude of waveform at given time. Using this__ requires a separate
+// Blip_Synth for each waveform.
+static inline void Synth_update( struct Blip_Synth_* this__, blip_time_t t, int amp )
+{
+	int delta = amp - this__->last_amp;
+	this__->last_amp = amp;
+	Synth_offset_resampled( this__, t * this__->buf->factor_ + this__->buf->offset_, delta, this__->buf );
+}
+
+// Add an amplitude transition of specified delta, optionally into specified buffer
+// rather than the one set with output(). Delta can be positive or negative.
+// The actual change in amplitude is delta * (volume / range)
+static inline void Synth_offset( struct Blip_Synth_* this__, blip_time_t t, int delta, struct Blip_Buffer* buf )
+{
+	Synth_offset_resampled( this__, t * buf->factor_ + buf->offset_, delta, buf );
+}
+/* void Synth_offset( struct Blip_Synth_* this__, blip_time_t t, int delta ) {
+	Synth_offset( this__, t, delta, this__->buf ); } */
+
+// Same as offset(), except code is inlined for higher performance
+static inline void Synth_offset_inline( struct Blip_Synth_* this__, blip_time_t t, int delta, struct Blip_Buffer* buf )
+{
+	Synth_offset_resampled( this__, t * buf->factor_ + buf->offset_, delta, buf );
+}
+/* void offset_inline( struct Blip_Synth_* this__, blip_time_t t, int delta ) const {
+	offset_resampled( t * this__->buf->factor_ + this__->buf->offset_, delta, this__->buf );
+} */
+
+
+// Optimized reading from Blip_Buffer, for use in custom sample output
+
+// Begin reading from buffer. Name should be unique to the current block.
+#define BLIP_READER_BEGIN( name, blip_buffer ) \
+	buf_t_* BLIP_RESTRICT name##_reader_buf = (blip_buffer).buffer_;\
+	blip_long name##_reader_accum = (blip_buffer).reader_accum_
+
+// Get value to pass to BLIP_READER_NEXT()
+#define BLIP_READER_BASS( blip_buffer ) ((blip_buffer).bass_shift_)
+
+// Constant value to use instead of BLIP_READER_BASS(), for slightly more optimal
+// code at the cost of having no bass control
+/* static int blip_reader_default_bass = 9; */
+
+// Current sample
+#define BLIP_READER_READ( name )        (name##_reader_accum >> (blip_sample_bits - 16))
+
+// Current raw sample in full internal resolution
+#define BLIP_READER_READ_RAW( name )    (name##_reader_accum)
+
+// Advance to next sample
+#define BLIP_READER_NEXT( name, bass ) \
+	(void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass)))
+
+// End reading samples from buffer. The number of samples read must now be removed
+// using Blip_remove_samples().
+#define BLIP_READER_END( name, blip_buffer ) \
+	(void) ((blip_buffer).reader_accum_ = name##_reader_accum)
+
+#endif
Index: apps/codecs/libgbs/gb_oscs.h
===================================================================
--- apps/codecs/libgbs/gb_oscs.h	(revision 0)
+++ apps/codecs/libgbs/gb_oscs.h	(revision 0)
@@ -0,0 +1,117 @@
+// Private oscillators used by Gb_Apu
+
+// Gb_Snd_Emu 0.1.5
+#ifndef GB_OSCS_H
+#define GB_OSCS_H
+
+#include "blargg_common.h"
+#include "blip_buffer.h"
+
+// Oscillator
+
+enum { trigger = 0x80 };
+enum { len_enabled_mask = 0x40 };
+
+struct Gb_Osc
+{	
+	struct Blip_Buffer* outputs [4]; // NULL, right, left, center
+	struct Blip_Buffer* output;
+	int output_select;
+	uint8_t* regs; // osc's 5 registers
+	
+	int delay;
+	int last_amp;
+	int volume;
+	int length;
+	int enabled;
+};
+
+void Osc_reset( struct Gb_Osc* this );
+void Osc_clock_length( struct Gb_Osc* this ); ICODE_ATTR
+static inline int Osc_frequency( struct Gb_Osc* this )
+{
+	return (this->regs [4] & 7) * 0x100 + this->regs [3];
+}
+
+// Square
+
+typedef struct Blip_Synth_ Synth;
+enum { period_mask = 0x70 };
+enum { shift_mask  = 0x07 };
+
+struct Gb_Square
+{	
+    Synth* synth;
+	struct Gb_Osc osc;
+	int sweep_delay;
+	int sweep_freq;
+	int phase;
+	int env_delay;
+};
+
+void Square_reset( struct Gb_Square* this );
+void Square_clock_sweep( struct Gb_Square* this ); ICODE_ATTR
+void Square_clock_envelope( struct Gb_Square* this ); ICODE_ATTR
+bool Square_write_register( struct Gb_Square* this, int, int ); ICODE_ATTR
+void Square_run( struct Gb_Square* this, blip_time_t, blip_time_t, int playing ); ICODE_ATTR
+
+// Noise
+
+struct Gb_Noise
+{
+	Synth* synth;
+	struct Gb_Osc osc;
+	unsigned bits;
+	int env_delay;
+};
+
+void Noise_reset( struct Gb_Noise* this );
+void Noise_clock_envelope( struct Gb_Noise* this ); ICODE_ATTR
+bool Noise_write_register( struct Gb_Noise* this, int, int ); ICODE_ATTR
+void Noise_run( struct Gb_Noise* this, blip_time_t, blip_time_t, int playing ); ICODE_ATTR
+
+// Wave
+
+enum { wave_size = 32 };
+
+struct Gb_Wave
+{
+	Synth* synth;
+	struct Gb_Osc osc;
+	int wave_pos;
+	uint8_t wave [wave_size];
+};
+
+void Wave_reset( struct Gb_Wave* this );
+void Wave_run( struct Gb_Wave* this, blip_time_t, blip_time_t, int playing ); ICODE_ATTR
+
+static inline void Wave_write_register( struct Gb_Wave* this, int reg, int data )
+{
+	switch ( reg )
+	{
+	case 0:
+		if ( !(data & 0x80) )
+			this->osc.enabled = false;
+		break;
+	
+	case 1:
+		this->osc.length = 256 - this->osc.regs [1];
+		break;
+	
+	case 2:
+		this->osc.volume = data >> 5 & 3;
+		break;
+	
+	case 4:
+		if ( data & trigger & this->osc.regs [0] )
+		{
+			this->wave_pos = 0;
+			this->osc.enabled = true;
+			if ( this->osc.length == 0 )
+				this->osc.length = 256;
+		}
+	}
+}
+
+
+#endif
Index: apps/codecs/libgbs/m3u_playlist.c
===================================================================
--- apps/codecs/libgbs/m3u_playlist.c	(revision 0)
+++ apps/codecs/libgbs/m3u_playlist.c	(revision 0)
@@ -0,0 +1,420 @@
+// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
+
+#include "m3u_playlist.h"
+#include "gbs_emu.h"
+
+#include <string.h>
+#if defined (ROCKBOX)
+#include <codecs/lib/codeclib.h>
+#else
+#include <fcntl.h>
+#endif
+
+/* Copyright (C) 2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "blargg_source.h"
+
+static char* skip_white( char* in )
+{
+	while ( *in == ' ' || *in == '\t' )
+		in++;
+	return in;
+}
+
+inline unsigned from_dec( unsigned n ) { return n - '0'; }
+
+static char* parse_filename( char* in, struct entry_t* entry )
+{
+	entry->file = in;
+	entry->type = "";
+	char* out = in;
+	while ( 1 )
+	{
+		int c = *in;
+		if ( !c ) break;
+		in++;
+		
+		if ( c == ',' ) // commas in filename
+		{
+			char* p = skip_white( in );
+			if ( *p == '$' || from_dec( *p ) <= 9 )
+			{
+				in = p;
+				break;
+			}
+		}
+		
+		if ( c == ':' && in [0] == ':' && in [1] && in [2] != ',' ) // ::type suffix
+		{
+			entry->type = ++in;
+			while ( (c = *in) != 0 && c != ',' )
+				in++;
+			if ( c == ',' )
+			{
+				*in++ = 0; // terminate type
+				in = skip_white( in );
+			}
+			break;
+		}
+		
+		if ( c == '\\' ) // \ prefix for special characters
+		{
+			c = *in;
+			if ( !c ) break;
+			in++;
+		}
+		*out++ = (char) c;
+	}
+	*out = 0; // terminate string
+	return in;
+}
+
+static char* next_field( char* in, int* result )
+{
+	while ( 1 )
+	{
+		in = skip_white( in );
+		
+		if ( !*in )
+			break;
+		
+		if ( *in == ',' )
+		{
+			in++;
+			break;
+		}
+		
+		*result = 1;
+		in++;
+	}
+	return skip_white( in );
+}
+
+static char* parse_int_( char* in, int* out )
+{
+	int n = 0;
+	while ( 1 )
+	{
+		unsigned d = from_dec( *in );
+		if ( d > 9 )
+			break;
+		in++;
+		n = n * 10 + d;
+		*out = n;
+	}
+	return in;
+}
+
+static char* parse_int( char* in, int* out, int* result )
+{
+	return next_field( parse_int_( in, out ), result );
+}
+
+// Returns 16 or greater if not hex
+inline int from_hex_char( int h )
+{
+	h -= 0x30;
+	if ( (unsigned) h > 9 )
+		h = ((h - 0x11) & 0xDF) + 10;
+	return h;
+}
+
+static char* parse_track( char* in, struct entry_t* entry, int* result )
+{
+	if ( *in == '$' )
+	{
+		in++;
+		int n = 0;
+		while ( 1 )
+		{
+			int h = from_hex_char( *in );
+			if ( h > 15 )
+				break;
+			in++;
+			n = n * 16 + h;
+			entry->track = n;
+		}
+	}
+	else
+	{
+		in = parse_int_( in, &entry->track );
+		if ( entry->track >= 0 )
+			entry->decimal_track = 1;
+	}
+	return next_field( in, result );
+}
+
+static char* parse_time_( char* in, int* out )
+{
+	*out = -1;
+	int n = -1;
+	in = parse_int_( in, &n );
+	if ( n >= 0 )
+	{
+		*out = n;
+		while ( *in == ':' )
+		{
+			n = -1;
+			in = parse_int_( in + 1, &n );
+			if ( n >= 0 )
+				*out = *out * 60 + n;
+		}
+		*out *= 1000;
+		if ( *in == '.' )
+		{
+			n = -1;
+			in = parse_int_( in + 1, &n );
+			if ( n >= 0 )
+				*out = *out + n; 
+		}
+	}
+	return in;
+}
+
+static char* parse_time( char* in, int* out, int* result )
+{
+	return next_field( parse_time_( in, out ), result );
+}
+
+static char* parse_name( char* in )
+{
+	char* out = in;
+	while ( 1 )
+	{
+		int c = *in;
+		if ( !c ) break;
+		in++;
+		
+		if ( c == ',' ) // commas in string
+		{
+			char* p = skip_white( in );
+			if ( *p == ',' || *p == '-' || from_dec( *p ) <= 9 )
+			{
+				in = p;
+				break;
+			}
+		}
+		
+		if ( c == '\\' ) // \ prefix for special characters
+		{
+			c = *in;
+			if ( !c ) break;
+			in++;
+		}
+		*out++ = (char) c;
+	}
+	*out = 0; // terminate string
+	return in;
+}
+
+static int parse_line( char* in, struct entry_t* entry )
+{
+	int result = 0;
+	
+	// file
+	entry->file = in;
+	entry->type = "";
+	in = parse_filename( in, entry );
+	
+	// track
+	entry->track = -1;
+	entry->decimal_track = 0;
+	in = parse_track( in, entry, &result );
+	
+	// name
+	entry->name = in;
+	in = parse_name( in );
+	
+	// time
+	entry->length = -1;
+	in = parse_time( in, &entry->length, &result );
+	
+	// loop
+	entry->intro = -1;
+	entry->loop  = -1;
+	if ( *in == '-' )
+	{
+		entry->loop = entry->length;
+		in++;
+	}
+	else
+	{
+		in = parse_time_( in, &entry->loop );
+		if ( entry->loop >= 0 )
+		{
+			entry->intro = 0;
+			if ( *in == '-' ) // trailing '-' means that intro length was specified 
+			{
+				in++;
+				entry->intro = entry->loop;
+				entry->loop  = entry->length - entry->intro;
+			}
+		}
+	}
+	in = next_field( in, &result );
+	
+	// fade
+	entry->fade = -1;
+	in = parse_time( in, &entry->fade, &result );
+	
+	// repeat
+	entry->repeat = -1;
+	in = parse_int( in, &entry->repeat, &result );
+	
+	return result;
+}
+
+static void parse_comment( char* in, struct info_t* info, bool first )
+{
+	in = skip_white( in + 1 );
+	const char* field = in;
+	while ( *in && *in != ':' && *in != '@' )
+		in++;
+		
+	/* Support Kaminari playlist format */
+    if ( *in == '@' ) {
+        field = in + 1;
+		
+        while ( *in && *in != ' ' && *in != '\t' )
+            in++;
+		
+        const char* text = skip_white( in + 1 );
+		if ( *text )
+		{
+			*in = 0;
+			     if ( !stricmp( "Title"   , field ) ) info->title = text;
+			else if ( !stricmp( "Composer", field ) ) info->composer = text;
+			else if ( !stricmp( "Artist", field ) 
+			       || !stricmp( "Sequencer", field) 
+				   || !stricmp( "Date",      field) 
+				   || !stricmp( "Copyright", field) ) info->engineer = text;
+			else if ( !stricmp( "Ripping" , field ) ) info->ripping  = text;
+			else if ( !stricmp( "Tagging" , field ) ) info->tagging  = text;
+			else
+				text = 0;
+			if ( text )
+				return;
+			*in = ':';
+		}
+		
+		return;
+    }
+	
+	if ( *in == ':' )
+	{
+		const char* text = skip_white( in + 1 );
+		if ( *text )
+		{
+			*in = 0;
+			     if ( !strcmp( "Composer", field ) ) info->composer = text;
+			else if ( !strcmp( "Engineer", field ) ) info->engineer = text;
+			else if ( !strcmp( "Ripping" , field ) ) info->ripping  = text;
+			else if ( !strcmp( "Tagging" , field ) ) info->tagging  = text;
+			else
+				text = 0;
+			if ( text )
+				return;
+			*in = ':';
+		}
+	}
+	
+	if ( first )
+		info->title = field;
+}
+
+blargg_err_t parse( struct M3u_Playlist* this )
+{
+	this->info.title    = "";
+	this->info.composer = "";
+	this->info.engineer = "";
+	this->info.ripping  = "";
+	this->info.tagging  = "";
+	
+	int const CR = 13;
+	int const LF = 10;
+	
+    *((this->data + this->raw_size - 1)) = LF; // terminate input
+	
+	this->first_error = 0;
+	bool first_comment = true;
+	int line  = 0;
+	int count = 0;
+	char* in = this->data;
+	while ( in < (this->data + this->raw_size) )
+	{
+		// find end of line and terminate it
+		line++;
+		char* begin = in;
+		while ( *in != CR && *in != LF )
+		{
+			if ( !*in )
+				return "Not an m3u playlist";
+			in++;
+		}
+		if ( in [0] == CR && in [1] == LF ) // treat CR,LF as a single line
+			*in++ = 0;
+		*in++ = 0;
+		
+		// parse line
+		if ( *begin == '#' )
+		{
+			parse_comment( begin, &this->info, first_comment );
+			first_comment = false;
+		}
+		else if ( *begin )
+		{
+			if ( (int) this->size <= count )
+				this->size = count * 2 + 64;
+			
+			if ( !parse_line( begin, &this->entries [count] ) )
+				count++;
+			else if ( !this->first_error )
+				this->first_error = line;
+			first_comment = false;
+		}
+	}
+	if ( count <= 0 )
+		return "Not an m3u playlist";
+	
+	if ( !(this->info.composer [0] | this->info.engineer [0] | this->info.ripping [0] | this->info.tagging [0]) )
+		this->info.title = "";
+	
+	this->size = count;
+	return 0;
+}
+
+blargg_err_t Gbs_load_m3u( struct Gbs_Emu* this, const char* path )
+{
+#if defined (ROCKBOX)
+	int fd = ci->open( path, O_RDONLY );
+#else
+	int fd = open( path, O_RDONLY );
+#endif
+	if ( fd < 0 ) return "Error when loading file";
+
+#if defined (ROCKBOX)
+	this->m3u.raw_size = ci->read( fd, this->m3u.data, max_size );
+	ci->close( fd );
+#else
+	this->m3u.raw_size = read( fd, this->m3u.data, max_size );
+	close( fd );
+#endif
+
+	blargg_err_t err = parse( &this->m3u );
+	if ( err ) 
+		return err;
+
+	if ( this->m3u.size > 0 ) 
+		this->track_count = (byte) this->m3u.size; 
+
+	return 0;
+}
Index: apps/codecs/libgbs/gbs_emu.h
===================================================================
--- apps/codecs/libgbs/gbs_emu.h	(revision 0)
+++ apps/codecs/libgbs/gbs_emu.h	(revision 0)
@@ -0,0 +1,184 @@
+// Nintendo Game Boy GBS music file emulator
+
+// Game_Music_Emu 0.5.2
+#ifndef GBS_EMU_H
+#define GBS_EMU_H
+
+#include "rom_data.h"
+#include "multi_buffer.h"
+#include "gb_apu.h"
+#include "gb_cpu.h"
+#include "m3u_playlist.h"
+
+/* typedef uint8_t byte; */
+typedef short sample_t;
+
+enum { joypad_addr = 0xFF00 };
+enum { ram_addr = 0xA000 };
+enum { hi_page = 0xFF00 - ram_addr };
+enum { buf_size = 2048 };
+
+// GBS file header
+enum { header_size = 112 };
+struct header_t
+{
+	char tag [3];
+	byte vers;
+	byte track_count;
+	byte first_track;
+	byte load_addr [2];
+	byte init_addr [2];
+	byte play_addr [2];
+	byte stack_ptr [2];
+	byte timer_modulo;
+	byte timer_mode;
+	char game [32];
+	char author [32];
+	char copyright [32];
+};
+
+struct Gbs_Emu {
+	// Header for currently loaded file
+	struct header_t header;
+	
+	// M3u Playlist
+	struct M3u_Playlist m3u;
+	
+	// cpu registers
+	struct registers_t r;
+	
+	// timer
+	blip_time_t cpu_time;
+	blip_time_t play_period;
+	blip_time_t next_play;
+
+	// Sound 
+	struct Gb_Apu apu;
+	struct Stereo_Buffer buf;
+	long clock_rate_;
+	long sample_rate_;
+	unsigned buf_changed_count;
+	int voice_count_;
+	double gain_;
+	double tempo_;
+	
+	// track-specific
+	byte track_count;
+	volatile bool track_ended;
+	int current_track_;
+	blargg_long out_time;  // number of samples played since start of track
+	blargg_long emu_time;  // number of samples emulator has generated since start of track
+	bool emu_track_ended_; // emulator has reached end of track
+	
+	// fading
+	blargg_long fade_start;
+	int fade_step;
+	
+	// silence detection
+	// Disable automatic end-of-track detection and skipping of silence at beginning
+	bool ignore_silence;
+
+	int max_initial_silence;
+	int mute_mask_;
+	int silence_lookahead; // speed to run emulator when looking ahead for silence
+	long silence_time;     // number of samples where most recent silence began
+	long silence_count;    // number of samples of silence to play before using buf
+	long buf_remain;       // number of samples left in silence buffer
+	
+	sample_t buf_ [buf_size];
+	
+	// rom & ram
+	struct Rom_Data rom; 
+	byte ram [0x4000 + 0x2000 + cpu_padding];
+};
+
+
+// Basic functionality
+// Initializes Gbs_Emu structure
+void Gbs_init( struct Gbs_Emu* this );
+
+// Stops (clear) Gbs_Emu structure
+void Gbs_stop( struct Gbs_Emu* this );
+
+// Loads a file from memory
+blargg_err_t Gbs_load( struct Gbs_Emu* this, void* data, long size );
+
+// Set output sample rate. Must be called only once before loading file.
+blargg_err_t Gbs_set_sample_rate( struct Gbs_Emu* this, long sample_rate );
+
+// Start a track, where 0 is the first track. Also clears warning string.
+blargg_err_t Gbs_start_track( struct Gbs_Emu* this, int );
+
+// Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
+// errors set warning string, and major errors also end track.
+blargg_err_t Gbs_play( struct Gbs_Emu* this, long count, sample_t* buf ); ICODE_ATTR
+
+// Loads an m3u playlist
+blargg_err_t Gbs_load_m3u( struct Gbs_Emu* this, const char* path );
+
+
+// Track status/control
+// Number of milliseconds (1000 msec = 1 second) played since beginning of track
+long Track_tell( struct Gbs_Emu* this );
+
+// Seek to new time in track. Seeking backwards or far forward can take a while.
+blargg_err_t Track_seek( struct Gbs_Emu* this, long msec );
+
+// Skip n samples
+blargg_err_t Track_skip( struct Gbs_Emu* this, long n );
+
+// Set start time and length of track fade out. Once fade ends track_ended() returns
+// true. Fade time can be changed while track is playing.
+void Track_set_fade( struct Gbs_Emu* this, long start_msec, long length_msec );
+
+// Get track length in milliseconds
+static inline long Track_get_length( struct Gbs_Emu* this, int n )
+{
+    long length = 120 * 1000;  /* 2 minutes */ 
+	if ( (this->m3u.size > 0) && (n < this->m3u.size) ) {
+		struct entry_t* entry = &this->m3u.entries [n];
+		length = entry->length;
+		if ( length <= 0 ) {
+			long intro = entry->intro > 0 ? entry->intro : 120 * 1000;
+			long loop = entry->loop > 0 ? entry->loop : 0;
+			length = intro + 2 * loop; /* intro + 2 loops */
+		}
+	} 
+	
+	return length;
+}
+
+
+// Sound customization
+// Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
+// Track length as returned by track_info() assumes a tempo of 1.0.
+void Sound_set_tempo( struct Gbs_Emu* this, double );
+	
+// Mute/unmute voice i, where voice 0 is first voice
+void Sound_mute_voice( struct Gbs_Emu* this, int index, bool mute );
+	
+// Set muting state of all voices at once using a bit mask, where -1 mutes them all,
+// 0 unmutes them all, 0x01 mutes just the first voice, etc.
+void Sound_mute_voices( struct Gbs_Emu* this, int mask );
+
+// Change overall output amplitude, where 1.0 results in minimal clamping.
+// Must be called before set_sample_rate().
+static inline void Sound_set_gain( struct Gbs_Emu* this, double g )
+{
+	assert( !this->sample_rate_ ); // you must set gain before setting sample rate
+	this->gain_ = g;
+}
+
+
+// Emulation (You shouldn't touch these)
+
+blargg_err_t Run_clocks( struct Gbs_Emu* this, blip_time_t*, int ); ICODE_ATTR
+void Set_bank( struct Gbs_Emu* this, int ); ICODE_ATTR
+
+void Update_timer( struct Gbs_Emu* this ); ICODE_ATTR
+void Cpu_jsr( struct Gbs_Emu* this, gb_addr_t ); ICODE_ATTR
+int Cpu_read( struct Gbs_Emu* this, gb_addr_t );  ICODE_ATTR
+void Cpu_write( struct Gbs_Emu* this, gb_addr_t, int ); ICODE_ATTR
+
+
+#endif
Index: apps/codecs/libgbs/rom_data.c
===================================================================
--- apps/codecs/libgbs/rom_data.c	(revision 0)
+++ apps/codecs/libgbs/rom_data.c	(revision 0)
@@ -0,0 +1,76 @@
+// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
+
+#include "rom_data.h"
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include <string.h>
+#include "blargg_source.h"
+
+// Rom_Data
+
+blargg_err_t Rom_load( struct Rom_Data* this_, void* data, long size,
+		int header_size, void* header_out, int fill )
+{
+	long file_offset = pad_size;
+	
+	this_->rom_addr = 0;
+	this_->mask     = 0;
+	this_->size    = 0;
+	
+	if ( size <= header_size ) // <= because there must be data after header
+		return gme_wrong_file_type;
+	
+	// Read header
+	memcpy( header_out, data, header_size );
+	
+	this_->file_size = size - header_size;
+	this_->file_data = (byte*) data + header_size;
+	
+	memset( this_->unmapped, fill, rom_size );
+	memcpy( &this_->unmapped [file_offset], this_->file_data, 
+		this_->file_size < pad_size ? this_->file_size : pad_size );
+	
+	return 0;
+}
+
+void Rom_set_addr( struct Rom_Data* this_, long addr )
+{
+	this_->rom_addr = addr - bank_size - pad_extra;
+	
+	long rounded = (addr + this_->file_size + bank_size - 1) / bank_size * bank_size;
+	if ( rounded <= 0 )
+	{
+		rounded = 0;
+	}
+	else
+	{
+		int shift = 0;
+		unsigned long max_addr = (unsigned long) (rounded - 1);
+		while ( max_addr >> shift )
+			shift++;
+		this_->mask = (1L << shift) - 1;
+	}
+	
+	if ( addr < 0 )
+		addr = 0;
+	this_->size = rounded;
+	this_->rsize_ = rounded - this_->rom_addr + pad_extra;
+
+	if ( 0 )
+	{
+		dprintf( "addr: %X\n", addr );
+		dprintf( "file_size: %d\n", this_->file_size );
+		dprintf( "rounded: %d\n", rounded );
+		dprintf( "mask: $%X\n", this_->mask );
+	}
+}
Index: apps/codecs/libgbs/blargg_config.h
===================================================================
--- apps/codecs/libgbs/blargg_config.h	(revision 0)
+++ apps/codecs/libgbs/blargg_config.h	(revision 0)
@@ -0,0 +1,27 @@
+// Library configuration. Modify this file as necessary.
+
+#ifndef BLARGG_CONFIG_H
+#define BLARGG_CONFIG_H
+
+// Uncomment to enable platform-specific optimizations
+//#define BLARGG_NONPORTABLE 1
+
+// Uncomment if automatic byte-order determination doesn't work
+#ifdef ROCKBOX_BIG_ENDIAN
+	#define BLARGG_BIG_ENDIAN 1
+#endif
+
+// Uncomment if you get errors in the bool section of blargg_common.h
+#define BLARGG_COMPILER_HAS_BOOL 1
+
+// To handle undefined reference to assert
+#define NDEBUG 1
+
+// Use standard config.h if present
+#define HAVE_CONFIG_H 1
+
+#ifdef HAVE_CONFIG_H
+	#include "config.h"
+#endif
+
+#endif
Index: apps/codecs/libgbs/multi_buffer.h
===================================================================
--- apps/codecs/libgbs/multi_buffer.h	(revision 0)
+++ apps/codecs/libgbs/multi_buffer.h	(revision 0)
@@ -0,0 +1,72 @@
+// Multi-channel sound buffer interface, and basic mono and stereo buffers
+
+// Blip_Buffer 0.4.1
+#ifndef MULTI_BUFFER_H
+#define MULTI_BUFFER_H
+
+#include "blargg_common.h"
+#include "blip_buffer.h"
+
+// Get indexed channel, from 0 to channel count - 1
+struct channel_t {
+	struct Blip_Buffer* center;
+	struct Blip_Buffer* left;
+	struct Blip_Buffer* right;
+};
+
+enum { type_index_mask = 0xFF };
+enum { wave_type = 0x100, noise_type = 0x200, mixed_type = wave_type | noise_type };
+enum { buf_count = 3 };
+	
+struct Stereo_Buffer {
+	struct Blip_Buffer bufs [buf_count];
+	struct channel_t chan;
+	int stereo_added;
+	int was_stereo;
+	
+	unsigned channels_changed_count_;
+	long sample_rate_;
+	int length_;
+	int samples_per_frame_;
+};
+
+// Initializes Stereo_Buffer structure
+void Buffer_init( struct Stereo_Buffer* this_ );
+
+blargg_err_t Buffer_set_sample_rate( struct Stereo_Buffer* this_, long, int msec );
+void Buffer_clock_rate( struct Stereo_Buffer* this_, long );
+void Buffer_bass_freq( struct Stereo_Buffer* this_, int );
+void Buffer_clear( struct Stereo_Buffer* this_ );
+struct channel_t Buffer_channel( struct Stereo_Buffer* this_ );
+void Buffer_end_frame( struct Stereo_Buffer* this_, blip_time_t ); ICODE_ATTR
+	
+long Buffer_read_samples( struct Stereo_Buffer* this_, blip_sample_t*, long ); ICODE_ATTR
+	
+// Count of changes to channel configuration. Incremented whenever
+// a change is made to any of the Blip_Buffers for any channel.
+unsigned Buffer_channels_changed_count( struct Stereo_Buffer* this_ ); ICODE_ATTR
+void Buffer_channels_changed( struct Stereo_Buffer* this_ ); ICODE_ATTR
+	
+void Buffer_mix_stereo_no_center( struct Stereo_Buffer* this_, blip_sample_t*, blargg_long ); ICODE_ATTR
+void Buffer_mix_stereo( struct Stereo_Buffer* this_, blip_sample_t*, blargg_long ); ICODE_ATTR
+void Buffer_mix_mono( struct Stereo_Buffer* this_, blip_sample_t*, blargg_long ); ICODE_ATTR
+
+// Number of samples per output frame (1 = mono, 2 = stereo)
+static inline int Buffer_samples_per_frame( struct Stereo_Buffer* this_ )
+{
+	return this_->samples_per_frame_;
+}
+
+// See Blip_Buffer.h
+static inline long Buffer_sample_rate( struct Stereo_Buffer* this_ )
+{
+	return this_->sample_rate_;
+}
+
+// Length of buffer, in milliseconds
+static inline int Buffer_length( struct Stereo_Buffer* this_ )
+{
+	return this_->length_;
+}
+
+#endif
Index: apps/codecs/libgbs/m3u_playlist.h
===================================================================
--- apps/codecs/libgbs/m3u_playlist.h	(revision 0)
+++ apps/codecs/libgbs/m3u_playlist.h	(revision 0)
@@ -0,0 +1,56 @@
+// M3U playlist file parser, with support for subtrack information
+
+// Game_Music_Emu 0.5.2
+#ifndef M3U_PLAYLIST_H
+#define M3U_PLAYLIST_H
+
+#include "blargg_common.h"
+
+enum { max_entries = 255 };
+enum { max_size = 0x1000 };
+
+struct info_t
+{
+	const char* title;
+	const char* composer;
+	const char* engineer;
+	const char* ripping;
+	const char* tagging;
+};
+
+struct entry_t
+{
+	const char* file; // filename without stupid ::TYPE suffix
+	const char* type; // if filename has ::TYPE suffix, this will be "TYPE". "" if none.
+	const char* name;
+	bool decimal_track; // true if track was specified in hex
+	                    // integers are -1 if not present
+	int track;  // 1-based
+	int length; // milliseconds
+	int intro;
+	int loop;
+	int fade;
+	int repeat; // count
+};
+
+struct M3u_Playlist {
+	int size;	
+	long raw_size;
+	int first_error;
+	
+	struct info_t info;
+	struct entry_t entries [max_entries];
+	char data [max_size];
+};
+
+// Initialize M3u_Playlist structure
+static inline void M3u_clear( struct M3u_Playlist* this )
+{
+	this->first_error = 0;
+	memset( this->entries, 0, sizeof this->entries );	
+	memset( this->data, 0, sizeof this->data );
+	this->raw_size = 0;
+	this->size = 0;
+}
+
+#endif
Index: apps/codecs/libgbs/gb_cpu_io.h
===================================================================
--- apps/codecs/libgbs/gb_cpu_io.h	(revision 0)
+++ apps/codecs/libgbs/gb_cpu_io.h	(revision 0)
@@ -0,0 +1,72 @@
+
+#include "gbs_emu.h"
+
+#include "blargg_source.h"
+
+int Cpu_read( struct Gbs_Emu* this, gb_addr_t addr )
+{
+	int result = *Cpu_get_code( addr );
+	if ( (unsigned) (addr - start_addr) < register_count )
+		result = Apu_read_register( &this->apu, this->cpu_time - Cpu_remain(), addr );
+#ifndef NDEBUG
+	else if ( (unsigned) (addr - 0x8000) < 0x2000 || (unsigned) (addr - 0xE000) < 0x1F00 )
+		dprintf( "Read from unmapped memory $%.4x\n", (unsigned) addr );
+	else if ( (unsigned) (addr - 0xFF01) < 0xFF80 - 0xFF01 )
+		dprintf( "Unhandled I/O read 0x%4X\n", (unsigned) addr );
+#endif
+	return result;
+}
+
+void Cpu_write( struct Gbs_Emu* this, gb_addr_t addr, int data )
+{
+	unsigned offset = addr - ram_addr;
+	if ( offset <= 0xFFFF - ram_addr )
+	{
+		this->ram [offset] = data;
+		if ( (addr ^ 0xE000) <= 0x1F80 - 1 )
+		{
+			if ( (unsigned) (addr - start_addr) < register_count )
+			{
+				GME_APU_HOOK( this, addr - start_addr, data );
+				Apu_write_register( &this->apu, this->cpu_time - Cpu_remain(), addr, data );
+			}
+			else if ( (addr ^ 0xFF06) < 2 )
+				Update_timer( this );
+			else if ( addr == joypad_addr )
+				this->ram [offset] = 0; // keep joypad return value 0
+			else
+				this->ram [offset] = 0xFF;
+
+			//if ( addr == 0xFFFF )
+			//  dprintf( "Wrote interrupt mask\n" );
+		}
+	}
+	else if ( (addr ^ 0x2000) <= 0x2000 - 1 )
+	{
+		Set_bank( this, data );
+	}
+#ifndef NDEBUG
+	else if ( (unsigned) (addr - 0x8000) < 0x2000 || (unsigned) (addr - 0xE000) < 0x1F00 )
+	{
+		dprintf( "Wrote to unmapped memory $%.4x\n", (unsigned) addr );
+	}
+#endif
+}
+
+#define CPU_READ_FAST( emu, addr, time, out ) \
+	CPU_READ_FAST_( emu, addr, time, out )
+
+#define CPU_READ_FAST_( emu, addr, time, out ) \
+{\
+	out = READ_PROG( addr );\
+	if ( (unsigned) (addr - start_addr) <= register_count )\
+		out = Apu_read_register( &emu->apu, emu->cpu_time - time * clocks_per_instr, addr );\
+	else\
+		check( out == Cpu_read( emu->addr ) );\
+}
+
+#define CPU_READ( emu, addr, time ) \
+	Cpu_read( emu, addr )
+
+#define CPU_WRITE( emu, addr, data, time ) \
+	Cpu_write( emu, addr, data )
Index: apps/codecs/libgbs/rom_data.h
===================================================================
--- apps/codecs/libgbs/rom_data.h	(revision 0)
+++ apps/codecs/libgbs/rom_data.h	(revision 0)
@@ -0,0 +1,72 @@
+// Common aspects of emulators which use rom data
+
+// Game_Music_Emu 0.5.2
+#ifndef ROM_DATA_H
+#define ROM_DATA_H
+
+#include "blargg_common.h"
+#include "blargg_source.h"
+
+// ROM data handler, used by several Classic_Emu derivitives. Loads file data
+// with padding on both sides, allowing direct use in bank mapping. The main purpose
+// is to allow all file data to be loaded with only one read() call (for efficiency).
+
+extern const char gme_wrong_file_type []; // declared in gme.h
+
+enum { bank_size = 0x4000 };
+enum { pad_extra = 8 };
+enum { pad_size = bank_size + pad_extra };
+enum { rom_size = 2 * pad_size };
+
+struct  Rom_Data {	
+	byte* file_data;
+	long file_size;
+	
+	blargg_long rom_addr;
+	blargg_long mask;
+	blargg_long size; // TODO: eliminate
+	blargg_long rsize_;
+	
+	// Unmapped space
+	byte unmapped [rom_size];
+};
+
+// Load file data, using already-loaded header 'h' if not NULL. Copy header
+// from loaded file data into *out and fill unmapped bytes with 'fill'.
+blargg_err_t Rom_load( struct Rom_Data* this_, void* data, long size, int header_size, void* header_out, int fill );
+
+// Set address that file data should start at
+void Rom_set_addr( struct Rom_Data* this_, long addr );
+
+// Mask address to nearest power of two greater than size()
+static inline blargg_long mask_addr( blargg_long addr, blargg_long mask )
+{
+	#ifdef check
+		check( addr <= mask );
+	#endif
+	return addr & mask;
+}
+
+// Pointer to page starting at addr. Returns unmapped() if outside data.
+static inline byte* Rom_at_addr( struct Rom_Data* this_, blargg_long addr )
+{
+	blargg_ulong offset = mask_addr( addr, this_->mask ) - this_->rom_addr;
+	if ( offset > (blargg_ulong) (this_->rsize_ - pad_size) )
+		offset = 0; // unmapped
+			
+	if ( offset < pad_size	) return &this_->unmapped [offset];
+	else return &this_->file_data [offset - pad_size];
+}
+
+
+#ifndef GME_APU_HOOK
+	#define GME_APU_HOOK( emu, addr, data ) ((void) 0)
+#endif
+
+#ifndef GME_FRAME_HOOK
+	#define GME_FRAME_HOOK( emu ) ((void) 0)
+#else
+	#define GME_FRAME_HOOK_DEFINED 1
+#endif
+
+#endif
Index: apps/codecs/libgbs/gb_apu.c
===================================================================
--- apps/codecs/libgbs/gb_apu.c	(revision 0)
+++ apps/codecs/libgbs/gb_apu.c	(revision 0)
@@ -0,0 +1,311 @@
+// Gb_Snd_Emu 0.1.5. http://www.slack.net/~ant/
+
+#include "gb_apu.h"
+
+#include <string.h>
+
+/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
+can redistribute it and/or modify it under the terms of the GNU Lesser
+General Public License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version. This
+module is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details. You should have received a copy of the GNU Lesser General Public
+License along with this module; if not, write to the Free Software Foundation,
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "blargg_source.h"
+#include "gb_oscs.h"
+
+unsigned const vol_reg    = 0xFF24;
+unsigned const status_reg = 0xFF26;
+
+void Apu_init( struct Gb_Apu* this )
+{
+	Synth_init( &this->square_synth );
+    Synth_init( &this->other_synth );
+    
+	this->square1.synth = &this->square_synth;
+	this->square2.synth = &this->square_synth;
+	this->wave.synth  = &this->other_synth;
+	this->noise.synth = &this->other_synth;
+	
+	this->oscs [0] = &this->square1.osc;
+	this->oscs [1] = &this->square2.osc;
+	this->oscs [2] = &this->wave.osc;
+	this->oscs [3] = &this->noise.osc;
+	
+	int i;
+	for ( i = 0; i < osc_count; i++ )
+	{
+		struct Gb_Osc* osc = this->oscs [i];
+		osc->regs = &this->regs [i * 5];
+		osc->output = 0;
+		osc->outputs [0] = 0;
+		osc->outputs [1] = 0;
+		osc->outputs [2] = 0;
+		osc->outputs [3] = 0;
+	}
+	
+	Apu_set_tempo( this, 1.0 );
+	Apu_volume( this, 1.0 );
+	Apu_reset( this );
+}
+
+void Apu_osc_output( struct Gb_Apu* this, int index, struct Blip_Buffer* center, struct Blip_Buffer* left, struct Blip_Buffer* right )
+{
+	require( (unsigned) index < osc_count );
+	require( (center && left && right) || (!center && !left && !right) );
+	struct Gb_Osc* osc = this->oscs [index];
+	osc->outputs [1] = right;
+	osc->outputs [2] = left;
+	osc->outputs [3] = center;
+	osc->output = osc->outputs [osc->output_select];
+}
+
+void Apu_output( struct Gb_Apu* this, struct Blip_Buffer* center, struct Blip_Buffer* left, struct Blip_Buffer* right )
+{
+	int i;
+	for ( i = 0; i < osc_count; i++ )
+		Apu_osc_output( this, i, center, left, right );
+}
+
+void Apu_update_volume( struct Gb_Apu* this )
+{
+	// TODO: doesn't handle differing left/right global volume (support would
+	// require modification to all oscillator code)
+	int data = this->regs [vol_reg - start_addr];
+	double vol = (max( data & 7, data >> 4 & 7 ) + 1) * this->volume_unit;
+	Synth_volume( &this->square_synth, vol );
+	Synth_volume( &this->other_synth, vol );
+}
+
+static unsigned char const powerup_regs [0x20] = {
+	0x80,0x3F,0x00,0xFF,0xBF, // square 1
+	0xFF,0x3F,0x00,0xFF,0xBF, // square 2
+	0x7F,0xFF,0x9F,0xFF,0xBF, // wave
+	0xFF,0xFF,0x00,0x00,0xBF, // noise
+	0x00, // left/right enables
+	0x77, // master volume
+	0x80, // power
+	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
+};
+
+void Apu_set_tempo( struct Gb_Apu* this, double t )
+{
+	this->frame_period = 4194304 / 256; // 256 Hz
+	if ( t != 1.0 )
+		this->frame_period = (blip_time_t) (this->frame_period / t);
+}
+
+void Apu_reset( struct Gb_Apu* this )
+{
+	this->next_frame_time = 0;
+	this->last_time       = 0;
+	this->frame_count     = 0;
+	
+	Square_reset( &this->square1 );
+	Square_reset( &this->square2 );
+	Wave_reset( &this->wave );
+	Noise_reset( &this->noise );
+	this->noise.bits = 1;
+	this->wave.wave_pos = 0;
+	
+	// avoid click at beginning
+	this->regs [vol_reg - start_addr] = 0x77;
+	Apu_update_volume( this );
+	
+	this->regs [status_reg - start_addr] = 0x01; // force power
+	Apu_write_register( this, 0, status_reg, 0x00 );
+	
+	static unsigned char const initial_wave [] ICONST_ATTR = {
+		0x84,0x40,0x43,0xAA,0x2D,0x78,0x92,0x3C, // wave table
+		0x60,0x59,0x59,0xB0,0x34,0xB8,0x2E,0xDA
+	};
+	memcpy( this->wave.wave, initial_wave, sizeof this->wave.wave );
+}
+
+void Apu_run_until( struct Gb_Apu* this, blip_time_t end_time )
+{
+	require( end_time >= this->last_time ); // end_time must not be before previous time
+	if ( end_time == this->last_time )
+		return;
+	
+	while ( true )
+	{
+		blip_time_t time = this->next_frame_time;
+		if ( time > end_time )
+			time = end_time;
+		
+		// run oscillators
+		int i;
+		for ( i = 0; i < osc_count; ++i )
+		{
+			struct Gb_Osc* osc = this->oscs [i];
+			if ( osc->output )
+			{
+				Blip_set_modified( osc->output ); // TODO: misses optimization opportunities?
+				int playing = false;
+				if ( osc->enabled && osc->volume &&
+						(!(osc->regs [4] & len_enabled_mask) || osc->length) )
+					playing = -1;
+				switch ( i )
+				{
+				case 0: Square_run( &this->square1, this->last_time, time, playing ); break;
+				case 1: Square_run( &this->square2, this->last_time, time, playing ); break;
+				case 2: Wave_run  ( &this->wave, this->last_time, time, playing ); break;
+				case 3: Noise_run ( &this->noise, this->last_time, time, playing ); break;
+				}
+			}
+		}
+		this->last_time = time;
+		
+		if ( time == end_time )
+			break;
+		
+		this->next_frame_time += this->frame_period;
+		
+		// 256 Hz actions
+		Osc_clock_length( &this->square1.osc );
+		Osc_clock_length( &this->square2.osc );
+		Osc_clock_length( &this->wave.osc );
+		Osc_clock_length( &this->noise.osc );
+		
+		this->frame_count = (this->frame_count + 1) & 3;
+		if ( this->frame_count == 0 )
+		{
+			// 64 Hz actions
+			Square_clock_envelope( &this->square1 );
+			Square_clock_envelope( &this->square2 );
+			Noise_clock_envelope( &this->noise );
+		}
+		
+		if ( this->frame_count & 1 )
+			Square_clock_sweep( &this->square1 ); // 128 Hz action
+	}
+}
+
+void Apu_end_frame( struct Gb_Apu* this, blip_time_t end_time )
+{
+	if ( end_time > this->last_time )
+		Apu_run_until( this, end_time );
+	
+	assert( this->next_frame_time >= end_time );
+	this->next_frame_time -= end_time;
+	
+	assert( this->last_time >= end_time );
+	this->last_time -= end_time;
+}
+
+void Apu_write_register( struct Gb_Apu* this, blip_time_t time, unsigned addr, int data )
+{
+	require( (unsigned) data < 0x100 );
+	
+	int reg = addr - start_addr;
+	if ( (unsigned) reg >= register_count )
+		return;
+	
+	Apu_run_until( this, time );
+	
+	int old_reg = this->regs [reg];
+	this->regs [reg] = data;
+	
+	if ( addr < vol_reg )
+	{
+		Apu_write_osc( this, reg / 5, reg, data );
+	}
+	else if ( addr == vol_reg && data != old_reg ) // global volume
+	{
+		// return all oscs to 0
+		int i;
+		for ( i = 0; i < osc_count; i++ )
+		{
+			struct Gb_Osc* osc = this->oscs [i];
+			int amp = osc->last_amp;
+			osc->last_amp = 0;
+			if ( amp && osc->enabled && osc->output )
+				Synth_offset( &this->other_synth, time, -amp, osc->output );
+		}
+		
+		if ( this->wave.osc.outputs [3] )
+			Synth_offset( &this->other_synth, time, 30, this->wave.osc.outputs [3] );
+		
+		Apu_update_volume( this );
+		
+		if ( this->wave.osc.outputs [3] )
+			Synth_offset( &this->other_synth, time, -30, this->wave.osc.outputs [3] );
+		
+		// oscs will update with new amplitude when next run
+	}
+	else if ( addr == 0xFF25 || addr == status_reg )
+	{
+		int mask = (this->regs [status_reg - start_addr] & 0x80) ? ~0 : 0;
+		int flags = this->regs [0xFF25 - start_addr] & mask;
+		
+		// left/right assignments
+		int i;
+		for ( i = 0; i < osc_count; i++ )
+		{
+			struct Gb_Osc* osc = this->oscs [i];
+			osc->enabled &= mask;
+			int bits = flags >> i;
+			struct Blip_Buffer* old_output = osc->output;
+			osc->output_select = (bits >> 3 & 2) | (bits & 1);
+			osc->output = osc->outputs [osc->output_select];
+			if ( osc->output != old_output )
+			{
+				int amp = osc->last_amp;
+				osc->last_amp = 0;
+				if ( amp && old_output )
+					Synth_offset( &this->other_synth, time, -amp, old_output );
+			}
+		}
+		
+		if ( addr == status_reg && data != old_reg )
+		{
+			if ( !(data & 0x80) )
+			{
+				unsigned i;
+				for ( i = 0; i < sizeof powerup_regs; i++ )
+				{
+					if ( i != status_reg - start_addr )
+						Apu_write_register( this, time, i + start_addr, powerup_regs [i] );
+				}
+			}
+			else
+			{
+				//dprintf( "APU powered on\n" );
+			}
+		}
+	}
+	else if ( addr >= 0xFF30 )
+	{
+		int index = (addr & 0x0F) * 2;
+		this->wave.wave [index] = data >> 4;
+		this->wave.wave [index + 1] = data & 0x0F;
+	}
+}
+
+int Apu_read_register( struct Gb_Apu* this, blip_time_t time, unsigned addr )
+{
+	Apu_run_until( this, time );
+	
+	int index = addr - start_addr;
+	require( (unsigned) index < register_count );
+	int data = this->regs [index];
+	
+	if ( addr == status_reg )
+	{
+		data = (data & 0x80) | 0x70;
+		int i;
+		for ( i = 0; i < osc_count; i++ )
+		{
+			const struct Gb_Osc* osc = this->oscs [i];
+			if ( osc->enabled && (osc->length || !(osc->regs [4] & len_enabled_mask)) )
+				data |= 1 << i;
+		}
+	}
+	
+	return data;
+}
Index: apps/codecs/libgbs/libgbs.make
===================================================================
--- apps/codecs/libgbs/libgbs.make	(revision 0)
+++ apps/codecs/libgbs/libgbs.make	(revision 0)
@@ -0,0 +1,17 @@
+
+# libgbs
+GBSLIB := $(CODECDIR)/libgbs.a
+GBSLIB_SRC := $(call preprocess, $(APPSDIR)/codecs/libgbs/SOURCES)
+GBSLIB_OBJ := $(call c2obj, $(GBSLIB_SRC))
+OTHER_SRC += $(GBSLIB_SRC)
+
+$(GBSLIB): $(GBSLIB_OBJ)
+	$(SILENT)$(shell rm -f $@)
+	$(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null
+
+GBSFLAGS = $(filter-out -O%,$(CODECFLAGS)) -fno-strict-aliasing
+GBSFLAGS += -O1
+
+$(CODECDIR)/libgbs/%.o: $(ROOTDIR)/apps/codecs/libgbs/%.c
+	$(SILENT)mkdir -p $(dir $@)
+	$(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(GBSFLAGS) -c $< -o $@
Index: apps/codecs/lib/codeclib.c
===================================================================
--- apps/codecs/lib/codeclib.c	(revision 29139)
+++ apps/codecs/lib/codeclib.c	(working copy)
@@ -92,6 +92,11 @@
     return(ci->strlen(s));
 }
 
+char *strrchr(const char *s, int c)
+{
+    return(ci->strrchr(s, c));
+}
+
 char *strcpy(char *dest, const char *src)
 {
     return(ci->strcpy(dest,src));
@@ -107,6 +112,11 @@
     return(ci->strcmp(s1,s2));
 }
 
+int strcasecmp(const char *s1, const char *s2)
+{
+    return(ci->strcasecmp(s1, s2));
+}
+
 void *memcpy(void *dest, const void *src, size_t n)
 {
     return(ci->memcpy(dest,src,n));
Index: apps/codecs/lib/codeclib.h
===================================================================
--- apps/codecs/lib/codeclib.h	(revision 29139)
+++ apps/codecs/lib/codeclib.h	(working copy)
@@ -59,6 +59,7 @@
 void *memmove(void *s1, const void *s2, size_t n);
 
 size_t strlen(const char *s);
+char *strrchr(const char *s, int c);
 char *strcpy(char *dest, const char *src);
 char *strcat(char *dest, const char *src);
 
@@ -66,6 +67,7 @@
  * breaks if we write down strcmp's prototype */
 #undef strcmp
 int strcmp(const char *s1, const char *s2);
+int strcasecmp(const char *s1, const char *s2);
 
 void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
 
Index: apps/codecs/codecs.make
===================================================================
--- apps/codecs/codecs.make	(revision 29139)
+++ apps/codecs/codecs.make	(working copy)
@@ -43,6 +43,7 @@
 include $(APPSDIR)/codecs/libatrac/libatrac.make
 include $(APPSDIR)/codecs/libpcm/libpcm.make
 include $(APPSDIR)/codecs/libtta/libtta.make
+include $(APPSDIR)/codecs/libgbs/libgbs.make
 
 # compile flags for codecs
 CODECFLAGS = $(CFLAGS) -fstrict-aliasing -I$(APPSDIR)/codecs \
@@ -93,6 +94,7 @@
 $(CODECDIR)/vox.codec : $(CODECDIR)/libpcm.a
 $(CODECDIR)/wav64.codec : $(CODECDIR)/libpcm.a
 $(CODECDIR)/tta.codec : $(CODECDIR)/libtta.a
+$(CODECDIR)/gbs.codec : $(CODECDIR)/libgbs.a
 
 $(CODECS): $(CODECLIB) # this must be last in codec dependency list
 
Index: apps/codecs/SOURCES
===================================================================
--- apps/codecs/SOURCES	(revision 29139)
+++ apps/codecs/SOURCES	(working copy)
@@ -33,6 +33,7 @@
 wav64.c
 tta.c
 wmapro.c
+gbs.c
 
 #ifdef HAVE_RECORDING
 
