split in java and c++ parts
This commit is contained in:
186
java/src/old/Converter.java
Normal file
186
java/src/old/Converter.java
Normal file
@@ -0,0 +1,186 @@
|
||||
package old;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javazoom.jl.decoder.Bitstream;
|
||||
import javazoom.jl.decoder.BitstreamException;
|
||||
import mimis.exception.worker.ActivateException;
|
||||
import mimis.exception.worker.DeactivateException;
|
||||
import mimis.worker.Worker;
|
||||
|
||||
import com.Ostermiller.util.CircularByteBuffer;
|
||||
|
||||
public class Converter extends Worker {
|
||||
public static final String COMMAND = "lame --mp3input --cbr %s - - --quiet";
|
||||
public static final int BYTES = 4096; // bytes
|
||||
public static final int BUFFER = 30000; // milliseconds
|
||||
public static final int BUFFERING = 1000; // milliseconds
|
||||
|
||||
protected int targetRate;
|
||||
protected int rate;
|
||||
protected int buffer;
|
||||
protected boolean convert;
|
||||
|
||||
protected Process process;
|
||||
protected InputStream sourceInputStream, processInputStream, inputStream;
|
||||
protected OutputStream processOutputStream;
|
||||
protected CircularByteBuffer circularByteBuffer;
|
||||
protected BufferWorker bufferWorker;
|
||||
|
||||
public Converter(InputStream inputStream) {
|
||||
this(inputStream, -1);
|
||||
}
|
||||
|
||||
public Converter(InputStream inputStream, int targetRate) {
|
||||
this.sourceInputStream = inputStream;
|
||||
this.targetRate = targetRate;
|
||||
bufferWorker = new BufferWorker();
|
||||
convert = false;
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
super.exit();
|
||||
bufferWorker.exit();
|
||||
}
|
||||
|
||||
public synchronized void activate() throws ActivateException {
|
||||
/* Read bitrate */
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(sourceInputStream);
|
||||
Bitstream bitStream = new Bitstream(bufferedInputStream);
|
||||
try {
|
||||
rate = bitStream.readFrame().bitrate() / 1000;
|
||||
buffer = BUFFER * rate / 8;
|
||||
} catch (BitstreamException e) {
|
||||
log.error(e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
|
||||
/* Check for need to convert */
|
||||
if (targetRate < 0 || rate == targetRate) {
|
||||
log.debug("No conversion required");
|
||||
inputStream = sourceInputStream;
|
||||
} else {
|
||||
log.debug("Converting from " + rate + "kbps to " + targetRate + "kbps");
|
||||
try {
|
||||
String command = String.format(COMMAND, rate > targetRate ? "-B " + targetRate : "-F -b " + targetRate);
|
||||
log.debug("Starting process: " + command);
|
||||
process = Runtime.getRuntime().exec(command);
|
||||
processInputStream = process.getInputStream();
|
||||
processOutputStream = process.getOutputStream();
|
||||
|
||||
/* Buffer output */
|
||||
circularByteBuffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
|
||||
inputStream = circularByteBuffer.getInputStream();
|
||||
bufferWorker.start();
|
||||
convert = true;
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
}
|
||||
super.activate();
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
protected void deactivate() throws DeactivateException {
|
||||
super.deactivate();
|
||||
try {
|
||||
sourceInputStream.close();
|
||||
bufferWorker.stop();
|
||||
if (convert) {
|
||||
circularByteBuffer.clear();
|
||||
convert = false;
|
||||
}
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
throw new DeactivateException();
|
||||
}
|
||||
}
|
||||
|
||||
protected void work() {
|
||||
if (!convert) {
|
||||
try {
|
||||
synchronized (this) {
|
||||
wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
byte[] bytes = new byte[BYTES];
|
||||
int read = 0;
|
||||
try {
|
||||
log.debug("Writing input to process");
|
||||
while ((read = sourceInputStream.read(bytes)) > 0 && !deactivate) {
|
||||
/* Limit buffer size */
|
||||
while (inputStream.available() > buffer) {
|
||||
int progress = (int) ((1 - (inputStream.available() - buffer) / (float) buffer) * 100);
|
||||
log.trace("Waiting for buffer to empty: " + progress + "%");
|
||||
sleep(BUFFERING);
|
||||
}
|
||||
processOutputStream.write(bytes, 0, read);
|
||||
}
|
||||
processOutputStream.close();
|
||||
log.debug("Stopped writing input to process");
|
||||
process.waitFor();
|
||||
log.debug("Process finished");
|
||||
} catch (IOException e) {
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
}
|
||||
stop();
|
||||
}
|
||||
|
||||
public synchronized InputStream getInputStream() {
|
||||
if (!active()) {
|
||||
if (!activate) {
|
||||
start();
|
||||
}
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
public synchronized void setInputStream(InputStream inputStream) {
|
||||
this.inputStream = inputStream;
|
||||
}
|
||||
|
||||
class BufferWorker extends Worker {
|
||||
protected void work() {
|
||||
byte[] bytes = new byte[BYTES];
|
||||
int read = 0;
|
||||
try {
|
||||
OutputStream bufferOutputStream = circularByteBuffer.getOutputStream();
|
||||
log.debug("Start buffering process output");
|
||||
while ((read = processInputStream.read(bytes, 0, BYTES)) > 0) {
|
||||
bufferOutputStream.write(bytes, 0, read);
|
||||
}
|
||||
log.debug("Finished buffering process output");
|
||||
bufferOutputStream.close();
|
||||
} catch (IOException e) {}
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Mp3 mp3 = new Mp3(new File("stream.mp3"), 128);
|
||||
InputStream inputStream = mp3.getInputStream();
|
||||
|
||||
/* Play */
|
||||
//Utils.play(inputStream);
|
||||
|
||||
/* Write to file */
|
||||
Utils.write(inputStream, new File("output.mp3"));
|
||||
mp3.exit();
|
||||
}
|
||||
}
|
||||
198
java/src/old/List.java
Normal file
198
java/src/old/List.java
Normal file
@@ -0,0 +1,198 @@
|
||||
package old;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
import mimis.exception.worker.ActivateException;
|
||||
import mimis.worker.Worker;
|
||||
|
||||
import com.Ostermiller.util.BufferOverflowException;
|
||||
import com.Ostermiller.util.CircularByteBuffer;
|
||||
import com.Ostermiller.util.CircularObjectBuffer;
|
||||
|
||||
public class List extends Worker {
|
||||
public static final int STEP = 80; // milliseconds
|
||||
public static final int RATE = 192; // kbps
|
||||
public static final int OVERLAP = 20000; // milliseconds
|
||||
|
||||
protected File file;
|
||||
protected String[] fileArray;
|
||||
|
||||
protected int rate;
|
||||
protected int chunk;
|
||||
protected int overlap;
|
||||
protected byte[] bytes;
|
||||
protected boolean next;
|
||||
protected Mp3 mp3, nextMp3;
|
||||
|
||||
protected InputStream mp3InputStream;
|
||||
protected OutputStream audioOutputStream;
|
||||
protected CircularByteBuffer circularByteBuffer;
|
||||
protected CircularObjectBuffer<String> circularStringBuffer;
|
||||
|
||||
public List(File file) {
|
||||
this(file, RATE);
|
||||
}
|
||||
|
||||
public List(File file, int rate) {
|
||||
this.file = file;
|
||||
this.rate = rate;
|
||||
chunk = STEP * rate / 8;
|
||||
overlap = OVERLAP * RATE / 8;
|
||||
bytes = new byte[chunk];
|
||||
next = true;
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
super.exit();
|
||||
if (mp3 != null) {
|
||||
mp3.exit();
|
||||
}
|
||||
if (nextMp3 != null) {
|
||||
nextMp3.exit();
|
||||
}
|
||||
}
|
||||
|
||||
protected synchronized void activate() throws ActivateException {
|
||||
try {
|
||||
Scanner scanner = new Scanner(file);
|
||||
ArrayList<String> fileList = new ArrayList<String>();
|
||||
while (scanner.hasNextLine()) {
|
||||
fileList.add(scanner.nextLine());
|
||||
}
|
||||
if (fileList.size() > 0) {
|
||||
fileArray = fileList.toArray(new String[0]);
|
||||
|
||||
circularByteBuffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
|
||||
audioOutputStream = circularByteBuffer.getOutputStream();
|
||||
|
||||
circularStringBuffer = new CircularObjectBuffer<String>(CircularByteBuffer.INFINITE_SIZE);
|
||||
setNext();
|
||||
super.activate();
|
||||
notifyAll();
|
||||
return;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error(e);
|
||||
}
|
||||
throw new ActivateException();
|
||||
}
|
||||
|
||||
protected synchronized void work() {
|
||||
try {
|
||||
int left = chunk;
|
||||
while (left > 0) {
|
||||
/* Check for need to load next mp3 */
|
||||
int available = mp3InputStream == null ? -1 : mp3InputStream.available();
|
||||
boolean expect = mp3 == null ? false : mp3.active();
|
||||
|
||||
/* Act when no more data is expected */
|
||||
if (!expect) {
|
||||
if (available < overlap) {
|
||||
setNext();
|
||||
next = false;
|
||||
nextMp3.start();
|
||||
}
|
||||
if (available < 1) {
|
||||
swap();
|
||||
}
|
||||
}
|
||||
|
||||
/* Transfer data */
|
||||
int read = mp3InputStream.read(bytes, 0, left);
|
||||
left -= read;
|
||||
audioOutputStream.write(bytes, 0, read);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
/* Swap to next if stream has stopped */
|
||||
setNext();
|
||||
swap();
|
||||
} catch (IllegalStateException e) {
|
||||
log.error(e);
|
||||
}
|
||||
sleep(STEP);
|
||||
}
|
||||
|
||||
protected File getRandomFile() {
|
||||
return new File(fileArray[(int) (Math.random() * fileArray.length)]);
|
||||
}
|
||||
|
||||
public synchronized void setNext() {
|
||||
if (nextMp3 == null) {
|
||||
log.debug("Initialize next mp3");
|
||||
nextMp3 = new Mp3(getRandomFile(), rate);
|
||||
} else if (next) {
|
||||
log.debug("Load next mp3");
|
||||
nextMp3.setFile(getRandomFile());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void next() {
|
||||
log.debug("Stop current mp3");
|
||||
mp3.stop();
|
||||
}
|
||||
|
||||
public void swap() {
|
||||
log.debug("Swap to next mp3");
|
||||
Mp3 swapMp3 = mp3;
|
||||
mp3 = nextMp3;
|
||||
nextMp3 = swapMp3;
|
||||
next = true;
|
||||
|
||||
/* Swap stream and announce title */
|
||||
mp3InputStream = mp3.getInputStream();
|
||||
try {
|
||||
circularStringBuffer.write(mp3.getTitle());
|
||||
} catch (BufferOverflowException e) {
|
||||
log.error(e);
|
||||
} catch (IllegalStateException e) {
|
||||
log.error(e);
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized InputStream getInputStream() {
|
||||
if (circularByteBuffer == null) {
|
||||
start();
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
return circularByteBuffer.getInputStream();
|
||||
}
|
||||
|
||||
public synchronized CircularObjectBuffer<String> getMetaBuffer() {
|
||||
if (circularStringBuffer == null) {
|
||||
start();
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
return circularStringBuffer;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int rate = 192;
|
||||
List list = new List(new File("mp3"), rate);
|
||||
/*Shoutcast shoutcast = new Shoutcast(null, rate, 9876);
|
||||
shoutcast.start();
|
||||
shoutcast.setInputStream(list.getInputStream());
|
||||
shoutcast.setMetaBuffer(list.getMetaBuffer());*/
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(15000);
|
||||
list.next();
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
79
java/src/old/Mp3.java
Normal file
79
java/src/old/Mp3.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package old;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import mimis.exception.worker.ActivateException;
|
||||
|
||||
import org.farng.mp3.MP3File;
|
||||
import org.farng.mp3.TagException;
|
||||
|
||||
public class Mp3 extends Converter {
|
||||
protected File file;
|
||||
protected String title;
|
||||
|
||||
public Mp3(File file) {
|
||||
this(file, -1);
|
||||
}
|
||||
|
||||
public Mp3(File file, int targetRate) {
|
||||
super(null, targetRate);
|
||||
setFile(file);
|
||||
title = "";
|
||||
}
|
||||
|
||||
public synchronized void activate() throws ActivateException {
|
||||
/* Open file */
|
||||
try {
|
||||
sourceInputStream = new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error(e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
|
||||
/* Read ID3V2 tags */
|
||||
try {
|
||||
MP3File mp3File = new MP3File(file);
|
||||
String album = clean(mp3File.getID3v2Tag().getAlbumTitle());
|
||||
String artist = clean(mp3File.getID3v2Tag().getLeadArtist());
|
||||
String track = clean(mp3File.getID3v2Tag().getSongTitle());
|
||||
if (album.isEmpty()) {
|
||||
title = String.format("%s - %s", artist, track, album);
|
||||
} else {
|
||||
title = String.format("%s - %s {%s}", artist, track, album);
|
||||
}
|
||||
log.debug("Title: " + title);
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
} catch (TagException e) {
|
||||
log.error(e);
|
||||
}
|
||||
try {
|
||||
sourceInputStream.skip(100000);
|
||||
} catch (IOException e) {}
|
||||
super.activate();
|
||||
}
|
||||
|
||||
protected String clean(String input) {
|
||||
String output = input.replace("\0", "");
|
||||
return output.replace("<EFBFBD><EFBFBD>", "");
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Mp3 mp3 = new Mp3(new File("input.mp3"), 128);
|
||||
Utils.write(mp3.getInputStream(), new File("one.mp3"));
|
||||
mp3.setFile(new File("stream.mp3"));
|
||||
Utils.write(mp3.getInputStream(), new File("two.mp3"));
|
||||
mp3.exit();
|
||||
}
|
||||
}
|
||||
42
java/src/old/Utils.java
Normal file
42
java/src/old/Utils.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package old;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javazoom.jl.decoder.JavaLayerException;
|
||||
import javazoom.jl.player.Player;
|
||||
|
||||
public class Utils {
|
||||
public static final int BUFFER = 2048; // bytes
|
||||
|
||||
public static void play(InputStream inputStream) {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
new Player(new BufferedInputStream(inputStream)).play();
|
||||
} catch (JavaLayerException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(InputStream inputStream, File file) {
|
||||
byte[] bytes = new byte[BUFFER];
|
||||
int read = 0;
|
||||
try {
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
while ((read = inputStream.read(bytes)) > 0) {
|
||||
fileOutputStream.write(bytes, 0, read);
|
||||
}
|
||||
fileOutputStream.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user