comparison src/libsndfile-1.0.27/examples/sndfile-loopify.c @ 125:cd6cdf86811e

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