KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: SoundStreamBuilder.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.api.*;
13 import org.openlaszlo.iv.flash.url.*;
14
15 import java.io.*;
16
17 public class SoundStreamBuilder
18 {
19     /** Holds the MP3 samples are read from */
20
21     private MP3Helper mp3;
22
23     /** Calculated ideal samples per block: sampleRate / frameRate */
24
25     private int idealSamplesPerBlock = 0;
26
27     /** Frame rate of containing file */
28
29     private int frameRate = 0;
30
31     /** Total samples completed so far */
32
33     private int samplesCompleted = 0;
34
35     /** Total sound blocks completed so far */
36
37     private int blocksCompleted = 0;
38
39     /** Saves us the trouble of mark and reset */
40
41     private byte[] firstFrameCache = null;
42
43     /** Data size (skavish 05/03/2001) */
44     private int dataSize = 0;
45
46     /** Create SoundStreamBuilder from url and frame rate */
47
48     public static SoundStreamBuilder newSoundStreamBuilder( IVUrl url, int rate ) throws IOException, IVException
49     {
50         return newSoundStreamBuilder( Util.readUrl( url ), rate );
51     }
52
53     /** Create a SoundStreamBuilder for the provided buffer and frame rate */
54
55     public static SoundStreamBuilder newSoundStreamBuilder( FlashBuffer fob, int rate ) throws IOException, IVException
56     {
57         SoundStreamBuilder o = new SoundStreamBuilder();
58
59         o.mp3 = new MP3Helper( fob );
60         o.frameRate = rate >> 8;
61         o.dataSize = fob.getSize();
62
63         return o;
64     }
65
66     /** Get the SoundStreamHead object for this sound */
67
68     public SoundStreamHead getSoundStreamHead() throws IOException, IVException
69     {
70         SoundStreamHead o = new SoundStreamHead();
71
72         firstFrameCache = mp3.nextFrame();
73
74         o.streamCompression = Sound.COMPRESS_MP3;
75
76         // Set the rate from the MP3's frequency
77

78         switch ( mp3.getFrequency() )
79         {
80             case 11025: o.playbackRate = o.streamRate = Sound.RATE_11; break;
81             case 22050: o.playbackRate = o.streamRate = Sound.RATE_22; break;
82             case 44100: o.playbackRate = o.streamRate = Sound.RATE_44; break;
83         }
84
85         o.isPlayback16bit = o.isStream16bit = true;
86
87         o.isPlaybackStereo = o.isStreamStereo = mp3.getStereo();
88
89         // This can only be set once a block has been read
90

91         o.streamSampleCount = idealSamplesPerBlock = ( mp3.getFrequency() / frameRate );
92
93         return o;
94     }
95
96     /** Get the next StreamSoundBlock for this sound */
97
98     public SoundStreamBlock getNextSoundStreamBlock() throws IOException, IVException
99     {
100         byte [] buffer;
101         int sampleCount = 0;
102
103         ByteArrayOutputStream out = new ByteArrayOutputStream();
104
105         // Increment the blocks completed upfront, so the division in the
106
// while loop works nicely.
107

108         blocksCompleted++;
109
110         // Add frames to the ouput buffer while the average samples per block
111
// completed does not exceed the ideal.
112

113         while ( ( samplesCompleted / blocksCompleted ) < idealSamplesPerBlock )
114         {
115             // Get the next frame ( unless one is already cached )
116

117             if ( firstFrameCache == null )
118             {
119                 buffer = mp3.nextFrame();
120             }
121             else
122             {
123                 buffer = firstFrameCache;
124
125                 firstFrameCache = null;
126             }
127
128             // End loop prematurely if there are no more frames
129

130             if ( buffer == null )
131             {
132                 break;
133             }
134
135             // Write the frame to the output buffer
136

137             out.write( buffer, 0, buffer.length );
138
139             samplesCompleted += mp3.getSamplesPerFrame();
140             sampleCount += mp3.getSamplesPerFrame();
141         }
142
143         // If no samples are in the buffer at this point, there must be no more sound
144

145         if ( sampleCount == 0 )
146         {
147             return null;
148         }
149
150         // If all has gone well, build the new block and return it
151

152         MP3SoundStreamBlock o = new MP3SoundStreamBlock();
153
154         o.sampleCount = sampleCount;
155         o.delaySeek = 0;
156
157         o.data = new DataMarker( out.toByteArray(), 0, out.size() );
158
159         return o;
160     }
161
162     /**
163      * Return data size
164      */

165     public int getSize() {
166         return dataSize;
167     }
168
169 }
170
Popular Tags