import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import org.mp3transform.Bitstream;
import org.mp3transform.Decoder;
import org.mp3transform.Header;
import org.mp3transform.wav.*;
import it.sauronsoftware.jave.*;
import static x30_pkg.x30_util.DynamicObject;
class main {
static File mp3ToWAVUnlessExists(File in) { return mp3ToWAVUnlessExists(in, replaceFileExtension(in, ".wav")); }
static File mp3ToWAVUnlessExists(File in,
File out) {
if (!out.exists()) try {
mp3ToWAV(in, out);
} catch (Throwable __0) { printStackTrace(__0);
mp3ToWAV_jave(in, out);
}
return out;
}
static File replaceFileExtension(File f, String extOld, String extNew) {
return replaceExtension(f, extOld, extNew);
}
static File replaceFileExtension(File f, String extNew) {
return replaceExtension(f, extNew);
}
static File replaceFileExtension(String extOld, String extNew, File f) {
return replaceFileExtension(f, extOld, extNew);
}
// mp3transform
static void mp3ToWAV(String in, String out) { try {
mp3ToWAV(new File(in), new File(out));
} catch (Exception __e) { throw rethrow(__e); } }
static File mp3ToWAV(File in, File out) { try {
mp3ToWAV(new BufferedInputStream(new FileInputStream(in)), out);
return out;
} catch (Exception __e) { throw rethrow(__e); } }
static void mp3ToWAV(InputStream in, File out) { try {
class WavConverter extends Decoder {
void convert(String sourceFileName, String destFileName)
throws IOException {
InputStream fileIn = new FileInputStream(sourceFileName);
BufferedInputStream in = new BufferedInputStream(fileIn, 128 * 1024);
convert(in, destFileName);
in.close();
}
void convert(InputStream sourceStream, String destFileName)
throws IOException {
int frameCount = Integer.MAX_VALUE;
WavConverter decoder = new WavConverter();
Bitstream stream = new Bitstream(sourceStream);
frameCount = Integer.MAX_VALUE;
try {
for (int frame = 0; frame < frameCount; frame++) {
Header header = stream.readFrame();
if (header == null) {
break;
}
if (decoder.channels == 0) {
int channels = (header.mode() == Header.MODE_SINGLE_CHANNEL) ? 1
: 2;
int freq = header.frequency();
decoder.initOutputBuffer(channels, freq, destFileName);
}
decoder.decodeFrame(header, stream);
stream.closeFrame();
}
} finally {
decoder.close();
}
}
private short[] buffer;
private WaveFileWriter outWave;
int channels;
public void initOutputBuffer(int numberOfChannels, int freq, String fileName)
throws IOException {
super.initOutputBuffer(null, numberOfChannels);
channels = numberOfChannels;
buffer = new short[BUFFER_SIZE];
for (int i = 0; i < numberOfChannels; ++i) {
bufferPointer[i] = (short) i;
}
outWave = nuObject(WaveFileWriter.class, fileName, freq, (short) 16, (short) numberOfChannels);
}
public void appendSamples(int channel, double[] f) {
int p = bufferPointer[channel];
for (int i = 0; i < 32; i++) {
double sample = f[i];
short s = ((sample > 32767.0f) ? 32767
: ((sample < -32768.0f) ? -32768 : (short) sample));
buffer[p] = s;
p += channels;
}
bufferPointer[channel] = p;
}
public void writeBuffer() throws IOException {
call(outWave, "writeData", buffer, bufferPointer[0]);
for (int i = 0; i < channels; ++i) {
bufferPointer[i] = i;
}
}
public void close() throws IOException {
callOpt(outWave, "close");
}
}
new WavConverter().convert(in, f2s(out));
} catch (Exception __e) { throw rethrow(__e); } }
static A printStackTrace(A e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
static void printStackTrace(String msg, Throwable e) {
printStackTrace(new Throwable(msg, e));
}
// JAVE/ffmpeg
static void mp3ToWAV_jave(File mp3, File wav) { try {
AudioAttributes audio = new AudioAttributes();
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("wav");
attrs.setAudioAttributes(audio);
File tempWAV = appendToFileName(wav, ".temp");
print("Converting " + f2s(mp3) + " to " + f2s(wav));
new Encoder().encode(mp3, tempWAV, attrs);
forceRenameFile(tempWAV, wav);
print(" [done]");
} catch (Exception __e) { throw rethrow(__e); } }
static File replaceExtension(File f, String extOld, String extNew) {
return newFile(replaceExtension(f2s(f), extOld, extNew));
}
static File replaceExtension(File f, String extNew) {
return replaceExtension(f, fileExtension(f), extNew);
}
static String replaceExtension(String s, String extOld, String extNew) {
s = dropSuffixIC(addPrefixOptIfNempty(".", extOld), s);
return s + addPrefixOptIfNempty(".", extNew);
}
static String replaceExtension(String name, String extNew) {
return replaceExtension(name, fileExtension(name), extNew);
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static Object nuObject(String className, Object... args) { try {
return nuObject(classForName(className), args);
} catch (Exception __e) { throw rethrow(__e); } }
// too ambiguous - maybe need to fix some callers
/*static O nuObject(O realm, S className, O... args) {
ret nuObject(_getClass(realm, className), args);
}*/
static A nuObject(Class c, Object... args) { try {
if (args.length == 0) return nuObjectWithoutArguments(c); // cached!
Constructor m = nuObject_findConstructor(c, args);
makeAccessible(m);
return (A) m.newInstance(args);
} catch (Exception __e) { throw rethrow(__e); } }
static Constructor nuObject_findConstructor(Class c, Object... args) {
for (Constructor m : c.getDeclaredConstructors()) {
if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
continue;
return m;
}
throw fail("Constructor " + c.getName() + getClasses(args) + " not found"
+ (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
}
static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static Object callOpt(Object o) {
return callF(o);
}
static Object callOpt(Object o, String method, Object... args) {
return callOpt_withVarargs(o, method, args);
}
static String f2s(File f) {
return f == null ? null : f.getAbsolutePath();
}
static String f2s(String s) { return f2s(newFile(s)); }
static String f2s(java.nio.file.Path p) {
return p == null ? null : f2s(p.toFile());
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal