The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. //==============================================================================
  18. /**
  19. Some shared helpers methods for using the high-performance audio paths on
  20. Android devices (OpenSL and Oboe).
  21. @tags{Audio}
  22. */
  23. namespace juce::AndroidHighPerformanceAudioHelpers
  24. {
  25. //==============================================================================
  26. static double getNativeSampleRate()
  27. {
  28. return audioManagerGetProperty ("android.media.property.OUTPUT_SAMPLE_RATE").getDoubleValue();
  29. }
  30. static int getNativeBufferSizeHint()
  31. {
  32. // This property is a hint of a native buffer size but it does not guarantee the size used.
  33. auto deviceBufferSize = audioManagerGetProperty ("android.media.property.OUTPUT_FRAMES_PER_BUFFER").getIntValue();
  34. if (deviceBufferSize == 0)
  35. return 192;
  36. return deviceBufferSize;
  37. }
  38. static bool isProAudioDevice()
  39. {
  40. static bool isSapaSupported = SystemStats::getDeviceManufacturer().containsIgnoreCase ("SAMSUNG")
  41. && DynamicLibrary().open ("libapa_jni.so");
  42. return androidHasSystemFeature ("android.hardware.audio.pro") || isSapaSupported;
  43. }
  44. static bool hasLowLatencyAudioPath()
  45. {
  46. return androidHasSystemFeature ("android.hardware.audio.low_latency");
  47. }
  48. static bool canUseHighPerformanceAudioPath (int nativeBufferSize, int requestedBufferSize, int requestedSampleRate)
  49. {
  50. return ((requestedBufferSize % nativeBufferSize) == 0)
  51. && approximatelyEqual ((double) requestedSampleRate, getNativeSampleRate())
  52. && isProAudioDevice();
  53. }
  54. //==============================================================================
  55. static int getMinimumBuffersToEnqueue (int nativeBufferSize, double requestedSampleRate)
  56. {
  57. if (canUseHighPerformanceAudioPath (nativeBufferSize, nativeBufferSize, (int) requestedSampleRate))
  58. {
  59. // see https://developer.android.com/ndk/guides/audio/opensl/opensl-prog-notes.html#sandp
  60. // "For Android 4.2 (API level 17) and earlier, a buffer count of two or more is required
  61. // for lower latency. Beginning with Android 4.3 (API level 18), a buffer count of one
  62. // is sufficient for lower latency."
  63. return (getAndroidSDKVersion() >= 18 ? 1 : 2);
  64. }
  65. // not using low-latency path so we can use the absolute minimum number of buffers to queue
  66. return 1;
  67. }
  68. static int buffersToQueueForBufferDuration (int nativeBufferSize, int bufferDurationInMs, double sampleRate) noexcept
  69. {
  70. auto maxBufferFrames = static_cast<int> (std::ceil (bufferDurationInMs * sampleRate / 1000.0));
  71. auto maxNumBuffers = static_cast<int> (std::ceil (static_cast<double> (maxBufferFrames)
  72. / static_cast<double> (nativeBufferSize)));
  73. return jmax (getMinimumBuffersToEnqueue (nativeBufferSize, sampleRate), maxNumBuffers);
  74. }
  75. static int getMaximumBuffersToEnqueue (int nativeBufferSize, double maximumSampleRate) noexcept
  76. {
  77. static constexpr int maxBufferSizeMs = 200;
  78. return jmax (8, buffersToQueueForBufferDuration (nativeBufferSize, maxBufferSizeMs, maximumSampleRate));
  79. }
  80. static Array<int> getAvailableBufferSizes (int nativeBufferSize, Array<double> availableSampleRates)
  81. {
  82. auto minBuffersToQueue = getMinimumBuffersToEnqueue (nativeBufferSize, getNativeSampleRate());
  83. auto maxBuffersToQueue = getMaximumBuffersToEnqueue (nativeBufferSize, findMaximum (availableSampleRates.getRawDataPointer(),
  84. availableSampleRates.size()));
  85. Array<int> bufferSizes;
  86. for (int i = minBuffersToQueue; i <= maxBuffersToQueue; ++i)
  87. bufferSizes.add (i * nativeBufferSize);
  88. return bufferSizes;
  89. }
  90. static int getDefaultBufferSize (int nativeBufferSize, double currentSampleRate)
  91. {
  92. static constexpr int defaultBufferSizeForLowLatencyDeviceMs = 40;
  93. static constexpr int defaultBufferSizeForStandardLatencyDeviceMs = 100;
  94. auto defaultBufferLength = (hasLowLatencyAudioPath() ? defaultBufferSizeForLowLatencyDeviceMs
  95. : defaultBufferSizeForStandardLatencyDeviceMs);
  96. auto defaultBuffersToEnqueue = buffersToQueueForBufferDuration (nativeBufferSize, defaultBufferLength, currentSampleRate);
  97. return defaultBuffersToEnqueue * nativeBufferSize;
  98. }
  99. } // namespace juce::AndroidHighPerformanceAudioHelpers