android_media_PlaybackParams.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2015, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _ANDROID_MEDIA_PLAYBACK_PARAMS_H_
  17. #define _ANDROID_MEDIA_PLAYBACK_PARAMS_H_
  18. #include <media/AudioResamplerPublic.h>
  19. namespace android {
  20. // This entire class is inline as it is used from both core and media
  21. struct PlaybackParams {
  22. AudioPlaybackRate audioRate;
  23. bool speedSet;
  24. bool pitchSet;
  25. bool audioFallbackModeSet;
  26. bool audioStretchModeSet;
  27. struct fields_t {
  28. jclass clazz;
  29. jmethodID constructID;
  30. jfieldID speed;
  31. jfieldID pitch;
  32. jfieldID audio_fallback_mode;
  33. jfieldID audio_stretch_mode;
  34. jfieldID set;
  35. jint set_speed;
  36. jint set_pitch;
  37. jint set_audio_fallback_mode;
  38. jint set_audio_stretch_mode;
  39. void init(JNIEnv *env) {
  40. jclass lclazz = env->FindClass("android/media/PlaybackParams");
  41. if (lclazz == NULL) {
  42. return;
  43. }
  44. clazz = (jclass)env->NewGlobalRef(lclazz);
  45. if (clazz == NULL) {
  46. return;
  47. }
  48. constructID = env->GetMethodID(clazz, "<init>", "()V");
  49. speed = env->GetFieldID(clazz, "mSpeed", "F");
  50. pitch = env->GetFieldID(clazz, "mPitch", "F");
  51. audio_fallback_mode = env->GetFieldID(clazz, "mAudioFallbackMode", "I");
  52. audio_stretch_mode = env->GetFieldID(clazz, "mAudioStretchMode", "I");
  53. set = env->GetFieldID(clazz, "mSet", "I");
  54. set_speed =
  55. env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "SET_SPEED", "I"));
  56. set_pitch =
  57. env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "SET_PITCH", "I"));
  58. set_audio_fallback_mode = env->GetStaticIntField(
  59. clazz, env->GetStaticFieldID(clazz, "SET_AUDIO_FALLBACK_MODE", "I"));
  60. set_audio_stretch_mode = env->GetStaticIntField(
  61. clazz, env->GetStaticFieldID(clazz, "SET_AUDIO_STRETCH_MODE", "I"));
  62. env->DeleteLocalRef(lclazz);
  63. }
  64. void exit(JNIEnv *env) {
  65. env->DeleteGlobalRef(clazz);
  66. clazz = NULL;
  67. }
  68. };
  69. void fillFromJobject(JNIEnv *env, const fields_t& fields, jobject params) {
  70. audioRate.mSpeed = env->GetFloatField(params, fields.speed);
  71. audioRate.mPitch = env->GetFloatField(params, fields.pitch);
  72. audioRate.mFallbackMode =
  73. (AudioTimestretchFallbackMode)env->GetIntField(params, fields.audio_fallback_mode);
  74. audioRate.mStretchMode =
  75. (AudioTimestretchStretchMode)env->GetIntField(params, fields.audio_stretch_mode);
  76. int set = env->GetIntField(params, fields.set);
  77. speedSet = set & fields.set_speed;
  78. pitchSet = set & fields.set_pitch;
  79. audioFallbackModeSet = set & fields.set_audio_fallback_mode;
  80. audioStretchModeSet = set & fields.set_audio_stretch_mode;
  81. }
  82. jobject asJobject(JNIEnv *env, const fields_t& fields) {
  83. jobject params = env->NewObject(fields.clazz, fields.constructID);
  84. if (params == NULL) {
  85. return NULL;
  86. }
  87. env->SetFloatField(params, fields.speed, (jfloat)audioRate.mSpeed);
  88. env->SetFloatField(params, fields.pitch, (jfloat)audioRate.mPitch);
  89. env->SetIntField(params, fields.audio_fallback_mode, (jint)audioRate.mFallbackMode);
  90. env->SetIntField(params, fields.audio_stretch_mode, (jint)audioRate.mStretchMode);
  91. env->SetIntField(
  92. params, fields.set,
  93. (speedSet ? fields.set_speed : 0)
  94. | (pitchSet ? fields.set_pitch : 0)
  95. | (audioFallbackModeSet ? fields.set_audio_fallback_mode : 0)
  96. | (audioStretchModeSet ? fields.set_audio_stretch_mode : 0));
  97. return params;
  98. }
  99. };
  100. } // namespace android
  101. #endif // _ANDROID_MEDIA_PLAYBACK_PARAMS_H_