source: trunk/src/org/apollo/io/MixIO.java@ 1023

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

Improved mix settings... Also fix widget anchoring bug in Apollo

File size: 4.6 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 */
133 public static void saveBanks(String mixBatchPath, List<TrackMixSubject> banks) {
134
135 assert(mixBatchPath != null);
136 assert(banks != null);
137
138 File mixFile = new File(mixBatchPath);
139 if (!mixFile.exists()) {
140 try {
141 mixFile.createNewFile();
142 } catch (IOException e) {
143 e.printStackTrace();
144 System.err.println("ERROR: Unable to save mixes");
145 return;
146 }
147 }
148
149 FileWriter out = null;
150
151 try {
152
153 out = new FileWriter(mixFile, false);
154
155 for (int i = 0; i < banks.size(); i++) {
156
157 TrackMixSubject mix = banks.get(i);
158 assert(mix != null);
159 out.write(mix.toParseableString() + "\n");
160 }
161
162 } catch (IOException e) {
163 e.printStackTrace();
164
165 // Clean up
166 } finally {
167 if (out != null) {
168 try {
169 out.close();
170 } catch (IOException e) {
171 e.printStackTrace();
172 }
173 }
174 }
175
176 }
177
178 /**
179 *
180 * @param banksFilepath
181 * The filepath of the banks file to load.
182 *
183 * @param banks
184 * A list to load the banks into. Must not be null - must be empty.
185 *
186 * @return
187 * True of success. False if IO operatoin failed / file does not exist.
188 */
189 public static boolean loadBanks(
190 List<TrackMixSubject> banks,
191 String banksFilepath) {
192
193 assert(banks != null);
194 assert(banksFilepath != null);
195 assert(banks.isEmpty());
196
197 File mixFile = new File(banksFilepath);
198 if (!mixFile.exists()) {
199 try {
200 mixFile.createNewFile();
201 } catch (IOException e) {
202 e.printStackTrace();
203 System.err.println("ERROR: The master mix has been lost");
204 }
205 return false;
206 }
207
208 BufferedReader in = null;
209
210 try {
211
212 // Open the vbase for reading
213 in = new BufferedReader(new FileReader(mixFile));
214
215 String line = null;
216 // Read the sbase file...
217 while ((line = in.readLine()) != null) {
218
219 line = line.trim();
220 if (line.length() == 0) continue;
221
222 TrackMixSubject mix = TrackMixSubject.fromString(line);
223
224 if (mix != null)
225 banks.add(mix);
226
227 }
228
229 } catch (Exception e) {
230 e.printStackTrace();
231 // Clean up
232 } finally {
233 if (in != null) {
234 try {
235 in.close();
236 } catch (IOException e) {
237 e.printStackTrace();
238 }
239 }
240 }
241
242 return true;
243 }
244
245}
Note: See TracBrowser for help on using the repository browser.