Advertisement
Guest User

Math.cpp

a guest
Sep 29th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1.  
  2. #include "Math.h"
  3. #include "MathDefs.h"
  4. #include <stdlib.h>
  5.  
  6. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_pow
  7.   (JNIEnv *, jclass, jfloat base, jfloat exponent)
  8. {
  9.     return powf(base, exponent);
  10. }
  11.  
  12. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_log10
  13.   (JNIEnv *, jclass, jfloat value)
  14. {
  15.     return log10f(value);
  16. }
  17.  
  18. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_min
  19.   (JNIEnv *, jclass, jfloat a, jfloat b)
  20. {
  21.     return fmin(a, b);
  22. }
  23.  
  24. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_max
  25.   (JNIEnv *, jclass, jfloat a, jfloat b)
  26. {
  27.     return fmax(a, b);
  28. }
  29.  
  30. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_floor
  31.   (JNIEnv *, jclass, jfloat value)
  32. {
  33.     return floorf(value);
  34. }
  35.  
  36. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_ceil
  37.   (JNIEnv *, jclass, jfloat value)
  38. {
  39.     return ceilf(value);
  40. }
  41.  
  42. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_sin
  43.   (JNIEnv *, jclass, jfloat angle)
  44. {
  45.     return sinf(angle);
  46. }
  47.  
  48. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_cos
  49.   (JNIEnv *, jclass, jfloat angle)
  50. {
  51.     return cosf(angle);
  52. }
  53.  
  54. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_sqrt
  55.   (JNIEnv *, jclass, jfloat value)
  56. {
  57.     return sqrtf(value);
  58. }
  59.  
  60. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_atan2
  61.   (JNIEnv *, jclass, jfloat y, jfloat x)
  62. {
  63.     return atan2f(y, x);
  64. }
  65.  
  66. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_abs
  67.   (JNIEnv *, jclass, jfloat real, jfloat imag)
  68. {
  69.     return sqrtf(real * real + imag * imag);
  70. }
  71.  
  72. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_arg
  73.   (JNIEnv *, jclass, jfloat real, jfloat imag)
  74. {
  75.     return atan2f(imag, real);
  76. }
  77.  
  78. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_real
  79.   (JNIEnv *, jclass, jfloat abs, jfloat arg)
  80. {
  81.     return abs * cosf(arg);
  82. }
  83.  
  84. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_imag
  85.   (JNIEnv *, jclass, jfloat abs, jfloat arg)
  86. {
  87.     return abs * sinf(arg);
  88. }
  89.  
  90. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_random
  91.   (JNIEnv *, jclass, jfloat min, jfloat max)
  92. {
  93.     // TODO: min < result <= max
  94.  
  95.     float next = rand() / (float)RAND_MAX;
  96.  
  97.     // min <= result < max
  98.     return min + next * (max - min);
  99. }
  100.  
  101. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_princarg
  102.   (JNIEnv *, jclass, jfloat phase)
  103. {
  104.     return princargf(phase);
  105. }
  106.  
  107. JNIEXPORT jshort JNICALL Java_de_jurihock_voicesmith_dsp_Math_mean
  108.   (JNIEnv *env, jclass, jshortArray _buffer, jint offset, jint length)
  109. {
  110.     short* buffer = (short*)env->GetPrimitiveArrayCritical(_buffer, 0);
  111.  
  112.         long mean = 0;
  113.  
  114.         for (int i = offset; i < offset + length; i++)
  115.         {
  116.             mean += buffer[i];
  117.         }
  118.  
  119.         mean /= length;
  120.  
  121.     env->ReleasePrimitiveArrayCritical(_buffer, buffer, 0);
  122.         return (short)mean;
  123. }
  124.  
  125. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_rms___3SII
  126.   (JNIEnv *env, jclass, jshortArray _buffer, jint offset, jint length)
  127. {
  128.     short* buffer = (short*)env->GetPrimitiveArrayCritical(_buffer, 0);
  129.  
  130.         float rms = 0;
  131.  
  132.         for (int i = offset; i < offset + length; i++)
  133.         {
  134.             float value = buffer[i] / SHRT_MAX_F;
  135.             rms += value * value;
  136.         }
  137.  
  138.         rms = sqrtf(rms / length);
  139.  
  140.     env->ReleasePrimitiveArrayCritical(_buffer, buffer, 0);
  141.     return rms;
  142. }
  143.  
  144. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_rms___3SIIS
  145.   (JNIEnv *env, jclass, jshortArray _buffer, jint offset, jint length, jshort mean)
  146. {
  147.     short* buffer = (short*)env->GetPrimitiveArrayCritical(_buffer, 0);
  148.  
  149.         float rms = 0;
  150.  
  151.         for (int i = offset; i < offset + length; i++)
  152.         {
  153.             float value = (buffer[i] - mean) / SHRT_MAX_F;
  154.             rms += value * value;
  155.         }
  156.  
  157.         rms = sqrtf(rms / length);
  158.  
  159.     env->ReleasePrimitiveArrayCritical(_buffer, buffer, 0);
  160.     return rms;
  161. }
  162.  
  163. JNIEXPORT jfloat JNICALL Java_de_jurihock_voicesmith_dsp_Math_rms2dbfs
  164.  (JNIEnv *, jclass, jfloat value, jfloat min, jfloat max)
  165. {
  166.     value = fmin(fmax(value,min),max);
  167.  
  168.     return 10.0f*log10f(value);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement