Commit 442fbd30fd7f34db9c16e04ebcde734b64483099

Authored by Filipe Gama
1 parent 7c46ef5a

Added seek(begin) pointer to read sound files

... ... @@ -8,15 +8,16 @@
int main(int argc, char**argv){
blc_channel output;
char const *filename, *output_name, *type_str, *length_str, *period_str, *samplerate_str, *iterations_nb_str;
int period, samplerate, iterations_nb;
char const *filename, *iterations_nb_str, *length_str, *output_name, *period_str, *samplerate_str, *seek_pos_str, *type_str;
int iterations_nb, period, samplerate;
size_t length;
uint32_t type;
SF_INFO sfinfo;
SNDFILE *sndfile;
sf_count_t read_items_nb;
sf_count_t read_items_nb, seek_pos;
blc_program_set_description("Record blc_channel input in a sound file");
blc_program_add_option(&seek_pos_str, 'b', "begin", "integer", "Sample to begin at in the file (i.e. sample at which we should begin reading the file)", "0");
blc_program_add_option(&filename, 'f', "file", "string", "filename of the file to write", NULL);
blc_program_add_option(&iterations_nb_str, 'n', "number", "integer", "number of samples to acquire (-1 for infinity)", "-1");
blc_program_add_option(&output_name, 'o', "output", "string", "Channel where to put the sound", DEFAULT_OUTPUT_NAME);
... ... @@ -26,34 +27,39 @@ int main(int argc, char**argv){
blc_program_add_option(&type_str, 't', "type", "INT8|FL32", "Type of the data.", "FL32");
blc_program_init(&argc, &argv, blc_quit);
blc_command_forward_blc_channels();
SSCANF(1, samplerate_str, "%d", &samplerate);
if (filename==NULL){
blc_program_args_display_help();
EXIT_ON_ERROR("You need to specify a filename: -f<filename>");
blc_program_args_display_help();
EXIT_ON_ERROR("You need to specify a filename: -f<filename>");
}
type=STRING_TO_UINT32(type_str);
if (sscanf(length_str, "%ld", &length)!=1) EXIT_ON_ERROR("Reading size '%s'", length_str);
if (sscanf(period_str, "%d", &period)!=1) EXIT_ON_ERROR("Reading period in '%s'", period_str);
iterations_nb=strtol(iterations_nb_str, NULL, 10);
seek_pos=strtol(seek_pos_str, NULL, 10);
if (strcmp(DEFAULT_OUTPUT_NAME, output_name)==0) asprintf((char**)&output_name, ":sound%d", getpid());
output.create_or_open(output_name, BLC_CHANNEL_WRITE, type, 'LPCM', 1, length);
output.publish();
CLEAR(sfinfo);
sndfile=sf_open(filename, SFM_READ, &sfinfo);
// if (sfinfo.format!=samplerate) EXIT_ON_ERROR("The file has been recorded with a samplerate of '%d' and you request '%d'", sfinfo.samplerate, samplerate);
if (sndfile==NULL) EXIT_ON_ERROR("Opening '%s'. libsndfile: '%s'", filename, sf_strerror(NULL));
if (sf_seek(sndfile, length*seek_pos, SEEK_CUR) == -1) EXIT_ON_ERROR("Seeking position %i before start or after end of file. libsndfile: '%s'", length*seek_pos, sf_strerror(NULL));
blc_loop_try_add_waiting_semaphore(output.sem_ack_data);
blc_loop_try_add_posting_semaphore(output.sem_new_data);
BLC_COMMAND_LOOP(period*1000){
if (iterations_nb==blc_loop_iteration) blc_command_ask_quit();
if (iterations_nb==blc_loop_iteration) blc_command_ask_quit();
read_items_nb=sf_read_float(sndfile, output.floats, output.total_length);
if (read_items_nb!=output.total_length) {
color_eprintf(BLC_YELLOW, "%s: Iteration %llu: Missing data. Read '%lld' items instead of '%ld'. It is probably the end of the file\n", blc_program_name, blc_loop_iteration, read_items_nb, output.total_length);
... ... @@ -61,7 +67,7 @@ int main(int argc, char**argv){
break;
}
}
sf_close(sndfile);
return EXIT_SUCCESS;
}
... ...
... ... @@ -6,20 +6,21 @@
int main(int argc, char**argv){
blc_channel input;
char const *filename, *input_name, *samplerate_str, *output_length_str;
char const *filename, *input_name, *samplerate_str, *output_length_str, *iterations_nb_str;
SF_INFO sfinfo;
SNDFILE *sndfile;
sf_count_t write_items_nb;
int samplerate, length;
int samplerate, length, iterations_nb;
blc_program_set_description("Save the data from blc_channel in a sound file (wav, ogg, ...)");
blc_program_add_option(&filename, 'f', "file", "filename", "filename of the file to write", NULL);
blc_program_add_option(&iterations_nb_str, 'n', "number", "integer", "number of samples to acquire (-1 for infinity)", "-1");
blc_program_add_option(&output_length_str, 's', "size", "integer", "items nb in buffer", "4096");
blc_program_add_option(&samplerate_str, 'S', "samplerate", "integer", "frequency of sampling", "44100");
blc_program_add_parameter(&input_name, "blc_channel-in", 1, "blc channel containing the sound to record", NULL);
blc_program_init(&argc, &argv, blc_quit);
blc_command_forward_blc_channels();
SSCANF(1, samplerate_str, "%d", &samplerate);
SSCANF(1, output_length_str, "%d", &length);
... ... @@ -28,25 +29,28 @@ int main(int argc, char**argv){
color_eprintf(BLC_RED, "You need to set a filename with option --file=\n");
exit(1);
}
iterations_nb=strtol(iterations_nb_str, NULL, 10);
input.open(input_name, BLC_CHANNEL_READ);
CLEAR(sfinfo);
sfinfo.format=SF_FORMAT_WAV | SF_FORMAT_FLOAT;
sfinfo.channels=1;
sfinfo.samplerate=samplerate;
sndfile=sf_open(filename, SFM_WRITE, &sfinfo);
if (sndfile==NULL) EXIT_ON_ERROR("Opening '%s'. libsndfile: '%s'", filename, sf_strerror(NULL));
blc_loop_try_add_waiting_semaphore(input.sem_new_data);
blc_loop_try_add_posting_semaphore(input.sem_ack_data);
BLC_COMMAND_LOOP(0){
if (iterations_nb==blc_loop_iteration) blc_command_ask_quit();
write_items_nb=sf_write_float(sndfile, input.floats, input.total_length);
if (write_items_nb!=input.total_length) EXIT_ON_ERROR("Iteration %d: Missing data. Read '%d' items instead of '%d'", blc_loop_iteration, write_items_nb, input.total_length);
}
sf_close(sndfile);
return EXIT_SUCCESS;
}
... ...