/[svn]/jsampler/trunk/src/org/jsampler/HF.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/HF.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (show annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 4513 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2011 Grigor Iliev <grigor@grigoriliev.com>
5 *
6 * This file is part of JSampler.
7 *
8 * JSampler is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * JSampler is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with JSampler; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23 package org.jsampler;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28
29 import java.util.logging.Level;
30
31 import org.linuxsampler.lscp.LSException;
32 import org.linuxsampler.lscp.LscpException;
33
34 import static org.jsampler.JSI18n.i18n;
35
36
37 /**
38 * This class contains some helper function.
39 * @author Grigor Iliev
40 */
41 public class HF {
42
43 /**
44 * Returns more meaningful, non-<code>null</code> message.
45 * @return More meaningful, non-<code>null</code> message.
46 */
47 public static String
48 getErrorMessage(Exception e) {
49 String msg = e.getMessage();
50
51 if(e instanceof LSException) {
52 LSException x = (LSException)e;
53 } else if(e instanceof LscpException) {
54
55 } else { msg = (msg != null ? msg : i18n.getError("unknownError")); }
56
57 return msg;
58 }
59
60 /**
61 * Deletes the specified file, if exists and
62 * is located in the JSampler's home directory.
63 * @param file The file to delete.
64 */
65 public static void
66 deleteFile(String file) {
67 String s = CC.getJSamplerHome();
68 if(s == null) return;
69
70 try {
71 File f = new File(s + File.separator + file);
72 if(f.isFile()) f.delete();
73 } catch(Exception x) {
74 CC.getLogger().log(Level.INFO, getErrorMessage(x), x);
75 }
76 }
77
78 /**
79 * Create a backup copy of the specified file, located in the JSampler's home directory.
80 * @param file The name of the file to backup.
81 * @param bkpFile The backup name of the file.
82 * @return <code>true</code> if the file is backuped successfully.
83 */
84 public static boolean
85 createBackup(String file, String bkpFile) {
86 if(file == null || bkpFile == null) return false;
87 if(file.length() == 0 || bkpFile.length() == 0) return false;
88
89 String s = CC.getJSamplerHome();
90 if(s == null) return false;
91
92 File f = new File(s + File.separator + file);
93 if(!f.isFile()) return false;
94
95 try {
96 FileInputStream fis = new FileInputStream(s + File.separator + file);
97
98 FileOutputStream fos;
99 fos = new FileOutputStream(s + File.separator + bkpFile, false);
100
101 int i = fis.read();
102 while(i != -1) {
103 fos.write(i);
104 i = fis.read();
105 }
106 } catch(Exception x) {
107 CC.getLogger().log(Level.INFO, getErrorMessage(x), x);
108 return false;
109 }
110
111 return true;
112 }
113
114 /**
115 * Converts the volume value specified in percents to decibels.
116 */
117 public static double
118 percentsToDecibels(int vol) {
119 if(vol == 0) return Double.NEGATIVE_INFINITY;
120 double i = vol;
121 i /= 100;
122 i = 20 * Math.log10(i);
123 return i;
124 }
125
126
127
128 /**
129 * Converts the volume value specified in decibels to percents.
130 */
131 public static int
132 decibelsToPercents(double vol) {
133 if(vol == Double.NEGATIVE_INFINITY) return 0;
134 double i = Math.pow(10, vol/20);
135 i *= 100;
136 return (int)i;
137 }
138
139 /**
140 * Converts the volume value specified in decibels to volume factor.
141 */
142 public static float
143 decibelsToFactor(double vol) {
144 if(vol == Double.NEGATIVE_INFINITY) return 0;
145 double i = Math.pow(10, vol/20);
146 return (float)i;
147 }
148
149 /**
150 * Converts the volume value specified in percents to volume factor.
151 */
152 public static float
153 percentsToFactor(int vol) {
154 float f = vol;
155 f /= 100;
156 return f;
157 }
158
159 /** Tests whether the application can read/write the specified file. */
160 public static boolean
161 canReadWrite(String file) {
162 File f = new File(file);
163 if(f.isDirectory()) return false;
164 if(f.canRead() && f.canWrite()) return true;
165 return false;
166 }
167
168 /** Tests whether the application can read/write files in the specified directory. */
169 public static boolean
170 canReadWriteFiles(String dir) {
171 File f = new File(dir);
172 if(!f.isDirectory()) return false;
173 if(f.canRead() && f.canWrite() && f.canExecute()) return true;
174 return false;
175 }
176 }

  ViewVC Help
Powered by ViewVC