3 Copyright 2008 Tor-Einar Jarnbjo
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.omtk.vorbis {
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;
29 import flash.external.ExternalInterface;
31 import flash.media.Sound;
33 import org.omtk.ogg.UncachedUrlStream;
34 import org.omtk.ogg.EndOfOggStreamError;
35 import flash.utils.setTimeout;
37 public class VorbisSound extends Sound {
39 public static var METADATA_UPDATE: String = "metadata_update";
41 private var urlStream: URLStream;
43 private var oggStream:UncachedUrlStream;
44 private var vorbisStream:VorbisStream;
46 private var bytesAvailable:int;
48 private var initialized:Boolean = false;
49 private var playing:Boolean = false;
51 private var fill1:Boolean = true;
52 private var fill2:Boolean = true;
54 private var stopped: Boolean = false;
55 private var completeEventDispatched: Boolean = false;
57 public function VorbisSound(url: URLRequest ) {
58 urlStream = new URLStream();
59 urlStream.endian = Endian.LITTLE_ENDIAN;
64 oggStream = new UncachedUrlStream(urlStream);
65 oggStream.addEventListener('progress', progress);
66 bytesAvailable = oggStream.bytesAvailable;
67 addEventListener("sampleData", sampleGenerator);
70 private function initialize():void {
71 vorbisStream = new VorbisStream(oggStream.getLogicalOggStream());
72 setTimeout(dispatchEvent, 100, new Event(METADATA_UPDATE));
76 private var samplesPlayed: int = 0;
78 private function sampleGenerator(event:SampleDataEvent):void {
84 if(Mdct.initialized && !initialized && bytesAvailable > 64*1024) {
88 if(initialized && bytesAvailable > 16*1024 && !vorbisStream.finished) {
90 cnt = vorbisStream.readPcm(event.data);
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);
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);
105 if(initialized && vorbisStream.finished && oggStream.bytesAvailable == 0 && !completeEventDispatched) {
106 dispatchEvent(new Event(Event.COMPLETE));
107 completeEventDispatched = true;
110 //trace("samples played: " + samplesPlayed + "/" + oggStream.bytesAvailable);
114 public function stop(): void {
118 public function progress(event:ProgressEvent):void {
119 bytesAvailable = oggStream.bytesAvailable;
122 public function get position(): int {
123 return samplesPlayed * 1000 / 44100;
126 public function getMetaData(key: String):String {
127 if(vorbisStream != null) {
128 return vorbisStream.commentHeader.comments[key];