001: /*
002: * $Id: MP3Sound.java,v 1.2 2002/02/15 23:44:28 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: */
007:
008: package org.openlaszlo.iv.flash.api.sound;
009:
010: import org.openlaszlo.iv.flash.parser.*;
011: import org.openlaszlo.iv.flash.util.*;
012: import org.openlaszlo.iv.flash.url.*;
013: import org.openlaszlo.iv.flash.api.*;
014:
015: import java.io.*;
016:
017: /**
018: * A sound whoose SOUNDDATA is in mp3 format
019: *
020: * @author James Taylor
021: *
022: */
023:
024: public class MP3Sound extends Sound {
025: public int delaySeek;
026: public DataMarker data;
027:
028: /** Create MP3Sound from url */
029:
030: public static MP3Sound newMP3Sound(IVUrl url) throws IVException,
031: IOException {
032: return newMP3Sound(Util.readUrl(url));
033: }
034:
035: /** Create MP3Sound from buffer */
036:
037: public static MP3Sound newMP3Sound(FlashBuffer fob)
038: throws IVException, IOException {
039: byte[] buffer;
040:
041: // MP3Helper will load the MP3 from the url -- FIXME: need to consider the media cache here
042:
043: MP3Helper mp3 = new MP3Helper(fob);
044:
045: // Read the mp3 frames and put the into an array in memory. Must read
046: // before extracting properties.
047:
048: FlashBuffer fb = new FlashBuffer(fob.getSize());
049:
050: while ((buffer = mp3.nextFrame()) != null) {
051: fb.writeArray(buffer, 0, buffer.length);
052: }
053:
054: MP3Sound sound = new MP3Sound();
055:
056: sound.compressFormat = COMPRESS_MP3;
057:
058: // Set the rate from the MP3's frequency
059:
060: switch (mp3.getFrequency()) {
061: case 11025:
062: sound.rate = RATE_11;
063: break;
064: case 22050:
065: sound.rate = RATE_22;
066: break;
067: case 44100:
068: sound.rate = RATE_44;
069: break;
070: }
071:
072: // MP3's are always 16 bit
073:
074: sound.isSample16bit = true;
075:
076: // Stereo includes anything that isn't mono ( joint stereo, dual mono )
077:
078: sound.isStereo = mp3.getStereo();
079:
080: // Sample count as determined for just the frames read
081:
082: sound.sampleCount = mp3.getSamples();
083:
084: // Default the delay seek to 0 until a better idea comes along
085:
086: sound.delaySeek = 0;
087:
088: // Datamarker containing just valid MP3 frames
089: // no copying here!
090: sound.data = new DataMarker(fb.getBuf(), 0, fb.getSize());
091:
092: return sound;
093: }
094:
095: public int getSize() {
096: return data.buffer.length;
097: }
098:
099: /** Write MP3Sound to output buffer */
100:
101: public void write(FlashOutput fob) {
102: fob.writeTag(getTag(), 2 + 1 + 4 + 2 + data.length());
103: fob.writeDefID(this );
104: fob.initBits();
105: fob.writeBits(compressFormat, 4);
106: fob.writeBits(rate, 2);
107: fob.writeBool(isSample16bit);
108: fob.writeBool(isStereo);
109: fob.flushBits();
110: fob.writeDWord(sampleCount);
111: fob.writeWord(delaySeek); // Only addition for MP3SOUNDDATA
112: data.write(fob);
113: }
114:
115: /** Copies this sound into the provided item ( must be an MP3Sound ) */
116:
117: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
118: super .copyInto(item, copier);
119:
120: ((MP3Sound) item).delaySeek = delaySeek;
121: ((MP3Sound) item).data = (DataMarker) data.getCopy();
122:
123: return item;
124: }
125:
126: /** Duplicates this sound */
127:
128: public FlashItem getCopy(ScriptCopier copier) {
129: return copyInto(new MP3Sound(), copier);
130: }
131:
132: /** DelaySeek accessor */
133:
134: public int getDelaySeek() {
135: return delaySeek;
136: }
137:
138: /** DelaySeek mutator */
139:
140: public void setDelaySeek(int val) {
141: delaySeek = val;
142: }
143: }
|