2011-08-07 20:01:04 +00:00
|
|
|
// Blip_Buffer 0.4.1. http://www.slack.net/~ant/
|
|
|
|
|
|
|
|
#include "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 */
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
#include "blargg_source.h"
|
2011-08-07 20:01:04 +00:00
|
|
|
|
|
|
|
void Blip_init( struct Blip_Buffer* this )
|
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
this->factor_ = UINT_MAX/2 + 1;;
|
|
|
|
this->buffer_center_ = NULL;
|
|
|
|
this->buffer_size_ = 0;
|
|
|
|
this->sample_rate_ = 0;
|
|
|
|
this->bass_shift_ = 0;
|
|
|
|
this->clock_rate_ = 0;
|
|
|
|
this->bass_freq_ = 16;
|
|
|
|
this->length_ = 0;
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
// assumptions code makes about implementation-defined features
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// right shift of negative value preserves sign
|
|
|
|
buf_t_ i = -0x7FFFFFFE;
|
|
|
|
assert( (i >> 1) == -0x3FFFFFFF );
|
2011-08-31 19:19:49 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
// casting to short truncates to 16 bits and sign-extends
|
|
|
|
i = 0x18000;
|
|
|
|
assert( (short) i == -0x8000 );
|
|
|
|
#endif
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
Blip_clear( this );
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
void Blip_clear( struct Blip_Buffer* this )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
bool const entire_buffer = true;
|
|
|
|
|
|
|
|
this->offset_ = 0;
|
2011-08-07 20:01:04 +00:00
|
|
|
this->reader_accum_ = 0;
|
2011-08-31 19:19:49 +00:00
|
|
|
this->modified = false;
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
if ( this->buffer_ )
|
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int count = (entire_buffer ? this->buffer_size_ : Blip_samples_avail( this ));
|
|
|
|
memset( this->buffer_, 0, (count + blip_buffer_extra_) * sizeof (delta_t) );
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
blargg_err_t Blip_set_sample_rate( struct Blip_Buffer* this, int new_rate, int msec )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
// Limit to maximum size that resampled time can represent
|
|
|
|
int max_size = (((blip_resampled_time_t) -1) >> BLIP_BUFFER_ACCURACY) -
|
|
|
|
blip_buffer_extra_ - 64; // TODO: -64 isn't needed
|
|
|
|
int new_size = (new_rate * (msec + 1) + 999) / 1000;
|
|
|
|
if ( new_size > max_size )
|
|
|
|
new_size = max_size;
|
|
|
|
|
|
|
|
// Resize buffer
|
|
|
|
if ( this->buffer_size_ != new_size ) {
|
|
|
|
this->buffer_center_ = this->buffer_ + BLIP_MAX_QUALITY/2;
|
|
|
|
this->buffer_size_ = new_size;
|
|
|
|
}
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
// update things based on the sample rate
|
|
|
|
this->sample_rate_ = new_rate;
|
2011-08-31 19:19:49 +00:00
|
|
|
this->length_ = new_size * 1000 / new_rate - 1;
|
2011-08-07 20:01:04 +00:00
|
|
|
if ( this->clock_rate_ )
|
|
|
|
Blip_set_clock_rate( this, this->clock_rate_ );
|
|
|
|
Blip_bass_freq( this, this->bass_freq_ );
|
2011-08-31 19:19:49 +00:00
|
|
|
|
|
|
|
Blip_clear( this );
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
blip_resampled_time_t Blip_clock_rate_factor( struct Blip_Buffer* this, int rate )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int factor = (int) ( this->sample_rate_ * (1LL << BLIP_BUFFER_ACCURACY) / rate);
|
2011-08-07 20:01:04 +00:00
|
|
|
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;
|
2011-08-31 19:19:49 +00:00
|
|
|
if ( freq > 0 && this->sample_rate_ )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
|
|
|
shift = 13;
|
2011-08-31 19:19:49 +00:00
|
|
|
int f = (freq << 16) / this->sample_rate_;
|
2011-08-07 20:01:04 +00:00
|
|
|
while ( (f >>= 1) && --shift ) { }
|
|
|
|
}
|
2011-08-31 19:19:49 +00:00
|
|
|
this->bass_shift_ = shift;
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Blip_end_frame( struct Blip_Buffer* this, blip_time_t t )
|
|
|
|
{
|
|
|
|
this->offset_ += t * this->factor_;
|
2011-08-31 19:19:49 +00:00
|
|
|
assert( Blip_samples_avail( this ) <= (int) this->buffer_size_ ); // time outside buffer length
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
int Blip_count_samples( struct Blip_Buffer* this, blip_time_t t )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
blip_resampled_time_t last_sample = Blip_resampled_time( this, t ) >> BLIP_BUFFER_ACCURACY;
|
|
|
|
blip_resampled_time_t first_sample = this->offset_ >> BLIP_BUFFER_ACCURACY;
|
|
|
|
return (int) (last_sample - first_sample);
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
blip_time_t Blip_count_clocks( struct Blip_Buffer* this, int count )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
void Blip_remove_samples( struct Blip_Buffer* this, int count )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
|
|
|
if ( count )
|
|
|
|
{
|
|
|
|
Blip_remove_silence( this, count );
|
2011-08-31 19:19:49 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
// copy remaining samples to beginning and clear old samples
|
2011-08-31 19:19:49 +00:00
|
|
|
int remain = Blip_samples_avail( this ) + blip_buffer_extra_;
|
2011-08-07 20:01:04 +00:00
|
|
|
memmove( this->buffer_, this->buffer_ + count, remain * sizeof *this->buffer_ );
|
|
|
|
memset( this->buffer_ + remain, 0, count * sizeof *this->buffer_ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
int Blip_read_samples( struct Blip_Buffer* this, blip_sample_t out_ [], int max_samples, bool stereo )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int count = Blip_samples_avail( this );
|
2011-08-07 20:01:04 +00:00
|
|
|
if ( count > max_samples )
|
|
|
|
count = max_samples;
|
2011-08-31 19:19:49 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
if ( count )
|
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int const bass = this->bass_shift_;
|
|
|
|
delta_t const* reader = this->buffer_ + count;
|
|
|
|
int reader_sum = this->reader_accum_;
|
|
|
|
|
|
|
|
blip_sample_t* BLARGG_RESTRICT out = out_ + count;
|
|
|
|
if ( stereo )
|
|
|
|
out += count;
|
|
|
|
int offset = -count;
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
if ( !stereo )
|
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
do
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int s = reader_sum >> delta_bits;
|
|
|
|
|
|
|
|
reader_sum -= reader_sum >> bass;
|
|
|
|
reader_sum += reader [offset];
|
|
|
|
|
|
|
|
BLIP_CLAMP( s, s );
|
|
|
|
out [offset] = (blip_sample_t) s;
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
2011-08-31 19:19:49 +00:00
|
|
|
while ( ++offset );
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
do
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int s = reader_sum >> delta_bits;
|
|
|
|
|
|
|
|
reader_sum -= reader_sum >> bass;
|
|
|
|
reader_sum += reader [offset];
|
|
|
|
|
|
|
|
BLIP_CLAMP( s, s );
|
|
|
|
out [offset * 2] = (blip_sample_t) s;
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
2011-08-31 19:19:49 +00:00
|
|
|
while ( ++offset );
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
2011-08-31 19:19:49 +00:00
|
|
|
|
|
|
|
this->reader_accum_ = reader_sum;
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
Blip_remove_samples( this, count );
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
void Blip_mix_samples( struct Blip_Buffer* this, blip_sample_t const in [], int count )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
delta_t* out = this->buffer_center_ + (this->offset_ >> BLIP_BUFFER_ACCURACY);
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
int const sample_shift = blip_sample_bits - 16;
|
|
|
|
int prev = 0;
|
2011-08-31 19:19:49 +00:00
|
|
|
while ( --count >= 0 )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
int s = *in++ << sample_shift;
|
2011-08-07 20:01:04 +00:00
|
|
|
*out += s - prev;
|
|
|
|
prev = s;
|
|
|
|
++out;
|
|
|
|
}
|
|
|
|
*out -= prev;
|
|
|
|
}
|
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
// Blip_Synth
|
2011-08-07 20:01:04 +00:00
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
void volume_unit( struct Blip_Synth* this, int new_unit )
|
2011-08-07 20:01:04 +00:00
|
|
|
{
|
2011-08-31 19:19:49 +00:00
|
|
|
this->delta_factor = (int) (new_unit * (1LL << blip_sample_bits) / FP_ONE_VOLUME);
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Synth_init( struct Blip_Synth* this )
|
|
|
|
{
|
|
|
|
this->buf = 0;
|
|
|
|
this->last_amp = 0;
|
|
|
|
this->delta_factor = 0;
|
|
|
|
}
|