// Game_Music_Emu 0.5.5. http://www.slack.net/~ant/ #include "nsf_emu.h" #include "multi_buffer.h" #include "blargg_endian.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" const char gme_wrong_file_type [] ICONST_ATTR = "Wrong file type for this_ emulator"; long const clock_divisor = 12; 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) // number of frames until play interrupts init int const initial_play_delay = 7; // KikiKaikai needed this to work int const rom_addr = 0x8000; void clear_track_vars( struct Nsf_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 = INT_MAX / 2 + 1; this_->fade_step = 1; this_->silence_time = 0; this_->silence_count = 0; this_->buf_remain = 0; } static int pcm_read( void* emu, addr_t addr ) { return *Cpu_get_code( &((struct Nsf_Emu*) emu)->cpu, addr ); } void Nsf_init( struct Nsf_Emu* this_ ) { this_->sample_rate = 0; this_->mute_mask_ = 0; this_->tempo = 1.0; this_->gain = 1.0; this_->is_extended = 0; // defaults this_->max_initial_silence = 2; this_->ignore_silence = false; this_->voice_count = 0; // Set sound gain Sound_set_gain( this_, 1.4 ); // Unload clear_track_vars( this_ ); // Init & clear nsfe info Info_init( &this_->info ); Info_unload( &this_->info ); // TODO: extremely hacky! Cpu_init( &this_->cpu ); Apu_init( &this_->apu ); Apu_dmc_reader( &this_->apu, pcm_read, this_ ); // Clear m3u playlist M3u_clear( &this_->m3u ); } // Setup blargg_err_t init_sound( struct Nsf_Emu* this_ ) { /* if ( header_.chip_flags & ~(fds_flag | namco_flag | vrc6_flag | fme7_flag) ) warning( "Uses unsupported audio expansion hardware" ); **/ this_->voice_count = apu_osc_count; double adjusted_gain = 1.0 / 0.75 * this_->gain; #ifdef NSF_EMU_APU_ONLY { if ( this_->header_.chip_flags ) set_warning( "Uses unsupported audio expansion hardware" ); } #else { if ( vrc6_enabled( this_ ) ) { Vrc6_init( &this_->vrc6 ); adjusted_gain *= 0.75; this_->voice_count += vrc6_osc_count; } if ( fme7_enabled( this_ ) ) { Fme7_init( &this_->fme7 ); adjusted_gain *= 0.75; this_->voice_count += fme7_osc_count; } if ( mmc5_enabled( this_ ) ) { Mmc5_init( &this_->mmc5 ); adjusted_gain *= 0.75; this_->voice_count += mmc5_osc_count; } if ( fds_enabled( this_ ) ) { Fds_init( &this_->fds ); adjusted_gain *= 0.75; this_->voice_count += fds_osc_count ; } if ( namco_enabled( this_ ) ) { Namco_init( &this_->namco ); adjusted_gain *= 0.75; this_->voice_count += namco_osc_count; } if ( vrc7_enabled( this_ ) ) { #ifndef NSF_EMU_NO_VRC7 Vrc7_init( &this_->vrc7 ); #endif // Increased vrc7 gain adjusted_gain *= 0.95; this_->voice_count += vrc7_osc_count; } if ( vrc7_enabled( this_ ) ) Vrc7_volume( &this_->vrc7, adjusted_gain ); if ( namco_enabled( this_ ) ) Namco_volume( &this_->namco, adjusted_gain ); if ( vrc6_enabled( this_ ) ) Vrc6_volume( &this_->vrc6, adjusted_gain ); if ( fme7_enabled( this_ ) ) Fme7_volume( &this_->fme7, adjusted_gain ); if ( mmc5_enabled( this_ ) ) Apu_volume( &this_->mmc5.apu, adjusted_gain ); if ( fds_enabled( this_ ) ) Fds_volume( &this_->fds, adjusted_gain ); } #endif if ( adjusted_gain > this_->gain ) adjusted_gain = this_->gain; Apu_volume( &this_->apu, adjusted_gain ); return 0; } // Header stuff bool valid_tag( struct header_t* this_ ) { return 0 == memcmp( this_->tag, "NESM\x1A", 5 ); } // True if file supports only PAL speed static bool pal_only( struct header_t* this_ ) { return (this_->speed_flags & 3) == 1; } static double clock_rate( struct header_t* this_ ) { return pal_only( this_ ) ? 1662607.125 : 1789772.727272727; } int play_period( struct header_t* this_ ) { // NTSC int clocks = 29780; int value = 0x411A; byte const* rate_ptr = this_->ntsc_speed; // PAL if ( pal_only( this_ ) ) { clocks = 33247; value = 0x4E20; rate_ptr = this_->pal_speed; } // Default rate int rate = get_le16( rate_ptr ); if ( rate == 0 ) rate = value; // Custom rate if ( rate != value ) clocks = (int) (rate * clock_rate( this_ ) * (1.0/1000000.0)); return clocks; } // Gets address, given pointer to it in file header. If zero, returns rom_addr. addr_t get_addr( byte const in [] ) { addr_t addr = get_le16( in ); if ( addr == 0 ) addr = rom_addr; return addr; } static blargg_err_t check_nsf_header( struct header_t* h ) { if ( !valid_tag( h ) ) return gme_wrong_file_type; return 0; } blargg_err_t Nsf_load( struct Nsf_Emu* this_, void* data, long size ) { // Unload Info_unload( &this_->info ); // TODO: extremely hacky! this_->voice_count = 0; clear_track_vars( this_ ); assert( offsetof (struct header_t,unused [4]) == header_size ); if ( !memcmp( data, "NESM\x1A", 5 ) ) { this_->is_extended = 0; Nsf_disable_playlist( this_, true ); RETURN_ERR( Rom_load( &this_->rom, data, size, header_size, &this_->header, 0 ) ); return Nsf_post_load( this_ ); } this_->is_extended = 1; blargg_err_t err = Info_load( &this_->info, data, size, this_ ); Nsf_disable_playlist( this_, false ); return err; } blargg_err_t Nsf_post_load( struct Nsf_Emu* this_ ) { RETURN_ERR( check_nsf_header( &this_->header ) ); /* if ( header_.vers != 1 ) warning( "Unknown file version" ); */ // set up data addr_t load_addr = get_le16( this_->header.load_addr ); /* if ( load_addr < (fds_enabled() ? sram_addr : rom_addr) ) warning( "Load address is too low" ); */ Rom_set_addr( &this_->rom, load_addr % bank_size ); /* if ( header_.vers != 1 ) warning( "Unknown file version" ); */ set_play_period( this_, play_period( &this_->header ) ); // sound and memory blargg_err_t err = init_sound( this_ ); if ( err ) return err; // Post load Sound_set_tempo( this_, this_->tempo ); // Remute voices Sound_mute_voices( this_, this_->mute_mask_ ); // Set track_count this_->track_count = this_->header.track_count; // Change clock rate & setup buffer this_->clock_rate__ = (long) (clock_rate( &this_->header ) + 0.5); Buffer_clock_rate( &this_->buf, this_->clock_rate__ ); this_->buf_changed_count = Buffer_channels_changed_count( &this_->buf ); // Clear m3u playlist M3u_clear( &this_->m3u ); return 0; } void Nsf_disable_playlist( struct Nsf_Emu* this_, bool b ) { Info_disable_playlist( &this_->info, b ); this_->track_count = this_->info.track_count; } void Nsf_clear_playlist( struct Nsf_Emu* this_ ) { Nsf_disable_playlist( this_, true ); M3u_clear( &this_->m3u ); } void write_bank( struct Nsf_Emu* this_, int bank, int data ) { // Find bank in ROM int offset = mask_addr( data * bank_size, this_->rom.mask ); /* if ( offset >= rom.size() ) warning( "invalid bank" ); */ void const* rom_data = Rom_at_addr( &this_->rom, offset ); #ifndef NSF_EMU_APU_ONLY if ( bank < bank_count - fds_banks && fds_enabled( this_ ) ) { // TODO: FDS bank switching is kind of hacky, might need to // treat ROM as RAM so changes won't get lost when switching. byte* out = sram( this_ ); if ( bank >= fds_banks ) { out = fdsram( this_ ); bank -= fds_banks; } memcpy( &out [bank * bank_size], rom_data, bank_size ); return; } #endif if ( bank >= fds_banks ) Cpu_map_code( &this_->cpu, (bank + 6) * bank_size, bank_size, rom_data, false ); } void map_memory( struct Nsf_Emu* this_ ) { // Map standard things Cpu_reset( &this_->cpu, unmapped_code( this_ ) ); Cpu_map_code( &this_->cpu, 0, 0x2000, this_->low_ram, low_ram_size ); // mirrored four times Cpu_map_code( &this_->cpu, sram_addr, sram_size, sram( this_ ), 0 ); // Determine initial banks byte banks [bank_count]; static byte const zero_banks [sizeof this_->header.banks] = { 0 }; if ( memcmp( this_->header.banks, zero_banks, sizeof zero_banks ) ) { banks [0] = this_->header.banks [6]; banks [1] = this_->header.banks [7]; memcpy( banks + fds_banks, this_->header.banks, sizeof this_->header.banks ); } else { // No initial banks, so assign them based on load_addr int i, first_bank = (get_addr( this_->header.load_addr ) - sram_addr) / bank_size; unsigned total_banks = this_->rom.size / bank_size; for ( i = bank_count; --i >= 0; ) { int bank = i - first_bank; if ( (unsigned) bank >= total_banks ) bank = 0; banks [i] = bank; } } // Map banks int i; for ( i = (fds_enabled( this_ ) ? 0 : fds_banks); i < bank_count; ++i ) write_bank( this_, i, banks [i] ); // Map FDS RAM if ( fds_enabled( this_ ) ) Cpu_map_code( &this_->cpu, rom_addr, fdsram_size, fdsram( this_ ), 0 ); } void set_voice( struct Nsf_Emu* this_, int i, struct Blip_Buffer* buf, struct Blip_Buffer* left, struct Blip_Buffer* right) { #if defined(ROCKBOX) (void) left; (void) right; #endif if ( i < apu_osc_count ) { Apu_osc_output( &this_->apu, i, buf ); return; } i -= apu_osc_count; #ifndef NSF_EMU_APU_ONLY { if ( vrc6_enabled( this_ ) && (i -= vrc6_osc_count) < 0 ) { Vrc6_osc_output( &this_->vrc6, i + vrc6_osc_count, buf ); return; } if ( fme7_enabled( this_ ) && (i -= fme7_osc_count) < 0 ) { Fme7_osc_output( &this_->fme7, i + fme7_osc_count, buf ); return; } if ( mmc5_enabled( this_ ) && (i -= mmc5_osc_count) < 0 ) { Mmc5_set_output( &this_->mmc5, i + mmc5_osc_count, buf ); return; } if ( fds_enabled( this_ ) && (i -= fds_osc_count) < 0 ) { Fds_set_output( &this_->fds, i + fds_osc_count, buf ); return; } if ( namco_enabled( this_ ) && (i -= namco_osc_count) < 0 ) { Namco_osc_output( &this_->namco, i + namco_osc_count, buf ); return; } if ( vrc7_enabled( this_ ) && (i -= vrc7_osc_count) < 0 ) { Vrc7_set_output( &this_->vrc7, i + vrc7_osc_count, buf ); return; } } #endif } // Emulation // Music Emu blargg_err_t Nsf_set_sample_rate( struct Nsf_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, 80 ); this_->sample_rate = rate; return 0; } void Sound_mute_voice( struct Nsf_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 Nsf_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) ) { set_voice( this_, 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 set_voice( this_, i, ch.center, ch.left, ch.right ); } } } void Sound_set_tempo( struct Nsf_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; set_play_period( this_, (int) (play_period( &this_->header ) / t) ); Apu_set_tempo( &this_->apu, t ); #ifndef NSF_EMU_APU_ONLY if ( fds_enabled( this_ ) ) Fds_set_tempo( &this_->fds, t ); #endif } inline void push_byte( struct Nsf_Emu* this_, int b ) { this_->low_ram [0x100 + this_->cpu.r.sp--] = b; } // Jumps to routine, given pointer to address in file header. Pushes idle_addr // as return address, NOT old PC. void jsr_then_stop( struct Nsf_Emu* this_, byte const addr [] ) { this_->cpu.r.pc = get_addr( addr ); push_byte( this_, (idle_addr - 1) >> 8 ); push_byte( this_, (idle_addr - 1) ); } int cpu_read( struct Nsf_Emu* this_, addr_t addr ) { #ifndef NSF_EMU_APU_ONLY { if ( namco_enabled( this_ ) && addr == namco_data_reg_addr ) return Namco_read_data( &this_->namco ); if ( fds_enabled( this_ ) && (unsigned) (addr - fds_io_addr) < fds_io_size ) return Fds_read( &this_->fds, Cpu_time( &this_->cpu ), addr ); if ( mmc5_enabled( this_ ) ) { int i = addr - 0x5C00; if ( (unsigned) i < mmc5_exram_size ) return this_->mmc5.exram [i]; int m = addr - 0x5205; if ( (unsigned) m < 2 ) return (this_->mmc5_mul [0] * this_->mmc5_mul [1]) >> (m * 8) & 0xFF; } } #endif /* Unmapped read */ return addr >> 8; } int unmapped_read( struct Nsf_Emu* this_, addr_t addr ) { switch ( addr ) { case 0x2002: case 0x4016: case 0x4017: return addr >> 8; } // Unmapped read return addr >> 8; } void cpu_write( struct Nsf_Emu* this_, addr_t addr, int data ) { #ifndef NSF_EMU_APU_ONLY { nes_time_t time = Cpu_time( &this_->cpu ); if ( fds_enabled( this_) && (unsigned) (addr - fds_io_addr) < fds_io_size ) { Fds_write( &this_->fds, time, addr, data ); return; } if ( namco_enabled( this_) ) { if ( addr == namco_addr_reg_addr ) { Namco_write_addr( &this_->namco, data ); return; } if ( addr == namco_data_reg_addr ) { Namco_write_data( &this_->namco, time, data ); return; } } if ( vrc6_enabled( this_) ) { int reg = addr & (vrc6_addr_step - 1); int osc = (unsigned) (addr - vrc6_base_addr) / vrc6_addr_step; if ( (unsigned) osc < vrc6_osc_count && (unsigned) reg < vrc6_reg_count ) { Vrc6_write_osc( &this_->vrc6, time, osc, reg, data ); return; } } if ( fme7_enabled( this_) && addr >= fme7_latch_addr ) { switch ( addr & fme7_addr_mask ) { case fme7_latch_addr: Fme7_write_latch( &this_->fme7, data ); return; case fme7_data_addr: Fme7_write_data( &this_->fme7, time, data ); return; } } if ( mmc5_enabled( this_) ) { if ( (unsigned) (addr - mmc5_regs_addr) < mmc5_regs_size ) { Mmc5_write_register( &this_->mmc5, time, addr, data ); return; } int m = addr - 0x5205; if ( (unsigned) m < 2 ) { this_->mmc5_mul [m] = data; return; } int i = addr - 0x5C00; if ( (unsigned) i < mmc5_exram_size ) { this_->mmc5.exram [i] = data; return; } } if ( vrc7_enabled( this_) ) { if ( addr == 0x9010 ) { Vrc7_write_reg( &this_->vrc7, data ); return; } if ( (unsigned) (addr - 0x9028) <= 0x08 ) { Vrc7_write_data( &this_->vrc7, time, data ); return; } } } #endif // Unmapped_write } void unmapped_write( struct Nsf_Emu* this_, addr_t addr, int data ) { switch ( addr ) { case 0x8000: // some write to $8000 and $8001 repeatedly case 0x8001: case 0x4800: // probably namco sound mistakenly turned on in MCK case 0xF800: case 0xFFF8: // memory mapper? return; } if ( mmc5_enabled( this_ ) && addr == 0x5115 ) return; // FDS memory if ( fds_enabled( this_ ) && (unsigned) (addr - 0x8000) < 0x6000 ) return; } void fill_buf( struct Nsf_Emu* this_ ); blargg_err_t Nsf_start_track( struct Nsf_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; } // Else try with nsfe playlist else track = Info_remap_track( &this_->info, track ); this_->current_track = track; Buffer_clear( &this_->buf ); #ifndef NSF_EMU_APU_ONLY if ( mmc5_enabled( this_ ) ) { this_->mmc5_mul [0] = 0; this_->mmc5_mul [1] = 0; memset( this_->mmc5.exram, 0, mmc5_exram_size ); } if ( fds_enabled( this_ ) ) Fds_reset( &this_->fds ); if ( namco_enabled( this_ ) ) Namco_reset( &this_->namco ); if ( vrc6_enabled( this_ ) ) Vrc6_reset( &this_->vrc6 ); if ( fme7_enabled( this_ ) ) Fme7_reset( &this_->fme7 ); if ( mmc5_enabled( this_ ) ) Apu_reset( &this_->mmc5.apu, false, 0 ); if ( vrc7_enabled( this_ ) ) Vrc7_reset( &this_->vrc7 ); #endif int speed_flags = 0; #ifdef NSF_EMU_EXTRA_FLAGS speed_flags = this_->header.speed_flags; #endif Apu_reset( &this_->apu, pal_only( &this_->header ), (speed_flags & 0x20) ? 0x3F : 0 ); Apu_write_register( &this_->apu, 0, 0x4015, 0x0F ); Apu_write_register( &this_->apu, 0, 0x4017, (speed_flags & 0x10) ? 0x80 : 0 ); memset( unmapped_code( this_ ), halt_opcode, unmapped_size ); memset( this_->low_ram, 0, low_ram_size ); memset( sram( this_ ), 0, sram_size ); map_memory( this_ ); // Arrange time of first call to play routine this_->play_extra = 0; this_->next_play = this_->play_period; this_->play_delay = initial_play_delay; this_->saved_state.pc = idle_addr; // Setup for call to init routine this_->cpu.r.a = track; this_->cpu.r.x = pal_only( &this_->header ); this_->cpu.r.sp = 0xFF; jsr_then_stop( this_, this_->header.init_addr ); /* if ( this_->cpu.r.pc < get_addr( header.load_addr ) ) warning( "Init address < load address" ); */ 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; } void run_once( struct Nsf_Emu* this_, nes_time_t end ) { // Emulate until next play call if possible if ( run_cpu_until( this_, min( this_->next_play, end ) ) ) { // Halt instruction encountered if ( this_->cpu.r.pc != idle_addr ) { // special_event( "illegal instruction" ); Cpu_set_time( &this_->cpu, this_->cpu.end_time ); return; } // Init/play routine returned this_->play_delay = 1; // play can now be called regularly if ( this_->saved_state.pc == idle_addr ) { // nothing to run nes_time_t t = this_->cpu.end_time; if ( Cpu_time( &this_->cpu ) < t ) Cpu_set_time( &this_->cpu, t ); } else { // continue init routine that was interrupted by play routine this_->cpu.r = this_->saved_state; this_->saved_state.pc = idle_addr; } } if ( Cpu_time( &this_->cpu ) >= this_->next_play ) { // Calculate time of next call to play routine this_->play_extra ^= 1; // extra clock every other call this_->next_play += this_->play_period + this_->play_extra; // Call routine if ready if ( this_->play_delay && !--this_->play_delay ) { // Save state if init routine is still running if ( this_->cpu.r.pc != idle_addr ) { check( this_->saved_state.pc == idle_addr ); this_->saved_state = this_->cpu.r; // special_event( "play called during init" ); } jsr_then_stop( this_, this_->header.play_addr ); } } } void run_until( struct Nsf_Emu* this_, nes_time_t end ) { while ( Cpu_time( &this_->cpu ) < end ) run_once( this_, end ); } void end_frame( struct Nsf_Emu* this_, nes_time_t end ) { if ( Cpu_time( &this_->cpu ) < end ) run_until( this_, end ); Cpu_adjust_time( &this_->cpu, -end ); // Localize to new time frame this_->next_play -= end; check( this_->next_play >= 0 ); if ( this_->next_play < 0 ) this_->next_play = 0; Apu_end_frame( &this_->apu, end ); #ifndef NSF_EMU_APU_ONLY if ( fds_enabled( this_ ) ) Fds_end_frame( &this_->fds, end ); if ( fme7_enabled( this_ ) ) Fme7_end_frame( &this_->fme7, end ); if ( mmc5_enabled( this_ ) ) Apu_end_frame( &this_->mmc5.apu, end ); if ( namco_enabled( this_ ) ) Namco_end_frame( &this_->namco, end ); if ( vrc6_enabled( this_ ) ) Vrc6_end_frame( &this_->vrc6, end ); if ( vrc7_enabled( this_ ) ) Vrc7_end_frame( &this_->vrc7, end ); #endif } // Tell/Seek blargg_long msec_to_samples( long sample_rate, blargg_long msec ) { blargg_long sec = msec / 1000; msec -= sec * 1000; return (sec * sample_rate + msec * sample_rate / 1000) * stereo; } long Track_tell( struct Nsf_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 Nsf_Emu* this_, long msec ) { blargg_long time = msec_to_samples( this_->sample_rate, msec ); if ( time < this_->out_time ) RETURN_ERR( Nsf_start_track( this_, this_->current_track ) ); return Track_skip( this_, time - this_->out_time ); } blargg_err_t skip_( struct Nsf_Emu* this_, long count ); ICODE_ATTR blargg_err_t Track_skip( struct Nsf_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; } blargg_err_t play_( struct Nsf_Emu* this_, long count, sample_t* out ); ICODE_ATTR blargg_err_t skip_( struct Nsf_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; } // Fading void Track_set_fade( struct Nsf_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( this_->sample_rate, start_msec ); } // 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 Nsf_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 Nsf_Emu* this_, long count, sample_t* out ); ICODE_ATTR void emu_play( struct Nsf_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 Nsf_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 Nsf_play( struct Nsf_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 //debug_printf( "%*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; } blargg_err_t play_( struct Nsf_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 - 100; RETURN_ERR( run_clocks( this_, &clocks_emulated, msec ) ); assert( clocks_emulated ); Buffer_end_frame( &this_->buf, clocks_emulated ); } } return 0; } blargg_err_t run_clocks( struct Nsf_Emu* this_, blip_time_t* duration, int msec ) { #if defined(ROCKBOX) (void) msec; #endif end_frame( this_, *duration ); return 0; }