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, |
755 | 755 | return true; |
756 | 756 | } |
757 | 757 | |
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 | |
| 759 | template<class AudioDataType> |
| 760 | void 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 | } |
771 | 777 | } |
772 | 778 | |
773 | 779 | void AudioOutputBase::ApplyPreAmp(void *buffer, int samples, int sample_bytes) |
… |
… |
void AudioOutputBase::ApplyPreAmp(void *buffer, int samples, int sample_bytes) |
775 | 781 | if ( !pre_amp_enabled ) return; |
776 | 782 | // NOTE: This only handles 1, 2 or 4 byte samples. |
777 | 783 | if ( sample_bytes == 4 ) |
778 | | __APPLY_PRE_AMP(int,INT_MIN,INT_MAX) |
| 784 | _ApplyPreAmp<int>((int*)(buffer), samples); |
779 | 785 | else if ( sample_bytes == 2 ) |
780 | | __APPLY_PRE_AMP(short,SHRT_MIN,SHRT_MAX) |
| 786 | _ApplyPreAmp<short>((short*)(buffer), samples); |
781 | 787 | else if ( sample_bytes == 1 ) |
782 | | __APPLY_PRE_AMP(char,CHAR_MIN,CHAR_MAX) |
783 | | |
784 | | return; |
| 788 | _ApplyPreAmp<char>((char*)(buffer), samples); |
785 | 789 | } |
786 | | #undef __APPLY_PRE_AMP |
787 | 790 | |
788 | 791 | bool AudioOutputBase::AddSamples(char *buffer, int samples, long long timecode) |
789 | 792 | { |
diff --git a/libs/libmyth/audiooutputbase.h b/libs/libmyth/audiooutputbase.h
index 1662a56..d5ff6ca 100644
a
|
b
|
class AudioOutputBase : public AudioOutput |
106 | 106 | int GetAudioData(unsigned char *buffer, int buf_size, bool fill_buffer); |
107 | 107 | |
108 | 108 | void ApplyPreAmp(void *buffer, int samples, int sample_bytes); |
| 109 | template<class AudioDataType> |
| 110 | void _ApplyPreAmp(AudioDataType* buffer, int samples); |
109 | 111 | void _AddSamples(void *buffer, bool interleaved, int samples, long long timecode); |
110 | 112 | |
111 | 113 | void OutputAudioLoop(void); |