wavcut.cpp 3.77 KB
#include "wavcut.h"

#include <vector>
// #include <iostream>


int sfreq = 22050;		///< Temporal storage of sample rate
int speechlen;		///< samples of one recorded segments
FILE *fp = NULL;		///< File pointer for WAV output
int sid = 0;		///< current file ID (for SPOUT_FILE)
char *outpath = NULL;	///< work space for output file name formatting
boolean writing_file = FALSE; ///< TRUE if writing to a file
int trigger_sample;
char *pathAudio;
char* outputPath;
char* id_file;
static vector<Audiofile> audioList;


Wavcut::Wavcut(char* _pathAudio, char* _outputPath,  char* _id){

	pathAudio = _pathAudio;
	outputPath = _outputPath;
	id_file = _id;
	//printf("%s\n","Entrou aquiiiii" )
}

Wavcut::~Wavcut(){
	//printf("%s\n","Wavcut finalizado" );
}


int Wavcut::adin_callback_file(SP16 *now, int len, Recog *recog)
 {
	  /* cria novo arquivo wav para salvar o audio sem silencio*/
	if (speechlen == 0) {
	  
	     sprintf(outpath, "%s%d.wav",outputPath, sid);
	
		 if (access(outpath, F_OK) == 0) {
		 	 if (access(outpath, W_OK) != 0) {
		   		return(-1);
		 	} 
       
        }

	  	if ((fp = wrwav_open(outpath, sfreq)) != NULL) {
	  	 	//fprintf(stderr, "novo arquivo\n");
	    }else{
	   		return -1;
	 	}
		writing_file = TRUE;
	}
 
  /* write recorded sample to file */
  if (wrwav_data(fp, &(now[0]), len) == FALSE) {
    return -1;
  }

  /* accumulate sample num of this segment */
speechlen += len;

return(0);
}

//acumula o tempo de cada segmento
void registra_tempo(Recog *recog, void *data)
{
  trigger_sample = recog->adin->last_trigger_sample;
}

boolean close_files()
{
  if (writing_file) {
 
    if (wrwav_close(fp) == FALSE) {
     fprintf(stderr, "adinrec: failed to close file\n");
     return FALSE;
   }
   char* fileout=(char *)mymalloc(100);;
  // sprintf(fileout,*outpath);
   sprintf(fileout, outpath);
  audioList.push_back(Audiofile(fileout,(float)trigger_sample / (float)sfreq,
    (float)(trigger_sample + speechlen) / (float)sfreq));
  
 writing_file = FALSE;
}

return TRUE;
}  


 int Wavcut::initialize(Jconf *jconf) {
	
  sid = 0;
  audioList.clear();
  //Jconf *jconf;
  Recog *recog;
  
  int ret;
  boolean is_continues;

  /* cria instancia do reconhecedor */
  recog = j_recog_new();
  /* carrega as configurações contidas no jconfig */
 // jconf = j_config_load_file_new("/home/ezequiel/speech-recognizer/wavcut.jconf");
  
	jconf->input.sfreq = sfreq;
  /*adciona a configuração ao reconhecedor*/
  recog->jconf = jconf;

  outpath = (char *)mymalloc(256);

/*registra calback do contador de tempo*/
  callback_add(recog, CALLBACK_EVENT_SPEECH_START, registra_tempo, NULL);

  	/*Inicializa o reconhecedor*/
	if (j_adin_init(recog) == FALSE) {
  		fprintf(stderr, "Error in initializing adin device\n");
  	return 0;
	} 

	/*Abre o quivo de áudio para ser cortado*/
  	if(j_open_stream(recog,pathAudio) == -2)
   		return sid;
    
    /* loop de detecção de voz*/

  do {
  
   speechlen = 0;

   ret = adin_go(adin_callback_file, NULL, recog);

  
   switch(ret) {
      case -1:		     /* device read error or callback error */
     //fprintf(stderr, "[error]\n");
     break;
      case 0:			/* reached to end of input */
     //fprintf(stderr, "[eof]\n");
     return sid;
     break;
      default:	
     break;
   }

   if (ret == -1) {
	/* error in input device or callback function, so terminate program here */
     return sid;
   }
      /* um intervalo de silencio detectado */
   
     if (close_files() == FALSE) 
      return sid;
      
      /* incremento do contador de partes cortadas */
    
    
	 sid++;

	is_continues = FALSE;
	if (ret > 0 || ret == -2) {
		is_continues = TRUE;
	}

   } while (is_continues); 

  /*Quando termina de ler todo áudio finaliza*/
  adin_end(recog->adin);

  return sid;

}

vector<Audiofile> Wavcut::list_audio_files(){


  return audioList;
}