-
Notifications
You must be signed in to change notification settings - Fork 6
/
DecoderDemo.java
243 lines (193 loc) · 5.56 KB
/
DecoderDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
** DecoderDemo.java
**
** Copyright (c) 2011 Peter McQuillan
**
** All Rights Reserved.
**
** Distributed under the BSD Software License (see license.txt)
**
*/
import com.beatofthedrum.alacdecoder.*;
class DecoderDemo
{
static java.io.FileOutputStream output_stream;
static int output_opened;
static int write_wav_format = 1;
static String input_file_n = "";
static String output_file_n = "";
// Reformat samples from longs in processor's native endian mode to
// little-endian data with (possibly) less than 3 bytes / sample.
public static byte[] format_samples(int bps, int[] src, int samcnt)
{
int temp = 0;
int counter = 0;
int counter2 = 0;
byte[] dst = new byte[65536];
switch (bps)
{
case 1:
while (samcnt > 0)
{
dst[counter] = (byte)(0x00FF & (src[counter] + 128));
counter++;
samcnt--;
}
break;
case 2:
while (samcnt > 0)
{
temp = src[counter2];
dst[counter] = (byte)temp;
counter++;
dst[counter] = (byte)(temp >>> 8);
counter++;
counter2++;
samcnt = samcnt - 2;
}
break;
case 3:
while (samcnt > 0)
{
dst[counter] = (byte)src[counter2];
counter++;
counter2++;
samcnt--;
}
break;
}
return dst;
}
static void setup_environment(int argc, String[] argv)
{
int i = argc;
int escaped = 0;
if (argc < 2)
usage();
int arg_idx = 0;
// loop through command-line arguments
while (arg_idx < argc)
{
if (argv[arg_idx].startsWith("-"))
{
if (argv[arg_idx].startsWith("-r") || argv[arg_idx].startsWith("-R"))
{
// raw PCM output
write_wav_format = 0;
}
}
else if (input_file_n.length() == 0)
{
input_file_n = argv[arg_idx];
}
else if (output_file_n.length() == 0)
{
output_file_n = argv[arg_idx];
}
else
{
System.out.println("extra unknown argument: " + argv[arg_idx]);
usage();
}
arg_idx++;
}
if (input_file_n.length() == 0 || output_file_n.length() == 0 )
usage();
}
static void GetBuffer(AlacContext ac)
{
int destBufferSize = 1024 *24 * 3; // 24kb buffer = 4096 frames = 1 alac sample (we support max 24bps)
byte[] pcmBuffer = new byte[65536];
int total_unpacked_bytes = 0;
int bytes_unpacked;
int[] pDestBuffer = new int[destBufferSize];
int bps = AlacUtils.AlacGetBytesPerSample(ac);
while (true)
{
bytes_unpacked = AlacUtils.AlacUnpackSamples(ac, pDestBuffer);
total_unpacked_bytes += bytes_unpacked;
if (bytes_unpacked > 0)
{
pcmBuffer = format_samples(bps, pDestBuffer, bytes_unpacked);
try
{
output_stream.write(pcmBuffer, 0, bytes_unpacked);
}
catch(java.io.IOException ioe)
{
System.err.println("Error writing data to output file. Error: " + ioe);
}
}
if (bytes_unpacked == 0)
break;
} // end of while
}
static void usage()
{
System.out.println("Usage: alac [options] inputfile outputfile");
System.out.println("Decompresses the ALAC file specified");
System.out.println("Options:");
System.out.println(" -r write output as raw PCM data. Default");
System.out.println(" is in WAV format.");
System.out.println("");
System.out.println("This port of the code is (c) Peter McQuillan 2011");
System.out.println("Original software is (c) 2005 David Hammerton");
System.exit(1);
}
public static void main(String [] args)
{
AlacContext ac = new AlacContext();
int output_size;
int total_samples;
int sample_rate;
int num_channels;
int byteps;
int bitps;
output_opened = 0;
setup_environment(args.length, args); // checks all the parameters passed on command line
try
{
output_stream = new java.io.FileOutputStream(output_file_n);
output_opened = 1;
}
catch(java.io.IOException ioe)
{
System.out.println("Cannot open output file: " + output_file_n + " : Error : " + ioe);
output_opened = 0;
System.exit(1);
}
ac = AlacUtils.AlacOpenFileInput(input_file_n);
if (ac.error)
{
System.err.println("Sorry an error has occured");
System.err.println(ac.error_message);
System.exit(1);
}
num_channels = AlacUtils.AlacGetNumChannels(ac);
System.out.println("The Apple Lossless file has " + num_channels + " channels");
total_samples = AlacUtils.AlacGetNumSamples(ac);
System.out.println("The Apple Lossless file has " + total_samples + " samples");
byteps = AlacUtils.AlacGetBytesPerSample(ac);
System.out.println("The Apple Lossless file has " + byteps + " bytes per sample");
sample_rate = AlacUtils.AlacGetSampleRate(ac);
bitps = AlacUtils.AlacGetBitsPerSample(ac);
/* write wav output headers */
if (write_wav_format != 0)
{
WavWriter.wavwriter_writeheaders(output_stream, (total_samples * byteps * num_channels), num_channels, sample_rate, byteps, bitps);
}
/* will convert the entire buffer */
GetBuffer(ac);
AlacUtils.AlacCloseFile(ac);
if (output_opened != 0)
{
try
{
output_stream.close();
}
catch(java.io.IOException ioe)
{
}
}
}
}