jack2 codebase
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.

172 lines
5.2KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackGlobals.h"
  17. #include "JackEngineControl.h"
  18. #include "JackPortType.h"
  19. #include <string.h>
  20. #if defined (__APPLE__)
  21. #include <Accelerate/Accelerate.h>
  22. #elif defined (__SSE__) && !defined (__sun__)
  23. #include <xmmintrin.h>
  24. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  25. #include <arm_neon.h>
  26. #endif
  27. namespace Jack
  28. {
  29. static void AudioBufferInit(void* buffer, size_t buffer_size, jack_nframes_t)
  30. {
  31. memset(buffer, 0, buffer_size);
  32. }
  33. static inline void MixAudioBuffer(jack_default_audio_sample_t* mixbuffer, jack_default_audio_sample_t* buffer, jack_nframes_t frames)
  34. {
  35. #ifdef __APPLE__
  36. vDSP_vadd(buffer, 1, mixbuffer, 1, mixbuffer, 1, frames);
  37. #else
  38. jack_nframes_t frames_group = frames / 4;
  39. frames = frames % 4;
  40. while (frames_group > 0) {
  41. #if defined (__SSE__) && !defined (__sun__)
  42. __m128 vec = _mm_add_ps(_mm_load_ps(mixbuffer), _mm_load_ps(buffer));
  43. _mm_store_ps(mixbuffer, vec);
  44. mixbuffer += 4;
  45. buffer += 4;
  46. frames_group--;
  47. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  48. float32x4_t vec = vaddq_f32(vld1q_f32(mixbuffer), vld1q_f32(buffer));
  49. vst1q_f32(mixbuffer, vec);
  50. mixbuffer += 4;
  51. buffer += 4;
  52. frames_group--;
  53. #else
  54. register jack_default_audio_sample_t mixFloat1 = *mixbuffer;
  55. register jack_default_audio_sample_t sourceFloat1 = *buffer;
  56. register jack_default_audio_sample_t mixFloat2 = *(mixbuffer + 1);
  57. register jack_default_audio_sample_t sourceFloat2 = *(buffer + 1);
  58. register jack_default_audio_sample_t mixFloat3 = *(mixbuffer + 2);
  59. register jack_default_audio_sample_t sourceFloat3 = *(buffer + 2);
  60. register jack_default_audio_sample_t mixFloat4 = *(mixbuffer + 3);
  61. register jack_default_audio_sample_t sourceFloat4 = *(buffer + 3);
  62. buffer += 4;
  63. frames_group--;
  64. mixFloat1 += sourceFloat1;
  65. mixFloat2 += sourceFloat2;
  66. mixFloat3 += sourceFloat3;
  67. mixFloat4 += sourceFloat4;
  68. *mixbuffer = mixFloat1;
  69. *(mixbuffer + 1) = mixFloat2;
  70. *(mixbuffer + 2) = mixFloat3;
  71. *(mixbuffer + 3) = mixFloat4;
  72. mixbuffer += 4;
  73. #endif
  74. }
  75. while (frames > 0) {
  76. register jack_default_audio_sample_t mixFloat1 = *mixbuffer;
  77. register jack_default_audio_sample_t sourceFloat1 = *buffer;
  78. buffer++;
  79. frames--;
  80. mixFloat1 += sourceFloat1;
  81. *mixbuffer = mixFloat1;
  82. mixbuffer++;
  83. }
  84. #endif
  85. }
  86. static void AudioBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
  87. {
  88. void* buffer;
  89. // Copy first buffer
  90. #if defined (__SSE__) && !defined (__sun__)
  91. jack_nframes_t frames_group = nframes / 4;
  92. jack_nframes_t remaining_frames = nframes % 4;
  93. jack_default_audio_sample_t* source = static_cast<jack_default_audio_sample_t*>(src_buffers[0]);
  94. jack_default_audio_sample_t* target = static_cast<jack_default_audio_sample_t*>(mixbuffer);
  95. while (frames_group > 0) {
  96. __m128 vec = _mm_load_ps(source);
  97. _mm_store_ps(target, vec);
  98. source += 4;
  99. target += 4;
  100. --frames_group;
  101. }
  102. for (jack_nframes_t i = 0; i != remaining_frames; ++i) {
  103. target[i] = source[i];
  104. }
  105. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  106. jack_nframes_t frames_group = nframes / 4;
  107. jack_nframes_t remaining_frames = nframes % 4;
  108. jack_default_audio_sample_t* source = static_cast<jack_default_audio_sample_t*>(src_buffers[0]);
  109. jack_default_audio_sample_t* target = static_cast<jack_default_audio_sample_t*>(mixbuffer);
  110. while (frames_group > 0) {
  111. float32x4_t vec = vld1q_f32(source);
  112. vst1q_f32(target, vec);
  113. source += 4;
  114. target += 4;
  115. --frames_group;
  116. }
  117. for (jack_nframes_t i = 0; i != remaining_frames; ++i) {
  118. target[i] = source[i];
  119. }
  120. #else
  121. memcpy(mixbuffer, src_buffers[0], nframes * sizeof(jack_default_audio_sample_t));
  122. #endif
  123. // Mix remaining buffers
  124. for (int i = 1; i < src_count; ++i) {
  125. buffer = src_buffers[i];
  126. MixAudioBuffer(static_cast<jack_default_audio_sample_t*>(mixbuffer), static_cast<jack_default_audio_sample_t*>(buffer), nframes);
  127. }
  128. }
  129. static size_t AudioBufferSize()
  130. {
  131. return GetEngineControl()->fBufferSize * sizeof(jack_default_audio_sample_t);
  132. }
  133. const JackPortType gAudioPortType =
  134. {
  135. JACK_DEFAULT_AUDIO_TYPE,
  136. AudioBufferSize,
  137. AudioBufferInit,
  138. AudioBufferMixdown
  139. };
  140. } // namespace Jack