home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 November / CMCD1104.ISO / Software / Complet / ZynAddFX / Setup_ZynAddSubFX-2.1.1.exe / JACK_RTaudiooutput.C < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  5.9 KB  |  199 lines

  1. /*
  2.   ZynAddSubFX - a software synthesizer
  3.  
  4.   JACKaudiooutput.C - Audio output for JACK
  5.   Copyright (C) 2002 Nasca Octavian Paul
  6.   Author: Nasca Octavian Paul
  7.  
  8.   This program is free software; you can redistribute it and/or modify
  9.   it under the terms of version 2 of the GNU General Public License 
  10.   as published by the Free Software Foundation.
  11.  
  12.   This program is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License (version 2) for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License (version 2)
  18.   along with this program; if not, write to the Free Software Foundation,
  19.   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20.  
  21. */
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <pthread.h>
  26. #include <unistd.h>
  27.  
  28.  
  29. extern "C" {
  30. #include <jack/ringbuffer.h>
  31. };
  32. #include "JACKaudiooutput.h"
  33.  
  34. Master *jackmaster;
  35. jack_client_t *jackclient;
  36. jack_port_t *outport_left,*outport_right;
  37. jack_ringbuffer_t *rb=NULL;
  38.  
  39. REALTYPE *jackoutl,*jackoutr;
  40. int jackfinish=0;
  41.  
  42. void *thread_blocked(void *arg);
  43. int jackprocess(jack_nframes_t nframes,void *arg);
  44. int jacksrate(jack_nframes_t nframes,void *arg);
  45. void jackshutdown(void *arg);
  46.  
  47. pthread_cond_t more_data=PTHREAD_COND_INITIALIZER;
  48. pthread_mutex_t zyn_thread_lock=PTHREAD_MUTEX_INITIALIZER;
  49.  
  50. pthread_t bthr;
  51.  
  52.  
  53. bool JACKaudiooutputinit(Master *master_){
  54.     jackmaster=master_;
  55.     jackclient=0;
  56.     char tmpstr[100];
  57.  
  58.     jackoutl=new REALTYPE [SOUND_BUFFER_SIZE];
  59.     jackoutr=new REALTYPE [SOUND_BUFFER_SIZE];
  60.     
  61.     int rbbufsize=SOUND_BUFFER_SIZE*sizeof (REALTYPE)*2*2;
  62.     printf("%d\n",rbbufsize);
  63.     rb=jack_ringbuffer_create(rbbufsize);
  64.     for (int i=0;i<rbuffersize) rb->buf[i]=0.0;
  65.  
  66.  
  67.     for (int i=0;i<15;i++){
  68.     if (i!=0) snprintf(tmpstr,100,"ZynAddSubFX_%d",i);
  69.         else snprintf(tmpstr,100,"ZynAddSubFX");
  70.     jackclient=jack_client_new(tmpstr);
  71.     if (jackclient!=0) break;
  72.     };
  73.  
  74.     if (jackclient==0) {
  75.     fprintf(stderr,"\nERROR: Cannot make a jack client (possible reasons: JACK server is not running or jackd is launched by root and zynaddsubfx by another user.).\n\n\n");
  76.     return(false);
  77.     };
  78.  
  79.     fprintf(stderr,"Internal SampleRate   = %d\nJack Output SampleRate= %d\n",SAMPLE_RATE,jack_get_sample_rate(jackclient));
  80.     if ((unsigned int)jack_get_sample_rate(jackclient)!=(unsigned int) SAMPLE_RATE) 
  81.     fprintf(stderr,"It is recomanded that the both samplerates to be equal.\n");
  82.     
  83.     jack_set_process_callback(jackclient,jackprocess,0);    
  84.     jack_set_sample_rate_callback(jackclient,jacksrate,0);    
  85.     jack_on_shutdown(jackclient,jackshutdown,0);    
  86.     
  87.     outport_left=jack_port_register(jackclient,"out_1",
  88.     JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput|JackPortIsTerminal,0);    
  89.     outport_right=jack_port_register(jackclient,"out_2",
  90.     JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput|JackPortIsTerminal,0);    
  91.  
  92.     if (jack_activate(jackclient)){
  93.     fprintf(stderr,"Cannot activate jack client\n");
  94.     return(false);
  95.     };
  96.  
  97.     pthread_create(&bthr,NULL,thread_blocked,NULL);
  98.     
  99.     /*
  100.     jack_connect(jackclient,jack_port_name(outport_left),"alsa_pcm:out_1");
  101.     jack_connect(jackclient,jack_port_name(outport_right),"alsa_pcm:out_2");
  102.      */
  103.      
  104.      return(true);
  105. };
  106.  
  107. void *thread_blocked(void *arg){
  108.     int datasize=SOUND_BUFFER_SIZE*sizeof (REALTYPE);
  109.  
  110.     //try to get realtime
  111.     sched_param sc;
  112.     sc.sched_priority=50;
  113.     int err=sched_setscheduler(0,SCHED_FIFO,&sc);
  114.     
  115.     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
  116.     pthread_mutex_lock(&zyn_thread_lock);
  117.  
  118.     while (jackfinish==0){
  119.     while (jack_ringbuffer_write_space(rb)>=datasize){
  120.         pthread_mutex_lock(&jackmaster->mutex);
  121.         jackmaster->GetAudioOutSamples(SOUND_BUFFER_SIZE,jack_get_sample_rate(jackclient),jackoutl,jackoutr);
  122.         pthread_mutex_unlock(&jackmaster->mutex);
  123.     
  124.         jack_ringbuffer_write(rb, (char *) jackoutl,datasize);
  125.         jack_ringbuffer_write(rb, (char *) jackoutr,datasize);
  126.     };
  127.     pthread_cond_wait(&more_data,&zyn_thread_lock);
  128.     };
  129.     pthread_mutex_unlock(&zyn_thread_lock);
  130.     
  131.     return(0);
  132. };
  133.  
  134.  
  135. int jackprocess(jack_nframes_t nframes,void *arg){
  136.     jack_default_audio_sample_t *outl=(jack_default_audio_sample_t *) jack_port_get_buffer (outport_left, nframes);
  137.     jack_default_audio_sample_t *outr=(jack_default_audio_sample_t *) jack_port_get_buffer (outport_right, nframes);
  138.  
  139.     int datasize=nframes*sizeof (REALTYPE);
  140.     int incoming_datasize=SOUND_BUFFER_SIZE*sizeof (REALTYPE);
  141.     int data_read=0;
  142.  
  143.  
  144.     if (jack_ringbuffer_read_space(rb)>=(2*incoming_datasize)){
  145.     if (datasize>incoming_datasize){
  146.         data_read=0;
  147.         while (data_read < datasize){
  148.         jack_ringbuffer_read(rb, (char *) outl+data_read,datasize);
  149.         jack_ringbuffer_read(rb, (char *) outr+data_read,datasize);
  150.         data_read+=incoming_datasize;
  151.         };
  152.     } else if (datasize==incoming_datasize){
  153.         jack_ringbuffer_read(rb, (char *) outl,datasize);
  154.         jack_ringbuffer_read(rb, (char *) outr,datasize);
  155.     } else {
  156.     };
  157.     } else {//the ringbuffer is empty or there are too small amount of samples in it
  158.     for (int i=0;i<nframes;i++){
  159.         outl[i]=0.0;outr[i]=0.0;
  160.     }; 
  161.     };
  162. /*    if (jack_ringbuffer_read_space(rb)>=datasize){
  163.     jack_ringbuffer_read(rb, (char *) outl,datasize);
  164.     jack_ringbuffer_read(rb, (char *) outr,datasize);
  165.     } else {//the ringbuffer is empty or there are too small amount of samples in it
  166.     for (int i=0;i<nframes;i++){
  167.         outl[i]=0.0;outr[i]=0.0;
  168.     };
  169.     };
  170. */    
  171.     if (pthread_mutex_trylock(&zyn_thread_lock)==0){
  172.     pthread_cond_signal(&more_data);
  173.     pthread_mutex_unlock(&zyn_thread_lock);
  174.     };
  175.     
  176.     return(0);
  177. };
  178.  
  179. void JACKfinish(){
  180.     jackfinish=1;
  181.     jack_ringbuffer_free(rb);
  182.     jack_client_close(jackclient);
  183.  
  184.     usleep(100000);
  185.     delete(jackoutl);
  186.     delete(jackoutr);
  187. };
  188.  
  189. int jacksrate(jack_nframes_t nframes,void *arg){
  190.     
  191.     return(0);
  192. };
  193.  
  194. void jackshutdown(void *arg){
  195. };
  196.  
  197.  
  198.  
  199.