diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fbc264ae76d6f746445711582b1635136225a63..8b67b722c782723bf2c4b07d138edb189521e03d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,7 @@ cmake_minimum_required(VERSION 2.6) -project(i_load_sound) +project(sndfile) -find_path(SNDFILE_INCLUDE_DIR sndfile.h) -find_library(SNDFILE_LIBRARIES NAMES sndfile) +subdirs(i_sndfile) +subdirs(o_sndfile) -find_package(blc_channel REQUIRED) -find_package(blc_program REQUIRED) - -add_definitions(${BL_DEFINITIONS}) -include_directories(${BL_INCLUDE_DIRS} ${FFTW_INCLUDE_DIR}) -add_executable(i_load_sound i_load_sound.cpp) -target_link_libraries(i_load_sound ${BL_LIBRARIES} ${SNDFILE_LIBRARIES}) diff --git a/README.md b/README.md index 37fb923b5c9bfc3a0e11b10c094ca61a191a5c74..09383e8232dba9a0853071fc9885ee9c3097eb9f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ +Install +======= -Need to install libsndfile: +Need libsndfile: OSX --- @@ -10,4 +12,6 @@ OSX Ubuntu ------ - sudo apt-get install libsndfile \ No newline at end of file + sudo apt-get install libsndfile + + \ No newline at end of file diff --git a/i_sndfile/CMakeLists.txt b/i_sndfile/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..32eaebee61b5fb7b0dc4c0abd13d33cf1499b0ea --- /dev/null +++ b/i_sndfile/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.6) +project(i_sndfile) + + +find_path(SNDFILE_INCLUDE_DIR sndfile.h) +find_library(SNDFILE_LIBRARIES NAMES sndfile) + +find_package(blc_channel REQUIRED) +find_package(blc_program REQUIRED) + +add_definitions(${BL_DEFINITIONS}) +include_directories(${BL_INCLUDE_DIRS} ${SNDFILE_INCLUDE_DIR}) +add_executable(i_sndfile i_sndfile.cpp) +target_link_libraries(i_sndfile ${BL_LIBRARIES} ${SNDFILE_LIBRARIES}) diff --git a/i_load_sound.cpp b/i_sndfile/i_sndfile.cpp similarity index 83% rename from i_load_sound.cpp rename to i_sndfile/i_sndfile.cpp index 095714346f345b3ea16abdc0454eb5bfe3655dcc..9f7f1633785f9cc4b8209c94ab91118aaaab308d 100644 --- a/i_load_sound.cpp +++ b/i_sndfile/i_sndfile.cpp @@ -4,7 +4,7 @@ #include "blc_program.h" #include <unistd.h> //getpid -#define DEFAULT_OUTPUT_NAME "/sound<pid>" +#define DEFAULT_OUTPUT_NAME ":sound<pid>" //':' int main(int argc, char**argv){ blc_channel output; @@ -27,7 +27,7 @@ int main(int argc, char**argv){ 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); - if (strcmp(DEFAULT_OUTPUT_NAME, output_name)==0) asprintf((char**)&output_name, "/sound%d", getpid()); + 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(); @@ -36,14 +36,17 @@ int main(int argc, char**argv){ sndfile=sf_open(filename, SFM_READ, &sfinfo); if (sndfile==NULL) EXIT_ON_ERROR("Opening '%s'. libsndfile: '%s'", filename, 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){ read_items_nb=sf_read_float(sndfile, output.floats, output.total_length); if (read_items_nb!=output.total_length) { - PRINT_WARNING("Missing data. Read '%d' items instead of '%d'", read_items_nb, output.total_length); + PRINT_WARNING("Iteration %d: Missing data. Read '%d' items instead of '%d'", blc_loop_iteration, read_items_nb, output.total_length); break; } } sf_close(sndfile); return EXIT_SUCCESS; -} \ No newline at end of file +} diff --git a/o_sndfile/CMakeLists.txt b/o_sndfile/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..53898d2f3fc7c98ae2d8ce36d30f058323ec2661 --- /dev/null +++ b/o_sndfile/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.6) +project(o_sndfile) + + +find_path(SNDFILE_INCLUDE_DIR sndfile.h) +find_library(SNDFILE_LIBRARIES NAMES sndfile) + +find_package(blc_channel REQUIRED) +find_package(blc_program REQUIRED) + +add_definitions(${BL_DEFINITIONS}) +include_directories(${BL_INCLUDE_DIRS} ${SNDFILE_INCLUDE_DIR}) +add_executable(o_sndfile o_sndfile.cpp) +target_link_libraries(o_sndfile ${BL_LIBRARIES} ${SNDFILE_LIBRARIES}) diff --git a/o_sndfile/o_sndfile.cpp b/o_sndfile/o_sndfile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..417fe2b17e70f55a5a95e6aaab87fad5d0af5cc5 --- /dev/null +++ b/o_sndfile/o_sndfile.cpp @@ -0,0 +1,48 @@ +#include <sndfile.h> + +#include "blc_channel.h" +#include "blc_program.h" +#include <unistd.h> //getpid + +int main(int argc, char**argv){ + blc_channel input; + char const *filename, *input_name; + SF_INFO sfinfo; + SNDFILE *sndfile; + sf_count_t write_items_nb; + + blc_program_add_option(&filename, 'f', "file", "string", "filename of the file to write", NULL); + blc_program_add_parameter(&input_name, "blc_channel-in", 1, "blc channel containing the sound", NULL); + blc_program_init(&argc, &argv, NULL); + + if (filename==NULL) { + blc_program_option_display_help(); + color_eprintf(BLC_RED, "You need to set a filename with option --file=\n"); + exit(1); + } + + if (strcmp(blc_get_filename_extension(filename), "wav")!=0) EXIT_ON_ERROR("Only '.wav' extention is managed but your extension is '.%s'", blc_get_filename_extension(filename)); + + input.open(input_name, BLC_CHANNEL_READ); + input.publish(); + + CLEAR(sfinfo); + + sfinfo.format=SF_FORMAT_WAV | SF_FORMAT_FLOAT; + sfinfo.channels=1; + sfinfo.samplerate=44100; + + 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){ + 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; +}