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.

244 lines
6.8KB

  1. /*
  2. JAaudiooutput.C - Audio output for JACK
  3. Copyright (C) 2002-2009 Nasca Octavian Paul
  4. Author: Robin Gareus <robin@gareus.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  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 General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifdef HAVE_JACK
  17. #ifndef DEFAULT_RB_SIZE
  18. #define DEFAULT_RB_SIZE 16384
  19. #endif
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <pthread.h>
  23. #include <jack/jack.h>
  24. #include <jack/ringbuffer.h>
  25. #include "JAaudiooutput.h"
  26. Player *myplayer=NULL;
  27. jack_client_t *j_client = NULL;
  28. jack_port_t **j_output_port = NULL;
  29. jack_default_audio_sample_t **j_out = NULL;
  30. jack_ringbuffer_t *rb = NULL;
  31. int m_channels = 2;
  32. int thread_run = 0;
  33. pthread_t player_thread_id;
  34. pthread_mutex_t player_thread_lock = PTHREAD_MUTEX_INITIALIZER;
  35. pthread_cond_t buffer_ready = PTHREAD_COND_INITIALIZER;
  36. #ifdef ENABLE_RESAMPLING
  37. #include <samplerate.h>
  38. float m_fResampleRatio = 1.0;
  39. #ifndef SRC_QUALITY // alternatives: SRC_SINC_MEDIUM_QUALITY, SRC_SINC_BEST_QUALITY, (SRC_ZERO_ORDER_HOLD, SRC_LINEAR)
  40. #define SRC_QUALITY SRC_SINC_FASTEST
  41. #endif
  42. #endif
  43. void jack_shutdown_callback(void *arg){
  44. fprintf(stderr, "jack server [unexpectedly] shut down.\n");
  45. j_client=NULL;
  46. JACKclose();
  47. }
  48. int jack_audio_callback(jack_nframes_t nframes, void *arg){
  49. int c,s;
  50. for(c=0; c< m_channels; c++) {
  51. j_out[c] = (jack_default_audio_sample_t*) jack_port_get_buffer(j_output_port[c], nframes);
  52. }
  53. if(jack_ringbuffer_read_space(rb) < m_channels * nframes * sizeof(jack_default_audio_sample_t)) {
  54. for(c=0; c< m_channels; c++) {
  55. memset(j_out[c], 0, nframes * sizeof(jack_default_audio_sample_t));
  56. }
  57. } else {
  58. /* de-interleave */
  59. for(s=0; s<nframes; s++) {
  60. for(c=0; c< m_channels; c++) {
  61. jack_ringbuffer_read(rb, (char*) &j_out[c][s], sizeof(jack_default_audio_sample_t));
  62. }
  63. }
  64. }
  65. /* Tell the player thread there is work to do. */
  66. if(pthread_mutex_trylock(&player_thread_lock) == 0) {
  67. pthread_cond_signal(&buffer_ready);
  68. pthread_mutex_unlock(&player_thread_lock);
  69. }
  70. return(0);
  71. };
  72. void *jack_player_thread(void *){
  73. const int nframes = 4096;
  74. float *tmpbuf = (float*) calloc(nframes * m_channels, sizeof(float));
  75. float *bufptr = tmpbuf;
  76. #ifdef ENABLE_RESAMPLING
  77. SRC_STATE* src_state = src_new(SRC_QUALITY, 2, NULL);
  78. SRC_DATA src_data;
  79. int nframes_r = floorf((float) nframes*m_fResampleRatio); ///< # of frames after resampling
  80. float *smpbuf = (float*) calloc((1+nframes_r) * m_channels, sizeof(float));
  81. src_data.input_frames = nframes;
  82. src_data.output_frames = nframes_r;
  83. src_data.end_of_input = 0;
  84. src_data.src_ratio = m_fResampleRatio;
  85. src_data.input_frames_used = 0;
  86. src_data.output_frames_gen = 0;
  87. src_data.data_in = tmpbuf;
  88. src_data.data_out = smpbuf;
  89. #else
  90. int nframes_r = nframes;
  91. #endif
  92. size_t rbsize = nframes_r * m_channels * sizeof(float);
  93. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  94. pthread_mutex_lock(&player_thread_lock);
  95. while(thread_run) {
  96. myplayer->getaudiobuffer(nframes, tmpbuf);
  97. #ifdef ENABLE_RESAMPLING
  98. if(m_fResampleRatio != 1.0) {
  99. src_process(src_state, &src_data);
  100. bufptr = smpbuf;
  101. rbsize = src_data.output_frames_gen * m_channels * sizeof(float);
  102. }
  103. #endif
  104. jack_ringbuffer_write(rb, (char *) bufptr, rbsize);
  105. while(thread_run && jack_ringbuffer_write_space(rb) <= rbsize) {
  106. pthread_cond_wait(&buffer_ready, &player_thread_lock);
  107. }
  108. }
  109. pthread_mutex_unlock(&player_thread_lock);
  110. free(tmpbuf);
  111. #ifdef ENABLE_RESAMPLING
  112. src_delete(src_state);
  113. free(smpbuf);
  114. #endif
  115. }
  116. void JACKaudiooutputinit(Player *player_, int samplerate){
  117. int i;
  118. myplayer=player_;
  119. if(j_client || thread_run || rb) {
  120. fprintf(stderr, "already connected to jack.\n");
  121. return;
  122. }
  123. j_client = jack_client_open("paulstretch", (jack_options_t) 0, NULL);
  124. if(!j_client) {
  125. fprintf(stderr, "could not connect to jack.\n");
  126. return;
  127. }
  128. jack_on_shutdown(j_client, jack_shutdown_callback, NULL);
  129. jack_set_process_callback(j_client, jack_audio_callback, NULL);
  130. j_output_port=(jack_port_t**) calloc(m_channels, sizeof(jack_port_t*));
  131. for(i=0;i<m_channels;i++) {
  132. char channelid[16];
  133. snprintf(channelid, 16, "output_%i", i);
  134. j_output_port[i] = jack_port_register(j_client, channelid, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  135. if(!j_output_port[i]) {
  136. fprintf(stderr, "no more jack ports availabe.\n");
  137. JACKclose();
  138. return;
  139. }
  140. }
  141. j_out = (jack_default_audio_sample_t**) calloc(m_channels, sizeof(jack_default_audio_sample_t*));
  142. const size_t rbsize = DEFAULT_RB_SIZE * m_channels * sizeof(jack_default_audio_sample_t);
  143. rb = jack_ringbuffer_create(rbsize);
  144. memset(rb->buf, 0, rbsize);
  145. jack_nframes_t jsr = jack_get_sample_rate(j_client);
  146. if(jsr != samplerate) {
  147. #ifdef ENABLE_RESAMPLING
  148. m_fResampleRatio = (float) jsr / (float) samplerate;
  149. #else
  150. fprintf(stderr, "Note: paulstretch audio samplerate does not match JACK's samplerate.\n");
  151. #endif
  152. }
  153. thread_run = 1;
  154. pthread_create(&player_thread_id, NULL, jack_player_thread, NULL);
  155. pthread_yield();
  156. jack_activate(j_client);
  157. char *jack_autoconnect = getenv("JACK_AUTOCONNECT");
  158. if(!jack_autoconnect) {
  159. jack_autoconnect = (char*) "system:playback_";
  160. } else if(!strncmp(jack_autoconnect,"DISABLE", 7)) {
  161. jack_autoconnect = NULL;
  162. }
  163. if(jack_autoconnect) {
  164. int myc=0;
  165. const char **found_ports = jack_get_ports(j_client, jack_autoconnect, NULL, JackPortIsInput);
  166. for(i = 0; found_ports && found_ports[i]; i++) {
  167. if(jack_connect(j_client, jack_port_name(j_output_port[myc]), found_ports[i])) {
  168. fprintf(stderr, "can not connect to jack output\n");
  169. }
  170. if(myc >= m_channels) break;
  171. }
  172. }
  173. }
  174. void JACKclose(){
  175. if(thread_run) {
  176. thread_run = 0;
  177. if(pthread_mutex_trylock(&player_thread_lock) == 0) {
  178. pthread_cond_signal(&buffer_ready);
  179. pthread_mutex_unlock(&player_thread_lock);
  180. }
  181. pthread_join(player_thread_id, NULL);
  182. }
  183. if(j_client){
  184. jack_client_close(j_client);
  185. }
  186. if(j_output_port) {
  187. free(j_output_port);
  188. j_output_port=NULL;
  189. }
  190. if(j_out) {
  191. free(j_out);
  192. j_out = NULL;
  193. }
  194. if(rb) {
  195. jack_ringbuffer_free(rb);
  196. rb=NULL;
  197. }
  198. j_client=NULL;
  199. };
  200. #endif /* HAVE_JACK */
  201. /* vim: set ts=8 sw=4: */