From 575526ff3480553ab6c41cb90dc5ba8d66d09fa2 Mon Sep 17 00:00:00 2001 From: beveloper Date: Tue, 3 Jun 2003 22:51:00 +0000 Subject: [PATCH] added a resampling class git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3415 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/add-ons/media/media-add-ons/mixer/Jamfile | 1 + .../media/media-add-ons/mixer/Resampler.cpp | 128 ++++++++++++++++++ .../media/media-add-ons/mixer/Resampler.h | 59 ++++++++ 3 files changed, 188 insertions(+) create mode 100644 src/add-ons/media/media-add-ons/mixer/Resampler.cpp create mode 100644 src/add-ons/media/media-add-ons/mixer/Resampler.h diff --git a/src/add-ons/media/media-add-ons/mixer/Jamfile b/src/add-ons/media/media-add-ons/mixer/Jamfile index 498d7cd318..8f4f775778 100644 --- a/src/add-ons/media/media-add-ons/mixer/Jamfile +++ b/src/add-ons/media/media-add-ons/mixer/Jamfile @@ -6,6 +6,7 @@ Addon mixer.media_addon : media : AudioMixer.cpp MixerAddOn.cpp FillMixBuffer.cpp + Resampler.cpp ; LinkSharedOSLibs mixer.media_addon : be media ; diff --git a/src/add-ons/media/media-add-ons/mixer/Resampler.cpp b/src/add-ons/media/media-add-ons/mixer/Resampler.cpp new file mode 100644 index 0000000000..3d136c7a4a --- /dev/null +++ b/src/add-ons/media/media-add-ons/mixer/Resampler.cpp @@ -0,0 +1,128 @@ +/* Copyright (C) 2003 Marcus Overhagen + * Released under terms of the MIT license. + * + * A simple resampling class for the audio mixer. + * You pick the conversation function on object creation, + * and then call the Resample() function, specifying data pointer, + * offset (in bytes) to the next sample, and count of samples for + * both source and destination. + * + */ + +#include "Resampler.h" +#include "debug.h" + +Resampler::Resampler(uint32 src_format, uint32 dst_format) + : fFunc(0) +{ + if (dst_format == media_raw_audio_format::B_AUDIO_FLOAT) { + switch (src_format) { + case media_raw_audio_format::B_AUDIO_FLOAT: + fFunc = &Resampler::float_to_float; + return; + case media_raw_audio_format::B_AUDIO_INT: + fFunc = &Resampler::int32_to_float; + return; + case media_raw_audio_format::B_AUDIO_SHORT: + fFunc = &Resampler::int16_to_float; + return; + case media_raw_audio_format::B_AUDIO_CHAR: + fFunc = &Resampler::int8_to_float; + return; + case media_raw_audio_format::B_AUDIO_UCHAR: + fFunc = &Resampler::uint8_to_float; + return; + default: + FATAL("Resampler::Resampler: unknown source format 0x%x\n", src_format); + return; + } + } + + if (src_format == media_raw_audio_format::B_AUDIO_FLOAT) { + switch (dst_format) { + // float=>float already handled above + case media_raw_audio_format::B_AUDIO_INT: + fFunc = &Resampler::float_to_int32; + return; + case media_raw_audio_format::B_AUDIO_SHORT: + fFunc = &Resampler::float_to_int16; + return; + case media_raw_audio_format::B_AUDIO_CHAR: + fFunc = &Resampler::float_to_int8; + return; + case media_raw_audio_format::B_AUDIO_UCHAR: + fFunc = &Resampler::float_to_uint8; + return; + default: + FATAL("Resampler::Resampler: unknown destination format 0x%x\n", dst_format); + return; + } + } + + FATAL("Resampler::Resampler: source or destination format must be B_AUDIO_FLOAT\n"); +} + +Resampler::~Resampler() +{ +} + +status_t +Resampler::InitCheck() +{ + return (fFunc != 0) ? B_OK : B_ERROR; +} + +void +Resampler::float_to_float(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::int32_to_float(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::int16_to_float(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::int8_to_float(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::uint8_to_float(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::float_to_int32(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::float_to_int16(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::float_to_int8(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + +void +Resampler::float_to_uint8(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ +} + diff --git a/src/add-ons/media/media-add-ons/mixer/Resampler.h b/src/add-ons/media/media-add-ons/mixer/Resampler.h new file mode 100644 index 0000000000..83dda8cbdd --- /dev/null +++ b/src/add-ons/media/media-add-ons/mixer/Resampler.h @@ -0,0 +1,59 @@ +#ifndef _RESAMPLER_H +#define _RESAMPLER_H + +/* Copyright (C) 2003 Marcus Overhagen + * Released under terms of the MIT license. + * + * A simple resampling class for the audio mixer. + * You pick the conversation function on object creation, + * and then call the Resample() function, specifying data pointer, + * offset (in bytes) to the next sample, and count of samples for + * both source and destination. + * + */ + +#include + +class Resampler +{ +public: + Resampler(uint32 sourceformat, uint32 destformat); + virtual ~Resampler(); + + status_t InitCheck(); + + void Resample(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + +protected: + virtual void float_to_float (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void int32_to_float (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void int16_to_float (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void int8_to_float (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void uint8_to_float (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void float_to_int32 (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void float_to_int16 (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void float_to_int8 (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); + virtual void float_to_uint8 (const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain); +private: + void (Resampler::*fFunc) (const void *, int32, int32, void *, int32, int32, float); +}; + + +inline void +Resampler::Resample(const void *src, int32 src_sample_offset, int32 src_sample_count, + void *dst, int32 dst_sample_offset, int32 dst_sample_count, float gain) +{ + (this->*fFunc)(src, src_sample_offset, src_sample_count, dst, dst_sample_offset, dst_sample_count, gain); +} + +#endif