cog/Frameworks/GME/gme/Classic_Emu.cpp

125 lines
3.3 KiB
C++
Raw Normal View History

2013-09-28 03:24:23 +00:00
// Game_Music_Emu $vers. http://www.slack.net/~ant/
2007-10-11 23:11:58 +00:00
#include "Classic_Emu.h"
#include "Multi_Buffer.h"
2013-09-28 03:24:23 +00:00
/* Copyright (C) 2003-2008 Shay Green. This module is free software; you
2007-10-11 23:11:58 +00:00
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"
Classic_Emu::Classic_Emu()
{
2013-09-28 03:24:23 +00:00
buf = NULL;
stereo_buffer = NULL;
voice_types = NULL;
2007-10-11 23:11:58 +00:00
// avoid inconsistency in our duplicated constants
assert( (int) wave_type == (int) Multi_Buffer::wave_type );
assert( (int) noise_type == (int) Multi_Buffer::noise_type );
assert( (int) mixed_type == (int) Multi_Buffer::mixed_type );
}
Classic_Emu::~Classic_Emu()
{
delete stereo_buffer;
2013-09-28 03:24:23 +00:00
delete effects_buffer_;
effects_buffer_ = NULL;
2007-10-11 23:11:58 +00:00
}
void Classic_Emu::set_equalizer_( equalizer_t const& eq )
{
Music_Emu::set_equalizer_( eq );
update_eq( eq.treble );
if ( buf )
2013-09-28 03:24:23 +00:00
buf->bass_freq( (int) equalizer().bass );
2007-10-11 23:11:58 +00:00
}
2013-09-28 03:24:23 +00:00
blargg_err_t Classic_Emu::set_sample_rate_( int rate )
2007-10-11 23:11:58 +00:00
{
if ( !buf )
{
if ( !stereo_buffer )
CHECK_ALLOC( stereo_buffer = BLARGG_NEW Stereo_Buffer );
buf = stereo_buffer;
}
return buf->set_sample_rate( rate, 1000 / 20 );
}
void Classic_Emu::mute_voices_( int mask )
{
Music_Emu::mute_voices_( mask );
for ( int i = voice_count(); i--; )
{
if ( mask & (1 << i) )
{
2013-09-28 03:24:23 +00:00
set_voice( i, NULL, NULL, NULL );
2007-10-11 23:11:58 +00:00
}
else
{
2013-09-28 03:24:23 +00:00
Multi_Buffer::channel_t ch = buf->channel( i );
2007-10-11 23:11:58 +00:00
assert( (ch.center && ch.left && ch.right) ||
(!ch.center && !ch.left && !ch.right) ); // all or nothing
set_voice( i, ch.center, ch.left, ch.right );
}
}
}
2013-09-28 03:24:23 +00:00
void Classic_Emu::change_clock_rate( int rate )
2007-10-11 23:11:58 +00:00
{
clock_rate_ = rate;
buf->clock_rate( rate );
}
2013-09-28 03:24:23 +00:00
blargg_err_t Classic_Emu::setup_buffer( int rate )
2007-10-11 23:11:58 +00:00
{
change_clock_rate( rate );
2013-09-28 03:24:23 +00:00
RETURN_ERR( buf->set_channel_count( voice_count(), voice_types ) );
2007-10-11 23:11:58 +00:00
set_equalizer( equalizer() );
buf_changed_count = buf->channels_changed_count();
2013-09-28 03:24:23 +00:00
return blargg_ok;
2007-10-11 23:11:58 +00:00
}
blargg_err_t Classic_Emu::start_track_( int track )
{
RETURN_ERR( Music_Emu::start_track_( track ) );
buf->clear();
2013-09-28 03:24:23 +00:00
return blargg_ok;
2007-10-11 23:11:58 +00:00
}
2013-09-28 03:24:23 +00:00
blargg_err_t Classic_Emu::play_( int count, sample_t out [] )
2007-10-11 23:11:58 +00:00
{
2013-09-28 03:24:23 +00:00
// read from buffer, then refill buffer and repeat if necessary
int remain = count;
2007-10-11 23:11:58 +00:00
while ( remain )
{
2013-09-28 03:24:23 +00:00
buf->disable_immediate_removal();
2007-10-11 23:11:58 +00:00
remain -= buf->read_samples( &out [count - remain], remain );
if ( remain )
{
if ( buf_changed_count != buf->channels_changed_count() )
{
buf_changed_count = buf->channels_changed_count();
remute_voices();
}
2013-09-28 03:24:23 +00:00
// TODO: use more accurate length calculation
2007-10-11 23:11:58 +00:00
int msec = buf->length();
2013-09-28 03:24:23 +00:00
blip_time_t clocks_emulated = msec * clock_rate_ / 1000 - 100;
2007-10-11 23:11:58 +00:00
RETURN_ERR( run_clocks( clocks_emulated, msec ) );
assert( clocks_emulated );
buf->end_frame( clocks_emulated );
}
}
2013-09-28 03:24:23 +00:00
return blargg_ok;
2007-10-11 23:11:58 +00:00
}