[PD-cvs] externals/tb/sndfiler/src sndfiler.c,1.3,1.4

Tim Blechmann timblech at users.sourceforge.net
Fri Dec 16 09:34:12 CET 2005


Update of /cvsroot/pure-data/externals/tb/sndfiler/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9029

Modified Files:
	sndfiler.c 
Log Message:
using a semaphore instead of a condion variable for synchronization

Index: sndfiler.c
===================================================================
RCS file: /cvsroot/pure-data/externals/tb/sndfiler/src/sndfiler.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** sndfiler.c	29 Nov 2005 18:34:32 -0000	1.3
--- sndfiler.c	16 Dec 2005 08:34:10 -0000	1.4
***************
*** 46,49 ****
--- 46,50 ----
  #include "m_fifo.h"
  #include "pthread.h"
+ #include "semaphore.h"
  
  #endif /* USE_PD_MAIN */
***************
*** 90,94 ****
      }
      if (!template_find_field(_template, gensym("z"), 
!         &zonset, &ztype, &zarraytype))
      {
          error("array: template %s has no 'z' field", templatesym->s_name);
--- 91,95 ----
      }
      if (!template_find_field(_template, gensym("z"), 
!             &zonset, &ztype, &zarraytype))
      {
          error("array: template %s has no 'z' field", templatesym->s_name);
***************
*** 98,102 ****
      {
          error("array: template %s, 'z' field is not an array",
!                templatesym->s_name);
          return (0);
      }
--- 99,103 ----
      {
          error("array: template %s, 'z' field is not an array",
!             templatesym->s_name);
          return (0);
      }
***************
*** 129,135 ****
  {
      t_fifo* x_jobs;
! 
!     pthread_mutex_t mutex;
!     pthread_cond_t cond;
  } t_sfqueue;
  
--- 130,134 ----
  {
      t_fifo* x_jobs;
!     sem_t sem;
  } t_sfqueue;
  
***************
*** 159,164 ****
      {
          t_sfprocess * me;
!         pthread_cond_wait(&sndfiler_queue.cond, &sndfiler_queue.mutex);
! 
          while (me = (t_sfprocess *)fifo_get(sndfiler_queue.x_jobs))
          {
--- 158,163 ----
      {
          t_sfprocess * me;
!         sem_wait(&sndfiler_queue.sem);
!         
          while (me = (t_sfprocess *)fifo_get(sndfiler_queue.x_jobs))
          {
***************
*** 180,186 ****
      //initialize queue
      sndfiler_queue.x_jobs = fifo_init();
!     pthread_mutex_init (&sndfiler_queue.mutex,NULL);
!     pthread_cond_init (&sndfiler_queue.cond,NULL);
! 
      // initialize thread
      pthread_attr_init(&sf_attr);
--- 179,184 ----
      //initialize queue
      sndfiler_queue.x_jobs = fifo_init();
!     sem_init (&sndfiler_queue.sem,0,0);
!     
      // initialize thread
      pthread_attr_init(&sf_attr);
***************
*** 199,203 ****
      //start thread
      status = pthread_create(&sf_thread_id, &sf_attr, 
! 	(void *) sndfiler_thread,NULL);
    
      if (status != 0)
--- 197,201 ----
      //start thread
      status = pthread_create(&sf_thread_id, &sf_attr, 
!         (void *) sndfiler_thread,NULL);
    
      if (status != 0)
***************
*** 205,209 ****
      else
          post("Global sndfiler thread launched, priority: %d", 
! 	      sf_param.sched_priority);
  }
  
--- 203,207 ----
      else
          post("Global sndfiler thread launched, priority: %d", 
!             sf_param.sched_priority);
  }
  
***************
*** 227,231 ****
      fifo_put(sndfiler_queue.x_jobs, process);
  
!     pthread_cond_signal(&sndfiler_queue.cond);
  }
  
--- 225,229 ----
      fifo_put(sndfiler_queue.x_jobs, process);
  
!     sem_post(&sndfiler_queue.sem);
  }
  
***************
*** 248,267 ****
      // parse flags
      while (argc > 0 && argv->a_type == A_SYMBOL &&
! 	   *argv->a_w.w_symbol->s_name == '-')
      {
          char *flag = argv->a_w.w_symbol->s_name + 1;
! 	if (!strcmp(flag, "resize"))
! 	{
! 	    resize = 1;
! 	    argc -= 1; argv += 1;
! 	}
! 	else if (!strcmp(flag, "skip"))
! 	{
! 	  if (argc < 2 || argv[1].a_type != A_FLOAT ||
! 		     ((seek = argv[1].a_w.w_float) == 0))
! 	    goto usage;
! 	  argc -= 2; argv += 2;
! 	}
! 	else goto usage;
      }
      
--- 246,265 ----
      // parse flags
      while (argc > 0 && argv->a_type == A_SYMBOL &&
!         *argv->a_w.w_symbol->s_name == '-')
      {
          char *flag = argv->a_w.w_symbol->s_name + 1;
!         if (!strcmp(flag, "resize"))
!         {
!             resize = 1;
!             argc -= 1; argv += 1;
!         }
!         else if (!strcmp(flag, "skip"))
!         {
!             if (argc < 2 || argv[1].a_type != A_FLOAT ||
!                 ((seek = argv[1].a_w.w_float) == 0))
!                 goto usage;
!             argc -= 2; argv += 2;
!         }
!         else goto usage;
      }
      
***************
*** 278,307 ****
      {
          t_float *dummy;
! 	int size;
          t_garray *array;
           
          if(!(array = (t_garray *)pd_findbyclass(
!            atom_getsymbolarg(i+1, argc, argv), garray_class)))
! 	{
! 	    pd_error(x, "%s: no such array", atom_getsymbolarg(i+1, 
! 		   argc, argv)->s_name);
! 	    return;
! 	}
  	
! 	if(garray_getfloatarray(array, &size, &dummy))
! 	    arrays[i] = array;
! 	else
! 	{
! 	    pd_error(x, "%s: bad template for sndfiler", atom_getsymbolarg(i+1, 
! 		   argc, argv)->s_name);
! 	    return;
! 	}
  	
! 	// in multichannel mode: check if arrays have different length
! 	if (arraysize && arraysize != size && !resize)
! 	{
! 	  post("sndfiler: arrays have different lengths, resizing to last one ...");
! 	}
! 	arraysize = size;
      }
    
--- 276,305 ----
      {
          t_float *dummy;
!         int size;
          t_garray *array;
           
          if(!(array = (t_garray *)pd_findbyclass(
!                                                 atom_getsymbolarg(i+1, argc, argv), garray_class)))
!         {
!             pd_error(x, "%s: no such array", atom_getsymbolarg(i+1, 
!                          argc, argv)->s_name);
!             return;
!         }
  	
!         if(garray_getfloatarray(array, &size, &dummy))
!             arrays[i] = array;
!         else
!         {
!             pd_error(x, "%s: bad template for sndfiler", atom_getsymbolarg(i+1, 
!                          argc, argv)->s_name);
!             return;
!         }
  	
!         // in multichannel mode: check if arrays have different length
!         if (arraysize && arraysize != size && !resize)
!         {
!             post("sndfiler: arrays have different lengths, resizing to last one ...");
!         }
!         arraysize = size;
      }
    
***************
*** 312,316 ****
          int pos = 0;
          int maxchannels = (channel_count < info.channels) ?
!                            channel_count : info.channels;
  
          t_float * item = alloca(maxchannels * sizeof(t_float));
--- 310,314 ----
          int pos = 0;
          int maxchannels = (channel_count < info.channels) ?
!             channel_count : info.channels;
  
          t_float * item = alloca(maxchannels * sizeof(t_float));
***************
*** 318,344 ****
          t_int ** syncdata = getbytes(sizeof(t_int*) * 5);
  	
! 	// negative seek: offset from the end of the file
! 	if(seek<0)
! 	{
! 	    pos = sf_seek(sndfile, seek, SEEK_END);
! 	}
! 	if(seek>0)
! 	{
! 	    pos = sf_seek(sndfile, seek, SEEK_SET);
! 	}
! 	if(pos == -1)
! 	{
! 	    pd_error(x, "invalid seek in soundfile");
! 	    return;
! 	}
  	
! 	if(resize)
! 	{
! 	    writesize = (info.frames-pos);
! 	    arraysize = writesize;
! 	}
! 	else
! 	    writesize = (arraysize>(info.frames-pos)) ? 
! 	      info.frames-pos : arraysize;
  
  #if (_POSIX_MEMLOCK - 0) >=  200112L
--- 316,342 ----
          t_int ** syncdata = getbytes(sizeof(t_int*) * 5);
  	
!         // negative seek: offset from the end of the file
!         if(seek<0)
!         {
!             pos = sf_seek(sndfile, seek, SEEK_END);
!         }
!         if(seek>0)
!         {
!             pos = sf_seek(sndfile, seek, SEEK_SET);
!         }
!         if(pos == -1)
!         {
!             pd_error(x, "invalid seek in soundfile");
!             return;
!         }
  	
!         if(resize)
!         {
!             writesize = (info.frames-pos);
!             arraysize = writesize;
!         }
!         else
!             writesize = (arraysize>(info.frames-pos)) ? 
!                 info.frames-pos : arraysize;
  
  #if (_POSIX_MEMLOCK - 0) >=  200112L
***************
*** 364,381 ****
          }
  	
! 	// fill remaining elements with zero
! 	if(!resize && (arraysize>(info.frames-pos)))
! 	{
! 	    for (i = writesize; i != arraysize; ++i)
! 	    {
! 	        for (j = 0; j != info.channels; ++j)
! 	        {
! 	            if (j < channel_count)
! 	            {
! 		        helper_arrays[j][i] = 0;
! 	            }
! 	        }
! 	    }
! 	}
  
  #if (_POSIX_MEMLOCK - 0) >=  200112L
--- 362,379 ----
          }
  	
!         // fill remaining elements with zero
!         if(!resize && (arraysize>(info.frames-pos)))
!         {
!             for (i = writesize; i != arraysize; ++i)
!             {
!                 for (j = 0; j != info.channels; ++j)
!                 {
!                     if (j < channel_count)
!                     {
!                         helper_arrays[j][i] = 0;
!                     }
!                 }
!             }
!         }
  
  #if (_POSIX_MEMLOCK - 0) >=  200112L
***************
*** 392,406 ****
  
          sys_callback(sndfiler_synchonize, (t_int*)syncdata, 5);
! 	return;
      }
      else
      {
          pd_error(x, "Error opening file");
! 	return;
      }
      
!     usage:
  	pd_error(x, "usage: read [flags] filename array1 array2 ...");
!         post("flags: -skip <n> -resize ");
  }
  
--- 390,404 ----
  
          sys_callback(sndfiler_synchonize, (t_int*)syncdata, 5);
!         return;
      }
      else
      {
          pd_error(x, "Error opening file");
!         return;
      }
      
!  usage:
  	pd_error(x, "usage: read [flags] filename array1 array2 ...");
!     post("flags: -skip <n> -resize ");
  }
  
***************
*** 427,431 ****
          {
              vmess(&gl->gl_pd, gensym("bounds"), "ffff", 0., gl->gl_y1,
!                   (double)(frames > 1 ? frames-1 : 1), gl->gl_y2);
  
              /* close any dialogs that might have the wrong info now... */
--- 425,429 ----
          {
              vmess(&gl->gl_pd, gensym("bounds"), "ffff", 0., gl->gl_y1,
!                 (double)(frames > 1 ? frames-1 : 1), gl->gl_y2);
  
              /* close any dialogs that might have the wrong info now... */
***************
*** 462,466 ****
      fifo_put(sndfiler_queue.x_jobs, process);
  
!     pthread_cond_signal(&sndfiler_queue.cond);
  }
  
--- 460,464 ----
      fifo_put(sndfiler_queue.x_jobs, process);
  
!     sem_post(&sndfiler_queue.sem);
  }
  
***************
*** 562,567 ****
      return;
    
!     usage:
!         pd_error(x, "usage: resize tablename size");
  }
  
--- 560,565 ----
      return;
    
!  usage:
!     pd_error(x, "usage: resize tablename size");
  }
  
***************
*** 569,579 ****
  {
      sndfiler_class = class_new(gensym("sndfiler"), 
!                         (t_newmethod)sndfiler_new, 0,
!                         sizeof(t_sndfiler), 0, 0);
  
      class_addmethod(sndfiler_class, (t_method)sndfiler_read,
!                     gensym("read"), A_GIMME, 0);
      class_addmethod(sndfiler_class, (t_method)sndfiler_resize,
!                     gensym("resize"), A_GIMME, 0);
  
  #ifdef USE_PD_MAIN
--- 567,577 ----
  {
      sndfiler_class = class_new(gensym("sndfiler"), 
!         (t_newmethod)sndfiler_new, 0,
!         sizeof(t_sndfiler), 0, 0);
  
      class_addmethod(sndfiler_class, (t_method)sndfiler_read,
!         gensym("read"), A_GIMME, 0);
      class_addmethod(sndfiler_class, (t_method)sndfiler_resize,
!         gensym("resize"), A_GIMME, 0);
  
  #ifdef USE_PD_MAIN





More information about the Pd-cvs mailing list