parent
ec0a76fa0b
commit
5d43e90a4d
@ -1,167 +0,0 @@ |
||||
/* Simple example of using SoX libraries
|
||||
* |
||||
* Copyright (c) 2007-8 robs@users.sourceforge.net |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the |
||||
* Free Software Foundation; either version 2 of the License, or (at your |
||||
* option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along |
||||
* with this program; if not, write to the Free Software Foundation, Inc., |
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||||
*/ |
||||
|
||||
#ifdef NDEBUG /* N.B. assert used with active statements so enable always. */ |
||||
#undef NDEBUG /* Must undef above assert.h or other that might include it. */ |
||||
#endif |
||||
|
||||
#include "sox.h" |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <assert.h> |
||||
|
||||
static sox_format_t * in, * out; /* input and output files */ |
||||
|
||||
/* The function that will be called to input samples into the effects chain.
|
||||
* In this example, we get samples to process from a SoX-openned audio file. |
||||
* In a different application, they might be generated or come from a different |
||||
* part of the application. */ |
||||
static int input_drain( |
||||
sox_effect_t * effp, sox_sample_t * obuf, size_t * osamp) |
||||
{ |
||||
(void)effp; /* This parameter is not needed in this example */ |
||||
|
||||
/* ensure that *osamp is a multiple of the number of channels. */ |
||||
*osamp -= *osamp % effp->out_signal.channels; |
||||
|
||||
/* Read up to *osamp samples into obuf; store the actual number read
|
||||
* back to *osamp */ |
||||
*osamp = sox_read(in, obuf, *osamp); |
||||
|
||||
/* sox_read may return a number that is less than was requested; only if
|
||||
* 0 samples is returned does it indicate that end-of-file has been reached |
||||
* or an error has occurred */ |
||||
if (!*osamp && in->sox_errno) |
||||
fprintf(stderr, "%s: %s\n", in->filename, in->sox_errstr); |
||||
return *osamp? SOX_SUCCESS : SOX_EOF; |
||||
} |
||||
|
||||
/* The function that will be called to output samples from the effects chain.
|
||||
* In this example, we store the samples in a SoX-opened audio file. |
||||
* In a different application, they might perhaps be analysed in some way, |
||||
* or displayed as a wave-form */ |
||||
static int output_flow(sox_effect_t *effp LSX_UNUSED, sox_sample_t const * ibuf, |
||||
sox_sample_t * obuf LSX_UNUSED, size_t * isamp, size_t * osamp) |
||||
{ |
||||
/* Write out *isamp samples */ |
||||
size_t len = sox_write(out, ibuf, *isamp); |
||||
|
||||
/* len is the number of samples that were actually written out; if this is
|
||||
* different to *isamp, then something has gone wrong--most often, it's |
||||
* out of disc space */ |
||||
if (len != *isamp) { |
||||
fprintf(stderr, "%s: %s\n", out->filename, out->sox_errstr); |
||||
return SOX_EOF; |
||||
} |
||||
|
||||
/* Outputting is the last `effect' in the effect chain so always passes
|
||||
* 0 samples on to the next effect (as there isn't one!) */ |
||||
*osamp = 0; |
||||
|
||||
(void)effp; /* This parameter is not needed in this example */ |
||||
|
||||
return SOX_SUCCESS; /* All samples output successfully */ |
||||
} |
||||
|
||||
/* A `stub' effect handler to handle inputting samples to the effects
|
||||
* chain; the only function needed for this example is `drain' */ |
||||
static sox_effect_handler_t const * input_handler(void) |
||||
{ |
||||
static sox_effect_handler_t handler = { |
||||
"input", NULL, SOX_EFF_MCHAN, NULL, NULL, NULL, input_drain, NULL, NULL, 0 |
||||
}; |
||||
return &handler; |
||||
} |
||||
|
||||
/* A `stub' effect handler to handle outputting samples from the effects
|
||||
* chain; the only function needed for this example is `flow' */ |
||||
static sox_effect_handler_t const * output_handler(void) |
||||
{ |
||||
static sox_effect_handler_t handler = { |
||||
"output", NULL, SOX_EFF_MCHAN, NULL, NULL, output_flow, NULL, NULL, NULL, 0 |
||||
}; |
||||
return &handler; |
||||
} |
||||
|
||||
/*
|
||||
* Reads input file, applies vol & flanger effects, stores in output file. |
||||
* E.g. example1 monkey.au monkey.aiff |
||||
*/ |
||||
int main(int argc, char * argv[]) |
||||
{ |
||||
sox_effects_chain_t * chain; |
||||
sox_effect_t * e; |
||||
char * vol[] = {"3dB"}; |
||||
|
||||
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 have defined an input handler that inputs |
||||
* data from an audio file */ |
||||
e = sox_create_effect(input_handler()); |
||||
/* This becomes the first `effect' in the chain */ |
||||
assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS); |
||||
free(e); |
||||
|
||||
/* Create the `vol' effect, and initialise it with the desired parameters: */ |
||||
e = sox_create_effect(sox_find_effect("vol")); |
||||
assert(sox_effect_options(e, 1, vol) == 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); |
||||
|
||||
/* 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); |
||||
free(e); |
||||
|
||||
/* The last effect in the effect chain must be something that only consumes
|
||||
* samples; in this case, we have defined an output handler that outputs |
||||
* data to an audio file */ |
||||
e = sox_create_effect(output_handler()); |
||||
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; |
||||
} |
@ -1,118 +0,0 @@ |
||||
/* Simple example of using SoX libraries
|
||||
* |
||||
* Copyright (c) 2008 robs@users.sourceforge.net |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the |
||||
* Free Software Foundation; either version 2 of the License, or (at your |
||||
* option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along |
||||
* with this program; if not, write to the Free Software Foundation, Inc., |
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||||
*/ |
||||
|
||||
#ifdef NDEBUG /* N.B. assert used with active statements so enable always. */ |
||||
#undef NDEBUG /* Must undef above assert.h or other that might include it. */ |
||||
#endif |
||||
|
||||
#include "sox.h" |
||||
#include "util.h" |
||||
#include <stdio.h> |
||||
#include <math.h> |
||||
#include <assert.h> |
||||
|
||||
/*
|
||||
* Reads input file and displays a few seconds of wave-form, starting from |
||||
* a given time through the audio. E.g. example2 song2.au 30.75 1 |
||||
*/ |
||||
int main(int argc, char * argv[]) |
||||
{ |
||||
sox_format_t * in; |
||||
sox_sample_t * buf; |
||||
size_t blocks, block_size; |
||||
/* Period of audio over which we will measure its volume in order to
|
||||
* display the wave-form: */ |
||||
static const double block_period = 0.025; /* seconds */ |
||||
double start_secs = 0, period = 2; |
||||
char dummy; |
||||
uint64_t seek; |
||||
|
||||
/* All libSoX applications must start by initialising the SoX library */ |
||||
assert(sox_init() == SOX_SUCCESS); |
||||
|
||||
assert(argc > 1); |
||||
++argv, --argc; /* Move to 1st parameter */ |
||||
|
||||
/* Open the input file (with default parameters) */ |
||||
assert((in = sox_open_read(*argv, NULL, NULL, NULL))); |
||||
++argv, --argc; /* Move past this parameter */ |
||||
|
||||
if (argc) { /* If given, read the start time: */ |
||||
assert(sscanf(*argv, "%lf%c", &start_secs, &dummy) == 1); |
||||
++argv, --argc; /* Move past this parameter */ |
||||
} |
||||
|
||||
if (argc) { /* If given, read the period of time to display: */ |
||||
assert(sscanf(*argv, "%lf%c", &period, &dummy) == 1); |
||||
++argv, --argc; /* Move past this parameter */ |
||||
} |
||||
|
||||
/* Calculate the start position in number of samples: */ |
||||
seek = start_secs * in->signal.rate * in->signal.channels + .5; |
||||
/* Make sure that this is at a `wide sample' boundary: */ |
||||
seek -= seek % in->signal.channels; |
||||
/* Move the file pointer to the desired starting position */ |
||||
assert(sox_seek(in, seek, SOX_SEEK_SET) == SOX_SUCCESS); |
||||
|
||||
/* Convert block size (in seconds) to a number of samples: */ |
||||
block_size = block_period * in->signal.rate * in->signal.channels + .5; |
||||
/* Make sure that this is at a `wide sample' boundary: */ |
||||
block_size -= block_size % in->signal.channels; |
||||
/* Allocate a block of memory to store the block of audio samples: */ |
||||
assert((buf = malloc(sizeof(sox_sample_t) * block_size))); |
||||
|
||||
/* This example program requires that the audio has precisely 2 channels: */ |
||||
assert(in->signal.channels == 2); |
||||
|
||||
/* Read and process blocks of audio for the selected period or until EOF: */ |
||||
for (blocks = 0; sox_read(in, buf, block_size) == block_size && blocks * block_period < period; ++blocks) { |
||||
double left = 0, right = 0; |
||||
size_t i; |
||||
static const char line[] = "==================================="; |
||||
int l, r; |
||||
|
||||
for (i = 0; i < block_size; ++i) { |
||||
SOX_SAMPLE_LOCALS; |
||||
/* convert the sample from SoX's internal format to a `double' for
|
||||
* processing in this application: */ |
||||
double sample = SOX_SAMPLE_TO_FLOAT_64BIT(buf[i],); |
||||
|
||||
/* The samples for each channel are interleaved; in this example
|
||||
* we allow only stereo audio, so the left channel audio can be found in |
||||
* even-numbered samples, and the right channel audio in odd-numbered |
||||
* samples: */ |
||||
if (i & 1) |
||||
right = max(right, fabs(sample)); /* Find the peak volume in the block */ |
||||
else |
||||
left = max(left, fabs(sample)); /* Find the peak volume in the block */ |
||||
} |
||||
|
||||
/* Build up the wave form by displaying the left & right channel
|
||||
* volume as a line length: */ |
||||
l = (1 - left) * 35 + .5; |
||||
r = (1 - right) * 35 + .5; |
||||
printf("%8.3f%36s|%s\n", start_secs + blocks * block_period, line + l, line + r); |
||||
} |
||||
|
||||
/* All done; tidy up: */ |
||||
free(buf); |
||||
sox_close(in); |
||||
sox_quit(); |
||||
return 0; |
||||
} |
@ -1,98 +0,0 @@ |
||||
/* Simple example of using SoX libraries
|
||||
* |
||||
* Copyright (c) 2009 robs@users.sourceforge.net |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the |
||||
* Free Software Foundation; either version 2 of the License, or (at your |
||||
* option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along |
||||
* with this program; if not, write to the Free Software Foundation, Inc., |
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||||
*/ |
||||
|
||||
#include "sox.h" |
||||
#include <stdio.h> |
||||
|
||||
/* Concatenate audio files. Note that the files must have the same number
|
||||
* of channels and the same sample rate. |
||||
* |
||||
* Usage: example4 input-1 input-2 [... input-n] output |
||||
*/ |
||||
|
||||
#define check(x) do if (!(x)) { \ |
||||
fprintf(stderr, "check failed: %s\n", #x); goto error; } while (0) |
||||
|
||||
int main(int argc, char * argv[]) |
||||
{ |
||||
sox_format_t * output = NULL; |
||||
int i; |
||||
|
||||
check(argc >= 1 + 2 + 1); /* Need at least 2 input files + 1 output file. */ |
||||
check(sox_init() == SOX_SUCCESS); |
||||
|
||||
/* Defer openning the output file as we want to set its characteristics
|
||||
* based on those of the input files. */ |
||||
|
||||
for (i = 1; i < argc - 1; ++i) { /* For each input file... */ |
||||
sox_format_t * input; |
||||
static sox_signalinfo_t signal; /* static quashes `uninitialised' warning.*/ |
||||
|
||||
/* The (maximum) number of samples that we shall read/write at a time;
|
||||
* chosen as a rough match to typical operating system I/O buffer size: */ |
||||
#define MAX_SAMPLES (size_t)2048 |
||||
sox_sample_t samples[MAX_SAMPLES]; /* Temporary store whilst copying. */ |
||||
size_t number_read; |
||||
|
||||
/* Open this input file: */ |
||||
check(input = sox_open_read(argv[i], NULL, NULL, NULL)); |
||||
|
||||
if (i == 1) { /* If this is the first input file... */ |
||||
|
||||
/* Open the output file using the same signal and encoding character-
|
||||
* istics as the first input file. Note that here, input->signal.length |
||||
* will not be equal to the output file length so we are relying on |
||||
* libSoX to set the output length correctly (i.e. non-seekable output |
||||
* is not catered for); an alternative would be to first calculate the |
||||
* output length by summing the lengths of the input files and modifying |
||||
* the second parameter to sox_open_write accordingly. */ |
||||
check(output = sox_open_write(argv[argc - 1], |
||||
&input->signal, &input->encoding, NULL, NULL, NULL)); |
||||
|
||||
/* Also, we'll store the signal characteristics of the first file
|
||||
* so that we can check that these match those of the other inputs: */ |
||||
signal = input->signal; |
||||
} |
||||
else { /* Second or subsequent input file... */ |
||||
|
||||
/* Check that this input file's signal matches that of the first file: */ |
||||
check(input->signal.channels == signal.channels); |
||||
check(input->signal.rate == signal.rate); |
||||
} |
||||
|
||||
/* Copy all of the audio from this input file to the output file: */ |
||||
while ((number_read = sox_read(input, samples, MAX_SAMPLES))) |
||||
check(sox_write(output, samples, number_read) == number_read); |
||||
|
||||
check(sox_close(input) == SOX_SUCCESS); /* Finished with this input file.*/ |
||||
} |
||||
|
||||
check(sox_close(output) == SOX_SUCCESS); /* Finished with the output file. */ |
||||
output = NULL; |
||||
check(sox_quit() == SOX_SUCCESS); |
||||
return 0; |
||||
|
||||
error: /* Truncate output file on error: */ |
||||
if (output) { |
||||
FILE * f; |
||||
sox_close(output); |
||||
if ((f = fopen(argv[argc - 1], "w"))) fclose(f); |
||||
} |
||||
return 1; |
||||
} |
@ -1,83 +0,0 @@ |
||||
/* Simple example of using SoX libraries
|
||||
* |
||||
* Copyright (c) 2009 robs@users.sourceforge.net |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the |
||||
* Free Software Foundation; either version 2 of the License, or (at your |
||||
* option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along |
||||
* with this program; if not, write to the Free Software Foundation, Inc., |
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||||
*/ |
||||
|
||||
#ifdef NDEBUG /* N.B. assert used with active statements so enable always. */ |
||||
#undef NDEBUG /* Must undef above assert.h or other that might include it. */ |
||||
#endif |
||||
|
||||
#include "sox.h" |
||||
#include "util.h" |
||||
#include <stdio.h> |
||||
#include <assert.h> |
||||
|
||||
/* Example of reading and writing audio files stored in memory buffers
|
||||
* rather than actual files. |
||||
* |
||||
* Usage: example5 input output |
||||
*/ |
||||
|
||||
/* Uncomment following line for fixed instead of malloc'd buffer: */ |
||||
/*#define FIXED_BUFFER */ |
||||
|
||||
#if defined FIXED_BUFFER |
||||
#define buffer_size 123456 |
||||
static char buffer[buffer_size]; |
||||
#endif |
||||
|
||||
int main(int argc, char * argv[]) |
||||
{ |
||||
static sox_format_t * in, * out; /* input and output files */ |
||||
#define MAX_SAMPLES (size_t)2048 |
||||
sox_sample_t samples[MAX_SAMPLES]; /* Temporary store whilst copying. */ |
||||
#if !defined FIXED_BUFFER |
||||
char * buffer; |
||||
size_t buffer_size; |
||||
#endif |
||||
size_t number_read; |
||||
|
||||
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))); |
||||
#if defined FIXED_BUFFER |
||||
assert((out = sox_open_mem_write(buffer, buffer_size, &in->signal, NULL, "sox", NULL))); |
||||
#else |
||||
assert((out = sox_open_memstream_write(&buffer, &buffer_size, &in->signal, NULL, "sox", NULL))); |
||||
#endif |
||||
while ((number_read = sox_read(in, samples, MAX_SAMPLES))) |
||||
assert(sox_write(out, samples, number_read) == number_read); |
||||
sox_close(out); |
||||
sox_close(in); |
||||
|
||||
assert((in = sox_open_mem_read(buffer, buffer_size, NULL, NULL, NULL))); |
||||
assert((out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL))); |
||||
while ((number_read = sox_read(in, samples, MAX_SAMPLES))) |
||||
assert(sox_write(out, samples, number_read) == number_read); |
||||
sox_close(out); |
||||
sox_close(in); |
||||
#if !defined FIXED_BUFFER |
||||
free(buffer); |
||||
#endif |
||||
|
||||
sox_quit(); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue