wave混音的实现(2)

阅读更多
关于混音算法,参考的是 http://jacky-zhang.iteye.com/blog/766053,下面是具体的实现逻辑
package example.audio;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Mixing2 extends MIDlet implements CommandListener{
	private Command play1 = new Command("Play one", Command.OK, 0);
	private Command play2 = new Command("Play two", Command.OK, 1);
	private Command play3 = new Command("Play three", Command.OK, 6);
	private Command stop1 = new Command("Stop one", Command.CANCEL, 2);
	private Command stop2 = new Command("stop two", Command.CANCEL, 3);
	private Command stop3 = new Command("stop three", Command.CANCEL, 7);
	private Command playMix = new Command("play mix", Command.OK, 4);
	private Command stopRecord = new Command("stop record", Command.OK, 5);
	private Player player1;
	private Player player2;
	private Player player3;
	private Player player4;
	private String[] urls = {"/128kbps/intwap-5.wav","/128kbps/intwap-6.wav","/128kbps/intwap-7.wav"};
	private Form form = new Form("Test mixing");
	private byte[][] data = new byte[3][];
	private int[][] times = new int[3][];
	private long startTime = 0;
	private long size;
	private int idx1 = 0;
	private int idx2 = 0;
	private int idx3 = 0;
	private static final int FIRST = 1;
	private static final int SECOND = 2;
	private static final int THIRD = 3;
	private boolean start = false;
	
	public Mixing2() {}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {	}

	protected void pauseApp() {	}

	protected void startApp() throws MIDletStateChangeException {
		form.addCommand(play1);
		form.addCommand(stop1);
		form.addCommand(play2);
		form.addCommand(stop2);
		form.addCommand(play3);
		form.addCommand(stop3);
		form.addCommand(playMix);
		form.addCommand(stopRecord);
		form.setCommandListener(this);
		Display.getDisplay(this).setCurrent(form);
	}
	
	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == play1) {
			form.append("play one");
			if(startTime == 0) startTime = System.currentTimeMillis();
			new Thread(){
				public void run() {
					try {
						stopPlayer1();
						(player1 = initPlayer(FIRST)).start();
					} catch (MediaException e) {
						e.printStackTrace();
					}					
				}
			}.start();
			System.out.println("player1.start.time: "+System.currentTimeMillis());
		}
		else if (arg0 == stop1) {
			stopPlayer1();
		}
		else if (arg0 == play2) {
			form.append("play two");
			if(startTime == 0) startTime = System.currentTimeMillis();
			new Thread(){
				public void run() {
					try {
						stopPlayer2();
						(player2 = initPlayer(SECOND)).start();
					} catch (MediaException e) {
						e.printStackTrace();
					}					
				}
			}.start();
		}
		else if (arg0 == stop2) {
			stopPlayer2();
		}
		else if (arg0 == play3) {
			form.append("play three");
			if(startTime == 0) startTime = System.currentTimeMillis();
			new Thread(){
				public void run() {
					try {
						stopPlayer3();
						(player3 = initPlayer(THIRD)).start();
					} catch (MediaException e) {
						e.printStackTrace();
					}					
				}
			}.start();
		}
		else if (arg0 == stop3) {
			stopPlayer3();
		}
		else if (arg0 == playMix) {
			new Thread(){
				public void run() {
					try {
						ByteArrayOutputStream bos = new ByteArrayOutputStream();
						byte[] data = mixing();
						System.out.println("combine.................data "+data.length);
						try {
							bos.write(new WaveHeader(data.length, (short)16, 8000).getHeader());
							bos.write(data);
						} catch (IOException e) {
							e.printStackTrace();
						}
						System.out.println("ready....stop....player........ ");
						stopPlayer4();
						System.out.println("ready....mix....player........ ");
						player4 = Manager.createPlayer(new ByteArrayInputStream(bos.toByteArray()), "audio/x-wav");
						player4.prefetch();
						player4.realize();
						player4.start();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (MediaException e) {
						e.printStackTrace();
					}
					System.out.println("playing.........mix.......data ");
					for(int i=0;i= length) break;
						mixPices[i][(int) times[i][j-1]+k-44] = data[i][k];
					}
				}
			}
		}
		System.out.println("999999999999: "+(System.currentTimeMillis() - start));
		double f = 1.0f;
		int MAX = 127, MIN = -127;
		double STEPSIZE;
		for(int m=0;mMAX){
				double f0 = 127.0f/temp;
				f = f0;
				temp = MAX;
			}
			else if(temp < MIN){
				double f0 = -127.0f/temp;
				f = f0;
				temp = (byte) MIN;
			}
			if(f<1) {
				STEPSIZE = ((1.0f-f)/16);
				f += STEPSIZE;
			}
			mixData[m] = (byte) temp;
		}
		for(int n =0;n 
 
  • 128kbps.rar (1.3 MB)
  • 下载次数: 61

你可能感兴趣的:(thread,F#,算法,Blog,J#)