* moved "binPlayers" into libEmbedVideo (more modular folder layout)
[lhc/web/wiklou.git] / js2 / mwEmbed / libEmbedVideo / binPlayers / omtk-fx / src / as / org / omtk / vorbis / VorbisSound.as
1 /*
2
3 Copyright 2008 Tor-Einar Jarnbjo
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */
18
19 package org.omtk.vorbis {
20
21 import flash.net.URLRequest;
22 import flash.net.URLStream;
23 import flash.utils.ByteArray;
24 import flash.utils.Endian;
25 import flash.events.Event;
26 import flash.events.ProgressEvent;
27 import flash.events.SampleDataEvent;
28
29 import flash.external.ExternalInterface;
30
31 import flash.media.Sound;
32
33 import org.omtk.ogg.UncachedUrlStream;
34 import org.omtk.ogg.EndOfOggStreamError;
35 import flash.utils.setTimeout;
36
37 public class VorbisSound extends Sound {
38
39 public static var METADATA_UPDATE: String = "metadata_update";
40
41 private var urlStream: URLStream;
42
43 private var oggStream:UncachedUrlStream;
44 private var vorbisStream:VorbisStream;
45
46 private var bytesAvailable:int;
47
48 private var initialized:Boolean = false;
49 private var playing:Boolean = false;
50
51 private var fill1:Boolean = true;
52 private var fill2:Boolean = true;
53
54 private var stopped: Boolean = false;
55 private var completeEventDispatched: Boolean = false;
56
57 public function VorbisSound(url: URLRequest ) {
58 urlStream = new URLStream();
59 urlStream.endian = Endian.LITTLE_ENDIAN;
60 urlStream.load(url);
61
62 Mdct.initialize();
63
64 oggStream = new UncachedUrlStream(urlStream);
65 oggStream.addEventListener('progress', progress);
66 bytesAvailable = oggStream.bytesAvailable;
67 addEventListener("sampleData", sampleGenerator);
68 }
69
70 private function initialize():void {
71 vorbisStream = new VorbisStream(oggStream.getLogicalOggStream());
72 setTimeout(dispatchEvent, 100, new Event(METADATA_UPDATE));
73 initialized = true;
74 }
75
76 private var samplesPlayed: int = 0;
77
78 private function sampleGenerator(event:SampleDataEvent):void {
79
80 if(stopped) {
81 return;
82 }
83
84 if(Mdct.initialized && !initialized && bytesAvailable > 64*1024) {
85 initialize();
86 }
87
88 if(initialized && bytesAvailable > 16*1024 && !vorbisStream.finished) {
89 var cnt: int;
90 cnt = vorbisStream.readPcm(event.data);
91 samplesPlayed += cnt;
92 if(cnt < 2048 && oggStream.bytesAvailable > 0) {
93 vorbisStream = new VorbisStream(oggStream.getLogicalOggStream());
94 setTimeout(dispatchEvent, 100, new Event(METADATA_UPDATE));
95 samplesPlayed += vorbisStream.readPcm(event.data);
96 }
97 }
98 else if(vorbisStream == null || !vorbisStream.finished) {
99 for(var c:int=0; c<2048; c++) {
100 event.data.writeFloat(0);
101 event.data.writeFloat(0);
102 }
103 }
104
105 if(initialized && vorbisStream.finished && oggStream.bytesAvailable == 0 && !completeEventDispatched) {
106 dispatchEvent(new Event(Event.COMPLETE));
107 completeEventDispatched = true;
108 }
109
110 //trace("samples played: " + samplesPlayed + "/" + oggStream.bytesAvailable);
111
112 }
113
114 public function stop(): void {
115 stopped = true;
116 }
117
118 public function progress(event:ProgressEvent):void {
119 bytesAvailable = oggStream.bytesAvailable;
120 }
121
122 public function get position(): int {
123 return samplesPlayed * 1000 / 44100;
124 }
125
126 public function getMetaData(key: String):String {
127 if(vorbisStream != null) {
128 return vorbisStream.commentHeader.comments[key];
129 }
130 else {
131 return null;
132 }
133 }
134 }
135
136 }