head	1.6;
access;
symbols
	rel-0-2-1:1.4
	before_adj_struct:1.3.2.7
	jack:1.3.0.2
	rel-0-1-0:1.2
	rel-0-0-0:1.1;
locks; strict;
comment	@ * @;


1.6
date	2007.03.22.20.55.43;	author robertatwood;	state Exp;
branches;
next	1.5;

1.5
date	2006.04.23.22.03.14;	author robertatwood;	state Exp;
branches;
next	1.4;

1.4
date	2006.04.05.21.53.36;	author robertatwood;	state Exp;
branches;
next	1.3;

1.3
date	2005.08.20.18.26.42;	author robertatwood;	state Exp;
branches
	1.3.2.1;
next	1.2;

1.2
date	2005.04.03.20.05.39;	author robertatwood;	state Exp;
branches;
next	1.1;

1.1
date	2004.04.16.11.36.56;	author robertatwood;	state Exp;
branches;
next	;

1.3.2.1
date	2005.11.24.00.45.04;	author robertatwood;	state Exp;
branches;
next	1.3.2.2;

1.3.2.2
date	2005.11.24.01.23.44;	author robertatwood;	state Exp;
branches;
next	1.3.2.3;

1.3.2.3
date	2005.12.28.01.34.16;	author robertatwood;	state Exp;
branches;
next	1.3.2.4;

1.3.2.4
date	2005.12.28.16.13.35;	author robertatwood;	state Exp;
branches;
next	1.3.2.5;

1.3.2.5
date	2005.12.28.18.17.00;	author robertatwood;	state Exp;
branches;
next	1.3.2.6;

1.3.2.6
date	2005.12.29.00.20.53;	author robertatwood;	state Exp;
branches;
next	1.3.2.7;

1.3.2.7
date	2006.01.04.00.06.03;	author robertatwood;	state Exp;
branches;
next	1.3.2.8;

1.3.2.8
date	2006.01.04.02.31.33;	author robertatwood;	state Exp;
branches;
next	1.3.2.9;

1.3.2.9
date	2006.01.10.23.43.46;	author robertatwood;	state Exp;
branches;
next	1.3.2.10;

1.3.2.10
date	2006.04.05.21.42.24;	author robertatwood;	state Exp;
branches;
next	;


desc
@@


1.6
log
@Updated GPL text to reflect new address of Free Software foundation
Prepare to move repository to savannah.nongnu.org
@
text
@/**************************************************************
This file is part of the Variseq program.
Variseq -- a variable pitch and step sample sequencer
Copyright (C) 2004  Robert Atwood r.atwood (at) imperial (dot) ac (dot) uk

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
See the file COPYING for more information
**************************************************************/


#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include "constants.h"

#include "myFl.h"

#include <sndfile.h>
#include <samplerate.h>
#define VARISEQ_VERSION ("0.2.2")

pthread_mutex_t bar_lock =PTHREAD_MUTEX_INITIALIZER ;
pthread_mutex_t rewrite_lock=PTHREAD_MUTEX_INITIALIZER ;
pthread_cond_t bar_cond =PTHREAD_COND_INITIALIZER ;


#if ( defined JACK && defined PORTAUDIO )
#error Cannot define both
#endif
#ifdef JACK
#include <jack/jack.h>
   jack_port_t **ports;
	jack_default_audio_sample_t **out,**in;
   jack_client_t *client;


#elif defined PORTAUDIO 
#include <portaudio.h>
   PortAudioStream * mystream;
#else
/* no output defined */
#error NO OUTPUT DEFINED
#endif

#include "mysound.h"
#include "samples.h"
#include "notes.h"
#include "pattern.h"
#include "callbacks.h"
#include "replace.h"

typedef struct workspace_str{
  pattern * patp;
  samples * samp;
} Workspace ;

typedef struct scramble_str{
  int nscramble;
  int * scramblesamples;
  int * scrambletimes;
  int * scrambletypes; /* file, input, buffer */
  Workspace * wsp;
}Scramble;


int NLEVELS;
int NSLIDERS;

/* use a file chooser */
Fl_File_Chooser *fc;
int going=0;
int barcount=0;
double * noteratio;
samples * sampletable;
char * filename = NULL; 
sbuf mysound,origsound,insound;
int jack_ready=0;

/* objects in the main window  */
ctrl **sliders; 
Fl_Value_Input * speed;
Fl_Value_Input * msmooth;
Fl_Slider * mvol;
Fl_Output * bsize_disp;
/*
Fl_Output * bar_disp;
*/
Fl_Round_Button * rewrite_button;

pattern * thepattern;

extern Fl_Window * makewindow(int nsliders,int nlevels);
extern Fl_Window * fwindow();
Fl_Window * fwin;

const char notice[1000] = "Variseq v. 0.2.2, Copyright (C) 2004-2006  Robert Atwood\nVariseq comes with ABSOLUTELY NO WARRANTY; for details\nsee the file COPYING. This is free software, (GPL v2 or later) and you are welcome to\nredistribute it under certain conditions, see the file COPYING\nfor details.\n";

void print_usage (char * pname){
   printf("%s <nsliders> <nsteps>\n",pname);
   printf("create a sequencer with the specified number\n");
   printf("of sliders and number of steps on each slider.\n");
   printf("%s",notice);
   exit(0);
}


void init_noteratio( double ** nratio,int ntones ){
   *nratio = (double *)calloc(ntones+1,sizeof(double));
   double tonestep;
   double thisratio=1;
   int i;
   tonestep = pow( 2.0, 1.0/ (double)( ntones ) );
   for (i=0;i<=ntones;i++){
		(*nratio)[i]=thisratio;
		thisratio /=tonestep;
   }
}

void expand_sbuf (sbuf * bp,int newframes){
   char framedisp[32];
   /* better stop the stream before reallocating */
   /* assuming this routine is mainly used for the actual loop buffer */
   if(going ==1 ){
       #ifdef PORTAUDIO
       Pa_StopStream(mystream);
       #endif
   }
   bp->buffer = (float *) realloc(bp->buffer,newframes * 2 * sizeof(float));
   bp->binfo->frames = newframes;
   snprintf(framedisp,31,"%i",newframes);
   bsize_disp->value(framedisp);
   bsize_disp->redraw();
   if(going ==1 ){
      #ifdef PORTAUDIO
      Pa_StartStream(mystream);
      #endif
   }
   #ifndef NOPRINT
   printf("New soundbuffer size: %i frames\n",newframes);
   #endif
}

void init_sbuf (sbuf * bp, int nframes){
   bp->buffer = (float *) calloc(nframes * 2 , sizeof(float));
   bp->binfo = (SF_INFO *) calloc(1,sizeof(SF_INFO));
   bp->binfo->frames=nframes;
   bp->binfo->samplerate=44100;
   bp->binfo->channels=2;
   bp->binfo->format=65538;
   bp->binfo->sections=1;
   bp->binfo->seekable=1;
}

void read_sbuf(sbuf * bp, FILE * fp){
  fread(bp->binfo,sizeof(SF_INFO),1,fp);
  int nsamples =bp->binfo->frames * bp->binfo->channels;
  fread(bp->buffer,sizeof(float),nsamples,fp);
}
void save_sbuf(sbuf * bp, FILE * fp){
  int nsamples =bp->binfo->frames * bp->binfo->channels;
  fwrite(bp->binfo,sizeof(SF_INFO),1,fp);
  fwrite(bp->buffer,sizeof(float),nsamples,fp);
}
   

void free_sbuf (sbuf * bp){
   free (bp->buffer);
   free (bp->binfo);
   //free(bp);
}



void copy_sbuf(sbuf *op, sbuf *ip){
    memcpy(op->binfo,ip->binfo,sizeof(SF_INFO));
    memcpy(op->buffer,ip->buffer,ip->binfo->frames * ip->binfo->channels * sizeof(float));
}

/* routine to execute periodic alterations automatically */

void * bar_timer(void * udat){
     static int ctr=0;
     static char barnum[32];
     int i;
     int rflag=0;
     Scramble * scp = (Scramble *)(udat);
     Adj ** ap = &(scp->wsp->samp->adj);
     int * nsamples ;
     nsamples = &(scp->wsp->samp->nsamples);


     while (1){
		  pthread_mutex_lock(&bar_lock);
        #ifndef NOPRINT
           printf("%s lock bar and wait\n",__func__);
        #endif

		  pthread_cond_wait(&bar_cond,&bar_lock);

        #ifndef NOPRINT
           printf("%s unlock bar\n",__func__);
        #endif

		  pthread_mutex_unlock(&bar_lock);

		  #ifndef NOPRINT
		     printf("ctr %i, ",ctr);
		     printf("barcount %i\n",barcount);
		  #endif

		  /*
		  snprintf(barnum,32,"%i",ctr);
		  bar_disp->value(barnum);
		  bar_disp->redraw();
		  */

		  #ifndef NOPRINT
		     printf("%s nsamples %i \n",__func__,*nsamples);
		     printf("%s sampletable->nsamples %i \n",__func__,sampletable->nsamples);
		  #endif
		  for (i=0;i<*nsamples;i++){
		  #ifndef NOPRINT
		     printf("%s sample %i bars %i type %i\n",__func__,i,(*ap)[i].bars,(*ap)[i].type);
		  #endif
		      if ((*ap)[i].type != 0 ){
   		     if (ctr % (*ap)[i].bars == 0){
						#ifndef NOPRINT
						printf("Sample %i\n",i);
						#endif
						switch ((*ap)[i].type){
							case 2:
								replace_inp(scp->wsp->samp,i);
								rflag=1;
							break;
							case 3:
								replace_buf_rnd(scp->wsp->samp,i);
								rflag=1;
							break;
							default:
							/* don't do anything */
							break;
				     }
				  }
		      }
		   }/* loop through specified samples */
		   if (rflag == 1 ){
		     rewrite_button->do_callback();
		     rflag = 0;
		   }
		  ctr++;
     }
return (NULL);
}



/* callback for playing the audio stream */
/* transfer the data to the portaudio buffer */
/* loop at the end of the bugger */

#ifdef PORTAUDIO
int pa_cb (void * inp, void * outp, 
            unsigned long frames,
            PaTimestamp outTime,void *udata)
{

    static int n=0,nsamples=0,nin=0,ototal=0,itotal; /* count frames,samples */
    static int bflag=0;
    float * op,* ip;
    unsigned long i;
    int c,osamples,isamples,ibuflen;
    op = (float *) outp;
    ip = (float *) inp;

#ifdef INPUT
    isamples=insound.binfo->channels*frames;
    ibuflen=insound.binfo->channels*insound.binfo->frames;
    	for ( i=0;i<isamples;i++ ){
		     if(nin >= ibuflen){
		    	 nin=0;
		      }
            insound.buffer[nin] = *ip;
            ip++;
            nin++;
	    	 if(nin >= ibuflen){
  	    	    nin=0;
	    	 }
	   }
  #endif /*INPUT*/

    for(i=0;i<frames;i++){
		 if(n>=mysound.binfo->frames){
			 n=0;
			 nsamples=0;
			 barcount++;
			 bflag=1;
		  }
       for(c=0;c<mysound.binfo->channels;c++){
          /* loop at teh end of the buffer */
          *op++ = mysound.buffer[2*n+c];
          nsamples++;
        }/* loop channels */
       n++;
		 if(n>=mysound.binfo->frames){
			 n=0;
			 nsamples=0;
			 barcount++;
			 bflag=1;
		 }
     ototal++;
     }/* loop frames */
     /* wake up the end of bar routine */
     if (bflag){
        if( pthread_mutex_trylock(&bar_lock) == EBUSY){
           #ifndef NOPRINT
              printf("busy\n");
           #endif
        }else{
           #ifndef NOPRINT
              printf("%s trylock bar\n",__func__);
           #endif
           pthread_cond_signal(&bar_cond);
           #ifndef NOPRINT
              printf("%s unlock bar\n",__func__);
           #endif
           pthread_mutex_unlock(&bar_lock);
           bflag=0;
        }
     }

 return(0);
 }
 #endif /*PORTAUDIO*/

 #ifdef JACK
void ja_sd (void *arg) {
   printf("%s Shutting down due to jack shut-down\n",__func__);
	exit (1);
}
#define NOPRINT_callback
int ja_cb (jack_nframes_t frames, void *arg) {
    /* my variables */
    /* n keeps count of frames in my buffer */
    /* i counts the frames in the output buffer */
    /* TODO: use a JACK ring buffer ? */
    static int n=0,nsamples=0,nin=0,bflag=0; /* count frames,samples */
    static int initflag=0;
    unsigned long i;
    int c;
    unsigned int mm,nn;
	jack_transport_state_t ts ;

       #ifndef NOPRINT_callback
       printf("%s ready %i\n",__func__,jack_ready);
       printf("out[c] %x ports[c] %x\n",out[c],ports[c]);
       #endif

	if (jack_ready == 0){
	   return(0);
	} else if (initflag == 0 ){
	    initflag=1;
	    return(0);
	}

	ts = jack_transport_query(client, NULL);
   mm = mysound.binfo->channels;

   for(c=0;c<mm;c++){
       #ifndef NOPRINT_callback
       printf("out %x ports %x\n",out,ports);
       printf("out[c] %x ports[c] %x\n",out[c],ports[c]);
       #endif
       out[c] = (jack_default_audio_sample_t *)jack_port_get_buffer (ports[c], frames);
   }
	/* check if the jack is running */
	if (ts == JackTransportRolling) {

    for(i=0;i<frames;i++){
		 if(n>=mysound.binfo->frames){
			 n=0;
			 nsamples=0;
			 barcount++;
			 bflag=1;
		  }
          /* loop at teh end of the buffer */
          /* TODO: fix dependence on 2 channels */
        for(c=0;c<mm;c++){
          *(out[c] +i) = mysound.buffer[mm*n+c];
          nsamples++;
        }/* loop channels */
       n++;
		 if(n>=mysound.binfo->frames){
			 n=0;
			 nsamples=0;
			 barcount++;
			 bflag=1;
		 }
     }/* loop frames */

     /* wake up the end of bar routine  */
     if (bflag){
        if( pthread_mutex_trylock(&bar_lock) == EBUSY){
           #ifndef NOPRINT_callback
              printf("busy\n");
           #endif
        }else{
           #ifndef NOPRINT_callback
              printf("%s trylock bar\n",__func__);
           #endif
           pthread_cond_signal(&bar_cond);
           #ifndef NOPRINT_callback
              printf("%s unlock bar\n",__func__);
           #endif
           pthread_mutex_unlock(&bar_lock);
           bflag=0;
        }
     }

     #ifdef INPUT
     /* input section */

   nn=insound.binfo->channels;
   for(c=0;c<nn;c++){
       in[c] = (jack_default_audio_sample_t *)jack_port_get_buffer (ports[c+mm], frames);
   }

/* interlace the samples as */
    for(i=0;i<frames;i++){
		 if(nin>=insound.binfo->frames){
			 nin=0;
		  }
          /* loop at teh end of the buffer */
        for(c=0;c<nn;c++){
          insound.buffer[nn*nin+c] = *(in[c] +i)  ;
        }/* loop channels */
       nin++;
		 if(nin>=insound.binfo->frames){
			 nin=0;
		 }
     }/* end loop input frames */
     #endif /*INPUT*/
	} else if (ts == JackTransportStopped) {
    for(i=0;i<frames;i++){
        for(c=0;c<mm;c++){
          *(out[c] +i) = 0;
        }/* loop channels */
     }/* loop frames */
	}

	return 0;      
}
#endif



//
// 'fc_callback()' - Handle choices in the file chooser...
//

void fc_callback(Fl_File_Chooser *fc,	void *){

}




void quit_cb(Fl_Widget*, void*) {exit(0);}

void play_cb(Fl_Widget*, void * ) {
   thepattern->write(&mysound,0);
   if (going ==0){
		#ifdef PORTAUDIO
		#ifdef INPUT
		Pa_OpenDefaultStream(&mystream,2,2,paFloat32,44100,256,0,pa_cb,NULL);
		#else
		Pa_OpenDefaultStream(&mystream,0,mysound.binfo->channels,paFloat32,44100,256,0,pa_cb,NULL);
		#endif /*INPUT*/
		Pa_StartStream (mystream);
		#endif /*PORTAUDIO*/
		going = 1;
   }
   
  }

void stop_cb(Fl_Widget*, void*) {
  if (going == 1 ){
		#ifdef PORTAUDIO
     Pa_StopStream(mystream);
		#endif /*PORTAUDIO*/
  }
  going = 0;
}

void open_cb(Fl_Widget*, void*) {
   fc->show();
}

void silence_cb(Fl_Widget*, void*) {
  #ifndef NOPRINT
  printf("%s\n",__func__);
  #endif

  const char * retval;
  sbuf * newsound;
  memset(origsound.buffer,0,44100*2*sizeof(float));

  origsound.binfo->frames=44100;
     /* add the new sample to the sample table */
     sampletable->add_sample(&origsound,"silence");
     addbutton("silence");

}/*end of silence_cb*/

void load_cb(Fl_Widget*, void*) {
  #ifndef NOPRINT
  printf("load_cb \n");
  #endif
  const char * retval;
  sbuf * newsound;
  SF_INFO * sfinfo = (SF_INFO *)calloc(1,sizeof(SF_INFO));
  SNDFILE * infile;
  float * tempbuffer = (float * )calloc(44100*2,sizeof(float));
  int nread,ninp,i;
  newsound = (sbuf * ) calloc(1,sizeof(sbuf));
  char * filename ;

     filename = fl_file_chooser("Load wample","*.wav","in.wav",1);
     #ifndef NOPRINT
     printf("load sample: %s\n",filename);
     #endif
     if (filename == NULL) return;
  /* only request a maximum of 1 second per sample for now...*/
  infile = sf_open(filename,SFM_READ,sfinfo);
  if (sfinfo->frames < 44100 ) nread = sfinfo->frames;
  else nread = 44100;
 // never mind multichannel files for now... 
 //if (sfinfo->channels > 2) nread = (44100*2)/(sfinfo->channels);
  /* read into tempbuffer to avoid interrupting the audio stream */
  ninp = sf_readf_float(infile,tempbuffer,nread);
  
  if (sfinfo->channels >2 || sfinfo->channels < 1){
    fprintf(stderr,"ERROR:load_cb Cannot handle %i channels\n",sfinfo->channels);
  }else{
     if(sfinfo->channels == 2){
        memcpy(origsound.buffer,tempbuffer,ninp*2*sizeof(float));
     }
     if (sfinfo->channels == 1){
        for (i=0;i<ninp;i++){
           *(origsound.buffer + 2*i) = tempbuffer[i];
           *(origsound.buffer + 2*i + 1) = tempbuffer[i];
        }
     }
     origsound.binfo->frames=ninp;
     /* add the new sample to the sample table */
     sampletable->add_sample(&origsound,filename);
     addbutton(filename);
  }

  sf_close(infile);
  #ifndef NOPRINT
  printf("    filename = \"%s\"\n", filename ? filename : "(null)");
  #endif
  free (tempbuffer);
}
/* save sample (save the play buffer  as a wav file) */
void savebuf_cb(Fl_Widget*, void*) {
   SNDFILE * sf;
   SF_INFO sfinfo;
   char * fname = fl_file_chooser("Save buffer","*.wav","tmp.wav",1);
   if (fname == NULL) return;
   sfinfo.channels = mysound.binfo->channels;
   sfinfo.format = mysound.binfo->format;
   sfinfo.samplerate = mysound.binfo->samplerate;
   #ifndef NOPRINT
   printf("savebuffer: %s\n",fname);
   #endif
   sf = sf_open(fname,SFM_WRITE,&sfinfo);
   sf_writef_float(sf,mysound.buffer,mysound.binfo->frames);
   sf_close(sf);

   #ifdef INPUT
   sfinfo.channels = insound.binfo->channels;
   sfinfo.format = insound.binfo->format;
   sfinfo.samplerate = insound.binfo->samplerate;

   printf("channels: %i\n",sfinfo.channels);
   printf("format: %i\n",sfinfo.format);
   printf("rate: %i\n",sfinfo.samplerate);
   printf("frames: %i\n",insound.binfo->frames);

   sf = sf_open("buf.wav",SFM_WRITE,&sfinfo);
   sf_writef_float(sf,insound.buffer,insound.binfo->frames);
   sf_close(sf);
   #endif /*INPUT*/
}

/* ************************************ */
/* save pattern and samples in one file */
/* ************************************ */
void wspsave(Workspace * workp,FILE * fp) {
   char descr[64];
   memset(descr,0,64);
   snprintf(descr,64,"Workspace for\n%i sliders\n%i levels\n%i samples",NSLIDERS,NLEVELS,workp->samp->nsamples);
   fwrite(descr,sizeof(char),64,fp);
   fwrite(&NSLIDERS,sizeof(int),1,fp);
   fwrite(&NLEVELS,sizeof(int),1,fp);
   workp->samp->save_samples(fp);
   workp->patp->savepat(fp);

   snprintf(descr,64,"End Workspace\n");
   fwrite(descr,sizeof(char),64,fp);
   return;
}
void wspload(Workspace * workp,FILE * fp){
   char descr[64];
   int  temp;
   fread(descr,sizeof(char),64,fp);
   fread(&temp,sizeof(int),1,fp);
   if (temp != NSLIDERS){
     printf("ERROR: wrong number of sliders %i %i!\n",temp,NSLIDERS);
     exit(0);
   }
   fread(&temp,sizeof(int),1,fp);
   if (temp != NLEVELS){
     printf("ERROR: wrong number of levels %i %i!\n",temp,NLEVELS);
     exit(0);
   }
   printf("%s",descr);
     workp->samp->read_samples(fp);
     workp->patp->loadpat(fp);
     /*? i don't remember why this is necessary */
     /*TODO: figure this out!*/
     speed->value(workp->patp->speed);
     speed->do_callback();
     speed->redraw();
     speed->parent()->redraw();
   return;
}

void loadworksp_cb(Fl_Widget * w, void * udat) {
     Workspace * workp;
     FILE * fp;
     char * fname = fl_file_chooser("Load workspace","*.wks","out.wks");
     if (fname == NULL) return;

     workp = (Workspace *)(udat); 
     #ifndef NOPRINT
     printf("load workspace: %s\n",fname);
     #endif
     fp = fopen(fname,"rb");
     if (fp == NULL){
        fprintf(stderr,"ERROR %s could not open %s\n",__func__,fname);
        return;
     }
     wspload(workp,fp);
     fclose(fp);
}
void saveworksp_cb(Fl_Widget * w, void * udat) {
     Workspace * workp;
     FILE * fp;
     char * fname = fl_file_chooser("Save workspace","*.wks","out.wks");
     if (fname == NULL) return;

     workp = (Workspace *)(udat); 
     #ifndef NOPRINT
     printf("save workspace: %s\n",fname);
     #endif
     fp = fopen(fname,"wb");
     if (fp == NULL) fprintf(stderr,"ERROR %s could not open %s",__func__,fname);
     wspsave(workp,fp);
     fclose(fp);
}

/* just save the pattern */
void savepat_cb(Fl_Widget * w, void * udat) {
     //Fl_Menu_Item * m = (Fl_Menu_Item *)(w);
     pattern * patp = (pattern *)(udat); 
     FILE * fp;
     char * fname = fl_file_chooser("Save pattern","*.pat","out.pat");
     if (fname == NULL) return;
     #ifndef NOPRINT
     printf("save pattern: %s\n",fname);
     #endif
     fp = fopen(fname,"wb");
     if (fp == NULL) fprintf(stderr,"ERROR %s could not open %s",__func__,fname);
     patp->savepat(fp);
     fclose(fp);
}

void loadpat_cb(Fl_Widget * w, void * udat) {
     FILE * fp;
     pattern * patp; 
     char * fname ;

     patp = (pattern *)(udat); 
     fname = fl_file_chooser("Load pattern","*.pat","in.pat",1);
     if (fname == NULL) return;
     #ifndef NOPRINT
     printf("load pattern: %s\n",fname);
     #endif
     if (fname == NULL) return;

     fp = fopen(fname,"r");
     patp->loadpat(fp);
     fclose(fp);

     speed->value(patp->speed);
     speed->do_callback();
     speed->redraw();
     speed->parent()->redraw();
}



void dpat_cb(Fl_Widget* w, void*) {
     thepattern->dumppat(stdout);
}

void notice_cb(Fl_Widget* w, void*) {
  Fl_Window * nwin = new Fl_Window(600,100) ;
  Fl_Multiline_Output * shownotice = new Fl_Multiline_Output(5,5,590,90,"Notice");
  shownotice->value(notice);
  nwin->end();
  nwin->show();
}

void menu_cb(Fl_Widget* w, void*) {
  Fl_Menu_* mw = (Fl_Menu_*)w;
  const Fl_Menu_Item* m = mw->mvalue();
  #ifndef NOPRINT
  printf("Menu callback called\n");
  if (!m)
    printf("NULL\n");
  else if (m->shortcut())
    printf("%s - %s\n", m->label(), fl_shortcut_label(m->shortcut()));
  else
    printf("%s\n", m->label());
  #endif
}



int main(int argc, char **argv) {
 char * fname;
 FILE * fp;
 int i;
 int fflag=0;
 int nsliders,nlevels;

 Scramble sc;
 pthread_t bartimer_thread;
 printf ("Variseq version %s\n",VARISEQ_VERSION);
 


 #ifdef JACK
	printf("Setting up jack\n");
	jack_options_t options = JackNullOption;
	jack_status_t status;
	const char * client_name;
	jack_ready=0;

	/* become a jack client */
	printf("opening jack\n");
	client = jack_client_open ("variseq",options,&status) ;
	if (client == NULL) {
		fprintf (stderr, "jack client open failed\n");
		fprintf (stderr, "status = 0x%2.0x\n", status);
		exit (1);
	}
	if (status & JackServerStarted) {
		fprintf (stderr, "JACK server started\n");
	}
	if (status & JackNameNotUnique) {
		client_name = jack_get_client_name(client);
		fprintf (stderr, "unique name `%s' assigned\n", client_name);
	}
	jack_set_process_callback(client,ja_cb,&mysound);
	jack_on_shutdown(client,ja_sd,&mysound);
	printf("activating jack\n");
	if (jack_activate(client)){
		fprintf (stderr, "cannot activate client");
	}
	printf("finished activating jack\n");



#endif /*JACK*/
 if (argc <= 1 || argc > 3) print_usage(argv[0]);
 if ( argc == 2 ) {
    char descr[64];
    fname = strdup(argv[1]);
     printf("Opening workspace %s\n",fname);
    fflag = 1;
    fp = fopen(fname,"rb");
    fread(descr,sizeof(char),64,fp);
    printf("%s\n",descr);
    fread(&nsliders,sizeof(int),1,fp);
    fread(&nlevels,sizeof(int),1,fp);
    fclose(fp);
 }
  
/* Set up the main menu bar */
Fl_Menu_Item menutable[] = {
  /* the action list menu */
/*0*/  {"&Do",0,0,0,FL_SUBMENU},
          {"&Play",	FL_ALT+'p', play_cb, 0},
			 {"&Stop",	FL_ALT+'s', stop_cb, 0, FL_MENU_DIVIDER},
			 {"&Quit",	FL_ALT+'q', quit_cb, 0, FL_MENU_DIVIDER},
		 {0},
  /* The sample menu */
/*5*/  {"&Sample",0,0,0,FL_SUBMENU},
			 {"&Load",	FL_ALT+'l', load_cb, 0 },
			 {"Sile&nce",FL_ALT+'r',silence_cb	,	0},
			 {"&Save",0,savebuf_cb,	0},
			 {"Save&Workspace",0,saveworksp_cb,	0},
			 {"LoadWor&kspace",0,loadworksp_cb,	0},
		 {0},
  /* The pattern menu */
/*11*/  {"&Pattern",0,0,0,FL_SUBMENU},
			 {"Load",	0, loadpat_cb, 0, 0},
			 {"Save",   0, savepat_cb,	0, 0},
			 {"Dump",   0, dpat_cb,	0, 0},
  		 {0},
  /* The about menu */
/*16*/  {"&About",0,0,0,FL_SUBMENU},
			 {"Variseq",	0, notice_cb, 0, 0},
  		 {0},
  {0}/* end of menus */
 };

  Fl_Window *window;
   if (! fflag){
     nsliders = (atoi) (argv[1]);
     nlevels = (atoi) (argv[2]);
   }
  NSLIDERS = nsliders;
  NLEVELS = nlevels;


  fc = new Fl_File_Chooser(".", "*.sqn", 0, "Open File:");
  fc->callback(fc_callback,filename);
  bsize_disp = new Fl_Output(10,10,RCTRLS,SWIDTH,"");
  /*
  bar_disp = new Fl_Output(10,10,RCTRLS,SWIDTH,"");
  */
  rewrite_button = new Fl_Round_Button(10,10,RCTRLS,SWIDTH,"");

/* these global pointers need to be defined before makewindow is called! */
/* because they are used in callbacks as the user data                   */
  init_noteratio( &noteratio,nlevels);


  init_sbuf (&mysound,44100);
  init_sbuf (&insound,44100);
  init_sbuf (&origsound,44100);

  #ifdef PORTAUDIO
     Pa_Initialize();
  #endif

  #ifdef JACK
  int j;
     ports=(jack_port_t **)malloc((insound.binfo->channels + mysound.binfo->channels) * sizeof (jack_port_t *));
       #ifndef NOPRINT
       printf("ports %x\n",ports);
       #endif
     out=(jack_default_audio_sample_t **)malloc(mysound.binfo->channels *sizeof(jack_default_audio_sample_t *));

     for (i=0;i<mysound.binfo->channels;i++){
        char name[64];
        snprintf(name,64,"channel_%i",i);
        printf("%s\n",name);
        #ifndef NOPRINT
        printf("%s About to register %s\n",__func__,name);
        #endif /*NOPRINT*/
        ports[i]=jack_port_register(client,name,JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,0);
        #ifndef NOPRINT
        printf(" ports[%i] %x\n",i,ports[i]);
        printf("%s  registered %s\n",__func__,name);
        #endif /*NOPRINT*/
        if (ports[i] == NULL){
           fprintf(stderr,"ERROR:variseq: cannot connect to jack\n");
           jack_client_close(client);
           exit(1);
       }
    }

     #ifdef INPUT
     /*input setup*/
     j=i;
     in=(jack_default_audio_sample_t **)malloc(insound.binfo->channels*sizeof(jack_default_audio_sample_t *));
     for (i=0;i<insound.binfo->channels;i++){
        char name[64];
        snprintf(name,64,"in_channel_%i",i);
        printf("%s\n",name);
        ports[i+j]=jack_port_register(client,name,JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,0);
        if (ports[i+j] == NULL){
           fprintf(stderr,"ERROR:variseq: cannot conect jack\n");
           jack_client_close(client);
           exit(1);
       }
    }
    #endif /*INPUT*/

    jack_ready=1;

  #endif /*JACK*/
  sampletable=new samples (nlevels);
  thepattern =new pattern (nsliders,120);
  Workspace theworkspace;
  theworkspace.patp = thepattern;
  theworkspace.samp = sampletable;

  #ifdef SCRAMBLE
  sc.wsp=&theworkspace;
  sc.nscramble=3;
  sc.scramblesamples=(int *) calloc(3,sizeof(int));
  sc.scrambletimes=(int *) calloc(3,sizeof(int));
  sc.scrambletypes=(int *) calloc(3,sizeof(int));
  sc.scramblesamples[0] = 0;
  sc.scramblesamples[1] = 1;
  sc.scramblesamples[2] = 2;
  sc.scrambletimes[0] = 15;
  sc.scrambletimes[1] = 8;
  sc.scrambletimes[2] = 1;

  sc.scrambletypes[0] = 0;
  sc.scrambletypes[1] = 2;
  sc.scrambletypes[2] = 3;

  pthread_create(&bartimer_thread,NULL,bar_timer,&sc);
  #endif





  /* make the main window */
   window = makewindow(nsliders,nlevels);

  Fl_Menu_Bar menubar(0,0,window->w(),MENU);
	  menubar.menu(menutable);
	  menubar.callback(menu_cb);
   /* cannot figure out how to get the correct pointer into the user_data_ */
   /* during the initialization */
   int nums[20];
   for (int i=0;i<19;i++){
      nums[i]=i;
      menutable[i].user_data_=&(nums[i]);
   }

   menutable[12].user_data_ = thepattern;
   menutable[13].user_data_ = thepattern;
   menutable[9].user_data_ = (&theworkspace);
   menutable[10].user_data_ = (&theworkspace);
  window->add(&menubar);




  window->end();

  /* create the window for displaying the sample names */
  fwin = fwindow(); 
  fwin->end();

  if (fflag){
     fp = fopen(fname,"rb");
     if (fp == NULL) fprintf(stderr,"ERROR %s could not open %s",__func__,fname);
     wspload(&theworkspace,fp);
     fclose(fp);
  }
 // window->show(argc, argv);
  window->show();

  fwin->show();

  Fl::run();

  #ifdef JACK
  jack_client_close(client);
  #endif /*JACK*/
  return(0); 

}


/* $Id: player.C,v 1.5 2006/04/23 22:03:14 robertatwood Exp $ */
@


1.5
log
@Fixed popup window location and sample name label
@
text
@d18 1
a18 1
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
d22 1
d36 2
d768 2
d1005 1
a1005 1
/* $Id: player.C,v 1.4 2006/04/05 21:53:36 robertatwood Exp $ */
@


1.4
log
@Jack version is the main branch now
@
text
@d109 1
a109 1
const char notice[1000] = "Variseq v. 0.2.1, Copyright (C) 2004  Robert Atwood\nVariseq comes with ABSOLUTELY NO WARRANTY; for details\nsee the file COPYING. This is free software, (GPL v2 or later) and you are welcome to\nredistribute it under certain conditions, see the file COPYING\nfor details.\n";
d1000 1
a1000 1
/* $Id: player.C,v 1.3.2.10 2006/04/05 21:42:24 robertatwood Exp $ */
@


1.3
log
@Added a save/restore workspace function
Added window to display names of samples
@
text
@d22 2
d34 17
d52 5
a56 1
#include <samplerate.h>
d63 1
d65 1
a65 1
 typedef struct workspace_str{
d69 10
d85 1
a87 1
PortAudioStream * mystream;
d89 4
a92 1
sbuf mysound,origsound;
d98 3
d102 1
d136 5
a140 1
   if(going ==1 ) Pa_StopStream(mystream);
d146 5
a150 1
   if(going ==1 ) Pa_StartStream(mystream);
d182 1
a182 1
   free(bp);
d192 75
d274 9
a282 3
int pa_cb (void * inp, void * outp, unsigned long frames,PaTimestamp outTime,void *udata){
    static int n=0,nsamples=0; /* count frames,samples */
    float * op;
d284 1
a284 1
    int c;
d286 18
d308 2
d320 2
d323 1
d325 19
d346 120
d485 4
d490 1
d492 1
d499 5
a503 1
  if (going == 1 ) Pa_StopStream(mystream);
d511 16
a576 3



d593 15
d759 1
d762 38
d825 1
a825 1
			 {"&Replace",FL_ALT+'r',	0,	0},
d855 3
d863 2
a864 1
  Pa_Initialize();
d866 1
d868 52
d926 22
d975 1
d990 7
a996 1
  return Fl::run();
d1000 1
a1000 1
/* $Id: player.C,v 1.8 2005/08/20 18:23:08 robert Exp $ */
@


1.3.2.1
log
@Main jack callback compiles ...  so far so good
@
text
@d32 1
a33 17
#if ( defined JACK && defined PORTAUDIO )
#error Cannot define both
#endif
#ifdef JACK
#include <jack/jack.h>
   jack_port_t **ports;
	jack_default_audio_sample_t **out;
   jack_client_t *client;


#elif defined PORTAUDIO 
#include <portaudio.h>
   PortAudioStream * mystream;
#else
/* no output defined */
#error NO OUTPUT DEFINED
#endif
d53 1
d95 1
a95 5
   if(going ==1 ){
       #ifdef PORTAUDIO
       Pa_StopStream(mystream);
       #endif
   }
d101 1
a101 5
   if(going ==1 ){
      #ifdef PORTAUDIO
      Pa_StartStream(mystream);
      #endif
   }
a149 1
#ifdef PORTAUDIO
a173 54
 #endif

 #ifdef JACK
void ja_sd (void *arg) {
	exit (1);
}

int ja_cb (jack_nframes_t frames, void *arg) {
	jack_transport_state_t ts = jack_transport_query(client, NULL);
    /* my variables */
    /* n keeps count of frames in my buffer */
    /* i counts the frames in the output buffer */
    /* TODO: use a JACK ring buffer ? */
    static int n=0,nsamples=0; /* count frames,samples */
    unsigned long i;
    int c;
    unsigned int mm;
   mm = mysound.binfo->channels;

	/* check if the jack is running */
	if (ts == JackTransportRolling) {
   for(c=0;c<mm;c++){
       out[c] = (jack_default_audio_sample_t *)jack_port_get_buffer (ports[c], frames);
   }

    for(i=0;i<frames;i++){
		 if(n>=mysound.binfo->frames){
			 n=0;
			 nsamples=0;
		  }
          /* loop at teh end of the buffer */
          /* TODO: fix dependence on 2 channels */
        for(c=0;c<mm;c++){
          *(out[c] +i) = mysound.buffer[mm*n+c];
          nsamples++;
        }/* loop channels */
       n++;
		 if(n>=mysound.binfo->frames){
			 n=0;
			 nsamples=0;
		 }
     }/* loop frames */

		/*
		memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
	 */

	} else if (ts == JackTransportStopped) {

	}

	return 0;      
}
#endif
a192 1
		#ifdef PORTAUDIO
a194 1
		#endif /*PORTAUDIO*/
d201 1
a201 5
  if (going == 1 ){
		#ifdef PORTAUDIO
     Pa_StopStream(mystream);
		#endif /*PORTAUDIO*/
  }
a428 1
 int i;
a430 15
 #ifdef JACK
	/* become a jack client */
	if ((client = jack_client_new ("variseq")) == 0) {
		fprintf (stderr, "jack server not running?\n");
		exit (1);
	}
	jack_set_process_callback(client,ja_cb,&mysound);
	jack_on_shutdown(client,ja_sd,&mysound);
	if (jack_activate(client)){
		fprintf (stderr, "cannot activate client");
	}



#endif /*JACK*/
d491 1
a491 3
  #ifdef PORTAUDIO
     Pa_Initialize();
  #endif
a493 15
  #ifdef JACK
     ports=(jack_port_t **)malloc(mysound.binfo->frames * sizeof (jack_port_t *));
     out=(jack_default_audio_sample_t **)malloc(mysound.binfo->frames *sizeof(jack_default_audio_sample_t *));

     for (i=0;i<mysound.binfo->frames;i++){
        char name[64];
        snprintf(name,64,"channel_%i",i);
        ports[i]=jack_port_register(client,name,JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,0);
        if (ports[i] == NULL){
           fprintf(stderr,"ERROR: cannot conect jack\n");
           jack_client_close(client);
           exit(1);
       }
    }
  #endif /*JACK*/
d541 1
a541 5
  Fl::run();

  jack_client_close(client);
  return(0); 

d545 1
a545 1
/* $Id: player.C,v 1.3 2005/08/20 18:26:42 robertatwood Exp $ */
@


1.3.2.2
log
@Works at least, sound comes out with JACK running!
@
text
@a516 5
	printf("Setting up jack\n");
	jack_options_t options = JackNullOption;
	jack_status_t status;
	const char * client_name;

d518 1
a518 2
	printf("opening jack\n");
	if ((client = jack_client_open ("variseq",options,&status)) == NULL) {
a521 7
	if (status & JackServerStarted) {
		fprintf (stderr, "JACK server started\n");
	}
	if (status & JackNameNotUnique) {
		client_name = jack_get_client_name(client);
		fprintf (stderr, "unique name `%s' assigned\n", client_name);
	}
a523 1
	printf("activating jack\n");
a526 1
	printf("finished activating jack\n");
d600 1
a600 1
     for (i=0;i<mysound.binfo->channels;i++){
a602 1
        printf("%s\n",name);
d605 1
a605 1
           fprintf(stderr,"ERROR:variseq: cannot conect jack\n");
d666 1
a666 1
/* $Id: player.C,v 1.3.2.1 2005/11/24 00:45:04 robertatwood Exp $ */
@


1.3.2.3
log
@Began sample-replace segment
@
text
@a292 55
void replace_cb(Fl_Widget*, void*) {
  #ifndef NOPRINT
  printf("%s \n",__func__);
  #endif
  const char * retval;
  sbuf * newsound;
  SF_INFO * sfinfo = (SF_INFO *)calloc(1,sizeof(SF_INFO));
  SNDFILE * infile;
  float * tempbuffer = (float * )calloc(44100*2,sizeof(float));
  int nread,ninp,i;
  newsound = (sbuf * ) calloc(1,sizeof(sbuf));
  char * filename ;

     filename = fl_file_chooser("Load rample","*.wav","in.wav",1);
     #ifndef NOPRINT
     printf("load sample: %s\n",filename);
     #endif
     if (filename == NULL) return;
  /* only request a maximum of 1 second per sample for now...*/
  infile = sf_open(filename,SFM_READ,sfinfo);
  if (sfinfo->frames < 44100 ) nread = sfinfo->frames;
  else nread = 44100;
 // never mind multichannel files for now... 
 //if (sfinfo->channels > 2) nread = (44100*2)/(sfinfo->channels);
  /* read into tempbuffer to avoid interrupting the audio stream */
  ninp = sf_readf_float(infile,tempbuffer,nread);
  
  if (sfinfo->channels >2 || sfinfo->channels < 1){
    fprintf(stderr,"ERROR:load_cb Cannot handle %i channels\n",sfinfo->channels);
  }else{
     if(sfinfo->channels == 2){
        memcpy(origsound.buffer,tempbuffer,ninp*2*sizeof(float));
     }
     if (sfinfo->channels == 1){
        for (i=0;i<ninp;i++){
           *(origsound.buffer + 2*i) = tempbuffer[i];
           *(origsound.buffer + 2*i + 1) = tempbuffer[i];
        }
     }
     origsound.binfo->frames=ninp;
     /* sample is now read into the buffer */
     /* find out which sample to replace */
     
     /* add the new sample to the sample table */
     sampletable->replace_sample(&origsound,filename);
     /*addbutton(filename);*/
  }

  sf_close(infile);
  #ifndef NOPRINT
  printf("    filename = \"%s\"\n", filename ? filename : "(null)");
  #endif
  free (tempbuffer);
}/*end of replace_cb*/

d343 3
d571 1
a571 1
			 {"&Replace",FL_ALT+'r',replace_cb	,	0},
a659 1
  /* create the window for displaying the sample names */
d682 1
a682 1
/* $Id: player.C,v 1.3.2.2 2005/11/24 01:23:44 robertatwood Exp $ */
@


1.3.2.4
log
@Checkpoint-problem with sample table address
@
text
@a293 1
  #ifdef JUNK
a345 1
  #endif /*JUNK*/
a728 1
  #ifdef JACK
a729 1
  #endif /*JACK*/
d735 1
a735 1
/* $Id: player.C,v 1.3.2.3 2005/12/28 01:34:16 robertatwood Exp $ */
@


1.3.2.5
log
@Appears to work but needs file name placed in the textbox after replacement
@
text
@d156 1
a156 1
   //free(bp);
d739 1
a739 1
/* $Id: player.C,v 1.3.2.4 2005/12/28 16:13:35 robertatwood Exp $ */
@


1.3.2.6
log
@Input capture working with as compile time #ifdef
@
text
@d70 1
a70 1
sbuf mysound,origsound,insound;
d174 3
a176 7
int pa_cb (void * inp, void * outp, 
            unsigned long frames,
            PaTimestamp outTime,void *udata)
{

    static int n=0,nsamples=0,nin=0,ototal=0,itotal; /* count frames,samples */
    float * op,* ip;
d178 1
a178 1
    int c,osamples,isamples,ibuflen;
a179 18
    ip = (float *) inp;

#ifdef INPUT
    isamples=insound.binfo->channels*frames;
    ibuflen=insound.binfo->channels*insound.binfo->frames;
    	for ( i=0;i<isamples;i++ ){
		     if(nin >= ibuflen){
		    	 nin=0;
		      }
            insound.buffer[nin] = *ip;
            ip++;
            nin++;
	    	 if(nin >= ibuflen){
  	    	    nin=0;
	    	 }
	   }
  #endif /*INPUT*/

a194 1
     ototal++;
d198 1
a198 1
 #endif /*PORTAUDIO*/
a271 3
		#ifdef INPUT
		Pa_OpenDefaultStream(&mystream,2,2,paFloat32,44100,256,0,pa_cb,NULL);
		#else
a272 1
		#endif /*INPUT*/
a415 15

   #ifdef INPUT
   sfinfo.channels = insound.binfo->channels;
   sfinfo.format = insound.binfo->format;
   sfinfo.samplerate = insound.binfo->samplerate;

   printf("channels: %i\n",sfinfo.channels);
   printf("format: %i\n",sfinfo.format);
   printf("rate: %i\n",sfinfo.samplerate);
   printf("frames: %i\n",insound.binfo->frames);

   sf = sf_open("buf.wav",SFM_WRITE,&sfinfo);
   sf_writef_float(sf,insound.buffer,insound.binfo->frames);
   sf_close(sf);
   #endif /*INPUT*/
a659 6


  init_sbuf (&mysound,44100);
  init_sbuf (&insound,44100);
  init_sbuf (&origsound,44100);

d663 2
a664 1

d739 1
a739 1
/* $Id: player.C,v 1.3.2.5 2005/12/28 18:17:00 robertatwood Exp $ */
@


1.3.2.7
log
@Jack input as well, and scrambling in compile-time option
@
text
@a21 2
#include <pthread.h>
#include <errno.h>
a32 5
pthread_mutex_t bar_lock =PTHREAD_MUTEX_INITIALIZER ;
pthread_mutex_t rewrite_lock=PTHREAD_MUTEX_INITIALIZER ;
pthread_cond_t bar_cond =PTHREAD_COND_INITIALIZER ;


d39 1
a39 1
	jack_default_audio_sample_t **out,**in;
a55 1
#include "replace.h"
d57 1
a57 1
typedef struct workspace_str{
a60 10

typedef struct scramble_str{
  int nscramble;
  int * scramblesamples;
  int * scrambletimes;
  int * scrambletypes; /* file, input, buffer */
  Workspace * wsp;
}Scramble;


a66 1
int barcount=0;
a70 2

/* objects in the main window  */
a75 1
Fl_Output * bar_disp;
a76 1

a165 65
/* routine to execute periodic alterations automatically */

void * bar_timer(void * udat){
     static int ctr=0;
     static char barnum[32];
     int i;
     int rflag=0;
     Scramble * scp = (Scramble *)(udat);
     while (1){
		  pthread_mutex_lock(&bar_lock);
        #ifndef NOPRINT
        printf("%s lock bar and wait\n",__func__);
        #endif
		  pthread_cond_wait(&bar_cond,&bar_lock);
        #ifndef NOPRINT
        printf("%s unlock bar\n",__func__);
        #endif
		  pthread_mutex_unlock(&bar_lock);

		  #ifndef NOPRINT
		     printf("ctr %i, ",ctr);
		     printf("barcount %i\n",barcount);
		  #endif

		  snprintf(barnum,32,"%i",ctr);
		  bar_disp->value(barnum);
		  bar_disp->redraw();

		  for (i=0;i<scp->nscramble;i++){
		      if (ctr % scp->scrambletimes[i] == 0){
		         #ifndef NOPRINT
		         printf("Scrambling %i\n",i);
		         #endif
		         if (scp->scramblesamples[i] >= scp->wsp->samp->nsamples){
						#ifndef NOPRINT
						printf("No sample %i\n",scp->scramblesamples[i]);
						#endif
				  }else{
						#ifndef NOPRINT
						printf("Sample %i\n",scp->scramblesamples[i]);
						#endif
						switch (scp->scrambletypes[i]){
							case 2:
								replace_inp(scp->wsp->samp,scp->scramblesamples[i]);
								rflag=1;
							break;
							case 3:
								replace_buf_rnd(scp->wsp->samp,scp->scramblesamples[i]);
								rflag=1;
							break;
							default:
							/* don't do anything */
							break;
   }
				  }
		      }
		   }/* loop through specified samples */
		   if (rflag == 1 ){
		     rewrite_button->do_callback();
		     rflag = 0;
		   }
		  ctr++;
     }
return (NULL);
}
a179 1
    static int bflag=0;
a205 2
			 barcount++;
			 bflag=1;
a215 2
			 barcount++;
			 bflag=1;
a218 19
     /* wake up the end of bar routine */
     if (bflag){
        if( pthread_mutex_trylock(&bar_lock) == EBUSY){
           #ifndef NOPRINT
              printf("busy\n");
           #endif
        }else{
           #ifndef NOPRINT
              printf("%s trylock bar\n",__func__);
           #endif
           pthread_cond_signal(&bar_cond);
           #ifndef NOPRINT
              printf("%s unlock bar\n",__func__);
           #endif
           pthread_mutex_unlock(&bar_lock);
           bflag=0;
        }
     }

d234 1
a234 1
    static int n=0,nsamples=0,nin=0,bflag=0; /* count frames,samples */
d237 1
a237 1
    unsigned int mm,nn;
a249 2
			 barcount++;
			 bflag=1;
a260 2
			 barcount++;
			 bflag=1;
d264 3
a266 26
     /* wake up the end of bar routine  */
     if (bflag){
        if( pthread_mutex_trylock(&bar_lock) == EBUSY){
           #ifndef NOPRINT
              printf("busy\n");
           #endif
        }else{
           #ifndef NOPRINT
              printf("%s trylock bar\n",__func__);
           #endif
           pthread_cond_signal(&bar_cond);
           #ifndef NOPRINT
              printf("%s unlock bar\n",__func__);
           #endif
           pthread_mutex_unlock(&bar_lock);
           bflag=0;
        }
     }

     #ifdef INPUT
     /* input section */

   nn=insound.binfo->channels;
   for(c=0;c<nn;c++){
       in[c] = (jack_default_audio_sample_t *)jack_port_get_buffer (ports[c+mm], frames);
   }
a267 15
/* interlace the samples as */
    for(i=0;i<frames;i++){
		 if(nin>=insound.binfo->frames){
			 nin=0;
		  }
          /* loop at teh end of the buffer */
        for(c=0;c<nn;c++){
          insound.buffer[nn*nin+c] = *(in[c] +i)  ;
        }/* loop channels */
       nin++;
		 if(nin>=insound.binfo->frames){
			 nin=0;
		 }
     }/* end loop input frames */
     #endif /*INPUT*/
d320 2
a321 1
void silence_cb(Fl_Widget*, void*) {
d323 1
a323 1
  printf("%s\n",__func__);
a324 1

d327 6
a332 1
  memset(origsound.buffer,0,44100*2*sizeof(float));
d334 30
a363 1
  origsound.binfo->frames=44100;
d365 3
a367 2
     sampletable->add_sample(&origsound,"silence");
     addbutton("silence");
d369 7
a375 1
}/*end of silence_cb*/
a611 5

 Scramble sc;
 pthread_t bartimer_thread;


d667 1
a667 1
			 {"Sile&nce",FL_ALT+'r',silence_cb	,	0},
a696 1
  bar_disp = new Fl_Output(10,10,RCTRLS,SWIDTH,"");
d713 2
a714 3
  int j;
     ports=(jack_port_t **)malloc((insound.binfo->channels + mysound.binfo->channels) * sizeof (jack_port_t *));
     out=(jack_default_audio_sample_t **)malloc(mysound.binfo->channels *sizeof(jack_default_audio_sample_t *));
a726 17

     #ifdef INPUT
     /*input setup*/
     j=i;
     in=(jack_default_audio_sample_t **)malloc(insound.binfo->channels*sizeof(jack_default_audio_sample_t *));
     for (i=0;i<insound.binfo->channels;i++){
        char name[64];
        snprintf(name,64,"in_channel_%i",i);
        printf("%s\n",name);
        ports[i+j]=jack_port_register(client,name,JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,0);
        if (ports[i+j] == NULL){
           fprintf(stderr,"ERROR:variseq: cannot conect jack\n");
           jack_client_close(client);
           exit(1);
       }
    }
    #endif /*INPUT*/
a733 22
  #ifdef SCRAMBLE
  sc.wsp=&theworkspace;
  sc.nscramble=3;
  sc.scramblesamples=(int *) calloc(3,sizeof(int));
  sc.scrambletimes=(int *) calloc(3,sizeof(int));
  sc.scrambletypes=(int *) calloc(3,sizeof(int));
  sc.scramblesamples[0] = 0;
  sc.scramblesamples[1] = 1;
  sc.scramblesamples[2] = 2;
  sc.scrambletimes[0] = 15;
  sc.scrambletimes[1] = 8;
  sc.scrambletimes[2] = 1;

  sc.scrambletypes[0] = 0;
  sc.scrambletypes[1] = 2;
  sc.scrambletypes[2] = 3;

  pthread_create(&bartimer_thread,NULL,bar_timer,&sc);
  #endif



d786 1
a786 1
/* $Id: player.C,v 1.3.2.6 2005/12/29 00:20:53 robertatwood Exp $ */
@


1.3.2.8
log
@Sample scramblizer adjusters added
@
text
@a196 5
     Adj ** ap = &(scp->wsp->samp->adj);
     int * nsamples ;
     nsamples = &(scp->wsp->samp->nsamples);


d200 1
a200 1
           printf("%s lock bar and wait\n",__func__);
a201 1

a202 1

d204 1
a204 1
           printf("%s unlock bar\n",__func__);
a205 1

d217 10
a226 10
		  #ifndef NOPRINT
		     printf("%s nsamples %i \n",__func__,*nsamples);
		     printf("%s sampletable->nsamples %i \n",__func__,sampletable->nsamples);
		  #endif
		  for (i=0;i<*nsamples;i++){
		  #ifndef NOPRINT
		     printf("%s sample %i bars %i type %i\n",__func__,i,(*ap)[i].bars,(*ap)[i].type);
		  #endif
		      if ((*ap)[i].type != 0 ){
   		     if (ctr % (*ap)[i].bars == 0){
d228 1
a228 1
						printf("Sample %i\n",i);
d230 1
a230 1
						switch ((*ap)[i].type){
d232 1
a232 1
								replace_inp(scp->wsp->samp,i);
d236 1
a236 1
								replace_buf_rnd(scp->wsp->samp,i);
d242 1
a242 1
				     }
d945 1
a945 1
/* $Id: player.C,v 1.3.2.7 2006/01/04 00:06:03 robertatwood Exp $ */
@


1.3.2.9
log
@Jack and input autosampler
Need to use -DJACK -DINPUT -DSCRAMBLE
Sometimes seg faults due to unknown jack related problem
@
text
@a344 1
   printf("%s Shutting down due to jack shut-down\n",__func__);
d360 2
a362 4
       #ifndef NOPRINT
       printf("out %x ports %x\n",out,ports);
       printf("out[c] %x ports[c] %x\n",out[c],ports[c]);
       #endif
a364 2
	/* check if the jack is running */
	if (ts == JackTransportRolling) {
d431 1
a431 5
    for(i=0;i<frames;i++){
        for(c=0;c<mm;c++){
          *(out[c] +i) = 0;
        }/* loop channels */
     }/* loop frames */
d746 2
a747 4
	client = jack_client_open ("variseq",options,&status) ;
	if (client == NULL) {
		fprintf (stderr, "jack client open failed\n");
		fprintf (stderr, "status = 0x%2.0x\n", status);
a841 3
       #ifndef NOPRINT
       printf("ports %x\n",ports);
       #endif
a847 3
        #ifndef NOPRINT
        printf("%s About to register %s\n",__func__,name);
        #endif /*NOPRINT*/
a848 4
        #ifndef NOPRINT
        printf(" ports[%i] %x\n",i,ports[i]);
        printf("%s  registered %s\n",__func__,name);
        #endif /*NOPRINT*/
d850 1
a850 1
           fprintf(stderr,"ERROR:variseq: cannot connect to jack\n");
d953 1
a953 1
/* $Id: player.C,v 1.3.2.8 2006/01/04 02:31:33 robertatwood Exp $ */
@


1.3.2.10
log
@Jack or portaudio version with compile-time selection
Autoscramblizer option , input option
@
text
@a89 1
int jack_ready=0;
a96 1
/*
a97 1
*/
a220 1
		  /*
a223 1
		  */
d348 1
a348 1
#define NOPRINT_callback
d350 1
a355 1
    static int initflag=0;
a358 15
	jack_transport_state_t ts ;

       #ifndef NOPRINT_callback
       printf("%s ready %i\n",__func__,jack_ready);
       printf("out[c] %x ports[c] %x\n",out[c],ports[c]);
       #endif

	if (jack_ready == 0){
	   return(0);
	} else if (initflag == 0 ){
	    initflag=1;
	    return(0);
	}

	ts = jack_transport_query(client, NULL);
d362 1
a362 1
       #ifndef NOPRINT_callback
d396 1
a396 1
           #ifndef NOPRINT_callback
d400 1
a400 1
           #ifndef NOPRINT_callback
d404 1
a404 1
           #ifndef NOPRINT_callback
a751 1
	jack_ready=0;
a833 1
  /*
a834 1
  */
a892 3

    jack_ready=1;

d974 1
a974 1
/* $Id: player.C,v 1.3.2.9 2006/01/10 23:43:46 robertatwood Exp $ */
@


1.2
log
@Changed the buffer writing so that samples extend into unused notes.
Altered file choose usage to correctly use the FLTK chooser
@
text
@d41 7
d65 4
a68 1
const char notice[1000] = "Variseq v. 0.1.0, Copyright (C) 2004  Robert Atwood\nVariseq comes with ABSOLUTELY NO WARRANTY; for details\nsee the file COPYING. This is free software, (GPL v2 or later) and you are welcome to\nredistribute it under certain conditions, see the file COPYING\nfor details.\n";
d118 12
d250 2
a251 2
     sampletable->add_sample(&origsound);
     addbutton();
d268 1
d279 2
d282 76
d363 1
d380 1
d427 17
a443 1
 if (argc != 3) print_usage(argv[0]);
d458 2
d462 1
a462 1
/*10*/  {"&Pattern",0,0,0,FL_SUBMENU},
d468 1
a468 1
/*10*/  {"&About",0,0,0,FL_SUBMENU},
d475 6
a480 2
  int nsliders = (atoi) (argv[1]);
  int nlevels = (atoi) (argv[2]);
d496 4
d510 6
a515 1
   menutable[11].user_data_ = thepattern;
d517 3
d527 9
d539 2
d545 1
a545 1
/* $Id: player.C,v 1.3 2005/04/03 19:05:10 robert Exp $ */
@


1.1
log
@Initial version uploaded to Sourceforge
@
text
@a42 1
Fl_File_Chooser *ld;
d54 1
d58 1
a58 1
const char notice[1000] = "Variseq v. 0.00, Copyright (C) 2004  Robert Atwood\nVariseq comes with ABSOLUTELY NO WARRANTY; for details\nsee the file COPYING. This is free software, (GPL v2 or later) and you are welcome to\nredistribute it under certain conditions, see the file COPYING\nfor details.\n";
d163 25
a187 3
/* choose a file and open it */
void ld_callback(Fl_File_Chooser *fc,	void *){
  
d189 1
a189 1
  printf("ld_callback \n");
d198 1
d200 5
a204 9
  retval = fc->value();

  /* no file selected */
  if (retval == NULL) return;

  /* avoid gathering lost strings */
  if (filename != NULL) free(filename);
  filename = strdup(retval);

a208 1

d215 1
a215 1
    fprintf(stderr,"ERROR:ld_callback Cannot handle %i channels\n",sfinfo->channels);
a236 1
}
a239 27
void quit_cb(Fl_Widget*, void*) {exit(0);}

void play_cb(Fl_Widget*, void * ) {
  #ifndef NOPRINT
  printf("    filename = \"%s\"\n", filename ? filename : "(null)");
  #endif
   if (filename == NULL) return;
   thepattern->write(&mysound,0);
   if (going ==0){
		Pa_OpenDefaultStream(&mystream,0,mysound.binfo->channels,paFloat32,44100,256,0,pa_cb,NULL);
		Pa_StartStream (mystream);
		going = 1;
   }
   
  }

void stop_cb(Fl_Widget*, void*) {
  if (going == 1 ) Pa_StopStream(mystream);
  going = 0;
}

void open_cb(Fl_Widget*, void*) {
   fc->show();
}

void load_cb(Fl_Widget*, void*) {
   ld->show();
a359 2
  ld = new Fl_File_Chooser(".", "*.wav", 0, "Load Sample:");
  ld->callback(ld_callback,filename);
d361 1
d390 2
a391 1
  window->show(argc, argv);
d397 1
a397 1
/* $Id: player.C,v 1.9 2004/04/12 13:22:33 robert Exp $ */
@

