-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJMFaccess.java
More file actions
265 lines (261 loc) · 7.42 KB
/
Copy pathJMFaccess.java
File metadata and controls
265 lines (261 loc) · 7.42 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* FFT Export
* James Halliday
* 2001
* JMFaccess.java - handle Java Media Framework Stuff Here
*/
//Version 1.0
//////////////////////////IMPORTS//////////////////////////
import javax.media.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.control.*;
import javax.media.datasink.*;
/////////////////////////JMFaccess CLASS//////////////////////
public class JMFaccess implements ControllerListener, DataSinkListener
{
//useful constants
static final int MONO = 1;
static final int STEREO = 2;
static final String BASICAU = FileTypeDescriptor.BASIC_AUDIO;
static final String AIFF = FileTypeDescriptor.AIFF;
static final String WAV = FileTypeDescriptor.WAVE;
static final String MP3 = FileTypeDescriptor.MPEG_AUDIO;
static final String LINEAR = AudioFormat.LINEAR;
static final String MP3_COMPRESSION = AudioFormat.MPEGLAYER3;
static final int BIG_ENDIAN = AudioFormat.BIG_ENDIAN;
static final int LITTLE_ENDIAN = AudioFormat.LITTLE_ENDIAN;
static final int SIGNED = AudioFormat.SIGNED;
static final int UNSIGNED = AudioFormat.UNSIGNED;
//vars
static boolean filedone;
static boolean filesuccess;
//constructor
public JMFaccess()
{}
////////////////////////METHODS/////////////////////////////
public File dotranscode(File afile, double astart, double aend, Thread thred, boolean verbose, String filetype, String compression, int endian, int signed, int channels, int bitrate, int samprate)
{
//vars we'll need
File tempfile = null;
Processor JMFproc = null;
//start transcoding
if (verbose)
System.out.println("Creating Processor");
try
{JMFproc = Manager.createProcessor(new MediaLocator("file://" + afile));}
catch (Exception e)
{
System.out.println("Error: Input audio file is not in a valid format");
return null;
}
if (JMFproc == null)
{
System.out.println("Error: Input audio file is not in a valid format");
return null;
}
if (cancelcheck(thred))
return null;
JMFproc.addControllerListener(this);
if (verbose)
System.out.println("Configuring Processor");
//configure
JMFproc.configure();
//wait for configuration before going on
while (JMFproc.getState() < JMFproc.Configured)
{
if (cancelcheck(thred))
return null;
}
//make temp sound file
if (verbose)
System.out.println("Creating Temp Sound File");
try
{tempfile = File.createTempFile("sinktemp", ".au");}
catch (IOException ex)
{
System.out.println("Trouble Creating Temporary File.");
return null;
}
if (verbose)
System.out.println("Temp File Created: " + tempfile.getAbsolutePath());
//turn file into an .au (basic audio) file
JMFproc.setContentDescriptor(new FileTypeDescriptor(filetype));
System.out.println("Getting Track Information");
//get and set up tracks to be transferred
TrackControl tcs[];
AudioFormat af = new AudioFormat(compression, samprate, bitrate, channels, endian, signed);
if ((tcs = JMFproc.getTrackControls()) == null)
{
System.out.println("Trouble getting track controls.");
return null;
}
javax.media.Format supported[];
javax.media.Format f;
boolean flagg = false;
for (int i = 0; i < tcs.length; i++)
{
supported = tcs[i].getSupportedFormats();
if (supported == null)
continue;
for (int j = 0; j < supported.length; j++)
{
if (af.matches(supported[j]) && (f = af.intersects(supported[j])) != null && tcs[i].setFormat(f) != null)
{
// Success
flagg = true;
}
}
}
if (flagg == false)
{
System.out.println("Trouble getting track controls.");
return null;
}
if (cancelcheck(thred))
return null;
//realize processor
if (verbose)
System.out.println("Realizing Processor");
JMFproc.realize();
while (JMFproc.getState() < JMFproc.Realized)
{
if (cancelcheck(thred))
return null;
}
//make datasink
if (verbose)
System.out.println("Creating DataSink");
DataSink dsink = null;
DataSource ds;
ds = JMFproc.getDataOutput();
try
{
dsink = Manager.createDataSink(ds, new MediaLocator("file://" + tempfile.toString()));
dsink.open();
}
catch (Exception e)
{
System.out.println("Problem creating datasink.");
return null;
}
dsink.addDataSinkListener(this);
if (cancelcheck(thred))
return null;
//time to transfer file over
if (verbose)
System.out.println("Transcoding File");
filedone = false;
filesuccess = false;
try
{
//set audio to appropriate start point
JMFproc.setMediaTime(new Time(astart));
//set endpoint
if (aend <= 0)
aend = JMFproc.getDuration().getSeconds();
//make sure points were valid; otherwise analysis may be messed up
if ((astart > JMFproc.getDuration().getSeconds()) || (aend > JMFproc.getDuration().getSeconds()) || (aend <= astart) || (astart < 0))
{
System.out.println("Invalid time-points specified in audio file.");
dsink.close();
JMFproc.close();
return null;
}
JMFproc.setStopTime(new Time(aend));
JMFproc.start();
dsink.start();
}
catch (Exception e)
{
dsink.close();
System.out.println("Trouble Transcoding File");
return null;
}
//wait till file done
int perc = 0;
int tempperc = -1;
while (filedone == false)
{
if (thred.interrupted())
{
try
{
dsink.stop();
JMFproc.stop();
}
catch (IOException errur)
{}
dsink.close();
JMFproc.close();
System.out.println("User canceled.");
return null;
}
if (verbose)
{
//update every 10%
perc = (int)(((JMFproc.getMediaTime().getSeconds() - astart) * 10) / (aend - astart));
if (perc > tempperc)
{
tempperc = perc;
System.out.println("Transcode: " + (perc * 10) + "% complete");
}
}
}
//cleanup; temp file created!
try
{
JMFproc.stop();
dsink.stop();
}
catch (IOException e)
{}
dsink.close();
JMFproc.close();
if (verbose)
System.out.println("Temp File Created");
return tempfile;
}
//check for cancel
boolean cancelcheck(Thread thred)
{
if (thred.interrupted())
{
System.out.println("Process Canceled");
return true;
}
return false;
}
//event stuff
public void controllerUpdate(ControllerEvent e)
{
if (e instanceof EndOfMediaEvent)
{
filedone = true;
filesuccess = true;
}
if (e instanceof StopAtTimeEvent)
{
filedone = true;
filesuccess = true;
}
}
public void dataSinkUpdate(DataSinkEvent evt)
{
if (evt instanceof EndOfStreamEvent)
{
filedone = true;
filesuccess = true;
}
else if (evt instanceof DataSinkErrorEvent)
{
filedone = true;
filesuccess = false;
}
}
}