bob::io::audio::Writer::Writer(const char* filename, double rate, sox_encoding_t encoding, size_t bits_per_sample): m_filename(filename), m_opened(false) { sox_signalinfo_t siginfo; siginfo.rate = rate; siginfo.precision = bits_per_sample; siginfo.channels = SOX_UNSPEC; #ifdef SOX_UNKNOWN_LEN siginfo.length = SOX_UNKNOWN_LEN; #else siginfo.length = -1; #endif const char* extension = lsx_find_file_extension(filename); if (bob::io::audio::SUPPORTED_FORMATS.find(extension-1) == bob::io::audio::SUPPORTED_FORMATS.end()) { //unsupported file format boost::format m("file `%s' cannot be written by SoX (file format `%d' is unsupported -- use `sox --help-format all' for a list of all supported formats"); m % filename % extension; throw std::runtime_error(m.str()); } sox_format_t* f = 0; if (encoding == SOX_ENCODING_UNKNOWN) { f = sox_open_write(filename, &siginfo, 0, extension, 0, 0); } else { sox_encodinginfo_t encoding_info; encoding_info.encoding = encoding; encoding_info.bits_per_sample = bits_per_sample; encoding_info.compression = HUGE_VAL; f = sox_open_write(filename, &siginfo, &encoding_info, 0, 0, 0); } if (!f) { boost::format m("file `%s' is not writeable by SoX (internal call to `sox_open_write()' failed) -- we suggest you check writing permissions and existence of leading paths"); m % filename; throw std::runtime_error(m.str()); } m_file = boost::shared_ptr<sox_format_t>(f, std::ptr_fun(bob::io::audio::close_sox_file)); m_typeinfo.dtype = bob::io::base::array::t_float64; m_typeinfo.nd = 2; m_typeinfo.shape[0] = 0; m_typeinfo.shape[1] = 0; m_typeinfo.update_strides(); m_buffer = boost::shared_array<sox_sample_t>(new sox_sample_t[m_typeinfo.shape[0]]); m_opened = true; ///< file is now considered opened for business }
int main(int argc, char** argv) { if (argc != 1) { fprintf(stderr, "usage: %s < input_file\n", argv[0]); exit(1); } const int in_channels = 2, in_samples = 512, sample_rate = 44100; if (sox_init() != SOX_SUCCESS) { oops("sox_init()"); } sox_signalinfo_t out_si = {}; out_si.rate = sample_rate; out_si.channels = in_channels; out_si.precision = SOX_SAMPLE_PRECISION; sox_format_t* output = sox_open_write("default", &out_si, NULL, "alsa", NULL, NULL); if (!output) { oops("sox_open_read()"); } sox_sample_t samples[in_samples * in_channels]; float input[in_samples * in_channels]; size_t clips = 0; SOX_SAMPLE_LOCALS; for (;;) { ssize_t sz = read(STDIN_FILENO, input, sizeof(input)); if (sz < 0) { oops("read(stdin)"); } if (sz == 0) { break; } const size_t n_samples = sz / sizeof(float); for (size_t n = 0; n < n_samples; n++) { samples[n] = SOX_FLOAT_32BIT_TO_SAMPLE(input[n], clips); } if (sox_write(output, samples, n_samples) != n_samples) { oops("sox_write()"); } } if (sox_close(output) != SOX_SUCCESS) { oops("sox_close()"); } if (sox_quit() != SOX_SUCCESS) { oops("sox_quit()"); } return 0; }
/* * On an alsa capable system, plays an audio file starting 10 seconds in. * Copes with sample-rate and channel change if necessary since its * common for audio drivers to support a subset of rates and channel * counts. * E.g. example3 song2.ogg * * Can easily be changed to work with other audio device drivers supported * by libSoX; e.g. "oss", "ao", "coreaudio", etc. * See the soxformat(7) manual page. */ int main(int argc, char * argv[]) { static sox_format_t * in, * out; /* input and output files */ sox_effects_chain_t * chain; sox_effect_t * e; sox_signalinfo_t interm_signal; char * args[10]; assert(argc == 2); sox_globals.output_message_handler = output_message; sox_globals.verbosity = 1; assert(sox_init() == SOX_SUCCESS); assert(in = sox_open_read(argv[1], NULL, NULL, NULL)); /* Change "alsa" in this line to use an alternative audio device driver: */ assert(out= sox_open_write("default", &in->signal, NULL, "alsa", NULL, NULL)); chain = sox_create_effects_chain(&in->encoding, &out->encoding); interm_signal = in->signal; /* NB: deep copy */ e = sox_create_effect(sox_find_effect("input")); args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS); assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS); free(e); e = sox_create_effect(sox_find_effect("trim")); args[0] = "10", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS); assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS); free(e); if (in->signal.rate != out->signal.rate) { e = sox_create_effect(sox_find_effect("rate")); assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS); assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS); free(e); } if (in->signal.channels != out->signal.channels) { e = sox_create_effect(sox_find_effect("channels")); assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS); assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS); free(e); } e = sox_create_effect(sox_find_effect("output")); args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS); assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS); free(e); sox_flow_effects(chain, NULL, NULL); sox_delete_effects_chain(chain); sox_close(out); sox_close(in); sox_quit(); return 0; }
/* * Reads input file, applies vol & flanger effects, stores in output file. * E.g. example1 monkey.au monkey.aiff */ int main(int argc, char * argv[]){ static sox_format_t * in, * out; /* input and output files */ sox_effects_chain_t * chain; sox_effect_t * e; char * args[10]; assert(argc == 3); /* All libSoX applications must start by initialising the SoX library */ assert(sox_init() == SOX_SUCCESS); /* Open the input file (with default parameters) */ assert(in = sox_open_read(argv[1], NULL, NULL, NULL)); /* Open the output file; we must specify the output signal characteristics. * Since we are using only simple effects, they are the same as the input * file characteristics */ assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL)); /* Create an effects chain; some effects need to know about the input * or output file encoding so we provide that information here */ chain = sox_create_effects_chain(&in->encoding, &out->encoding); /* The first effect in the effect chain must be something that can source * samples; in this case, we use the built-in handler that inputs * data from an audio file */ e = sox_create_effect(sox_find_effect("input")); args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS); /* This becomes the first `effect' in the chain */ assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS); /* Create the `vol' effect, and initialise it with the desired parameters: */ e = sox_create_effect(sox_find_effect("vol")); args[0] = "3dB", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS); /* Add the effect to the end of the effects processing chain: */ assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS); /* Create the `flanger' effect, and initialise it with default parameters: */ e = sox_create_effect(sox_find_effect("flanger")); assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS); /* Add the effect to the end of the effects processing chain: */ assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS); /* The last effect in the effect chain must be something that only consumes * samples; in this case, we use the built-in handler that outputs * data to an audio file */ e = sox_create_effect(sox_find_effect("output")); args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS); assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS); /* Flow samples through the effects processing chain until EOF is reached */ sox_flow_effects(chain, NULL, NULL); /* All done; tidy up: */ sox_delete_effects_chain(chain); sox_close(out); sox_close(in); sox_quit(); return 0; }
int main(int argc, char* argv[]) { static sox_format_t *in_file, *out_file; sox_sample_t *buffer; size_t read; size_t sample_count; unsigned int sample_order[SONG_LENGTH_S]; if(argc != 3) { usage(); exit(EXIT_FAILURE); } if (sox_init() != SOX_SUCCESS) { fprintf(stderr, "error: could not initialize Sox\n"); exit(EXIT_FAILURE); } if((in_file = sox_open_read(argv[1], NULL, NULL, NULL)) == NULL) { fprintf(stderr, "error could not read input file\n"); exit(EXIT_FAILURE); } if((out_file = sox_open_write(argv[2], &in_file->signal, NULL, "wav", NULL, NULL)) == NULL) { fprintf(stderr, "error could not open output file\n"); exit(EXIT_FAILURE); } sample_count = SONG_LENGTH_S * in_file->signal.rate * in_file->signal.channels; buffer = (sox_sample_t *) malloc(sizeof(sox_sample_t) * sample_count); randomize_byte_order(sample_order); if (sox_read(in_file, buffer, sample_count) != sample_count) { fprintf(stderr, "Incorrect number of samples read"); } unsigned int i=0; for(i=0;i<SONG_LENGTH_S;i++) { sox_write(out_file, buffer + (sample_order[i] * (((unsigned int)(in_file->signal.rate) * in_file->signal.channels))), in_file->signal.rate * in_file->signal.channels); } free(buffer); sox_close(in_file); sox_close(out_file); sox_quit(); return 0; }