You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.3 KiB
114 lines
3.3 KiB
#include "audio.h" |
|
|
|
|
|
/* procura o bin flac no sistema */ |
|
int bin_flac( char **flac ){ |
|
|
|
*flac = calloc( 1, 128 ); |
|
|
|
FILE *FileOpen; |
|
FileOpen = popen("/usr/bin/which flac", "r"); |
|
fgets( *flac, SIZE_PATH_FLAC, FileOpen); |
|
if( **flac == '\0' ){ |
|
AGI_NORESPONSE("VERBOSE \"Programa FLAC não está instalado para converter arquivos\" \n"); |
|
return -1; |
|
} |
|
|
|
(*flac)[ strlen( *flac ) - 1 ] = '\0'; |
|
|
|
pclose(FileOpen); |
|
|
|
return 0; |
|
} |
|
|
|
|
|
/* Converter o audio sln -> flac */ |
|
int covert_flac( char *flac, char *name_audio ){ |
|
AGI_NORESPONSE("VERBOSE \"iniciar a conversão do áudio com flac\" \n"); |
|
|
|
char str_convert[SIZE_PATH_FLAC + SIZE_NAME_AUDIO + 256]; |
|
|
|
snprintf( str_convert, sizeof str_convert, "%s -8 --totally-silent --channels=1 --endian=little --sign=signed --bps=16 --force-raw-format --sample-rate=8000 %s.sln", flac, name_audio ); |
|
|
|
if( system(str_convert) != 0 ){ |
|
AGI_NORESPONSE( "VERBOSE \"Não foi possível converter o áudio com flac. Programa terminando\"\n"); |
|
exit(-1); |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
struct b_audio * get_audio_bytes( char *name_audio ){ |
|
|
|
char command_base64[ (2 * SIZE_NAME_AUDIO) + 164 ]; |
|
/* converter flac em base64 */ |
|
snprintf(command_base64, sizeof(command_base64), "base64 %s.flac -w 0 > %s.base64", name_audio, name_audio); |
|
|
|
/* verfica se tem erros na conversão */ |
|
if(system( command_base64 ) != 0){ |
|
AGI_NORESPONSE("VERBOSE \"não foi possível converter para base64. Programa terminado\"\n"); |
|
exit(-1); |
|
} |
|
|
|
FILE *fd_base64; |
|
char full_path_name_base64[ SIZE_NAME_AUDIO + 16 ]; |
|
snprintf(full_path_name_base64, 256, "%s.base64", name_audio); |
|
fd_base64 = fopen( full_path_name_base64, "r"); |
|
if(fd_base64 == NULL){ |
|
AGI_NORESPONSE("VERBOSE \"Não pode abrir o arquivo %s.base64\"\n", name_audio); |
|
exit(-1); |
|
} |
|
|
|
fseek(fd_base64, 0, SEEK_END); |
|
long fsize = ftell( fd_base64 ); |
|
fseek(fd_base64, 0, SEEK_SET); /* mesmo coo rewind(f); */ |
|
|
|
char *buffer_base64 = malloc(fsize + 1); |
|
fread( buffer_base64, fsize, 1, fd_base64 ); |
|
fclose( fd_base64 ); |
|
|
|
buffer_base64[fsize] = 0; |
|
struct b_audio *b_flac = calloc(1, sizeof( struct b_audio ) ); |
|
b_flac->bytes = NULL; |
|
b_flac->nmemb = 0; |
|
b_flac->bytes_encode = buffer_base64; |
|
b_flac->nmemb_encode = fsize + 1; |
|
|
|
return b_flac; |
|
} |
|
|
|
|
|
char *init_record( s_agi_parameter *param_agi ){ |
|
s_agi_return *response = NULL; |
|
agi_clear_response( response ); |
|
|
|
char * name_audio = calloc( 1, SIZE_NAME_AUDIO ); |
|
snprintf( name_audio, SIZE_NAME_AUDIO, "/tmp/speech_%s", get_param_value(param_agi, "agi_uniqueid")); |
|
|
|
/* Caso Esteja com BEEP ou beep será colocado o beep |
|
* Qualquer outro valor mandará vazio para não tocar beep |
|
*/ |
|
char *beep = get_param_n( param_agi, ARG_BEEP ); |
|
if( !beep || strcasecmp( beep, "BEEP" ) != 0 ){ |
|
beep = "NOBEEP"; |
|
} |
|
|
|
/* Enviar comando para gravar áudio */ |
|
response = agi_command( "RECORD FILE \"%s\" sln \"%s\" \"%d\" %s \"s=%d\" \n", |
|
name_audio, |
|
get_param_n( param_agi, ARG_INTKEY ), |
|
atoi(get_param_n( param_agi, ARG_ABS_TIMEOUT )), |
|
beep, |
|
atoi(get_param_n( param_agi, ARG_TIMEOUT )) |
|
); |
|
|
|
if( response->code == 200 && response->result != -1 ){ |
|
agi_clear_response( response ); |
|
} |
|
else { |
|
AGI_NORESPONSE( "VERBOSE \"ERRO record File. Programa terminado\"\n" ); |
|
exit(-1); |
|
} |
|
|
|
return name_audio; |
|
}
|
|
|