1b815c7f3Sopenharmony_ci/* 2b815c7f3Sopenharmony_ci** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com> 3b815c7f3Sopenharmony_ci** 4b815c7f3Sopenharmony_ci** All rights reserved. 5b815c7f3Sopenharmony_ci** 6b815c7f3Sopenharmony_ci** Redistribution and use in source and binary forms, with or without 7b815c7f3Sopenharmony_ci** modification, are permitted provided that the following conditions are 8b815c7f3Sopenharmony_ci** met: 9b815c7f3Sopenharmony_ci** 10b815c7f3Sopenharmony_ci** * Redistributions of source code must retain the above copyright 11b815c7f3Sopenharmony_ci** notice, this list of conditions and the following disclaimer. 12b815c7f3Sopenharmony_ci** * Redistributions in binary form must reproduce the above copyright 13b815c7f3Sopenharmony_ci** notice, this list of conditions and the following disclaimer in 14b815c7f3Sopenharmony_ci** the documentation and/or other materials provided with the 15b815c7f3Sopenharmony_ci** distribution. 16b815c7f3Sopenharmony_ci** * Neither the author nor the names of any contributors may be used 17b815c7f3Sopenharmony_ci** to endorse or promote products derived from this software without 18b815c7f3Sopenharmony_ci** specific prior written permission. 19b815c7f3Sopenharmony_ci** 20b815c7f3Sopenharmony_ci** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21b815c7f3Sopenharmony_ci** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22b815c7f3Sopenharmony_ci** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23b815c7f3Sopenharmony_ci** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24b815c7f3Sopenharmony_ci** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25b815c7f3Sopenharmony_ci** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26b815c7f3Sopenharmony_ci** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27b815c7f3Sopenharmony_ci** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28b815c7f3Sopenharmony_ci** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29b815c7f3Sopenharmony_ci** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30b815c7f3Sopenharmony_ci** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31b815c7f3Sopenharmony_ci*/ 32b815c7f3Sopenharmony_ci 33b815c7f3Sopenharmony_ci#include <stdio.h> 34b815c7f3Sopenharmony_ci#include <stdlib.h> 35b815c7f3Sopenharmony_ci#include <string.h> 36b815c7f3Sopenharmony_ci#include <math.h> 37b815c7f3Sopenharmony_ci 38b815c7f3Sopenharmony_ci#include <sndfile.h> 39b815c7f3Sopenharmony_ci 40b815c7f3Sopenharmony_ci#define BUFFER_LEN 4096 41b815c7f3Sopenharmony_ci 42b815c7f3Sopenharmony_cistatic void encode_file (const char *infilename, const char *outfilename, int filetype) ; 43b815c7f3Sopenharmony_ci 44b815c7f3Sopenharmony_ciint 45b815c7f3Sopenharmony_cimain (int argc, char **argv) 46b815c7f3Sopenharmony_ci{ 47b815c7f3Sopenharmony_ci if (argc != 2) 48b815c7f3Sopenharmony_ci { puts ("\nEncode a single input file into a number of different output ") ; 49b815c7f3Sopenharmony_ci puts ("encodings. These output encodings can then be moved to another ") ; 50b815c7f3Sopenharmony_ci puts ("OS for testing.\n") ; 51b815c7f3Sopenharmony_ci puts (" Usage : generate <filename>\n") ; 52b815c7f3Sopenharmony_ci exit (1) ; 53b815c7f3Sopenharmony_ci } ; 54b815c7f3Sopenharmony_ci 55b815c7f3Sopenharmony_ci /* A couple of standard WAV files. Make sure Win32 plays these. */ 56b815c7f3Sopenharmony_ci encode_file (argv [1], "pcmu8.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ; 57b815c7f3Sopenharmony_ci encode_file (argv [1], "pcm16.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; 58b815c7f3Sopenharmony_ci encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ; 59b815c7f3Sopenharmony_ci encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ; 60b815c7f3Sopenharmony_ci encode_file (argv [1], "gsm610.wav" , SF_FORMAT_WAV | SF_FORMAT_GSM610) ; 61b815c7f3Sopenharmony_ci 62b815c7f3Sopenharmony_ci /* Soundforge W64. */ 63b815c7f3Sopenharmony_ci encode_file (argv [1], "pcmu8.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ; 64b815c7f3Sopenharmony_ci encode_file (argv [1], "pcm16.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16) ; 65b815c7f3Sopenharmony_ci encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ; 66b815c7f3Sopenharmony_ci encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ; 67b815c7f3Sopenharmony_ci encode_file (argv [1], "gsm610.w64" , SF_FORMAT_W64 | SF_FORMAT_GSM610) ; 68b815c7f3Sopenharmony_ci 69b815c7f3Sopenharmony_ci return 0 ; 70b815c7f3Sopenharmony_ci} /* main */ 71b815c7f3Sopenharmony_ci 72b815c7f3Sopenharmony_ci/*============================================================================================ 73b815c7f3Sopenharmony_ci** Helper functions and macros. 74b815c7f3Sopenharmony_ci*/ 75b815c7f3Sopenharmony_ci 76b815c7f3Sopenharmony_ci#define PUT_DOTS(k) \ 77b815c7f3Sopenharmony_ci { while (k--) \ 78b815c7f3Sopenharmony_ci putchar ('.') ; \ 79b815c7f3Sopenharmony_ci putchar (' ') ; \ 80b815c7f3Sopenharmony_ci } 81b815c7f3Sopenharmony_ci 82b815c7f3Sopenharmony_ci/*======================================================================================== 83b815c7f3Sopenharmony_ci*/ 84b815c7f3Sopenharmony_ci 85b815c7f3Sopenharmony_cistatic void 86b815c7f3Sopenharmony_ciencode_file (const char *infilename, const char *outfilename, int filetype) 87b815c7f3Sopenharmony_ci{ static float buffer [BUFFER_LEN] ; 88b815c7f3Sopenharmony_ci 89b815c7f3Sopenharmony_ci SNDFILE *infile, *outfile ; 90b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 91b815c7f3Sopenharmony_ci int k, readcount ; 92b815c7f3Sopenharmony_ci 93b815c7f3Sopenharmony_ci printf (" %s -> %s ", infilename, outfilename) ; 94b815c7f3Sopenharmony_ci fflush (stdout) ; 95b815c7f3Sopenharmony_ci 96b815c7f3Sopenharmony_ci k = 16 - strlen (outfilename) ; 97b815c7f3Sopenharmony_ci PUT_DOTS (k) ; 98b815c7f3Sopenharmony_ci 99b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 100b815c7f3Sopenharmony_ci 101b815c7f3Sopenharmony_ci if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) 102b815c7f3Sopenharmony_ci { printf ("Error : could not open file : %s\n", infilename) ; 103b815c7f3Sopenharmony_ci puts (sf_strerror (NULL)) ; 104b815c7f3Sopenharmony_ci exit (1) ; 105b815c7f3Sopenharmony_ci } 106b815c7f3Sopenharmony_ci 107b815c7f3Sopenharmony_ci sfinfo.format = filetype ; 108b815c7f3Sopenharmony_ci 109b815c7f3Sopenharmony_ci if (! sf_format_check (&sfinfo)) 110b815c7f3Sopenharmony_ci { sf_close (infile) ; 111b815c7f3Sopenharmony_ci printf ("Invalid encoding\n") ; 112b815c7f3Sopenharmony_ci return ; 113b815c7f3Sopenharmony_ci } ; 114b815c7f3Sopenharmony_ci 115b815c7f3Sopenharmony_ci if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))) 116b815c7f3Sopenharmony_ci { printf ("Error : could not open file : %s\n", outfilename) ; 117b815c7f3Sopenharmony_ci puts (sf_strerror (NULL)) ; 118b815c7f3Sopenharmony_ci exit (1) ; 119b815c7f3Sopenharmony_ci } ; 120b815c7f3Sopenharmony_ci 121b815c7f3Sopenharmony_ci while ((readcount = (int) sf_read_float (infile, buffer, BUFFER_LEN)) > 0) 122b815c7f3Sopenharmony_ci sf_write_float (outfile, buffer, readcount) ; 123b815c7f3Sopenharmony_ci 124b815c7f3Sopenharmony_ci sf_close (infile) ; 125b815c7f3Sopenharmony_ci sf_close (outfile) ; 126b815c7f3Sopenharmony_ci 127b815c7f3Sopenharmony_ci printf ("ok\n") ; 128b815c7f3Sopenharmony_ci 129b815c7f3Sopenharmony_ci return ; 130b815c7f3Sopenharmony_ci} /* encode_file */ 131b815c7f3Sopenharmony_ci 132