head	1.3;
access;
symbols
	rel-0-2-1:1.2
	before_adj_struct:1.1.2.5
	jack:1.1.0.2;
locks; strict;
comment	@ * @;


1.3
date	2007.03.22.20.55.43;	author robertatwood;	state Exp;
branches;
next	1.2;

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

1.1
date	2005.12.28.01.34.16;	author robertatwood;	state dead;
branches
	1.1.2.1;
next	;

1.1.2.1
date	2005.12.28.01.34.16;	author robertatwood;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2005.12.28.16.13.35;	author robertatwood;	state Exp;
branches;
next	1.1.2.3;

1.1.2.3
date	2005.12.28.18.17.00;	author robertatwood;	state Exp;
branches;
next	1.1.2.4;

1.1.2.4
date	2005.12.29.00.20.53;	author robertatwood;	state Exp;
branches;
next	1.1.2.5;

1.1.2.5
date	2006.01.04.00.06.03;	author robertatwood;	state Exp;
branches;
next	1.1.2.6;

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


desc
@@


1.3
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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.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>

#include "notes.h"
#include "mysound.h"
#include "pattern.h"
extern sbuf origsound,insound,mysound;
void replace(samples * sampletable,int whichrepl) {
  #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 ;

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

     filename = fl_file_chooser("Replace rample","*.wav","in.wav",1);
     #ifndef NOPRINT
     printf("replace 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;
  /* read into tempbuffer to avoid interrupting the audio stream */
  ninp = sf_readf_float(infile,tempbuffer,nread);

     #ifndef NOPRINT
     printf("replace sample: %s frames %i ",filename,sfinfo->frames);
     printf("nread %i ",nread);
     printf("ninp %i\n",ninp);
     #endif
  if (nread <= MINFRAMES){
    printf("Not enough frames in the file.\n");
  }else if (sfinfo->channels >2 || sfinfo->channels < 1){
    fprintf(stderr,"ERROR:replace Cannot handle %i channels\n",sfinfo->channels);
  }else{
     if(sfinfo->channels == 2){
        memcpy(origsound.buffer,tempbuffer,44100*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 */
     /* replace sample in the sample table */
     sampletable->replace_sample(&origsound,filename,whichrepl);
  }

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

/* */
/* replace the sample with fixed amount of current input stream */
/* */
void replace_inp(samples * sampletable,int whichrepl) {
     #ifdef INPUT
        #ifndef NOPRINT
        printf("entering %s\n",__func__);
        #endif
     sampletable->replace_sample(&insound,"input",whichrepl);
     #endif
     return;
}/* end of replace_inp */

/* */
/* replace the sample with fixed amount of current output buffer */
/* */
void replace_buf_rnd(samples * sampletable,int whichrepl) {

     float * tempbuffer = (float * )calloc(44100*2,sizeof(float));
     unsigned int nbufsamp,nneed,lastpos,startpos,ncopy;
     unsigned short int randuint;
     float frac;
        #ifndef NOPRINT
        printf("entering %s\n",__func__);
        #endif
     nbufsamp = mysound.binfo->frames * mysound.binfo->channels;
     nneed = 44100*mysound.binfo->channels;
     if (nbufsamp <= nneed){
		  /* less that required amount in buffer - use it all */
		  memcpy(tempbuffer,mysound.buffer,nbufsamp*sizeof(float));
		  ncopy=mysound.binfo->frames;
 	 }else{
 	    /* more than required amount, find a random starting point */
        int fd;
        fd = open ("/dev/urandom", O_RDONLY);
        read (fd, &(randuint), sizeof (unsigned short int));
        #ifndef NOPRINT
        fprintf (stderr, "Kernel random uint %hi\n", randuint);
        #endif
        close (fd);

 	    lastpos = nbufsamp - nneed;

 	    frac = ((float)(randuint))/((float)(65536));
 	    startpos =  ((unsigned int)(frac * lastpos));
        #ifndef NOPRINT
  	    printf("frac %g\n", frac);
        printf ( "lastpos %u\n", lastpos);
        printf ( "random position %u\n", startpos);
        #endif
        memcpy(tempbuffer,mysound.buffer+startpos,nneed * sizeof(float));
        ncopy=44100;
      }
      /* copy whole buffer including trailing silence if any */
      memcpy(origsound.buffer,tempbuffer,44100*2*sizeof(float));
      origsound.binfo->frames=ncopy;
      sampletable->replace_sample(&origsound,"buffer",whichrepl);

      free(tempbuffer);
      return;
}/* end of replace_buf_rnd */

@


1.2
log
@Jack version is the main branch now
@
text
@d18 1
a18 1
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
d21 1
@


1.1
log
@file replace.C was initially added on branch jack.
@
text
@d1 167
@


1.1.2.1
log
@Began sample-replace segment
@
text
@a0 91
/**************************************************************
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
See the file COPYING for more information
**************************************************************/
#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>

#include "notes.h"
#include "mysound.h"
#include "pattern.h"
extern sbuf origsound;
void replace(samples * sampletable,int whichrepl) {
  #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*/

@


1.1.2.2
log
@Checkpoint-problem with sample table address
@
text
@d81 1
a81 1
     sampletable->replace_sample(&origsound,filename,whichrepl);
@


1.1.2.3
log
@Appears to work but needs file name placed in the textbox after replacement
@
text
@d50 1
a50 1
     filename = fl_file_chooser("Replace rample","*.wav","in.wav",1);
d52 1
a52 1
     printf("replace sample: %s\n",filename);
a62 10

     #ifndef NOPRINT
     printf("replace sample: %s frames %i ",filename,sfinfo->frames);
     printf("nread %i ",nread);
     printf("ninp %i\n",ninp);
     #endif
  if (nread <= MINFRAMES){
    printf("Not enough frames in the file.\n");
    return;
  }
d65 1
a65 1
    fprintf(stderr,"ERROR:replace Cannot handle %i channels\n",sfinfo->channels);
@


1.1.2.4
log
@Input capture working with as compile time #ifdef
@
text
@d36 1
a36 1
extern sbuf origsound,insound;
d42 1
a42 2
  //sbuf * newsound;
#ifndef INSOUND
d47 1
a47 1
  //newsound = (sbuf * ) calloc(1,sizeof(sbuf));
a99 3
     #else 
     sampletable->replace_sample(&insound,"input",whichrepl);
     #endif /*INSOUND*/
@


1.1.2.5
log
@Jack input as well, and scrambling in compile-time option
@
text
@a20 5
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

d36 1
a36 1
extern sbuf origsound,insound,mysound;
d43 1
a50 4
     #ifndef NOPRINT
     printf("entering %s\n",__func__);
     #endif

d60 2
d72 4
a75 1
  }else if (sfinfo->channels >2 || sfinfo->channels < 1){
d79 1
a79 1
        memcpy(origsound.buffer,tempbuffer,44100*2*sizeof(float));
a87 1

d89 3
a91 1
     /* replace sample in the sample table */
d93 1
d101 1
a101 12
  free (sfinfo);
  return;
}/*end of replace*/

/* */
/* replace the sample with fixed amount of current input stream */
/* */
void replace_inp(samples * sampletable,int whichrepl) {
     #ifdef INPUT
        #ifndef NOPRINT
        printf("entering %s\n",__func__);
        #endif
d103 2
a104 52
     #endif
     return;
}/* end of replace_inp */

/* */
/* replace the sample with fixed amount of current output buffer */
/* */
void replace_buf_rnd(samples * sampletable,int whichrepl) {

     float * tempbuffer = (float * )calloc(44100*2,sizeof(float));
     unsigned int nbufsamp,nneed,lastpos,startpos,ncopy;
     unsigned short int randuint;
     float frac;
        #ifndef NOPRINT
        printf("entering %s\n",__func__);
        #endif
     nbufsamp = mysound.binfo->frames * mysound.binfo->channels;
     nneed = 44100*mysound.binfo->channels;
     if (nbufsamp <= nneed){
		  /* less that required amount in buffer - use it all */
		  memcpy(tempbuffer,mysound.buffer,nbufsamp*sizeof(float));
		  ncopy=mysound.binfo->frames;
 	 }else{
 	    /* more than required amount, find a random starting point */
        int fd;
        fd = open ("/dev/random", O_RDONLY);
        read (fd, &(randuint), sizeof (unsigned short int));
        #ifndef NOPRINT
        fprintf (stderr, "Kernel random uint %hi\n", randuint);
        #endif
        close (fd);

 	    lastpos = nbufsamp - nneed;

 	    frac = ((float)(randuint))/((float)(65536));
 	    startpos =  ((unsigned int)(frac * lastpos));
        #ifndef NOPRINT
  	    printf("frac %g\n", frac);
        printf ( "lastpos %u\n", lastpos);
        printf ( "random position %u\n", startpos);
        #endif
        memcpy(tempbuffer,mysound.buffer+startpos,nneed * sizeof(float));
        ncopy=44100;
      }
      /* copy whole buffer including trailing silence if any */
      memcpy(origsound.buffer,tempbuffer,44100*2*sizeof(float));
      origsound.binfo->frames=ncopy;
      sampletable->replace_sample(&origsound,"buffer",whichrepl);

      free(tempbuffer);
      return;
}/* end of replace_buf_rnd */
@


1.1.2.6
log
@Jack or portaudio version with compile-time selection
Autoscramblizer option , input option
@
text
@d140 1
a140 1
        fd = open ("/dev/urandom", O_RDONLY);
@


