浏览代码

Merge pull request #163 from motion-twin/lime

Sound on lime
Nicolas Cannasse 9 年之前
父节点
当前提交
0584b598f7
共有 3 个文件被更改,包括 92 次插入6 次删除
  1. 3 2
      h2d/Text.hx
  2. 3 3
      hxd/res/Font.hx
  3. 86 1
      hxd/snd/NativeChannel.hx

+ 3 - 2
h2d/Text.hx

@@ -46,10 +46,11 @@ class Text extends Drawable {
 		if( this.font == font ) return font;
 		this.font = font;
 		#if lime
-		if( font.tile.getTexture().format == ALPHA )
+		if( font.tile.getTexture().format == ALPHA ){
 			if( waShader == null ) addShader( waShader = new h3d.shader.WhiteAlpha() );
-		else
+		}else{
 			if( waShader != null ) removeShader( waShader );
+		}
 		#end
 		if( glyphs != null ) glyphs.remove();
 		glyphs = new TileGroup(font == null ? null : font.tile, this);

+ 3 - 3
hxd/res/Font.hx

@@ -3,7 +3,9 @@ package hxd.res;
 class Font extends Resource {
 
 	public function build( size : Int, ?options ) : h2d.Font {
-		#if flash
+		#if lime
+		return FontBuilder.getFont(name, size, options);
+		#elseif flash
 		var fontClass : Class<flash.text.Font> = cast Type.resolveClass("_R_" + ~/[^A-Za-z0-9_]/g.replace(entry.path, "_"));
 		if( fontClass == null ) throw "Embeded font not found " + entry.path;
 		var font = Type.createInstance(fontClass, []);
@@ -11,8 +13,6 @@ class Font extends Resource {
 		#elseif js
 		var name = "R_" + ~/[^A-Za-z0-9_]/g.replace(entry.path, "_");
 		return FontBuilder.getFont(name, size, options);
-		#elseif lime
-		return FontBuilder.getFont(name, size, options);
 		#else
 		throw "Not implemented for this platform";
 		return null;

+ 86 - 1
hxd/snd/NativeChannel.hx

@@ -13,6 +13,82 @@ private class ChannelMapper extends sdl.SoundChannel {
 		@:privateAccess native.onSample(haxe.io.Float32Array.fromBytes(haxe.io.Bytes.ofData(data)));
 	}
 }
+#elseif lime_openal
+import lime.audio.openal.AL;
+
+private class ALChannel {
+	var native : NativeChannel;
+	var samples : Int;
+
+	var buffers : Array<Int>;
+	var src : Int;
+	
+	var fbuf : haxe.io.Bytes;
+	var ibuf : haxe.io.Bytes;
+	var iview : lime.utils.ArrayBufferView;
+
+	public function new(samples, native){
+		this.native = native;
+		this.samples = samples;
+		buffers = AL.genBuffers(2);
+		src = AL.genSource();
+		AL.sourcef(src,AL.PITCH,1.0);
+		AL.sourcef(src,AL.GAIN,1.0);
+		fbuf = haxe.io.Bytes.alloc( samples<<3 );
+		ibuf = haxe.io.Bytes.alloc( samples<<2 );
+		iview = new lime.utils.Int16Array(ibuf);
+		
+		for ( b in buffers )
+			onSample(b);
+		forcePlay();
+		lime.app.Application.current.onUpdate.add( onUpdate );
+	}
+
+	public function stop() {
+		if( src != 0 ){
+			lime.app.Application.current.onUpdate.remove( onUpdate );
+			AL.deleteSource(src);
+			AL.deleteBuffers(buffers);
+			src = 0;
+			buffers = null;
+		}
+	}
+
+	@:noDebug function onSample( buf : Int ) {
+		@:privateAccess native.onSample(haxe.io.Float32Array.fromBytes(fbuf));
+		
+		// Convert Float32 to Int16
+		#if cpp
+		var fb = fbuf.getData();
+		var ib = ibuf.getData();
+		for( i in 0...samples<<1 )
+			untyped __global__.__hxcpp_memory_set_i16( ib, i<<1, __global__.__int__(__global__.__hxcpp_memory_get_float( fb, i<<2 ) * 0x7FFF) );
+		#else
+		for ( i in 0...samples << 1 ) {
+			var v = Std.int(fbuf.getFloat(i << 2) * 0x7FFF);
+			ibuf.set( i<<1, v );
+			ibuf.set( (i<<1) + 1, v>>>8 );
+		}
+		#end
+
+		AL.bufferData(buf, AL.FORMAT_STEREO16, iview, ibuf.length, 44100);
+		AL.sourceQueueBuffers(src, 1, [buf]);
+	}
+	
+	inline function forcePlay() {
+		if( AL.getSourcei(src,AL.SOURCE_STATE) != AL.PLAYING )
+			AL.sourcePlay(src);	
+	}
+
+	function onUpdate( i : Int ){
+		var r = AL.getSourcei(src,AL.BUFFERS_PROCESSED);
+		if( r > 0 ){
+			for( b in AL.sourceUnqueueBuffers(src,r) )
+				onSample(b);
+			forcePlay();
+		}
+	}
+}
 #end
 
 class NativeChannel {
@@ -36,6 +112,8 @@ class NativeChannel {
 	var tmpBuffer : haxe.io.Float32Array;
 	#elseif hxsdl
 	var channel : ChannelMapper;
+	#elseif lime_openal
+	var channel : ALChannel;
 	#end
 	public var bufferSamples(default, null) : Int;
 
@@ -53,6 +131,8 @@ class NativeChannel {
 		sproc.onaudioprocess = onJsSample;
 		#elseif hxsdl
 		channel = new ChannelMapper(bufferSamples, this);
+		#elseif lime_openal
+		channel = new ALChannel(bufferSamples, this);
 		#end
 	}
 
@@ -99,7 +179,12 @@ class NativeChannel {
 			channel.stop();
 			channel = null;
 		}
+		#elseif lime_openal
+		if( channel != null ) {
+			channel.stop();
+			channel = null;
+		}
 		#end
 	}
 
-}
+}