mirror of
https://github.com/libretro/beetle-pce-fast-libretro.git
synced 2024-11-26 17:40:40 +00:00
Add resampler and original Blip_Buffer in case we ever need it
This commit is contained in:
parent
c0f2e66ddd
commit
0290b33324
21
Makefile
21
Makefile
@ -91,6 +91,7 @@ else ifeq ($(core), wswan)
|
||||
NEED_BLIP = 1
|
||||
NEED_CD = 1
|
||||
NEED_THREADING = 1
|
||||
NEED_RESAMPLER = 1
|
||||
CORE_DEFINE := -DWANT_WSWAN_EMU
|
||||
CORE_DIR := $(MEDNAFEN_DIR)/wswan
|
||||
|
||||
@ -106,6 +107,18 @@ CORE_SOURCES := $(CORE_DIR)/gfx.cpp \
|
||||
TARGET_NAME := mednafen_wswan_libretro
|
||||
endif
|
||||
|
||||
ifeq ($(NEED_RESAMPLER), 1)
|
||||
FLAGS += -DNEED_RESAMPLER
|
||||
RESAMPLER_SOURCES += $(MEDNAFEN_DIR)/sound/Fir_Resampler.cpp
|
||||
ifeq ($(NEED_BLIP), 1)
|
||||
RESAMPLER_SOURCES += $(MEDNAFEN_DIR)/sound/Blip_Buffer_orig.cpp
|
||||
endif
|
||||
else
|
||||
ifeq ($(NEED_BLIP), 1)
|
||||
RESAMPLER_SOURCES += $(MEDNAFEN_DIR)/sound/Blip_Buffer.cpp
|
||||
endif
|
||||
endif
|
||||
|
||||
CORE_INCDIR := -I$(CORE_DIR)
|
||||
|
||||
ifeq ($(platform), unix)
|
||||
@ -209,9 +222,6 @@ TREMOR_SRC := $(wildcard $(MEDNAFEN_DIR)/tremor/*.c)
|
||||
FLAGS += -DNEED_CD
|
||||
endif
|
||||
|
||||
ifeq ($(NEED_BLIP), 1)
|
||||
BLIP_BUFFER += $(MEDNAFEN_DIR)/sound/Blip_Buffer.cpp
|
||||
endif
|
||||
|
||||
MEDNAFEN_SOURCES := $(MEDNAFEN_DIR)/cdrom/cdromif.cpp \
|
||||
$(MEDNAFEN_DIR)/mednafen.cpp \
|
||||
@ -231,7 +241,7 @@ MEDNAFEN_SOURCES := $(MEDNAFEN_DIR)/cdrom/cdromif.cpp \
|
||||
$(MEDNAFEN_DIR)/video/video.cpp \
|
||||
$(MEDNAFEN_DIR)/video/Deinterlacer.cpp \
|
||||
$(MEDNAFEN_DIR)/video/surface.cpp \
|
||||
$(BLIP_BUFFER) \
|
||||
$(RESAMPLER_SOURCES) \
|
||||
$(MEDNAFEN_DIR)/sound/Stereo_Buffer.cpp \
|
||||
$(MEDNAFEN_DIR)/file.cpp \
|
||||
$(MEDNAFEN_DIR)/okiadpcm.cpp \
|
||||
@ -248,6 +258,8 @@ SOURCES_C := $(MEDNAFEN_DIR)/trio/trio.c \
|
||||
$(MEDNAFEN_DIR)/trio/triostr.c
|
||||
|
||||
SOURCES := $(LIBRETRO_SOURCES) $(CORE_SOURCES) $(MEDNAFEN_SOURCES) $(HW_CPU_SOURCES) $(HW_MISC_SOURCES) $(HW_SOUND_SOURCES) $(HW_VIDEO_SOURCES)
|
||||
|
||||
|
||||
OBJECTS := $(SOURCES:.cpp=.o) $(SOURCES_C:.c=.o)
|
||||
|
||||
all: $(TARGET)
|
||||
@ -292,6 +304,7 @@ ifeq ($(NEED_BPP), 32)
|
||||
FLAGS += -DWANT_32BPP
|
||||
endif
|
||||
|
||||
|
||||
CXXFLAGS += $(FLAGS)
|
||||
CFLAGS += $(FLAGS) -std=gnu99
|
||||
|
||||
|
20
libretro.cpp
20
libretro.cpp
@ -15,7 +15,6 @@ static retro_input_state_t input_state_cb;
|
||||
|
||||
static MDFN_Surface *surf;
|
||||
|
||||
static uint32_t *mednafen_buf;
|
||||
static bool failed_init;
|
||||
|
||||
std::string retro_base_directory;
|
||||
@ -63,6 +62,12 @@ std::string retro_base_name;
|
||||
#define FB_WIDTH 224
|
||||
#define FB_HEIGHT 144
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef WANT_16BPP
|
||||
static uint16_t mednafen_buf[FB_WIDTH * FB_HEIGHT];
|
||||
#else
|
||||
static uint32_t mednafen_buf[FB_WIDTH * FB_HEIGHT];
|
||||
#endif
|
||||
const char *mednafen_core_str = MEDNAFEN_CORE_NAME;
|
||||
|
||||
@ -146,14 +151,9 @@ bool retro_load_game(const struct retro_game_info *info)
|
||||
if (!game)
|
||||
return false;
|
||||
|
||||
int fbWidth = FB_WIDTH;
|
||||
int fbHeight = FB_HEIGHT;
|
||||
int fbSize = fbWidth * fbHeight;
|
||||
|
||||
mednafen_buf = new uint32_t[fbSize];
|
||||
MDFN_PixelFormat pix_fmt(MDFN_COLORSPACE_RGB, 16, 8, 0, 24);
|
||||
|
||||
surf = new MDFN_Surface(mednafen_buf, fbWidth, fbHeight, FB_WIDTH, pix_fmt);
|
||||
surf = new MDFN_Surface(mednafen_buf, FB_WIDTH, FB_HEIGHT, FB_WIDTH, pix_fmt);
|
||||
|
||||
return game;
|
||||
}
|
||||
@ -337,8 +337,12 @@ void retro_run()
|
||||
update_input();
|
||||
|
||||
static int16_t sound_buf[0x10000];
|
||||
#if defined(WANT_WSWAN_EMU)
|
||||
static MDFN_Rect rects[FB_WIDTH];
|
||||
#else
|
||||
static MDFN_Rect rects[FB_HEIGHT];
|
||||
#ifdef WANT_PSX_EMU
|
||||
#endif
|
||||
#if defined(WANT_PSX_EMU) || defined(WANT_WSWAN_EMU)
|
||||
rects[0].w = ~0;
|
||||
#endif
|
||||
|
||||
|
@ -41,7 +41,9 @@
|
||||
#include "mempatcher.h"
|
||||
#include "md5.h"
|
||||
#include "clamp.h"
|
||||
|
||||
#ifdef NEED_RESAMPLER
|
||||
#include "Fir_Resampler.h"
|
||||
#endif
|
||||
|
||||
static const char *CSD_forcemono = gettext_noop("Force monophonic sound output.");
|
||||
static const char *CSD_enable = gettext_noop("Enable (automatic) usage of this module.");
|
||||
@ -728,7 +730,107 @@ void MDFNI_Kill(void)
|
||||
/* save settings */
|
||||
}
|
||||
|
||||
#if defined(WANT_LYNX_EMU) || defined(WANT_NES_EMU)
|
||||
#if defined(NEED_RESAMPLER)
|
||||
static double multiplier_save, volume_save;
|
||||
static Fir_Resampler<16> ff_resampler;
|
||||
|
||||
static void ProcessAudio(EmulateSpecStruct *espec)
|
||||
{
|
||||
if(espec->SoundVolume != 1)
|
||||
volume_save = espec->SoundVolume;
|
||||
|
||||
if(espec->soundmultiplier != 1)
|
||||
multiplier_save = espec->soundmultiplier;
|
||||
|
||||
if(espec->SoundBuf && espec->SoundBufSize)
|
||||
{
|
||||
int16 *const SoundBuf = espec->SoundBuf + espec->SoundBufSizeALMS * MDFNGameInfo->soundchan;
|
||||
int32 SoundBufSize = espec->SoundBufSize - espec->SoundBufSizeALMS;
|
||||
const int32 SoundBufMaxSize = espec->SoundBufMaxSize - espec->SoundBufSizeALMS;
|
||||
|
||||
if(multiplier_save != LastSoundMultiplier)
|
||||
{
|
||||
ff_resampler.time_ratio(multiplier_save, 0.9965);
|
||||
LastSoundMultiplier = multiplier_save;
|
||||
}
|
||||
|
||||
if(multiplier_save != 1)
|
||||
{
|
||||
{
|
||||
if(MDFNGameInfo->soundchan == 2)
|
||||
{
|
||||
for(int i = 0; i < SoundBufSize * 2; i++)
|
||||
ff_resampler.buffer()[i] = SoundBuf[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < SoundBufSize; i++)
|
||||
{
|
||||
ff_resampler.buffer()[i * 2] = SoundBuf[i];
|
||||
ff_resampler.buffer()[i * 2 + 1] = 0;
|
||||
}
|
||||
}
|
||||
ff_resampler.write(SoundBufSize * 2);
|
||||
|
||||
int avail = ff_resampler.avail();
|
||||
int real_read = std::min((int)(SoundBufMaxSize * MDFNGameInfo->soundchan), avail);
|
||||
|
||||
if(MDFNGameInfo->soundchan == 2)
|
||||
SoundBufSize = ff_resampler.read(SoundBuf, real_read ) >> 1;
|
||||
else
|
||||
SoundBufSize = ff_resampler.read_mono_hack(SoundBuf, real_read );
|
||||
|
||||
avail -= real_read;
|
||||
|
||||
if(avail > 0)
|
||||
{
|
||||
printf("ff_resampler.avail() > espec->SoundBufMaxSize * MDFNGameInfo->soundchan - %d\n", avail);
|
||||
ff_resampler.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(volume_save != 1)
|
||||
{
|
||||
if(volume_save < 1)
|
||||
{
|
||||
int volume = (int)(16384 * volume_save);
|
||||
|
||||
for(int i = 0; i < SoundBufSize * MDFNGameInfo->soundchan; i++)
|
||||
SoundBuf[i] = (SoundBuf[i] * volume) >> 14;
|
||||
}
|
||||
else
|
||||
{
|
||||
int volume = (int)(256 * volume_save);
|
||||
|
||||
for(int i = 0; i < SoundBufSize * MDFNGameInfo->soundchan; i++)
|
||||
{
|
||||
int temp = ((SoundBuf[i] * volume) >> 8) + 32768;
|
||||
|
||||
temp = clamp_to_u16(temp);
|
||||
|
||||
SoundBuf[i] = temp - 32768;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Optimize this.
|
||||
if(MDFNGameInfo->soundchan == 2 && MDFN_GetSettingB(std::string(std::string(MDFNGameInfo->shortname) + ".forcemono").c_str()))
|
||||
{
|
||||
for(int i = 0; i < SoundBufSize * MDFNGameInfo->soundchan; i += 2)
|
||||
{
|
||||
// We should use division instead of arithmetic right shift for correctness(rounding towards 0 instead of negative infinitininintinity), but I like speed.
|
||||
int32 mixed = (SoundBuf[i + 0] + SoundBuf[i + 1]) >> 1;
|
||||
|
||||
SoundBuf[i + 0] =
|
||||
SoundBuf[i + 1] = mixed;
|
||||
}
|
||||
}
|
||||
|
||||
espec->SoundBufSize = espec->SoundBufSizeALMS + SoundBufSize;
|
||||
} // end to: if(espec->SoundBuf && espec->SoundBufSize)
|
||||
}
|
||||
#elif defined(WANT_LYNX_EMU) || defined(WANT_NES_EMU)
|
||||
static void ProcessAudio(EmulateSpecStruct *espec)
|
||||
{
|
||||
if(espec->SoundBuf && espec->SoundBufSize)
|
||||
@ -790,6 +892,10 @@ void MDFNI_Emulate(EmulateSpecStruct *espec)
|
||||
{
|
||||
espec->SoundFormatChanged = true;
|
||||
last_sound_rate = espec->SoundRate;
|
||||
|
||||
#ifdef NEED_RESAMPLER
|
||||
ff_resampler.buffer_size((espec->SoundRate / 2) * 2);
|
||||
#endif
|
||||
}
|
||||
|
||||
espec->NeedSoundReverse = false;
|
||||
|
445
mednafen/sound/Blip_Buffer_orig.cpp
Normal file
445
mednafen/sound/Blip_Buffer_orig.cpp
Normal file
@ -0,0 +1,445 @@
|
||||
// Blip_Buffer 0.4.1. http://www.slack.net/~ant/
|
||||
|
||||
#include <blip/Blip_Buffer.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
|
||||
|
||||
#ifdef GEKKO
|
||||
#define LLONG_MAX __LONG_LONG_MAX__
|
||||
#define ULLONG_MAX (LLONG_MAX * 2ULL + 1)
|
||||
#endif
|
||||
|
||||
int const silent_buf_size = 1; // size used for Silent_Blip_Buffer
|
||||
|
||||
Blip_Buffer::Blip_Buffer()
|
||||
{
|
||||
factor_ = (blip_u64)ULLONG_MAX;
|
||||
offset_ = 0;
|
||||
buffer_ = 0;
|
||||
buffer_size_ = 0;
|
||||
sample_rate_ = 0;
|
||||
reader_accum_ = 0;
|
||||
bass_shift_ = 0;
|
||||
clock_rate_ = 0;
|
||||
bass_freq_ = 16;
|
||||
length_ = 0;
|
||||
|
||||
// assumptions code makes about implementation-defined features
|
||||
#ifndef NDEBUG
|
||||
// right shift of negative value preserves sign
|
||||
buf_t_ i = -0x7FFFFFFE;
|
||||
|
||||
// casting to short truncates to 16 bits and sign-extends
|
||||
i = 0x18000;
|
||||
#endif
|
||||
}
|
||||
|
||||
Blip_Buffer::~Blip_Buffer()
|
||||
{
|
||||
if ( buffer_size_ != silent_buf_size )
|
||||
free( buffer_ );
|
||||
}
|
||||
|
||||
Silent_Blip_Buffer::Silent_Blip_Buffer()
|
||||
{
|
||||
factor_ = 0;
|
||||
buffer_ = buf;
|
||||
buffer_size_ = silent_buf_size;
|
||||
memset( buf, 0, sizeof buf ); // in case machine takes exception for signed overflow
|
||||
}
|
||||
|
||||
void Blip_Buffer::clear( int entire_buffer )
|
||||
{
|
||||
offset_ = 0;
|
||||
reader_accum_ = 0;
|
||||
modified_ = 0;
|
||||
if ( buffer_ )
|
||||
{
|
||||
long count = (entire_buffer ? buffer_size_ : samples_avail());
|
||||
memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) );
|
||||
}
|
||||
}
|
||||
|
||||
Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec )
|
||||
{
|
||||
if ( buffer_size_ == silent_buf_size )
|
||||
{
|
||||
return "Internal (tried to resize Silent_Blip_Buffer)";
|
||||
}
|
||||
|
||||
// start with maximum length that resampled time can represent
|
||||
blip_s64 new_size = (ULLONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
|
||||
|
||||
// simple safety check, since code elsewhere may not be safe for sizes approaching (2 ^ 31).
|
||||
if(new_size > ((1LL << 30) - 1))
|
||||
new_size = (1LL << 30) - 1;
|
||||
|
||||
if ( msec != blip_max_length )
|
||||
{
|
||||
blip_s64 s = ((blip_s64)new_rate * (msec + 1) + 999) / 1000;
|
||||
if ( s < new_size )
|
||||
new_size = s;
|
||||
}
|
||||
|
||||
if ( buffer_size_ != new_size )
|
||||
{
|
||||
void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ );
|
||||
if ( !p )
|
||||
return "Out of memory";
|
||||
|
||||
//if(new_size > buffer_size_)
|
||||
// memset(buffer_ + buffer_size_, 0, (new_size + blip_buffer_extra_) * sizeof *buffer_
|
||||
|
||||
buffer_ = (buf_t_*) p;
|
||||
}
|
||||
|
||||
buffer_size_ = new_size;
|
||||
|
||||
// update things based on the sample rate
|
||||
sample_rate_ = new_rate;
|
||||
length_ = new_size * 1000 / new_rate - 1;
|
||||
if ( clock_rate_ )
|
||||
clock_rate( clock_rate_ );
|
||||
bass_freq( bass_freq_ );
|
||||
|
||||
clear();
|
||||
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const
|
||||
{
|
||||
double ratio = (double) sample_rate_ / rate;
|
||||
blip_s64 factor = (blip_s64) floor( ratio * (1LL << BLIP_BUFFER_ACCURACY) + 0.5 );
|
||||
return (blip_resampled_time_t) factor;
|
||||
}
|
||||
|
||||
void Blip_Buffer::bass_freq( int freq )
|
||||
{
|
||||
bass_freq_ = freq;
|
||||
int shift = 31;
|
||||
if ( freq > 0 )
|
||||
{
|
||||
shift = 13;
|
||||
long f = (freq << 16) / sample_rate_;
|
||||
while ( (f >>= 1) && --shift ) { }
|
||||
}
|
||||
bass_shift_ = shift;
|
||||
}
|
||||
|
||||
void Blip_Buffer::end_frame( blip_time_t t )
|
||||
{
|
||||
offset_ += t * factor_;
|
||||
}
|
||||
|
||||
void Blip_Buffer::remove_silence( long count )
|
||||
{
|
||||
offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
|
||||
}
|
||||
|
||||
long Blip_Buffer::count_samples( blip_time_t t ) const
|
||||
{
|
||||
unsigned long last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY;
|
||||
unsigned long first_sample = offset_ >> BLIP_BUFFER_ACCURACY;
|
||||
return (long) (last_sample - first_sample);
|
||||
}
|
||||
|
||||
blip_time_t Blip_Buffer::count_clocks( long count ) const
|
||||
{
|
||||
if ( !factor_ )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( count > buffer_size_ )
|
||||
count = buffer_size_;
|
||||
blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
|
||||
return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_);
|
||||
}
|
||||
|
||||
void Blip_Buffer::remove_samples( long count )
|
||||
{
|
||||
if ( count )
|
||||
{
|
||||
remove_silence( count );
|
||||
|
||||
// copy remaining samples to beginning and clear old samples
|
||||
long remain = samples_avail() + blip_buffer_extra_;
|
||||
memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ );
|
||||
memset( buffer_ + remain, 0, count * sizeof *buffer_ );
|
||||
}
|
||||
}
|
||||
|
||||
// Blip_Synth_
|
||||
|
||||
Blip_Synth_Fast_::Blip_Synth_Fast_()
|
||||
{
|
||||
buf = 0;
|
||||
last_amp = 0;
|
||||
delta_factor = 0;
|
||||
}
|
||||
|
||||
void Blip_Synth_Fast_::volume_unit( double new_unit )
|
||||
{
|
||||
delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5);
|
||||
}
|
||||
|
||||
#if !BLIP_BUFFER_FAST
|
||||
|
||||
Blip_Synth_::Blip_Synth_( short* p, int w ) :
|
||||
impulses( p ),
|
||||
width( w )
|
||||
{
|
||||
volume_unit_ = 0.0;
|
||||
kernel_unit = 0;
|
||||
buf = 0;
|
||||
last_amp = 0;
|
||||
delta_factor = 0;
|
||||
}
|
||||
|
||||
#undef PI
|
||||
#define PI 3.1415926535897932384626433832795029
|
||||
|
||||
static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff )
|
||||
{
|
||||
if ( cutoff >= 0.999 )
|
||||
cutoff = 0.999;
|
||||
|
||||
if ( treble < -300.0 )
|
||||
treble = -300.0;
|
||||
if ( treble > 5.0 )
|
||||
treble = 5.0;
|
||||
|
||||
double const maxh = 4096.0;
|
||||
double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) );
|
||||
double const pow_a_n = pow( rolloff, maxh - maxh * cutoff );
|
||||
double const to_angle = PI / 2 / maxh / oversample;
|
||||
for ( int i = 0; i < count; i++ )
|
||||
{
|
||||
double angle = ((i - count) * 2 + 1) * to_angle;
|
||||
double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle );
|
||||
double cos_nc_angle = cos( maxh * cutoff * angle );
|
||||
double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle );
|
||||
double cos_angle = cos( angle );
|
||||
|
||||
c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle;
|
||||
double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle);
|
||||
double b = 2.0 - cos_angle - cos_angle;
|
||||
double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle;
|
||||
|
||||
out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d
|
||||
}
|
||||
}
|
||||
|
||||
void blip_eq_t::generate( float* out, int count ) const
|
||||
{
|
||||
// lower cutoff freq for narrow kernels with their wider transition band
|
||||
// (8 points->1.49, 16 points->1.15)
|
||||
double oversample = blip_res * 2.25 / count + 0.85;
|
||||
double half_rate = sample_rate * 0.5;
|
||||
if ( cutoff_freq )
|
||||
oversample = half_rate / cutoff_freq;
|
||||
double cutoff = rolloff_freq * oversample / half_rate;
|
||||
|
||||
gen_sinc( out, count, blip_res * oversample, treble, cutoff );
|
||||
|
||||
// apply (half of) hamming window
|
||||
double to_fraction = PI / (count - 1);
|
||||
for ( int i = count; i--; )
|
||||
out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction );
|
||||
}
|
||||
|
||||
void Blip_Synth_::adjust_impulse()
|
||||
{
|
||||
// sum pairs for each phase and add error correction to end of first half
|
||||
int const size = impulses_size();
|
||||
for ( int p = blip_res; p-- >= blip_res / 2; )
|
||||
{
|
||||
int p2 = blip_res - 2 - p;
|
||||
long error = kernel_unit;
|
||||
for ( int i = 1; i < size; i += blip_res )
|
||||
{
|
||||
error -= impulses [i + p ];
|
||||
error -= impulses [i + p2];
|
||||
}
|
||||
if ( p == p2 )
|
||||
error /= 2; // phase = 0.5 impulse uses same half for both sides
|
||||
impulses [size - blip_res + p] += (short) error;
|
||||
//printf( "error: %ld\n", error );
|
||||
}
|
||||
|
||||
//for ( int i = blip_res; i--; printf( "\n" ) )
|
||||
// for ( int j = 0; j < width / 2; j++ )
|
||||
// printf( "%5ld,", impulses [j * blip_res + i + 1] );
|
||||
}
|
||||
|
||||
void Blip_Synth_::treble_eq( blip_eq_t const& eq )
|
||||
{
|
||||
float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2];
|
||||
|
||||
int const half_size = blip_res / 2 * (width - 1);
|
||||
eq.generate( &fimpulse [blip_res], half_size );
|
||||
|
||||
int i;
|
||||
|
||||
// need mirror slightly past center for calculation
|
||||
for ( i = blip_res; i--; )
|
||||
fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i];
|
||||
|
||||
// starts at 0
|
||||
for ( i = 0; i < blip_res; i++ )
|
||||
fimpulse [i] = 0.0f;
|
||||
|
||||
// find rescale factor
|
||||
double total = 0.0;
|
||||
for ( i = 0; i < half_size; i++ )
|
||||
total += fimpulse [blip_res + i];
|
||||
|
||||
//double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB
|
||||
//double const base_unit = 37888.0; // allows treble to +5 dB
|
||||
double const base_unit = 32768.0; // necessary for blip_unscaled to work
|
||||
double rescale = base_unit / 2 / total;
|
||||
kernel_unit = (long) base_unit;
|
||||
|
||||
// integrate, first difference, rescale, convert to int
|
||||
double sum = 0.0;
|
||||
double next = 0.0;
|
||||
int const impulses_size_local = this->impulses_size();
|
||||
for ( i = 0; i < impulses_size_local; i++ )
|
||||
{
|
||||
impulses [i] = (short) floor( (next - sum) * rescale + 0.5 );
|
||||
sum += fimpulse [i];
|
||||
next += fimpulse [i + blip_res];
|
||||
}
|
||||
adjust_impulse();
|
||||
|
||||
// volume might require rescaling
|
||||
double vol = volume_unit_;
|
||||
if ( vol )
|
||||
{
|
||||
volume_unit_ = 0.0;
|
||||
volume_unit( vol );
|
||||
}
|
||||
}
|
||||
|
||||
void Blip_Synth_::volume_unit( double new_unit )
|
||||
{
|
||||
if ( new_unit != volume_unit_ )
|
||||
{
|
||||
// use default eq if it hasn't been set yet
|
||||
if ( !kernel_unit )
|
||||
treble_eq( -8.0 );
|
||||
|
||||
volume_unit_ = new_unit;
|
||||
double factor = new_unit * (1L << blip_sample_bits) / kernel_unit;
|
||||
|
||||
if ( factor > 0.0 )
|
||||
{
|
||||
int shift = 0;
|
||||
|
||||
// if unit is really small, might need to attenuate kernel
|
||||
while ( factor < 2.0 )
|
||||
{
|
||||
shift++;
|
||||
factor *= 2.0;
|
||||
}
|
||||
|
||||
if ( shift )
|
||||
{
|
||||
kernel_unit >>= shift;
|
||||
|
||||
// keep values positive to avoid round-towards-zero of sign-preserving
|
||||
// right shift for negative values
|
||||
long offset = 0x8000 + (1 << (shift - 1));
|
||||
long offset2 = 0x8000 >> shift;
|
||||
for ( int i = impulses_size(); i--; )
|
||||
impulses [i] = (short) (((impulses [i] + offset) >> shift) - offset2);
|
||||
adjust_impulse();
|
||||
}
|
||||
}
|
||||
delta_factor = (int) floor( factor + 0.5 );
|
||||
//printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
long Blip_Buffer::read_samples( blip_sample_t* BLIP_RESTRICT out, long max_samples, int stereo )
|
||||
{
|
||||
long count = samples_avail();
|
||||
if ( count > max_samples )
|
||||
count = max_samples;
|
||||
|
||||
if ( count )
|
||||
{
|
||||
int const bass = BLIP_READER_BASS( *this );
|
||||
BLIP_READER_BEGIN( reader, *this );
|
||||
|
||||
if ( !stereo )
|
||||
{
|
||||
for ( blip_long 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
|
||||
{
|
||||
for ( blip_long 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 );
|
||||
|
||||
remove_samples( count );
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void Blip_Buffer::mix_samples( blip_sample_t const* in, long count )
|
||||
{
|
||||
if ( buffer_size_ == silent_buf_size )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
buf_t_* out = buffer_ + (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;
|
||||
}
|
||||
|
199
mednafen/sound/Fir_Resampler.cpp
Normal file
199
mednafen/sound/Fir_Resampler.cpp
Normal file
@ -0,0 +1,199 @@
|
||||
// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
|
||||
|
||||
#include "Fir_Resampler.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
/* Copyright (C) 2004-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"
|
||||
|
||||
#undef PI
|
||||
#define PI 3.1415926535897932384626433832795029
|
||||
|
||||
static void gen_sinc( double rolloff, int width, double offset, double spacing, double scale,
|
||||
int count, short* out )
|
||||
{
|
||||
double const maxh = 256;
|
||||
double const step = PI / maxh * spacing;
|
||||
double const to_w = maxh * 2 / width;
|
||||
double const pow_a_n = pow( rolloff, maxh );
|
||||
scale /= maxh * 2;
|
||||
|
||||
double angle = (count / 2 - 1 + offset) * -step;
|
||||
while ( count-- )
|
||||
{
|
||||
*out++ = 0;
|
||||
double w = angle * to_w;
|
||||
if ( fabs( w ) < PI )
|
||||
{
|
||||
double rolloff_cos_a = rolloff * cos( angle );
|
||||
double num = 1 - rolloff_cos_a -
|
||||
pow_a_n * cos( maxh * angle ) +
|
||||
pow_a_n * rolloff * cos( (maxh - 1) * angle );
|
||||
double den = 1 - rolloff_cos_a - rolloff_cos_a + rolloff * rolloff;
|
||||
double sinc = scale * num / den - scale;
|
||||
|
||||
out [-1] = (short) (cos( w ) * sinc + sinc);
|
||||
}
|
||||
angle += step;
|
||||
}
|
||||
}
|
||||
|
||||
Fir_Resampler_::Fir_Resampler_( int width, sample_t* impulses_ ) :
|
||||
width_( width ),
|
||||
write_offset( width * stereo - stereo ),
|
||||
impulses( impulses_ )
|
||||
{
|
||||
write_pos = 0;
|
||||
res = 1;
|
||||
imp_phase = 0;
|
||||
skip_bits = 0;
|
||||
step = stereo;
|
||||
ratio_ = 1.0;
|
||||
}
|
||||
|
||||
Fir_Resampler_::~Fir_Resampler_() { }
|
||||
|
||||
void Fir_Resampler_::clear()
|
||||
{
|
||||
imp_phase = 0;
|
||||
if ( buf.size() )
|
||||
{
|
||||
write_pos = &buf [write_offset];
|
||||
memset( buf.begin(), 0, write_offset * sizeof buf [0] );
|
||||
}
|
||||
}
|
||||
|
||||
blargg_err_t Fir_Resampler_::buffer_size( int new_size )
|
||||
{
|
||||
RETURN_ERR( buf.resize( new_size + write_offset ) );
|
||||
clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double Fir_Resampler_::time_ratio( double new_factor, double rolloff, double gain )
|
||||
{
|
||||
ratio_ = new_factor;
|
||||
|
||||
double fstep = 0.0;
|
||||
{
|
||||
double least_error = 2;
|
||||
double pos = 0;
|
||||
res = -1;
|
||||
for ( int r = 1; r <= max_res; r++ )
|
||||
{
|
||||
pos += ratio_;
|
||||
double nearest = floor( pos + 0.5 );
|
||||
double error = fabs( pos - nearest );
|
||||
if ( error < least_error )
|
||||
{
|
||||
res = r;
|
||||
fstep = nearest / res;
|
||||
least_error = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
skip_bits = 0;
|
||||
|
||||
step = stereo * (int) floor( fstep );
|
||||
|
||||
ratio_ = fstep;
|
||||
fstep = fmod( fstep, 1.0 );
|
||||
|
||||
double filter = (ratio_ < 1.0) ? 1.0 : 1.0 / ratio_;
|
||||
double pos = 0.0;
|
||||
input_per_cycle = 0;
|
||||
for ( int i = 0; i < res; i++ )
|
||||
{
|
||||
gen_sinc( rolloff, int (width_ * filter + 1) & ~1, pos, filter,
|
||||
double (0x7FFF * gain * filter),
|
||||
(int) width_, impulses + i * width_ );
|
||||
|
||||
pos += fstep;
|
||||
input_per_cycle += step;
|
||||
if ( pos >= 0.9999999 )
|
||||
{
|
||||
pos -= 1.0;
|
||||
skip_bits |= 1 << i;
|
||||
input_per_cycle++;
|
||||
}
|
||||
}
|
||||
|
||||
clear();
|
||||
|
||||
return ratio_;
|
||||
}
|
||||
|
||||
int Fir_Resampler_::input_needed( blargg_long output_count ) const
|
||||
{
|
||||
blargg_long input_count = 0;
|
||||
|
||||
unsigned long skip = skip_bits >> imp_phase;
|
||||
int remain = res - imp_phase;
|
||||
while ( (output_count -= 2) > 0 )
|
||||
{
|
||||
input_count += step + (skip & 1) * stereo;
|
||||
skip >>= 1;
|
||||
if ( !--remain )
|
||||
{
|
||||
skip = skip_bits;
|
||||
remain = res;
|
||||
}
|
||||
output_count -= 2;
|
||||
}
|
||||
|
||||
long input_extra = input_count - (write_pos - &buf [(width_ - 1) * stereo]);
|
||||
if ( input_extra < 0 )
|
||||
input_extra = 0;
|
||||
return input_extra;
|
||||
}
|
||||
|
||||
int Fir_Resampler_::avail_( blargg_long input_count ) const
|
||||
{
|
||||
int cycle_count = input_count / input_per_cycle;
|
||||
int output_count = cycle_count * res * stereo;
|
||||
input_count -= cycle_count * input_per_cycle;
|
||||
|
||||
blargg_ulong skip = skip_bits >> imp_phase;
|
||||
int remain = res - imp_phase;
|
||||
while ( input_count >= 0 )
|
||||
{
|
||||
input_count -= step + (skip & 1) * stereo;
|
||||
skip >>= 1;
|
||||
if ( !--remain )
|
||||
{
|
||||
skip = skip_bits;
|
||||
remain = res;
|
||||
}
|
||||
output_count += 2;
|
||||
}
|
||||
return output_count;
|
||||
}
|
||||
|
||||
int Fir_Resampler_::skip_input( long count )
|
||||
{
|
||||
int remain = write_pos - buf.begin();
|
||||
int max_count = remain - width_ * stereo;
|
||||
if ( count > max_count )
|
||||
count = max_count;
|
||||
|
||||
remain -= count;
|
||||
write_pos = &buf [remain];
|
||||
memmove( buf.begin(), &buf [count], remain * sizeof buf [0] );
|
||||
|
||||
return count;
|
||||
}
|
Loading…
Reference in New Issue
Block a user