KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > sound > MP3Sound


1 /*
2  * $Id: MP3Sound.java,v 1.2 2002/02/15 23:44:28 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  */

7
8 package org.openlaszlo.iv.flash.api.sound;
9
10 import org.openlaszlo.iv.flash.parser.*;
11 import org.openlaszlo.iv.flash.util.*;
12 import org.openlaszlo.iv.flash.url.*;
13 import org.openlaszlo.iv.flash.api.*;
14
15 import java.io.*;
16
17 /**
18  * A sound whoose SOUNDDATA is in mp3 format
19  *
20  * @author James Taylor
21  *
22  */

23
24 public class MP3Sound extends Sound
25 {
26     public int delaySeek;
27     public DataMarker data;
28
29     /** Create MP3Sound from url */
30
31     public static MP3Sound newMP3Sound( IVUrl url ) throws IVException, IOException
32     {
33         return newMP3Sound( Util.readUrl( url ) );
34     }
35
36     /** Create MP3Sound from buffer */
37
38     public static MP3Sound newMP3Sound( FlashBuffer fob ) throws IVException, IOException
39     {
40         byte [] buffer;
41
42         // MP3Helper will load the MP3 from the url -- FIXME: need to consider the media cache here
43

44         MP3Helper mp3 = new MP3Helper( fob );
45
46         // Read the mp3 frames and put the into an array in memory. Must read
47
// before extracting properties.
48

49         FlashBuffer fb = new FlashBuffer( fob.getSize() );
50
51         while ( ( buffer = mp3.nextFrame() ) != null )
52         {
53             fb.writeArray( buffer, 0, buffer.length );
54         }
55
56         MP3Sound sound = new MP3Sound();
57
58         sound.compressFormat = COMPRESS_MP3;
59
60         // Set the rate from the MP3's frequency
61

62         switch ( mp3.getFrequency() )
63         {
64             case 11025: sound.rate = RATE_11; break;
65             case 22050: sound.rate = RATE_22; break;
66             case 44100: sound.rate = RATE_44; break;
67         }
68
69         // MP3's are always 16 bit
70

71         sound.isSample16bit = true;
72
73         // Stereo includes anything that isn't mono ( joint stereo, dual mono )
74

75         sound.isStereo = mp3.getStereo();
76
77         // Sample count as determined for just the frames read
78

79         sound.sampleCount = mp3.getSamples();
80
81         // Default the delay seek to 0 until a better idea comes along
82

83         sound.delaySeek = 0;
84
85         // Datamarker containing just valid MP3 frames
86
// no copying here!
87
sound.data = new DataMarker( fb.getBuf(), 0, fb.getSize() );
88
89         return sound;
90     }
91
92     public int getSize() {
93         return data.buffer.length;
94     }
95
96     /** Write MP3Sound to output buffer */
97
98     public void write( FlashOutput fob )
99     {
100         fob.writeTag( getTag(), 2 + 1 + 4 + 2 + data.length() );
101         fob.writeDefID( this );
102         fob.initBits();
103         fob.writeBits( compressFormat, 4 );
104         fob.writeBits( rate, 2 );
105         fob.writeBool( isSample16bit );
106         fob.writeBool( isStereo );
107         fob.flushBits();
108         fob.writeDWord( sampleCount );
109         fob.writeWord( delaySeek ); // Only addition for MP3SOUNDDATA
110
data.write(fob);
111     }
112
113     /** Copies this sound into the provided item ( must be an MP3Sound ) */
114
115     protected FlashItem copyInto( FlashItem item, ScriptCopier copier )
116     {
117         super.copyInto( item, copier );
118
119         ( ( MP3Sound ) item ).delaySeek = delaySeek;
120         ( ( MP3Sound ) item ).data = ( DataMarker ) data.getCopy();
121
122         return item;
123     }
124
125     /** Duplicates this sound */
126
127     public FlashItem getCopy( ScriptCopier copier )
128     {
129         return copyInto( new MP3Sound(), copier );
130     }
131
132     /** DelaySeek accessor */
133
134     public int getDelaySeek() { return delaySeek; }
135
136     /** DelaySeek mutator */
137
138     public void setDelaySeek( int val ) { delaySeek = val; }
139 }
140
Popular Tags