From 473477aa65b896a8be64d833b8dcbc0266e84f5b Mon Sep 17 00:00:00 2001
From: Arnaud Blanchard <arnaud.blanchard@ensea.fr>
Date: Mon, 15 May 2017 17:43:16 +0200
Subject: [PATCH] Add option for samplerate

---
 i_sndfile/i_sndfile.cpp |  8 ++++++--
 o_sndfile/o_sndfile.cpp | 10 +++++++---
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/i_sndfile/i_sndfile.cpp b/i_sndfile/i_sndfile.cpp
index d633213..59e519c 100644
--- a/i_sndfile/i_sndfile.cpp
+++ b/i_sndfile/i_sndfile.cpp
@@ -8,8 +8,8 @@
 
 int main(int argc, char**argv){
     blc_channel output;
-    char const *filename, *output_name, *type_str, *length_str, *period_str;
-    int period;
+    char const *filename, *output_name, *type_str, *length_str, *period_str, *samplerate_str;
+    int period, samplerate;
     size_t length;
     uint32_t type;
     SF_INFO sfinfo;
@@ -21,9 +21,12 @@ int main(int argc, char**argv){
     blc_program_add_option(&output_name, 'o', "output", "string", "Channel containing the fast fourier transformation", DEFAULT_OUTPUT_NAME);
     blc_program_add_option(&period_str, 'p', "period", "integer", "Period between each sample in ms (0 as fast as possible)", "0");
     blc_program_add_option(&length_str, 's', "size", "integer", "Sample size", "1024");
+    blc_program_add_option(&samplerate_str, 'S', "samplerate", "integer", "frequency of sampling", "44100");
     blc_program_add_option(&type_str, 't', "type", "INT8|FL32", "Type of the data.", "FL32");
     blc_program_init(&argc, &argv, NULL);
     
+    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>");
@@ -40,6 +43,7 @@ int main(int argc, char**argv){
     
     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));
     
     blc_loop_try_add_waiting_semaphore(output.sem_ack_data);
diff --git a/o_sndfile/o_sndfile.cpp b/o_sndfile/o_sndfile.cpp
index cc766c4..dcdfa74 100644
--- a/o_sndfile/o_sndfile.cpp
+++ b/o_sndfile/o_sndfile.cpp
@@ -6,16 +6,20 @@
 
 int main(int argc, char**argv){
     blc_channel input;
-    char const *filename, *input_name;
+    char const *filename, *input_name, *samplerate_str;
     SF_INFO sfinfo;
     SNDFILE *sndfile;
     sf_count_t write_items_nb;
+    int samplerate;
     
     blc_program_set_description("Load a sound file (no mp3) and put the result in the blc_channel");
     blc_program_add_option(&filename, 'f', "file", "string", "filename of the file to write", NULL);
+    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", NULL);
     blc_program_init(&argc, &argv, NULL);
-        
+    
+    SSCANF(1, samplerate_str, "%d", &samplerate);
+
     if (filename==NULL) {
         blc_program_args_display_help();
         color_eprintf(BLC_RED, "You need to set a filename with option --file=\n");
@@ -29,7 +33,7 @@ int main(int argc, char**argv){
     CLEAR(sfinfo);
     sfinfo.format=SF_FORMAT_WAV | SF_FORMAT_FLOAT;
     sfinfo.channels=1;
-    sfinfo.samplerate=44100;
+    sfinfo.samplerate=samplerate;
     
     sndfile=sf_open(filename, SFM_WRITE, &sfinfo);
     if (sndfile==NULL) EXIT_ON_ERROR("Opening '%s'. libsndfile: '%s'", filename, sf_strerror(NULL));
-- 
GitLab