浏览代码

added OGG sound format support (requires extra haxelib)

Nicolas Cannasse 10 年之前
父节点
当前提交
f960e39db8
共有 4 个文件被更改,包括 49 次插入2 次删除
  1. 1 1
      hxd/res/FileTree.hx
  2. 6 0
      hxd/res/Sound.hx
  3. 41 0
      hxd/snd/OggData.hx
  4. 1 1
      hxd/snd/Worker.hx

+ 1 - 1
hxd/res/FileTree.hx

@@ -341,7 +341,7 @@ class FileTree {
 			return { e : macro loader.loadFont($epath), t : macro : hxd.res.Font };
 		case "fnt":
 			return { e : macro loader.loadBitmapFont($epath), t : macro : hxd.res.BitmapFont };
-		case "wav", "mp3":
+		case "wav", "mp3", "ogg":
 			return { e : macro loader.loadSound($epath), t : macro : hxd.res.Sound };
 		case "tmx":
 			return { e : macro loader.loadTiledMap($epath), t : macro : hxd.res.TiledMap };

+ 6 - 0
hxd/res/Sound.hx

@@ -15,6 +15,12 @@ class Sound extends Resource {
 			data = new hxd.snd.WavData(bytes);
 		case 255, 'I'.code: // MP3 (or ID3)
 			data = new hxd.snd.Mp3Data(bytes);
+		case 'O'.code: // Ogg (vorbis)
+			#if stb_ogg_sound
+			data = new hxd.snd.OggData(bytes);
+			#else
+			throw "OGG format requires -lib stb_ogg_sound (for " + entry.path+")";
+			#end
 		default:
 		}
 		if( data == null )

+ 41 - 0
hxd/snd/OggData.hx

@@ -0,0 +1,41 @@
+package hxd.snd;
+
+private class BytesOutput extends haxe.io.Output {
+	public var bytes : haxe.io.Bytes;
+	public var position : Int;
+	public function new() {
+	}
+	#if flash
+	override function writeFloat(f) {
+		bytes.setFloat(position, f);
+		position += 4;
+	}
+	#else
+	override function writeInt32(v) {
+		bytes.setInt32(position, v);
+		position += 4;
+	}
+	#end
+}
+
+class OggData extends Data {
+
+	var reader : stb.format.vorbis.Reader;
+	var output : BytesOutput;
+
+	public function new( bytes : haxe.io.Bytes ) {
+		reader = stb.format.vorbis.Reader.openFromBytes(bytes);
+		samples = reader.totalSample;
+		output = new BytesOutput();
+		trace(samples);
+	}
+
+	override public function decode(out:haxe.io.Bytes, outPos:Int, sampleStart:Int, sampleCount:Int) {
+		reader.currentSample = sampleStart;
+		output.bytes = out;
+		output.position = outPos;
+		reader.read(output, sampleCount, 2, 44100, true);
+	}
+
+
+}

+ 1 - 1
hxd/snd/Worker.hx

@@ -262,7 +262,7 @@ class Worker extends hxd.Worker<Message> {
 					}
 				}
 				if( c.position == 0 ) {
-					if( !c.loop ) {
+					if( !c.loop && c.next == null ) {
 						chan.channels.remove(c);
 						cmap.remove(c.id);
 						cid--;