summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/sampled/AudioUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/AudioUtils.java')
-rw-r--r--songdbj/org/tritonus/share/sampled/AudioUtils.java181
1 files changed, 181 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/AudioUtils.java b/songdbj/org/tritonus/share/sampled/AudioUtils.java
new file mode 100644
index 0000000000..21c838b032
--- /dev/null
+++ b/songdbj/org/tritonus/share/sampled/AudioUtils.java
@@ -0,0 +1,181 @@
1/*
2 * AudioUtils.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 2000 by Matthias Pfisterer
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Library General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26/*
27|<--- this code is formatted to fit into 80 columns --->|
28*/
29
30package org.tritonus.share.sampled;
31
32import java.io.File;
33import java.io.FileOutputStream;
34import java.io.InputStream;
35import java.io.IOException;
36import java.io.OutputStream;
37import java.util.Collection;
38import java.util.Iterator;
39
40import javax.sound.sampled.AudioSystem;
41import javax.sound.sampled.AudioFormat;
42import javax.sound.sampled.AudioFileFormat;
43import javax.sound.sampled.AudioInputStream;
44import javax.sound.sampled.spi.AudioFileWriter;
45
46import org.tritonus.share.TDebug;
47import org.tritonus.share.sampled.TConversionTool;
48
49
50
51public class AudioUtils
52{
53 public static long getLengthInBytes(AudioInputStream audioInputStream)
54 {
55 return getLengthInBytes(audioInputStream.getFormat(),
56 audioInputStream.getFrameLength());
57/*
58 long lLengthInFrames = audioInputStream.getFrameLength();
59 int nFrameSize = audioInputStream.getFormat().getFrameSize();
60 if (lLengthInFrames >= 0 && nFrameSize >= 1)
61 {
62 return lLengthInFrames * nFrameSize;
63 }
64 else
65 {
66 return AudioSystem.NOT_SPECIFIED;
67 }
68*/
69 }
70
71
72
73 /**
74 * if the passed value for lLength is
75 * AudioSystem.NOT_SPECIFIED (unknown
76 * length), the length in bytes becomes
77 * AudioSystem.NOT_SPECIFIED, too.
78 */
79 public static long getLengthInBytes(AudioFormat audioFormat,
80 long lLengthInFrames)
81 {
82 int nFrameSize = audioFormat.getFrameSize();
83 if (lLengthInFrames >= 0 && nFrameSize >= 1)
84 {
85 return lLengthInFrames * nFrameSize;
86 }
87 else
88 {
89 return AudioSystem.NOT_SPECIFIED;
90 }
91 }
92
93
94
95 public static boolean containsFormat(AudioFormat sourceFormat,
96 Iterator possibleFormats)
97 {
98 while (possibleFormats.hasNext())
99 {
100 AudioFormat format = (AudioFormat) possibleFormats.next();
101 if (AudioFormats.matches(format, sourceFormat))
102 {
103 return true;
104 }
105 }
106 return false;
107 }
108
109 /**
110 * Conversion milliseconds -> bytes
111 */
112
113 public static long millis2Bytes(long ms, AudioFormat format) {
114 return millis2Bytes(ms, format.getFrameRate(), format.getFrameSize());
115 }
116
117 public static long millis2Bytes(long ms, float frameRate, int frameSize) {
118 return (long) (ms*frameRate/1000*frameSize);
119 }
120
121 /**
122 * Conversion milliseconds -> bytes (bytes will be frame-aligned)
123 */
124 public static long millis2BytesFrameAligned(long ms, AudioFormat format) {
125 return millis2BytesFrameAligned(ms, format.getFrameRate(), format.getFrameSize());
126 }
127
128 public static long millis2BytesFrameAligned(long ms, float frameRate, int frameSize) {
129 return ((long) (ms*frameRate/1000))*frameSize;
130 }
131
132 /**
133 * Conversion milliseconds -> frames
134 */
135 public static long millis2Frames(long ms, AudioFormat format) {
136 return millis2Frames(ms, format.getFrameRate());
137 }
138
139 public static long millis2Frames(long ms, float frameRate) {
140 return (long) (ms*frameRate/1000);
141 }
142
143 /**
144 * Conversion bytes -> milliseconds
145 */
146 public static long bytes2Millis(long bytes, AudioFormat format) {
147 return (long) (bytes/format.getFrameRate()*1000/format.getFrameSize());
148 }
149
150 /**
151 * Conversion frames -> milliseconds
152 */
153 public static long frames2Millis(long frames, AudioFormat format) {
154 return (long) (frames/format.getFrameRate()*1000);
155 }
156
157
158 //$$fb 2000-07-18: added these debugging functions
159 public static String NS_or_number(int number) {
160 return (number==AudioSystem.NOT_SPECIFIED)?"NOT_SPECIFIED":String.valueOf(number);
161 }
162 public static String NS_or_number(float number) {
163 return (number==AudioSystem.NOT_SPECIFIED)?"NOT_SPECIFIED":String.valueOf(number);
164 }
165
166 /**
167 * For debugging purposes.
168 */
169 public static String format2ShortStr(AudioFormat format) {
170 return format.getEncoding() + "-" +
171 NS_or_number(format.getChannels()) + "ch-" +
172 NS_or_number(format.getSampleSizeInBits()) + "bit-" +
173 NS_or_number(((int)format.getSampleRate())) + "Hz-"+
174 (format.isBigEndian() ? "be" : "le");
175 }
176
177}
178
179
180
181/*** AudioUtils.java ***/