1 | package org.apollo.audio.util;
|
---|
2 |
|
---|
3 | import org.apollo.audio.ApolloSubjectChangedEvent;
|
---|
4 | import org.apollo.mvc.AbstractSubject;
|
---|
5 | import org.apollo.mvc.SubjectChangedEvent;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * A TrackMixSubject is basic in Apollo: where a track mix consists
|
---|
9 | * of a volume and a mute flag.
|
---|
10 | *
|
---|
11 | * Mixes have immutable IDs (represented as strings) to identify them...
|
---|
12 | *
|
---|
13 | * @author Brook Novak
|
---|
14 | *
|
---|
15 | */
|
---|
16 | public class TrackMixSubject extends AbstractSubject {
|
---|
17 |
|
---|
18 | private String channelID;
|
---|
19 |
|
---|
20 | private float volume;
|
---|
21 |
|
---|
22 | private boolean isMuted;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructor.
|
---|
26 | *
|
---|
27 | * @param id
|
---|
28 | * Must not be null / empty.
|
---|
29 | *
|
---|
30 | * @param volume
|
---|
31 | * The initial volume.
|
---|
32 | *
|
---|
33 | * @param isMuted
|
---|
34 | * The initial mute flag.
|
---|
35 | *
|
---|
36 | * @throws NullPointerException
|
---|
37 | * If id is null.
|
---|
38 | *
|
---|
39 | * @throws IllegalArgumentException
|
---|
40 | * If id is empty.
|
---|
41 | */
|
---|
42 | TrackMixSubject(String id, float volume, boolean isMuted) {
|
---|
43 | if (id == null) throw new NullPointerException("id");
|
---|
44 | if (id.length() == 0) throw new IllegalArgumentException("id is empty");
|
---|
45 | this.channelID = id;
|
---|
46 | this.volume = volume;
|
---|
47 | this.isMuted = isMuted;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @return True if this clip is muted. false otherwise.
|
---|
52 | */
|
---|
53 | public boolean isMuted() {
|
---|
54 | return isMuted;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Mutes / un-mutes this clip. Can be called even if not loaded.
|
---|
59 | *
|
---|
60 | * Raises an ApolloSubjectChangedEvent.MUTE event.
|
---|
61 | * Null is given as the value if the clip does not support mute. Otherwise
|
---|
62 | * true/false is given as the value of the mixed mute.
|
---|
63 | * The event is only raised if the mute is different ot the current mute.
|
---|
64 | *
|
---|
65 | * @param isMuted True to mute. False to unmute.
|
---|
66 | *
|
---|
67 | */
|
---|
68 | public void setMuted(boolean isMuted) {
|
---|
69 |
|
---|
70 | if (this.isMuted == isMuted) return;
|
---|
71 | this.isMuted = isMuted;
|
---|
72 |
|
---|
73 | fireSubjectChanged(new SubjectChangedEvent(
|
---|
74 | ApolloSubjectChangedEvent.MUTE));
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @return The volume for this clip.
|
---|
80 | */
|
---|
81 | public float getVolume() {
|
---|
82 | return volume;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Sets the volume for this clip. Can be called even if not loaded.
|
---|
87 | *
|
---|
88 | * raises a {@link ApolloSubjectChangedEvent.VOLUME} event.
|
---|
89 | *
|
---|
90 | * Null is given as the value if the clip does not support master gain.
|
---|
91 | * The event is only raised if the volume is different ot the current volume.
|
---|
92 | *
|
---|
93 | * Note that this will not actually set the volume of any track sequences, to update
|
---|
94 | * the volume you must subsequently call {@link #updateVolume(Object)}. This is to
|
---|
95 | * avoid setting volumes over all track sequences.
|
---|
96 | *
|
---|
97 | * @param vol
|
---|
98 | * The volume to set. Ranges from 0-1. Clamped.
|
---|
99 | *
|
---|
100 | */
|
---|
101 | public void setVolume(float vol) {
|
---|
102 |
|
---|
103 | // Clamp volume argument
|
---|
104 | if (vol < 0.0f)
|
---|
105 | vol = 0.0f;
|
---|
106 | else if (vol > 1.0f)
|
---|
107 | vol = 1.0f;
|
---|
108 |
|
---|
109 | if (vol == this.volume)
|
---|
110 | return;
|
---|
111 |
|
---|
112 | volume = vol;
|
---|
113 |
|
---|
114 | fireSubjectChanged(new SubjectChangedEvent(
|
---|
115 | ApolloSubjectChangedEvent.VOLUME));
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * The unique ID associated with this track.
|
---|
120 | *
|
---|
121 | * @return
|
---|
122 | * The immutable ID. Never null.
|
---|
123 | */
|
---|
124 | public String getChannelID() {
|
---|
125 | return channelID;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * A {@link #TrackMixSubject} is equal to another if they have the same ID.
|
---|
130 | */
|
---|
131 | @Override
|
---|
132 | public boolean equals(Object obj) {
|
---|
133 | return channelID.equals(obj);
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public int hashCode() {
|
---|
138 | return channelID.hashCode();
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static final char SEPERATOR = 7423; // something obscure
|
---|
142 |
|
---|
143 | public String toParseableString() {
|
---|
144 | return toParseableString(channelID, volume, isMuted);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static String toParseableString(String id, float volume, boolean isMuted) {
|
---|
148 | return id + SEPERATOR + volume + SEPERATOR + ((isMuted) ? "1" : "0");
|
---|
149 | }
|
---|
150 |
|
---|
151 | public static TrackMixSubject fromString(String st) {
|
---|
152 |
|
---|
153 | if (st == null) return null;
|
---|
154 |
|
---|
155 | String[] parts = st.split(Character.toString(SEPERATOR));
|
---|
156 |
|
---|
157 | if (parts.length != 3) return null;
|
---|
158 |
|
---|
159 | if (parts[0].length() == 0) return null;
|
---|
160 |
|
---|
161 | float vol;
|
---|
162 | boolean isMuted;
|
---|
163 |
|
---|
164 | try {
|
---|
165 | vol = Float.parseFloat(parts[1]);
|
---|
166 | } catch (NumberFormatException e) {
|
---|
167 | return null;
|
---|
168 | }
|
---|
169 |
|
---|
170 | isMuted = parts[2].length() > 0 && parts[2].charAt(0) == '1';
|
---|
171 |
|
---|
172 | return new TrackMixSubject(parts[0], vol, isMuted);
|
---|
173 |
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | }
|
---|