`
jacky-zhang
  • 浏览: 310407 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

wave混音的实现(2)

    博客分类:
  • j2me
阅读更多
关于混音算法,参考的是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<times.length;i++){
						times[i] = null;
					}
					startTime = 0;
					idx1 = idx2 = idx3 = 0;
				};
			}.start();
		}
		else if (arg0 == stopRecord) {
			stopPlayer1();
			stopPlayer2();
			stopPlayer3();
			System.gc();
		}
	}
	
	private void saveToDisk(){
//		FileConnection file = (FileConnection)Connector.open("file://localhost/" + );
	}
	
	private byte[] mixing(){
		size = (System.currentTimeMillis() - startTime) * 16;
		long start = System.currentTimeMillis();
		byte[] mixData = new byte[(int) size];
		byte[][] mixPices = new byte[times.length][];
		System.out.println("111111111111: "+size);
		for(int i=0; i< times.length;i++){
			if(times[i]==null) continue;
			mixPices[i] = new byte[(int) size];
			int length = data[i].length-44;
			for(int j =0; j<times[i].length;j++){
				if(times[i][j]==0)break;
				System.out.println("player"+i+".time: "+times[i][j]);
				if((j+1)%2==0)System.out.println((times[i][j]-times[i][j-1]));
				if((j+1)%2==0) {
					int max = times[i][j]-times[i][j-1];
					for (int k = 44; k < max; k++){
						if(k >= 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;m<mixData.length;m++){
			mixData[m] = 0;
			double temp = 0;
			for(int n =0;n<mixPices.length;n++){
				if(mixPices[n]==null) continue;
				temp += mixPices[n][m];
			}
			if(temp==0) continue;
			temp = temp * f;
			if(temp>MAX){
				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<mixPices.length;n++){
			mixPices[n] = null;
		}
		mixPices = null;
		System.out.println("mix cost time: "+(System.currentTimeMillis()-start));
		return mixData;
	}
	
	private Player initPlayer(int p){
		byte[] temp = null;
		PlayerListener plistener = null;
		if( p == FIRST) {
			if (data[0]==null) {
				data[0] = getWaveData(urls[0]);
			}
			temp = data[0];
			plistener = p1;
		}
		else if(p==SECOND) {
			if(data[1]==null){
				data[1] = getWaveData(urls[1]);
			}
			temp = data[1];
			plistener = p2;
		}
		else if(p==THIRD) {
			if(data[2]==null){
				data[2] = getWaveData(urls[2]);
			}
			temp = data[2];
			plistener = p3;
		}
		Player player = null;
		try {
			InputStream is = new ByteArrayInputStream(temp);
			player = Manager.createPlayer(is, "audio/x-wav");
			player.prefetch();
			player.realize();
			player.addPlayerListener(plistener);
//			player.setLoopCount(-1);
		} catch (MediaException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return player;
	}
	
	private void stopPlayer1(){
		if(player1!=null && player1.getState() != Player.CLOSED){
			form.append("stop one");
			try {
				player1.stop();
				player1.close();
				player1 = null;
//				System.gc();
			} catch (MediaException e) {
				e.printStackTrace();
			}			
		}
	}
	
	private void stopPlayer2(){
		if(player2!=null && player2.getState() != Player.CLOSED){
			form.append("stop two");
			try {
				player2.stop();
				player2.close();
				player2 = null;
//				System.gc();
			} catch (MediaException e) {
				e.printStackTrace();
			}			
		}
	}
	
	private void stopPlayer3(){
		if(player3!=null && player3.getState() != Player.CLOSED){
			form.append("stop three");
			try {
				player3.stop();
				player3.close();
				player3 = null;
//				System.gc();
			} catch (MediaException e) {
				e.printStackTrace();
			}			
		}
	}
	private void stopPlayer4(){
		if(player4!=null && player4.getState() != Player.CLOSED){
			form.append("stop mix");
			try {
				player4.stop();
				player4.close();
				player4 = null;
//				System.gc();
			} catch (MediaException e) {
				e.printStackTrace();
			}			
		}
	}
	
	PlayerListener p1 = new PlayerListener(){

		public void playerUpdate(Player arg0, String arg1, Object arg2) {
			if(arg1.equals(PlayerListener.END_OF_MEDIA)){
				System.out.println("end of media");
//				System.out.println(System.currentTimeMillis());
				times[0][idx1] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx1++;
				System.out.println("  player1.cost.time: "+arg0.getMediaTime());
			}
			else if(arg1.equals(PlayerListener.STOPPED)){
				System.out.println("stopped");
				times[0][idx1] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx1++;
//				System.out.println(System.currentTimeMillis());
			}
			else if(arg1.equals(PlayerListener.STARTED)){
				System.out.println("started");
				if(times[0] == null) times[0] = new int[100];
				times[0][idx1] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx1++;
				System.out.println(System.currentTimeMillis());
			}
		}
		
	};
	
	PlayerListener p2 = new PlayerListener(){

		public void playerUpdate(Player arg0, String arg1, Object arg2) {
			if(arg1.equals(PlayerListener.STARTED)){
				System.out.println("started");
				if(times[1] == null) times[1] = new int[100];
				times[1][idx2] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx2++;
//				System.out.println(System.currentTimeMillis());
			}
			else if(arg1.equals(PlayerListener.STOPPED)){
				System.out.println("stopped");
				times[1][idx2] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx2++;
//				System.out.println(System.currentTimeMillis());
			}
			else if(arg1.equals(PlayerListener.END_OF_MEDIA)){
				System.out.println(player2.getState());
				times[1][idx2] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx2++;
			}
		}
		
	};

	PlayerListener p3 = new PlayerListener(){

		public void playerUpdate(Player arg0, String arg1, Object arg2) {
			if(arg1.equals(PlayerListener.STARTED)){
				System.out.println("started");
				if(times[2] == null) times[2] = new int[100];
				times[2][idx3] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx3++;
//				System.out.println(System.currentTimeMillis());
			}
			else if(arg1.equals(PlayerListener.STOPPED)){
				System.out.println("stopped");
				times[2][idx3] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx3++;
//				System.out.println(System.currentTimeMillis());
			}
			else if(arg1.equals(PlayerListener.END_OF_MEDIA)){
				System.out.println(player3.getState());
				times[2][idx3] = (int) ((System.currentTimeMillis() - startTime) * 16);
				idx3++;
			}
		}
		
	};
	private byte[] getWaveData(String url){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		InputStream is = getClass().getResourceAsStream(url);
//		int m = 0;
		byte[] d = new byte[2048];
		while(true){
			try {
				int t = is.read(d);
//				m = is.read();
				if(t==-1) break;
				bos.write(d);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		byte[] data = bos.toByteArray();
		System.out.println(data.length);
		try {
			is.close();
			bos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

}
分享到:
评论

相关推荐

    Cool_Edit_Pro_2.1教程.txt

    &lt;一&gt;:Cool Edit Pro是一个集录音、混音、编辑于一体的多轨数字音频编辑软件。 这里我们以2.1版本为例介绍一下在一般制作编辑音频时经常用到的功能的使用方法。 先安装Cool Edit Pro 1.2(下简称“CE”),然后...

    Mplay.jl:具有基于OpenGL的GUI的Julia MIDI混音器播放器

    但是,使用Roland Sound Canvas VA软件合成器可以达到最佳效果: 亮点: 全功能MIDI播放器带有静音和独奏选项的混音器能够更改通道参数(延迟,合唱,混响,声像) 音量滑杆更改GM乐器的声音MIDI VU计显示音符,...

    计算机应用基础教案处理音频和视频.doc

    【提示】1、录制自己的声音(连接好麦克风、设置麦克风属性、在Goldwave软件中新建 一文件、开始录音) 2、合成声音(复制录制的声音、混音合成) 教师总结评价 第二课时 任务二:MV版动感相册 【任务描述】通过...

    WaveEditor Pro 1.71.apk

    •可选的波形颜色,可实现更好的立体可视化 •独立的音频格式转换实用程序 效果 •破碎机(专业版) •延迟(专业版) •失真(专业版) •移相器(专业版) •混响(专业版) •滤波器 •图形均衡器 •参数均衡器...

    VB编程资源大全(源码 多媒体)

    这个范例获得Windows下所有的wave输入输出设备的名称以及属性(22KB) 7,agent1.ZIP Microsoft Agent范例程序,你的系统中必须已经安装了Agent2.0以上版本(2KB) 8,sound_cap.ZIP 利用DirectSound和MS ...

    Visual C++ 编程资源大全(源码 多媒体2)

    tigerplay.zip 添翼虎MP3播放器及界面实现源程序 (1.2M VC 作者:添翼虎)(1228KB)&lt;END&gt;&lt;br&gt;6,ctt.zip 添翼虎打字测试软件及源程序 (44k C 作者:添翼虎)(44KB)&lt;END&gt;&lt;br&gt;7,quickcd.zip 小型CD播放器及源程序...

    VB编程资源大全(控件 多媒体)

    很方便地控制,控件也很小巧(15KB) 11,m002.zip 两个控件,一个用来实现wave与mp3之间的相互转换,另一个用来播放mp3。(包括示例程序)Cool!(269KB) 12,m001.zip 形状类似于收音机音量控制的滚动条(296...

    Chance:特定应用的视频生成器

    所有音频文件必须具有相同的采样率(该程序具有“内部”混音器,这是非常原始的)。 仅支持 Wave 文件。 配置 不支持注释,因为 JSON 规范不支持它们,而且我还没有实现另一种语法。 (看着 HOCON ..) 依赖关系 ...

Global site tag (gtag.js) - Google Analytics