Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.22 KB | None | 0 0
  1. /* Simple example of using SoX libraries
  2.  *
  3.  * Copyright (c) 2009 robs@users.sourceforge.net
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License as published by the
  7.  * Free Software Foundation; either version 2 of the License, or (at your
  8.  * option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  13.  * Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  */
  19.  
  20. #ifdef NDEBUG /* N.B. assert used with active statements so enable always. */
  21. #undef NDEBUG /* Must undef above assert.h or other that might include it. */
  22. #endif
  23.  
  24. #include "sox.h"
  25. #include "util.h"
  26. #include <stdio.h>
  27. #include <assert.h>
  28.  
  29. /* Example of reading and writing audio files stored in memory buffers
  30.  * rather than actual files.
  31.  *
  32.  * Usage: example_test_mem input output trim_start trim_end
  33.  * Compile: gcc -o example_test_mem example_test_mem.c -lsox -Wall -Wextra -g I/path/to/sox.h
  34.  */
  35.  
  36. /* Uncomment following line for fixed instead of malloc'd buffer: */
  37. /*#define FIXED_BUFFER */
  38.  
  39. #if defined FIXED_BUFFER
  40. #define buffer_size 123456
  41. static char buffer[buffer_size];
  42. #endif
  43.  
  44. int main(int argc, char * argv[])
  45. {
  46.   static sox_format_t * in, * out; /* input and output files */
  47.   #define MAX_SAMPLES (size_t)2048
  48.   sox_sample_t samples[MAX_SAMPLES]; /* Temporary store whilst copying. */
  49. #if !defined FIXED_BUFFER
  50.   char * buffer;
  51.   size_t buffer_size;
  52. #endif
  53.   size_t number_read;
  54.  
  55.   assert(argc == 5);
  56.  
  57.   printf("\nGiven input:\n%s %s %s %s %s\n\n", argv[0], argv[1], argv[2], argv[3], argv[4]);
  58.    
  59.   /* All libSoX applications must start by initialising the SoX library */
  60.   assert(sox_init() == SOX_SUCCESS);
  61.  
  62.   /* Open the input file (with default parameters) */
  63.   assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
  64. #if defined FIXED_BUFFER
  65.   assert(out = sox_open_mem_write(buffer, buffer_size, &in->signal, NULL, "sox", NULL));
  66. #else
  67.   assert(out = sox_open_memstream_write(&buffer, &buffer_size, &in->signal, NULL, "sox", NULL));
  68. #endif
  69.   while ((number_read = sox_read(in, samples, MAX_SAMPLES)))
  70.     assert(sox_write(out, samples, number_read) == number_read);
  71.   sox_close(out);
  72.   sox_close(in);
  73.  
  74.  
  75.   /* output file */
  76.   assert(in = sox_open_mem_read(buffer, buffer_size, NULL, NULL, NULL));
  77.   assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL));
  78.  
  79.  
  80.   /* some encoding stuf here */
  81.   sox_effects_chain_t * chain;
  82.   sox_effect_t * e;
  83.   char * args[10];
  84.  
  85.   /* effect chain */
  86.   chain = sox_create_effects_chain(&in->encoding, &out->encoding);
  87.  
  88.  
  89.   e = sox_create_effect(sox_find_effect("input"));
  90.   args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  91.   /* This becomes the first `effect' in the chain */
  92.   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  93.   free(e);
  94.  
  95.   e = sox_create_effect(sox_find_effect("trim"));
  96.   args[0] = argv[3], args[1] = argv[4], assert(sox_effect_options(e, 2, args) == SOX_SUCCESS);
  97.   /* Add the effect to the end of the effects processing chain: */
  98.   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  99.   free(e);
  100.  
  101.   e = sox_create_effect(sox_find_effect("output"));
  102.   args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  103.   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  104.   free(e);
  105.  
  106.   /* Flow samples through the effects processing chain until EOF is reached */
  107.   sox_flow_effects(chain, NULL, NULL);
  108.   /* All done; tidy up: */
  109.   sox_delete_effects_chain(chain);
  110.  
  111.   /* write to disk */
  112.   //while ((number_read = sox_read(in, samples, MAX_SAMPLES)))
  113.   //  assert(sox_write(out, samples, number_read) == number_read);
  114.    
  115.   /* close in & out */
  116.   sox_close(out);
  117.   sox_close(in);
  118.  
  119. #if !defined FIXED_BUFFER
  120.   free(buffer);
  121. #endif
  122.  
  123.   sox_quit();
  124.   return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement