aboutsummaryrefslogtreecommitdiff
path: root/src/hd24towav.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/hd24towav.cpp')
-rwxr-xr-xsrc/hd24towav.cpp233
1 files changed, 233 insertions, 0 deletions
diff --git a/src/hd24towav.cpp b/src/hd24towav.cpp
new file mode 100755
index 0000000..7d654ef
--- /dev/null
+++ b/src/hd24towav.cpp
@@ -0,0 +1,233 @@
1#ifdef WINDOWS
2namespace std {}
3#endif
4using namespace std;
5#include <string>
6#include <iostream>
7#include "convertlib.h"
8/*
9** Copyright (C) 2001-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
10**
11** This program is free software; you can redistribute it and/or modify
12** it under the terms of the GNU General Public License as published by
13** the Free Software Foundation; either version 2 of the License, or
14** (at your option) any later version.
15**
16** This program is distributed in the hope that it will be useful,
17** but WITHOUT ANY WARRANTY; without even the implied warranty of
18** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19** GNU General Public License for more details.
20**
21** You should have received a copy of the GNU General Public License
22** along with this program; if not, write to the Free Software
23** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24*/
25
26#include <stdio.h>
27
28/* Include this header file to use functions from libsndfile. */
29#include <sndfile.h>
30
31/* This will be the length of the buffer used to hold.frames while
32** we process them.
33*/
34#define BUFFER_LEN 1024
35
36/* libsndfile can handle more than 6 channels but we'll restrict it to 6. */
37#define MAX_CHANNELS 6
38string inputfilename;
39string outputfilename;
40string format;
41long rate;
42long bits;
43int convertfile(string inputfilename) {
44/* This is a buffer of double precision floating point values
45 * which will hold our data while we process it.
46 */
47 format="wav";
48 cout << "Converting " << inputfilename << endl;
49 if (rate==0) {
50 if (
51 (inputfilename.find("44k1",0) != string::npos)
52 ||(inputfilename.find("44K1",0) != string::npos)
53 ||(inputfilename.find("44100",0) != string::npos)
54 ) {
55 rate=44100;
56 }
57 if (
58 (inputfilename.find("48k",0) != string::npos)
59 ||(inputfilename.find("48K",0) != string::npos)
60 ||(inputfilename.find("48000",0) != string::npos)
61 ) {
62 rate=48000;
63 }
64 if (
65 (inputfilename.find("88k2",0) != string::npos)
66 ||(inputfilename.find("88K2",0) != string::npos)
67 ||(inputfilename.find("88200",0) != string::npos)
68 ) {
69 rate=88200;
70 }
71 if (
72 (inputfilename.find("96k",0) != string::npos)
73 ||(inputfilename.find("96K",0) != string::npos)
74 ||(inputfilename.find("96000",0) != string::npos)
75 ) {
76 rate=96000;
77 }
78 }
79 bits=24;
80 static int data [BUFFER_LEN] ;
81
82 /* A SNDFILE is very much like a FILE in the Standard C library. The
83 ** sf_open function return an SNDFILE* pointer when they sucessfully
84 ** open the specified file.
85 */
86 SNDFILE *infile, *outfile ;
87
88 /* A pointer to an SF_INFO stutct is passed to sf_open.
89 ** On read, the library fills this struct with information about the file.
90 ** On write, the struct must be filled in before calling sf_open.
91 */
92 SF_INFO sfinfoin ;
93 SF_INFO sfinfoout;
94 int readcount ;
95 if (inputfilename=="") {
96 cout << "Must specify --input filename" << endl;
97 return 1;
98 }
99 if (outputfilename=="") {
100 outputfilename=inputfilename;
101 string ext="";
102 if (format=="wav") {
103 ext=".wav";
104 }
105 if (outputfilename.substr(outputfilename.length()-4,4)==".raw") {
106 outputfilename=outputfilename.substr(0,outputfilename.length()-4)+ext;
107 }
108 if (outputfilename=="") {
109 cout << "Must specify --output filename" << endl;
110 return 1;
111 }
112 }
113 const char *infilename = inputfilename.c_str();
114 const char *outfilename = outputfilename.c_str();
115
116 /* Here's where we open the input file. We pass sf_open the file name and
117 ** a pointer to an SF_INFO struct.
118 ** On successful open, sf_open returns a SNDFILE* pointer which is used
119 ** for all subsequent operations on that file.
120 ** If an error occurs during sf_open, the function returns a NULL pointer.
121 **
122 ** If you are trying to open a raw headerless file you will need to set the
123 ** format and channels fields of sfinfo before calling sf_open(). For
124 ** instance to open a raw 16 bit stereo PCM file you would need the following
125 ** two lines:
126 **
127 ** sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
128 ** sfinfo.channels = 2 ;
129 */
130 sfinfoin.format = SF_FORMAT_RAW | SF_FORMAT_PCM_24|SF_ENDIAN_BIG ;
131 sfinfoin.channels = 1 ;
132
133 if (format=="wav") {
134 sfinfoout.format = SF_FORMAT_WAV ;
135 }
136 if (bits==16) {
137 sfinfoout.format|=SF_FORMAT_PCM_16;
138 }
139 if (bits==24) {
140 sfinfoout.format|=SF_FORMAT_PCM_24;
141 }
142
143 sfinfoout.samplerate=rate;
144 sfinfoout.channels = 1 ;
145 if (! (infile = sf_open (infilename, SFM_READ, &sfinfoin)))
146 { /* Open failed so print an error message. */
147 printf ("Not able to open input file %s.\n", infilename) ;
148 /* Print the error message from libsndfile. */
149 puts (sf_strerror (NULL)) ;
150 return 1 ;
151 } ;
152
153 /* Open the output file. */
154 if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfoout)))
155 { printf ("Not able to open output file %s.\n", outfilename) ;
156 puts (sf_strerror (NULL)) ;
157 return 1 ;
158 } ;
159
160 /* While there are.frames in the input file, read them, process
161 ** them and write them to the output file.
162 */
163 while ((readcount = sf_read_int (infile, data, BUFFER_LEN)))
164 {
165 sf_write_int (outfile, data, readcount) ;
166 } ;
167
168 /* Close input and output files. */
169 sf_close (infile) ;
170 sf_close (outfile) ;
171 return 0;
172}
173
174int parsecommandline(int argc, char **argv)
175{
176 int invalid = 0;
177
178 for (int c = 1; c < argc; c++) {
179 string arg = argv[c];
180
181 if (arg.substr(0, 1) != "-") {
182 inputfilename = arg;
183 outputfilename = "";
184 convertfile(inputfilename);
185 continue;
186 }
187
188 if (arg.substr(0,strlen("--input=")) == "--input=") {
189 inputfilename = arg.substr(strlen("--input="));
190 continue;
191 }
192
193 if (arg.substr(0,strlen("--output=")) == "--output=") {
194 outputfilename = arg.substr(strlen("--output="));
195 continue;
196 }
197
198 if (arg.substr(0,strlen("--rate=")) == "--rate=") {
199 rate = Convert::str2long(arg.substr(strlen("--rate=")));
200 continue;
201 }
202
203 if (arg.substr(0,strlen("--bits=")) == "--bits=") {
204 bits = Convert::str2long(arg.substr(strlen("--bits=")));
205 continue;
206 }
207
208 cout << "Invalid argument: " << arg << endl;
209 invalid = 1;
210 }
211 return invalid;
212}
213
214int main (int argc, char **argv)
215{
216 rate = 0;
217 int invalid = parsecommandline(argc, argv);
218
219 if (invalid!=0) {
220 return invalid;
221 }
222
223 return 0 ;
224} /* main */
225
226
227/*
228** Do not edit or modify anything in this comment block.
229** The arch-tag line is a file identity tag for the GNU Arch
230** revision control system.
231**
232** arch-tag: de9fdd1e-b807-41ef-9d51-075ba383e536
233*/