1 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 20 21 private MP3Helper mp3; 22 23 24 25 private int idealSamplesPerBlock = 0; 26 27 28 29 private int frameRate = 0; 30 31 32 33 private int samplesCompleted = 0; 34 35 36 37 private int blocksCompleted = 0; 38 39 40 41 private byte[] firstFrameCache = null; 42 43 44 private int dataSize = 0; 45 46 47 48 public static SoundStreamBuilder newSoundStreamBuilder( IVUrl url, int rate ) throws IOException, IVException 49 { 50 return newSoundStreamBuilder( Util.readUrl( url ), rate ); 51 } 52 53 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 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 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 91 o.streamSampleCount = idealSamplesPerBlock = ( mp3.getFrequency() / frameRate ); 92 93 return o; 94 } 95 96 97 98 public SoundStreamBlock getNextSoundStreamBlock() throws IOException, IVException 99 { 100 byte [] buffer; 101 int sampleCount = 0; 102 103 ByteArrayOutputStream out = new ByteArrayOutputStream(); 104 105 108 blocksCompleted++; 109 110 113 while ( ( samplesCompleted / blocksCompleted ) < idealSamplesPerBlock ) 114 { 115 117 if ( firstFrameCache == null ) 118 { 119 buffer = mp3.nextFrame(); 120 } 121 else 122 { 123 buffer = firstFrameCache; 124 125 firstFrameCache = null; 126 } 127 128 130 if ( buffer == null ) 131 { 132 break; 133 } 134 135 137 out.write( buffer, 0, buffer.length ); 138 139 samplesCompleted += mp3.getSamplesPerFrame(); 140 sampleCount += mp3.getSamplesPerFrame(); 141 } 142 143 145 if ( sampleCount == 0 ) 146 { 147 return null; 148 } 149 150 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 165 public int getSize() { 166 return dataSize; 167 } 168 169 } 170 | Popular Tags |