annotate src/libsndfile-1.0.27/examples/sndfile-loopify.c @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 1999-2015 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** All rights reserved.
Chris@40 5 **
Chris@40 6 ** Redistribution and use in source and binary forms, with or without
Chris@40 7 ** modification, are permitted provided that the following conditions are
Chris@40 8 ** met:
Chris@40 9 **
Chris@40 10 ** * Redistributions of source code must retain the above copyright
Chris@40 11 ** notice, this list of conditions and the following disclaimer.
Chris@40 12 ** * Redistributions in binary form must reproduce the above copyright
Chris@40 13 ** notice, this list of conditions and the following disclaimer in
Chris@40 14 ** the documentation and/or other materials provided with the
Chris@40 15 ** distribution.
Chris@40 16 ** * Neither the author nor the names of any contributors may be used
Chris@40 17 ** to endorse or promote products derived from this software without
Chris@40 18 ** specific prior written permission.
Chris@40 19 **
Chris@40 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@40 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Chris@40 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Chris@40 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Chris@40 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@40 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@40 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Chris@40 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Chris@40 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Chris@40 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Chris@40 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@40 31 */
Chris@40 32
Chris@40 33 /*
Chris@40 34 ** A quick/rough hack to add SF_INSTRUMENT data to a file. It compiles, but
Chris@40 35 ** no guarantees beyond that. Happy to receive patches to fix/improve it.
Chris@40 36 **
Chris@40 37 ** Code for this was stolen from programs/sndfile-convert.c and related code.
Chris@40 38 */
Chris@40 39
Chris@40 40 #include <stdio.h>
Chris@40 41 #include <stdlib.h>
Chris@40 42 #include <string.h>
Chris@40 43 #include <ctype.h>
Chris@40 44
Chris@40 45 #include <sndfile.h>
Chris@40 46
Chris@40 47 #include "common.h"
Chris@40 48
Chris@40 49 #define BUFFER_LEN (1 << 14)
Chris@40 50
Chris@40 51
Chris@40 52 typedef struct
Chris@40 53 { char *infilename, *outfilename ;
Chris@40 54 SF_INFO infileinfo, outfileinfo ;
Chris@40 55 } OptionData ;
Chris@40 56
Chris@40 57 const char * program_name (const char * argv0) ;
Chris@40 58 static void sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) ;
Chris@40 59 static void add_instrument_data (SNDFILE *outfile, const SF_INFO * in_info) ;
Chris@40 60
Chris@40 61 static void
Chris@40 62 usage_exit (const char *progname)
Chris@40 63 {
Chris@40 64 printf ("\nUsage : %s <input file> <output file>\n", progname) ;
Chris@40 65 puts ("") ;
Chris@40 66 exit (1) ;
Chris@40 67 } /* usage_exit */
Chris@40 68
Chris@40 69 int
Chris@40 70 main (int argc, char * argv [])
Chris@40 71 { const char *progname, *infilename, *outfilename ;
Chris@40 72 SNDFILE *infile = NULL, *outfile = NULL ;
Chris@40 73 SF_INFO in_sfinfo, out_sfinfo ;
Chris@40 74
Chris@40 75 progname = program_name (argv [0]) ;
Chris@40 76
Chris@40 77 if (argc < 3 || argc > 5)
Chris@40 78 usage_exit (progname) ;
Chris@40 79
Chris@40 80 infilename = argv [argc-2] ;
Chris@40 81 outfilename = argv [argc-1] ;
Chris@40 82
Chris@40 83 if (strcmp (infilename, outfilename) == 0)
Chris@40 84 { printf ("Error : Input and output filenames are the same.\n\n") ;
Chris@40 85 usage_exit (progname) ;
Chris@40 86 } ;
Chris@40 87
Chris@40 88 if (strlen (infilename) > 1 && infilename [0] == '-')
Chris@40 89 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
Chris@40 90 usage_exit (progname) ;
Chris@40 91 } ;
Chris@40 92
Chris@40 93 if (outfilename [0] == '-')
Chris@40 94 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
Chris@40 95 usage_exit (progname) ;
Chris@40 96 } ;
Chris@40 97
Chris@40 98 memset (&in_sfinfo, 0, sizeof (in_sfinfo)) ;
Chris@40 99
Chris@40 100 if ((infile = sf_open (infilename, SFM_READ, &in_sfinfo)) == NULL)
Chris@40 101 { printf ("Not able to open input file %s.\n", infilename) ;
Chris@40 102 puts (sf_strerror (NULL)) ;
Chris@40 103 return 1 ;
Chris@40 104 } ;
Chris@40 105
Chris@40 106 memcpy (&out_sfinfo, &in_sfinfo, sizeof (out_sfinfo)) ;
Chris@40 107 /* Open the output file. */
Chris@40 108 if ((outfile = sf_open (outfilename, SFM_WRITE, &out_sfinfo)) == NULL)
Chris@40 109 { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
Chris@40 110 return 1 ;
Chris@40 111 } ;
Chris@40 112
Chris@40 113 /* Add the loop data */
Chris@40 114 add_instrument_data (outfile, &in_sfinfo) ;
Chris@40 115
Chris@40 116 /* Copy the audio data */
Chris@40 117 sfe_copy_data_int (outfile, infile, in_sfinfo.channels) ;
Chris@40 118
Chris@40 119 sf_close (infile) ;
Chris@40 120 sf_close (outfile) ;
Chris@40 121
Chris@40 122 return 0 ;
Chris@40 123 } /* main */
Chris@40 124
Chris@40 125 const char *
Chris@40 126 program_name (const char * argv0)
Chris@40 127 { const char * tmp ;
Chris@40 128
Chris@40 129 tmp = strrchr (argv0, '/') ;
Chris@40 130 argv0 = tmp ? tmp + 1 : argv0 ;
Chris@40 131
Chris@40 132 /* Remove leading libtool name mangling. */
Chris@40 133 if (strstr (argv0, "lt-") == argv0)
Chris@40 134 return argv0 + 3 ;
Chris@40 135
Chris@40 136 return argv0 ;
Chris@40 137 } /* program_name */
Chris@40 138
Chris@40 139 static void
Chris@40 140 sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels)
Chris@40 141 { static int data [BUFFER_LEN] ;
Chris@40 142 int frames, readcount ;
Chris@40 143
Chris@40 144 frames = BUFFER_LEN / channels ;
Chris@40 145 readcount = frames ;
Chris@40 146
Chris@40 147 while (readcount > 0)
Chris@40 148 { readcount = sf_readf_int (infile, data, frames) ;
Chris@40 149 sf_writef_int (outfile, data, readcount) ;
Chris@40 150 } ;
Chris@40 151
Chris@40 152 return ;
Chris@40 153 } /* sfe_copy_data_int */
Chris@40 154
Chris@40 155 static void
Chris@40 156 add_instrument_data (SNDFILE *file, const SF_INFO *info)
Chris@40 157 { SF_INSTRUMENT instr ;
Chris@40 158
Chris@40 159 memset (&instr, 0, sizeof (instr)) ;
Chris@40 160
Chris@40 161 instr.gain = 1 ;
Chris@40 162 instr.basenote = 0 ;
Chris@40 163 instr.detune = 0 ;
Chris@40 164 instr.velocity_lo = 0 ;
Chris@40 165 instr.velocity_hi = 0 ;
Chris@40 166 instr.key_lo = 0 ;
Chris@40 167 instr.key_hi = 0 ;
Chris@40 168 instr.loop_count = 1 ;
Chris@40 169
Chris@40 170 instr.loops [0].mode = SF_LOOP_FORWARD ;
Chris@40 171 instr.loops [0].start = 0 ;
Chris@40 172 instr.loops [0].end = info->frames ;
Chris@40 173 instr.loops [0].count = 0 ;
Chris@40 174
Chris@40 175 if (sf_command (file, SFC_SET_INSTRUMENT, &instr, sizeof (instr)) == SF_FALSE)
Chris@40 176 { printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
Chris@40 177 exit (1) ;
Chris@40 178 } ;
Chris@40 179
Chris@40 180 return ;
Chris@40 181 } /* add_instrument_data */
Chris@40 182