source: trunk/src_apollo/org/apollo/io/MixIO.java@ 315

Last change on this file since 315 was 315, checked in by bjn8, 16 years ago

Apollo spin-off added

File size: 5.9 KB
Line 
1package org.apollo.io;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileReader;
6import java.io.FileWriter;
7import java.io.IOException;
8import java.util.List;
9
10import org.apollo.audio.util.TrackMixSubject;
11
12/**
13 * A naive approach for saving and loading of mixes....
14 * Very hacky at this point and if released should re-think how banks are saved and loaded.
15 *
16 * @author Brook Novak
17 *
18 */
19public class MixIO {
20
21 /**
22 * Loads the master mix
23 *
24 * @param masterMixFilepath
25 * @return
26 * The solo prefix.
27 */
28 public static TrackMixSubject loadMasterMix(String masterMixFilepath) {
29 assert(masterMixFilepath != null);
30
31 File mixFile = new File(masterMixFilepath);
32 if (!mixFile.exists()) {
33 try {
34 mixFile.createNewFile();
35 } catch (IOException e) {
36 e.printStackTrace();
37 System.err.println("ERROR: The master mix has been lost");
38 return null;
39 }
40 }
41
42 BufferedReader in = null;
43
44 TrackMixSubject masterMix = null;
45
46 try {
47
48 // Open the vbase for reading
49 in = new BufferedReader(new FileReader(mixFile));
50
51 String line = null;
52 // Read the sbase file and check all names
53 while ((line = in.readLine()) != null) {
54 line = line.trim();
55 masterMix = TrackMixSubject.fromString(line);
56 if (masterMix != null) break;
57 }
58
59 } catch (Exception e) {
60 e.printStackTrace();
61 // Clean up
62 } finally {
63 if (in != null) {
64 try {
65 in.close();
66 } catch (IOException e) {
67 e.printStackTrace();
68 }
69 }
70 }
71
72 return masterMix;
73 }
74
75 /**
76 * Saves a master mix
77 *
78 * @param masterMixFilepath
79 * Where to save
80 *
81 * @param mmix
82 * The mix to save as a master mix
83 */
84 public static void saveMasterMix(String masterMixFilepath, TrackMixSubject mmix) {
85
86 assert(masterMixFilepath != null);
87 assert(mmix != null);
88
89 File mixFile = new File(masterMixFilepath);
90 if (!mixFile.exists()) {
91 try {
92 mixFile.createNewFile();
93 } catch (IOException e) {
94 e.printStackTrace();
95 System.err.println("ERROR: Unable to save master mix");
96 return;
97 }
98 }
99
100 FileWriter out = null;
101
102 try {
103 out = new FileWriter(mixFile, false);
104 out.write(mmix.toParseableString());
105
106 } catch (IOException e) {
107 e.printStackTrace();
108
109 // Clean up
110 } finally {
111 if (out != null) {
112 try {
113 out.close();
114 } catch (IOException e) {
115 e.printStackTrace();
116 }
117 }
118 }
119
120 }
121
122
123 /**
124 * Saves banks to file
125 *
126 * @param mixBatchPath
127 * Where to save
128 *
129 * @param banks
130 * The mixes to write to file
131 *
132 * @param usingLocalFlags
133 * Parallel list with banks. The isUsingLocal flag...
134 */
135 public static void saveBanks(String mixBatchPath, List<TrackMixSubject> banks, List<Boolean> usingLocalFlags) {
136
137 assert(mixBatchPath != null);
138 assert(banks != null);
139 assert(usingLocalFlags != null);
140 assert(usingLocalFlags.size() == banks.size());
141
142 File mixFile = new File(mixBatchPath);
143 if (!mixFile.exists()) {
144 try {
145 mixFile.createNewFile();
146 } catch (IOException e) {
147 e.printStackTrace();
148 System.err.println("ERROR: Unable to save mixes");
149 return;
150 }
151 }
152
153 FileWriter out = null;
154
155 try {
156
157 out = new FileWriter(mixFile, false);
158
159 for (int i = 0; i < banks.size(); i++) {
160
161 TrackMixSubject mix = banks.get(i);
162 assert(mix != null);
163
164 Boolean flag = usingLocalFlags.get(i);
165 assert(flag != null);
166
167 out.write(mix.toParseableString() + "\n");
168 out.write(flag.booleanValue() ? "1\n" : "0\n");
169 }
170
171 } catch (IOException e) {
172 e.printStackTrace();
173
174 // Clean up
175 } finally {
176 if (out != null) {
177 try {
178 out.close();
179 } catch (IOException e) {
180 e.printStackTrace();
181 }
182 }
183 }
184
185 }
186
187 /**
188 *
189 * @param banksFilepath
190 * The filepath of the banks file to load.
191 *
192 * @param banks
193 * A list to load the banks into. Must not be null - must be empty.
194 *
195 * @param usingLocalFlags
196 * A list to load the channel flags into... Must not be null - must be empty.
197 *
198 * @return
199 * True of success. False if IO operatoin failed / file does not exist.
200 */
201 public static boolean loadBanks(
202 List<TrackMixSubject> banks,
203 List<Boolean> usingLocalFlags,
204 String banksFilepath) {
205
206 assert(banks != null);
207 assert(usingLocalFlags != null);
208 assert(banksFilepath != null);
209 assert(banks.isEmpty());
210 assert(usingLocalFlags.isEmpty());
211
212 File mixFile = new File(banksFilepath);
213 if (!mixFile.exists()) {
214 try {
215 mixFile.createNewFile();
216 } catch (IOException e) {
217 e.printStackTrace();
218 System.err.println("ERROR: The master mix has been lost");
219 }
220 return false;
221 }
222
223 BufferedReader in = null;
224
225 try {
226
227 // Open the vbase for reading
228 in = new BufferedReader(new FileReader(mixFile));
229
230 String line = null;
231 // Read the sbase file...
232 boolean waitingOnFlag = false;
233 while ((line = in.readLine()) != null) {
234
235 line = line.trim();
236 if (line.length() == 0) continue;
237
238 TrackMixSubject mix = TrackMixSubject.fromString(line);
239
240 if (mix != null) {
241
242 if (waitingOnFlag) { // missing flag for last mix line
243 usingLocalFlags.add(new Boolean(false)); // default to false
244 }
245
246 banks.add(mix);
247 waitingOnFlag = true;
248
249 } else if (waitingOnFlag) { // waiting on flag?
250
251 if (line.equals("1")) {
252 usingLocalFlags.add(new Boolean(true));
253 waitingOnFlag = false;
254 } else if (line.equals("0")) {
255 usingLocalFlags.add(new Boolean(false));
256 waitingOnFlag = false;
257 }
258 }
259
260
261 }
262
263 if (usingLocalFlags.size() == (banks.size() - 1)) { // missing last flag
264 usingLocalFlags.add(new Boolean(false));
265 }
266
267 assert(usingLocalFlags.size() == banks.size());
268
269 } catch (Exception e) {
270 e.printStackTrace();
271 // Clean up
272 } finally {
273 if (in != null) {
274 try {
275 in.close();
276 } catch (IOException e) {
277 e.printStackTrace();
278 }
279 }
280 }
281
282 return true;
283 }
284
285}
Note: See TracBrowser for help on using the repository browser.