summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/sampled/AudioUtils.java
blob: 21c838b032536442794b1223bac32142dce8ce73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 *	AudioUtils.java
 *
 *	This file is part of Tritonus: http://www.tritonus.org/
 */

/*
 *  Copyright (c) 2000 by Matthias Pfisterer
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as published
 *   by the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
|<---            this code is formatted to fit into 80 columns             --->|
*/

package org.tritonus.share.sampled;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Iterator;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.spi.AudioFileWriter;

import org.tritonus.share.TDebug;
import org.tritonus.share.sampled.TConversionTool;



public class AudioUtils
{
	public static long getLengthInBytes(AudioInputStream audioInputStream)
	{
		return getLengthInBytes(audioInputStream.getFormat(),
					audioInputStream.getFrameLength());
/*
		long	lLengthInFrames = audioInputStream.getFrameLength();
		int	nFrameSize = audioInputStream.getFormat().getFrameSize();
		if (lLengthInFrames >= 0 && nFrameSize >= 1)
		{
			return lLengthInFrames * nFrameSize;
		}
		else
		{
			return AudioSystem.NOT_SPECIFIED;
		}
*/
	}



	/**
	 *	if the passed value for lLength is
	 *	AudioSystem.NOT_SPECIFIED (unknown
	 *	length), the length in bytes becomes
	 *	AudioSystem.NOT_SPECIFIED, too.
	 */
	public static long getLengthInBytes(AudioFormat audioFormat,
					    long lLengthInFrames)
	{
		int	nFrameSize = audioFormat.getFrameSize();
		if (lLengthInFrames >= 0 && nFrameSize >= 1)
		{
			return lLengthInFrames * nFrameSize;
		}
		else
		{
			return AudioSystem.NOT_SPECIFIED;
		}
	}



	public static boolean containsFormat(AudioFormat sourceFormat,
					     Iterator possibleFormats)
	{
		while (possibleFormats.hasNext())
		{
			AudioFormat	format = (AudioFormat) possibleFormats.next();
			if (AudioFormats.matches(format, sourceFormat))
			{
				return true;
			}
		}
		return false;
	}

	/**
	* Conversion milliseconds -> bytes
	*/

	public static long millis2Bytes(long ms, AudioFormat format) {
		return millis2Bytes(ms, format.getFrameRate(), format.getFrameSize());
	}

	public static long millis2Bytes(long ms, float frameRate, int frameSize) {
		return (long) (ms*frameRate/1000*frameSize);
	}

	/**
	* Conversion milliseconds -> bytes (bytes will be frame-aligned)
	*/
	public static long millis2BytesFrameAligned(long ms, AudioFormat format) {
		return millis2BytesFrameAligned(ms, format.getFrameRate(), format.getFrameSize());
	}

	public static long millis2BytesFrameAligned(long ms, float frameRate, int frameSize) {
		return ((long) (ms*frameRate/1000))*frameSize;
	}

	/**
	* Conversion milliseconds -> frames
	*/
	public static long millis2Frames(long ms, AudioFormat format) {
		return millis2Frames(ms, format.getFrameRate());
	}

	public static long millis2Frames(long ms, float frameRate) {
		return (long) (ms*frameRate/1000);
	}

	/**
	* Conversion bytes -> milliseconds 
	*/
	public static long bytes2Millis(long bytes, AudioFormat format) {
		return (long) (bytes/format.getFrameRate()*1000/format.getFrameSize());
	}

	/**
	* Conversion frames -> milliseconds 
	*/
	public static long frames2Millis(long frames, AudioFormat format) {
		return (long) (frames/format.getFrameRate()*1000);
	}


    //$$fb 2000-07-18: added these debugging functions
    public static String NS_or_number(int number) {
		return (number==AudioSystem.NOT_SPECIFIED)?"NOT_SPECIFIED":String.valueOf(number);
    }
    public static String NS_or_number(float number) {
		return (number==AudioSystem.NOT_SPECIFIED)?"NOT_SPECIFIED":String.valueOf(number);
    }

    /** 
     * For debugging purposes.
     */
    public static String format2ShortStr(AudioFormat format) {
		return format.getEncoding() + "-" +
	    	NS_or_number(format.getChannels()) + "ch-" +
	    	NS_or_number(format.getSampleSizeInBits()) + "bit-" +
	    	NS_or_number(((int)format.getSampleRate())) + "Hz-"+
	    	(format.isBigEndian() ? "be" : "le");
    } 

}



/*** AudioUtils.java ***/