OSDN Git Service

[COMMON] Add high pass/low pass filter function.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Wed, 6 Mar 2019 15:05:54 +0000 (00:05 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Wed, 6 Mar 2019 15:05:54 +0000 (00:05 +0900)
source/src/common.cpp
source/src/common.h
source/src/qt/emuutils/CMakeLists.txt

index 5b04eea..fe0afd7 100644 (file)
@@ -2298,6 +2298,80 @@ bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rat
        return true;
 }
 
+// From https://en.wikipedia.org/wiki/High-pass_filter
+void DLL_PREFIX calc_high_pass_filter(int32_t* dst, int32_t* src, int sample_freq, int hpf_freq, int samples, double quality, bool is_add)
+{
+       if(src == NULL) return;
+       if(dst == NULL) return;
+       double ifreq = 1.0 / ((double)hpf_freq * 2.0 * M_PI);
+       double isample = 1.0 / (double)sample_freq;
+       float alpha = (float)(isample * quality / (ifreq + isample));
+
+       if(alpha >= 1.0f) alpha = 1.0f;
+       if(alpha <= 0.0f) alpha = 0.0f;
+       float ialpha = 1.0f - alpha;
+       
+       __DECL_ALIGNED(16) float tmp_v[samples * 2]; // 2ch stereo
+       __DECL_ALIGNED(16) float tmp_h[samples * 2];
+       for(int i = 0; i < (samples * 2); i ++) {
+               tmp_h[i] = (float)(src[i]);
+       }
+       tmp_v[0] = tmp_h[0];
+       tmp_v[1] = tmp_h[1];
+       for(int i = 2; i < (samples * 2); i += 2) {
+               tmp_v[i + 0] = tmp_h[i + 0] * alpha + tmp_v[i - 2 + 0] * ialpha;
+               tmp_v[i + 1] = tmp_h[i + 1] * alpha + tmp_v[i - 2 + 1] * ialpha;
+               tmp_v[i + 0] = tmp_h[i + 0] - tmp_v[i + 0];
+               tmp_v[i + 1] = tmp_h[i + 1] - tmp_v[i + 1];
+       }
+       if(is_add) {
+               for(int i = 0; i < (samples * 2); i++) {
+                       dst[i] = dst[i] + (int32_t)(tmp_v[i]);
+               }
+       } else {
+               for(int i = 0; i < (samples * 2); i++) {
+                       dst[i] = (int32_t)(tmp_v[i]);
+               }
+       }                       
+}
+
+// From https://en.wikipedia.org/wiki/Low-pass_filter
+void DLL_PREFIX calc_low_pass_filter(int32_t* dst, int32_t* src, int sample_freq, int lpf_freq, int samples, double quality, bool is_add)
+{
+       if(dst == NULL) return;
+       if(src == NULL) return;
+               
+       double ifreq = 1.0 / ((double)lpf_freq * (2.0 * M_PI));
+       double isample = 1.0 / (double)sample_freq;
+       float alpha = (float)(isample * quality / (ifreq + isample));
+       if(alpha >= 1.0f) alpha = 1.0f;
+       if(alpha <= 0.0f) alpha = 0.0f;
+       float ialpha = 1.0f - alpha;
+       
+       __DECL_ALIGNED(16) float tmp_v[samples * 2]; // 2ch stereo
+       __DECL_ALIGNED(16) float tmp_h[samples * 2];
+       
+       for(int i = 0; i < (samples * 2); i++) {
+               tmp_h[i] = (float)(src[i]);
+       }
+       tmp_v[0] = tmp_h[0];
+       tmp_v[1] = tmp_h[1];
+       for(int i = 2; i < (samples * 2); i += 2) {
+               tmp_v[i + 0] = tmp_h[i + 0] + alpha * tmp_v[i - 2 + 0] * ialpha; 
+               tmp_v[i + 1] = tmp_h[i + 1] + alpha * tmp_v[i - 2 + 1] * ialpha; 
+       }
+       if(is_add) {
+               for(int i = 0; i < (samples * 2); i++) {
+                       dst[i] = dst[i] + (int32_t)(tmp_v[i]);
+               }
+       } else {
+               for(int i = 0; i < (samples * 2); i++) {
+                       dst[i] = (int32_t)(tmp_v[i]);
+               }
+       }                       
+}
+
+
 DLL_PREFIX const _TCHAR *get_lib_common_version()
 {
 #if defined(__LIBEMU_UTIL_VERSION)
index 0e5fd6c..ec89959 100644 (file)
@@ -1433,6 +1433,11 @@ uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
 int DLL_PREFIX decibel_to_volume(int decibel);
 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
 
+// High pass filter and Low pass filter.
+void DLL_PREFIX calc_high_pass_filter(int32_t* dst, int32_t* src, int sample_freq, int hpf_freq, int samples, double quality = 1.0, bool is_add = true);
+void DLL_PREFIX calc_low_pass_filter(int32_t* dst, int32_t* src, int sample_freq, int lpf_freq, int samples, double quality = 1.0, bool is_add = true);
+
+
 #define array_length(array) (sizeof(array) / sizeof(array[0]))
 
 #define FROM_BCD(v)    (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
index e248af2..b511442 100644 (file)
@@ -1,6 +1,6 @@
 message("* qt/emuutils")
 
-SET(THIS_LIB_VERSION 2.15.3)
+SET(THIS_LIB_VERSION 2.15.4)
 
 set(s_qt_emuutils_headers
        ../gui/csp_logger.h