Source code: com/anotherbigidea/flash/structs/SoundInfo.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.structs;
35
36 import java.io.*;
37 import com.anotherbigidea.io.*;
38
39 /**
40 * A Sound Information structure - defines playback style and envelope
41 */
42 public class SoundInfo
43 {
44 /**
45 * A Point in a sound envelope
46 */
47 public static class EnvelopePoint
48 {
49 public int mark44;
50 public int level0;
51 public int level1;
52
53 public EnvelopePoint( int mark44, int level0, int level1 )
54 {
55 this.mark44 = mark44;
56 this.level0 = level0;
57 this.level1 = level1;
58 }
59 }
60
61 protected boolean noMultiplePlay; //only one instance can play at a time
62 protected boolean stopPlaying;
63
64 protected EnvelopePoint[] envelope;
65 protected int inPoint;
66 protected int outPoint;
67 protected int loopCount;
68
69 /**
70 * @param noMultiplePlay true = only play if not already playing
71 * @param stopSound true = stop playing the sound
72 * @param envelope may be null or empty for no envelope
73 * @param inPoint -1 for no in-point
74 * @param outPoint -1 for no out-point
75 * @param loopCount >1 for a loop count
76 */
77 public SoundInfo( boolean noMultiplePlay, boolean stopSound,
78 EnvelopePoint[] envelope,
79 int inPoint, int outPoint, int loopCount )
80 {
81 this.noMultiplePlay = noMultiplePlay;
82 this.stopPlaying = stopSound;
83 this.envelope = envelope;
84 this.inPoint = inPoint;
85 this.outPoint = outPoint;
86 this.loopCount = loopCount;
87 }
88
89 public boolean isNoMultiplePlay() { return this.noMultiplePlay; }
90 public boolean isStopPlaying() { return this.stopPlaying; }
91 public EnvelopePoint[] getEnvelope() { return this.envelope; }
92 public int getInPoint() { return this.inPoint; }
93 public int getOutPoint() { return this.outPoint; }
94 public int getLoopCount() { return this.loopCount; }
95
96 public SoundInfo( InStream in ) throws IOException
97 {
98 int flags = in.readUI8();
99
100 noMultiplePlay = ( (flags & 16) != 0 );
101 stopPlaying = ( (flags & 32) != 0 );
102 boolean hasEnvelope = ( (flags & 8) != 0 );
103 boolean hasLoops = ( (flags & 4) != 0 );
104 boolean hasOutPoint = ( (flags & 2) != 0 );
105 boolean hasInPoint = ( (flags & 1) != 0 );
106
107 if( hasInPoint ) inPoint = (int)in.readUI32();
108 else inPoint = -1;
109
110 if( hasOutPoint ) outPoint = (int)in.readUI32();
111 else outPoint = -1;
112
113 if( hasLoops ) loopCount = in.readUI16();
114 else loopCount = 1;
115
116 int envsize = 0;
117 if( hasEnvelope ) envsize = in.readUI8();
118
119 envelope = new EnvelopePoint[ envsize ];
120
121 for( int i = 0; i < envsize; i++ )
122 {
123 envelope[i] = new EnvelopePoint( (int)in.readUI32(),
124 in.readUI16(),
125 in.readUI16() );
126 }
127 }
128
129 public void write( OutStream out ) throws IOException
130 {
131 int flags = 0;
132 if( noMultiplePlay ) flags += 1;
133 if( stopPlaying ) flags += 2;
134
135 out.writeUBits( 4, flags );
136
137 boolean hasEnvelope = (envelope != null && envelope.length > 0);
138 boolean hasLoops = ( loopCount > 1 );
139 boolean hasOutPoint = ( outPoint >= 0 );
140 boolean hasInPoint = ( inPoint >= 0 );
141
142 flags = 0;
143 if( hasEnvelope ) flags += 8;
144 if( hasLoops ) flags += 4;
145 if( hasOutPoint ) flags += 2;
146 if( hasInPoint ) flags += 1;
147
148 out.writeUBits( 4, flags );
149
150 if( hasInPoint ) out.writeUI32( inPoint );
151 if( hasOutPoint ) out.writeUI32( outPoint );
152 if( hasLoops ) out.writeUI16( loopCount );
153
154 if( hasEnvelope )
155 {
156 out.writeUI8( envelope.length );
157
158 for( int i = 0; i < envelope.length; i++ )
159 {
160 out.writeUI32( envelope[i].mark44 );
161 out.writeUI16( envelope[i].level0 );
162 out.writeUI16( envelope[i].level1 );
163 }
164 }
165 }
166
167 public String toString()
168 {
169 return "SoundInfo: no-multiplay=" + noMultiplePlay +
170 " stop=" + stopPlaying +
171 " envelope=" + ((envelope==null)? "none": (""+ envelope.length + " points")) +
172 " in-point=" + inPoint +
173 " out-point=" + outPoint +
174 " loop-count=" + loopCount;
175 }
176 }