• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define BUFFER_LEN		(1 << 14)
48 
49 
50 typedef	struct
51 {	char	*infilename, *outfilename ;
52 	SF_INFO	infileinfo, outfileinfo ;
53 } OptionData ;
54 
55 const char * program_name (const char * argv0) ;
56 static void sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) ;
57 static void add_instrument_data (SNDFILE *outfile, const SF_INFO * in_info) ;
58 
59 static void
usage_exit(const char * progname)60 usage_exit (const char *progname)
61 {
62 	printf ("\nUsage : %s <input file> <output file>\n", progname) ;
63 	puts ("") ;
64 	exit (1) ;
65 } /* usage_exit */
66 
67 int
main(int argc,char * argv[])68 main (int argc, char * argv [])
69 {	const char	*progname, *infilename, *outfilename ;
70 	SNDFILE		*infile = NULL, *outfile = NULL ;
71 	SF_INFO		in_sfinfo, out_sfinfo ;
72 
73 	progname = program_name (argv [0]) ;
74 
75 	if (argc < 3 || argc > 5)
76 		usage_exit (progname) ;
77 
78 	infilename = argv [argc-2] ;
79 	outfilename = argv [argc-1] ;
80 
81 	if (strcmp (infilename, outfilename) == 0)
82 	{	printf ("Error : Input and output filenames are the same.\n\n") ;
83 		usage_exit (progname) ;
84 		} ;
85 
86 	if (strlen (infilename) > 1 && infilename [0] == '-')
87 	{	printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
88 		usage_exit (progname) ;
89 		} ;
90 
91 	if (outfilename [0] == '-')
92 	{	printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
93 		usage_exit (progname) ;
94 		} ;
95 
96 	memset (&in_sfinfo, 0, sizeof (in_sfinfo)) ;
97 
98 	if ((infile = sf_open (infilename, SFM_READ, &in_sfinfo)) == NULL)
99 	{	printf ("Not able to open input file %s.\n", infilename) ;
100 		puts (sf_strerror (NULL)) ;
101 		return 1 ;
102 		} ;
103 
104 	memcpy (&out_sfinfo, &in_sfinfo, sizeof (out_sfinfo)) ;
105 	/* Open the output file. */
106 	if ((outfile = sf_open (outfilename, SFM_WRITE, &out_sfinfo)) == NULL)
107 	{	printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
108 		return 1 ;
109 		} ;
110 
111 	/* Add the loop data */
112 	add_instrument_data (outfile, &in_sfinfo) ;
113 
114 	/* Copy the audio data */
115 	sfe_copy_data_int (outfile, infile, in_sfinfo.channels) ;
116 
117 	sf_close (infile) ;
118 	sf_close (outfile) ;
119 
120 	return 0 ;
121 } /* main */
122 
123 const char *
program_name(const char * argv0)124 program_name (const char * argv0)
125 {	const char * tmp ;
126 
127 	tmp = strrchr (argv0, '/') ;
128 	argv0 = tmp ? tmp + 1 : argv0 ;
129 
130 	/* Remove leading libtool name mangling. */
131 	if (strstr (argv0, "lt-") == argv0)
132 		return argv0 + 3 ;
133 
134 	return argv0 ;
135 } /* program_name */
136 
137 static void
sfe_copy_data_int(SNDFILE * outfile,SNDFILE * infile,int channels)138 sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels)
139 {	static int	data [BUFFER_LEN] ;
140 	int		frames, readcount ;
141 
142 	frames = BUFFER_LEN / channels ;
143 	readcount = frames ;
144 
145 	while (readcount > 0)
146 	{	readcount = sf_readf_int (infile, data, frames) ;
147 		sf_writef_int (outfile, data, readcount) ;
148 		} ;
149 
150 	return ;
151 } /* sfe_copy_data_int */
152 
153 static void
add_instrument_data(SNDFILE * file,const SF_INFO * info)154 add_instrument_data (SNDFILE *file, const SF_INFO *info)
155 {	SF_INSTRUMENT instr ;
156 
157 	memset (&instr, 0, sizeof (instr)) ;
158 
159 	instr.gain = 1 ;
160 	instr.basenote = 0 ;
161 	instr.detune = 0 ;
162 	instr.velocity_lo = 0 ;
163 	instr.velocity_hi = 0 ;
164 	instr.key_lo = 0 ;
165 	instr.key_hi = 0 ;
166 	instr.loop_count = 1 ;
167 
168 	instr.loops [0].mode = SF_LOOP_FORWARD ;
169 	instr.loops [0].start = 0 ;
170 	instr.loops [0].end = info->frames ;
171 	instr.loops [0].count = 0 ;
172 
173 	if (sf_command (file, SFC_SET_INSTRUMENT, &instr, sizeof (instr)) == SF_FALSE)
174 	{	printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
175 		exit (1) ;
176 		} ;
177 
178 	return ;
179 } /* add_instrument_data */
180 
181