Ticket #4804: libs_libmyth-change-pound-define-function-to-generic.2.patch

File libs_libmyth-change-pound-define-function-to-generic.2.patch, 3.1 KB (added by Erik Hovland <erik@…>, 16 years ago)

Replaces the previous patch. There was a bug where I accidentally dropped the multiply operator

  • libs/libmyth/audiooutputbase.cpp

    The #define macro that is really a function can be a generic function
    
    From: Erik Hovland <erik@hovland.org>
    
    instead.
    ---
    
     libs/libmyth/audiooutputbase.cpp |   41 ++++++++++++++++++++------------------
     libs/libmyth/audiooutputbase.h   |    2 ++
     2 files changed, 24 insertions(+), 19 deletions(-)
    
    diff --git a/libs/libmyth/audiooutputbase.cpp b/libs/libmyth/audiooutputbase.cpp
    index d6a63f1..e589816 100644
    a b bool AudioOutputBase::AddSamples(char *buffers[], int samples, 
    755755    return true;
    756756}
    757757
    758 #define __APPLY_PRE_AMP(TYPE,MIN,MAX) \
    759 { \
    760     TYPE *buf = (TYPE *)buffer; \
    761     for ( int i = 0; i < samples; ++i ) \
    762     { \
    763         float f = (float)buf[i] / (float)MAX; \
    764         f *= pre_amp_factor; \
    765         float a = abs(f); \
    766         if ( a > pre_amp_max_output ) pre_amp_max_output = a; \
    767         if ( f >= 1.0 ) buf[i] = MAX; \
    768         else if ( f <= -1.0 ) buf[i] = MIN; \
    769         else buf[i] = (TYPE)round(f*MAX); \
    770     } \
     758
     759template<class AudioDataType>
     760void AudioOutputBase::_ApplyPreAmp(AudioDataType* buffer, int samples)
     761{
     762    if (samples < 0)
     763        return;
     764
     765    for (int i = 0; i < samples; i++)
     766    {
     767        float f =
     768            static_cast<float>(buffer[i])
     769            / static_cast<float>(numeric_limits<AudioDataType>::max());
     770        f *= pre_amp_factor;
     771        float a = abs(f);
     772        if (a > pre_amp_max_output) pre_amp_max_output = a;
     773        if (f >= 1.0) buffer[i] = numeric_limits<AudioDataType>::max();
     774        else if (f <= -1.0) buffer[i] = numeric_limits<AudioDataType>::min();
     775        else buffer[i] = static_cast<AudioDataType>(round(f * numeric_limits<AudioDataType>::max()));
     776    }
    771777}
    772778
    773779void AudioOutputBase::ApplyPreAmp(void *buffer, int samples, int sample_bytes)
    void AudioOutputBase::ApplyPreAmp(void *buffer, int samples, int sample_bytes) 
    775781    if ( !pre_amp_enabled ) return;
    776782    // NOTE: This only handles 1, 2 or 4 byte samples.
    777783    if ( sample_bytes == 4 )
    778         __APPLY_PRE_AMP(int,INT_MIN,INT_MAX)
     784        _ApplyPreAmp<int>((int*)(buffer), samples);
    779785    else if ( sample_bytes == 2 )
    780         __APPLY_PRE_AMP(short,SHRT_MIN,SHRT_MAX)
     786        _ApplyPreAmp<short>((short*)(buffer), samples);
    781787    else if ( sample_bytes == 1 )
    782         __APPLY_PRE_AMP(char,CHAR_MIN,CHAR_MAX)
    783 
    784     return;
     788        _ApplyPreAmp<char>((char*)(buffer), samples);
    785789}
    786 #undef __APPLY_PRE_AMP
    787790
    788791bool AudioOutputBase::AddSamples(char *buffer, int samples, long long timecode)
    789792{
  • libs/libmyth/audiooutputbase.h

    diff --git a/libs/libmyth/audiooutputbase.h b/libs/libmyth/audiooutputbase.h
    index 1662a56..d5ff6ca 100644
    a b class AudioOutputBase : public AudioOutput 
    106106    int GetAudioData(unsigned char *buffer, int buf_size, bool fill_buffer);
    107107
    108108    void ApplyPreAmp(void *buffer, int samples, int sample_bytes);
     109    template<class AudioDataType>
     110    void _ApplyPreAmp(AudioDataType* buffer, int samples);
    109111    void _AddSamples(void *buffer, bool interleaved, int samples, long long timecode);
    110112
    111113    void OutputAudioLoop(void);