/[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 2302 - (show annotations) (download)
Thu Dec 15 23:13:30 2011 UTC (12 years, 4 months ago) by iliev
File size: 4911 byte(s)
* Initial support for Android platforms (only sampler channel
  manipulation for now - see the screenshots on the website)

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 import java.text.NumberFormat;
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 private static NumberFormat numberFormat = NumberFormat.getInstance();
43
44 static {
45 numberFormat.setMaximumFractionDigits(1);
46 }
47
48 /**
49 * Returns more meaningful, non-<code>null</code> message.
50 * @return More meaningful, non-<code>null</code> message.
51 */
52 public static String
53 getErrorMessage(Exception e) {
54 String msg = e.getMessage();
55
56 if(e instanceof LSException) {
57 LSException x = (LSException)e;
58 } else if(e instanceof LscpException) {
59
60 } else { msg = (msg != null ? msg : i18n.getError("unknownError")); }
61
62 return msg;
63 }
64
65 /**
66 * Deletes the specified file, if exists and
67 * is located in the JSampler's home directory.
68 * @param file The file to delete.
69 */
70 public static void
71 deleteFile(String file) {
72 String s = CC.getJSamplerHome();
73 if(s == null) return;
74
75 try {
76 File f = new File(s + File.separator + file);
77 if(f.isFile()) f.delete();
78 } catch(Exception x) {
79 CC.getLogger().log(Level.INFO, getErrorMessage(x), x);
80 }
81 }
82
83 /**
84 * Create a backup copy of the specified file, located in the JSampler's home directory.
85 * @param file The name of the file to backup.
86 * @param bkpFile The backup name of the file.
87 * @return <code>true</code> if the file is backuped successfully.
88 */
89 public static boolean
90 createBackup(String file, String bkpFile) {
91 if(file == null || bkpFile == null) return false;
92 if(file.length() == 0 || bkpFile.length() == 0) return false;
93
94 String s = CC.getJSamplerHome();
95 if(s == null) return false;
96
97 File f = new File(s + File.separator + file);
98 if(!f.isFile()) return false;
99
100 try {
101 FileInputStream fis = new FileInputStream(s + File.separator + file);
102
103 FileOutputStream fos;
104 fos = new FileOutputStream(s + File.separator + bkpFile, false);
105
106 int i = fis.read();
107 while(i != -1) {
108 fos.write(i);
109 i = fis.read();
110 }
111 } catch(Exception x) {
112 CC.getLogger().log(Level.INFO, getErrorMessage(x), x);
113 return false;
114 }
115
116 return true;
117 }
118
119 /**
120 * Converts the volume value specified in percents to decibels.
121 */
122 public static double
123 percentsToDecibels(int vol) {
124 if(vol == 0) return Double.NEGATIVE_INFINITY;
125 double i = vol;
126 i /= 100;
127 i = 20 * Math.log10(i);
128 return i;
129 }
130
131
132
133 /**
134 * Converts the volume value specified in decibels to percents.
135 */
136 public static int
137 decibelsToPercents(double vol) {
138 if(vol == Double.NEGATIVE_INFINITY) return 0;
139 double i = Math.pow(10, vol/20);
140 i *= 100;
141 return (int)i;
142 }
143
144 /**
145 * Converts the volume value specified in decibels to volume factor.
146 */
147 public static float
148 decibelsToFactor(double vol) {
149 if(vol == Double.NEGATIVE_INFINITY) return 0;
150 double i = Math.pow(10, vol/20);
151 return (float)i;
152 }
153
154 /**
155 * Converts the volume value specified in percents to volume factor.
156 */
157 public static float
158 percentsToFactor(int vol) {
159 float f = vol;
160 f /= 100;
161 return f;
162 }
163
164 /** Tests whether the application can read/write the specified file. */
165 public static boolean
166 canReadWrite(String file) {
167 File f = new File(file);
168 if(f.isDirectory()) return false;
169 if(f.canRead() && f.canWrite()) return true;
170 return false;
171 }
172
173 /** Tests whether the application can read/write files in the specified directory. */
174 public static boolean
175 canReadWriteFiles(String dir) {
176 File f = new File(dir);
177 if(!f.isDirectory()) return false;
178 if(f.canRead() && f.canWrite() && f.canExecute()) return true;
179 return false;
180 }
181
182 public static String
183 getVolumeString(int volume) {
184 if(CC.getViewConfig().isMeasurementUnitDecibel()) {
185 return numberFormat.format(HF.percentsToDecibels(volume)) + "dB";
186 } else {
187 return String.valueOf(volume) + "%";
188 }
189 }
190 }

  ViewVC Help
Powered by ViewVC