wavcut.cpp
3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#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;
}