Skip to content

Instantly share code, notes, and snippets.

@EgorLakomkin
Last active August 19, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorLakomkin/124a4b86dd8869d5536e2edceec0cc84 to your computer and use it in GitHub Desktop.
Save EgorLakomkin/124a4b86dd8869d5536e2edceec0cc84 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sox.h>
#include <assert.h>
#include <cstring>
#include <vector>
using namespace std;
int main() {
sox_init();
char* buffer;
size_t buffer_size;
sox_format_t* input = sox_open_read("/home/egor/hello.wav", NULL, NULL, NULL);
//sox_format_t* out;
sox_format_t* output = sox_open_memstream_write(&buffer, &buffer_size,
&input->signal, &input->encoding, "raw", NULL);
//assert(output = sox_open_write("/home/egor/hello_processed.wav", &input->signal, NULL, NULL, NULL, NULL));
sox_effects_chain_t* chain = sox_create_effects_chain(&input->encoding, &output->encoding);
char* sox_args[10];
//input effect
sox_effect_t* e = sox_create_effect(sox_find_effect("input"));
sox_args[0] = (char*)input;
assert(sox_effect_options(e, 1, sox_args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &input->signal, &input->signal) ==
SOX_SUCCESS);
free(e);
e = sox_create_effect(sox_find_effect("tempo"));
std::string tempo_str = "1.01";
sox_args[0] = (char*)tempo_str.c_str();
assert(sox_effect_options(e, 1, sox_args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &input->signal,&input->signal) ==
SOX_SUCCESS);
free(e);
e = sox_create_effect(sox_find_effect("output"));
sox_args[0] = (char*)output;
assert(sox_effect_options(e, 1, sox_args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &input->signal, &input->signal) ==
SOX_SUCCESS);
free(e);
sox_flow_effects(chain, NULL, NULL);
static const size_t maxSamples=4096;
sox_sample_t samples[maxSamples];
std::vector<sox_sample_t> audio_buffer;
for (size_t r; 0 != (r=sox_read(output,samples,maxSamples));)
for(int i=0;i<r ;i++)
audio_buffer.push_back(samples[i]);
std::cout << audio_buffer.size() << std::endl;
sox_delete_effects_chain(chain);
sox_close(input);
sox_close(output);
free(buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment