Search code examples
clinuxaudiosoxlibsox

How to change tempo of sound file using libsox in C?


I developed C code to change tempo(increase /deccrese) of sound file(3 to 10 seconds ogg files) using libsox library on Ubuntuusing following sample code.With tempo(value > 1) I am getting output file (with truncted voice) however for tempo (value < 1) I am getting proper output file(with all voice sample).

#include "sox.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/*
* Reads input file, applies effects, stores in output file.  
*executable file:example, input fie:audio.ogg ,output file:tempo.ogg 
E.g. ./example audio.ogg tempo.ogg
*/
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);
free(e);

 /* Create the `tempo' effect, and initialise it with the desired parameters: */
e = sox_create_effect(sox_find_effect("tempo"));
args[0] = "1.5", 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);
free(e);
/* 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);
free(e);

/* 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;
}

Here I used tempo speed=1.5 and I tried for all different values greater than one.Can anyone tell me what changes I have to make to get a proper increased tempo sound file?


Solution

  • This looks similar to src/example0.c. I think there is a bug in this sample in the way that it passes &in->signal as both parameters to each call to sox_add_effect().

    I believe this overwrites the in configuration with the effect's output configuration.

    Look at src/example6.c and how it uses the variable interim_signal as the input and &out->signal as the output.

    Roughly:

    sox_signalinfo_t interm_signal; /* @ intermediate points in the chain. */
    ...
    
    interm_signal = in->signal; /* NB: deep copy */
    ...
    
    /* 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, &interm_signal, &out->signal) == SOX_SUCCESS);
    free(e);