I have the code to get streams into code #1 I also have the gnu.getopt.jar file.
I have to get this part worked out, this part is the foundation of my program.
Can you tell me what I am suppose to do to use these programs to make my single audio files out of a lot of small files.
Code #1
/*
* SequenceAudioInputStream.j
ava
*
* This file is part of the Java Sound Examples.
*/
/*
* Copyright (c) 1999 - 2001 by Matthias Pfisterer <Matthias.Pfisterer@web.de
>
*
*
* 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 --->|
//SequenceAudioInputStream
.java
import java.io.ByteArrayInputStre
am;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.sound.sampled.AudioF
ormat;
import javax.sound.sampled.AudioI
nputStream
;
import javax.sound.sampled.AudioS
ystem;
import javax.sound.sampled.LineUn
availableE
xception;
import javax.sound.sampled.LineLi
stener;
import javax.sound.sampled.LineEv
ent;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Source
DataLine;
import javax.sound.sampled.DataLi
ne;
import javax.sound.sampled.FloatC
ontrol;
import javax.sound.sampled.Boolea
nControl;
public class SequenceAudioInputStream
extends AudioInputStream
{
private static final boolean DEBUG = true;
private List m_audioInputStreamList;
private int m_nCurrentStream;
public SequenceAudioInputStream(A
udioFormat
audioFormat, Collection audioInputStreams)
{
super(new ByteArrayInputStream(new byte[0]), audioFormat, AudioSystem.NOT_SPECIFIED)
;
m_audioInputStreamList = new ArrayList(audioInputStream
s);
m_nCurrentStream = 0;
}
// TODO: remove
private boolean addAudioInputStream(AudioI
nputStream
audioStream)
{
if (DEBUG)
{
out("SequenceAudioInputStr
eam.addAud
ioInputStr
eam(): called.");
}
// Contract.check(audioStream
!= null);
if (!getFormat().matches(audi
oStream.ge
tFormat())
)
{
if (DEBUG)
{
out("SequenceAudioInputStr
eam.addAud
ioInputStr
eam(): audio formats do not match, trying to convert.");
}
AudioInputStream asold = audioStream;
audioStream = AudioSystem.getAudioInputS
tream(getF
ormat(), asold);
if (audioStream == null)
{
out("### SequenceAudioInputStream.a
ddAudioInp
utStream()
: could not convert.");
return false;
}
if (DEBUG)
{
out(" converted");
}
}
// Contract.check(audioStream
!= null);
synchronized (m_audioInputStreamList)
{
m_audioInputStreamList.add
(audioStre
am);
m_audioInputStreamList.not
ifyAll();
}
if (DEBUG)
{
out("SequenceAudioInputStr
eam.addAud
ioInputStr
eam(): enqueued " + audioStream);
}
return true;
}
private AudioInputStream getCurrentStream()
{
return (AudioInputStream) m_audioInputStreamList.get
(m_nCurren
tStream);
}
private boolean advanceStream()
{
m_nCurrentStream++;
boolean bAnotherStreamAvailable = (m_nCurrentStream < m_audioInputStreamList.siz
e());
return bAnotherStreamAvailable;
}
public long getFrameLength()
{
long lLengthInFrames = 0;
Iterator streamIterator = m_audioInputStreamList.ite
rator();
while (streamIterator.hasNext())
{
AudioInputStream stream = (AudioInputStream) streamIterator.next();
long lLength = stream.getFrameLength();
if (lLength == AudioSystem.NOT_SPECIFIED)
{
return AudioSystem.NOT_SPECIFIED;
}
else
{
lLengthInFrames += lLength;
}
}
return lLengthInFrames;
}
public int read()
throws IOException
{
AudioInputStream stream = getCurrentStream();
int nByte = stream.read();
if (nByte == -1)
{
/*
The end of the current stream has been signaled.
We try to advance to the next stream.
*/
boolean bAnotherStreamAvailable = advanceStream();
if (bAnotherStreamAvailable)
{
/*
There is another stream. We recurse into this method
to read from it.
*/
return read();
}
else
{
/*
No more data. We signal EOF.
*/
return -1;
}
}
else
{
/*
The most common case: We return the byte.
*/
return nByte;
}
}
public int read(byte[] abData, int nOffset, int nLength)
throws IOException
{
AudioInputStream stream = getCurrentStream();
int nBytesRead = stream.read(abData, nOffset, nLength);
if (nBytesRead == -1)
{
/*
The end of the current stream has been signaled.
We try to advance to the next stream.
*/
boolean bAnotherStreamAvailable = advanceStream();
if (bAnotherStreamAvailable)
{
/*
There is another stream. We recurse into this method
to read from it.
*/
return read(abData, nOffset, nLength);
}
else
{
/*
No more data. We signal EOF.
*/
return -1;
}
}
else
{
/*
The most common case: We return the length.
*/
return nBytesRead;
}
}
public long skip(long lLength)
throws IOException
{
throw new IOException("skip() is not implemented in class SequenceInputStream. Mail <Matthias.Pfisterer@web.de
> if you need this feature.");
}
public int available()
throws IOException
{
return getCurrentStream().availab
le();
}
public void close()
throws IOException
{
// TODO: should we close all streams in the list?
}
public void mark(int nReadLimit)
{
throw new RuntimeException("mark() is not implemented in class SequenceInputStream. Mail <Matthias.Pfisterer@web.de
> if you need this feature.");
}
public void reset()
throws IOException
{
throw new IOException("reset() is not implemented in class SequenceInputStream. Mail <Matthias.Pfisterer@web.de
> if you need this feature.");
}
public boolean markSupported()
{
return false;
}
private static void out(String strMessage)
{
System.out.println(strMess
age);
}
}
/*** SequenceAudioInputStream.j
ava ***/
/*
* AudioConcat.java
*
* This file is part of the Java Sound Examples.
*/
/*
* Copyright (c) 1999 - 2001 by Matthias Pfisterer <Matthias.Pfisterer@web.de
>
*
* 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 --->|
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioF
ileFormat;
import javax.sound.sampled.AudioF
ormat;
import javax.sound.sampled.AudioI
nputStream
;
import javax.sound.sampled.AudioS
ystem;
import javax.sound.sampled.DataLi
ne;
import javax.sound.sampled.LineUn
availableE
xception;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Source
DataLine;
/* If the compilation fails because this class is not available,
get gnu.getopt from the URL given in the comment below.
*/
import gnu.getopt.Getopt;
// TODO: the name AudioConcat is no longer appropriate. There should be a name that is neutral to concat/mix.
/** <titleabbrev>AudioConcat</
titleabbre
v>
<title>Concatenating or mixing audio files</title>
<formalpara><title>Purpose
</title>
<para>This program reads multiple audio files and
writes a single one either
containing the data of all the other
files in order (concatenation mode, option <option>-c</option>)
or containing a mixdown of all the other files
(mixing mode, option <option>-m</option>).
For concatenation, the input files must have the same audio
format. They need not have the same file type.</para>
</formalpara>
<formalpara><title>Level</
title>
<para>experienced</para>
</formalpara>
<formalpara><title>Usage</
title>
<para>
<cmdsynopsis>
<command>java AudioConcat</command>
<arg choice="plain"><option>-h<
/option></
arg>
</cmdsynopsis>
<cmdsynopsis>
<command>java AudioConcat</command>
<arg choice="opt"><option>-D</o
ption></ar
g>
<group choice="plain">
<arg><option>-c</option></
arg>
<arg><option>-m</option></
arg>
</group>
<arg choice="plain"><option>-o <replaceable>outputfile</r
eplaceable
></option>
</arg>
<arg choice="plain" rep="repeat"><replaceable>
inputfile<
/replaceab
le></arg>
</cmdsynopsis>
</para>
</formalpara>
<formalpara><title>Paramet
ers</title
>
<variablelist>
<varlistentry>
<term><option>-c</option><
/term>
<listitem><para>selects concatenation mode</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-m</option><
/term>
<listitem><para>selects mixing mode</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-o <replaceable>outputfile</r
eplaceable
></option>
</term>
<listitem><para>The filename of the output file</para></listitem>
</varlistentry>
<varlistentry>
<term><replaceable>inputfi
le</replac
eable></te
rm>
<listitem><para>the name(s) of input file(s)</para></listitem>
</varlistentry>
</variablelist>
</formalpara>
<formalpara><title>Bugs, limitations</title>
<para>
This program is not well-tested. Output is always a WAV
file. Future versions should be able to convert
different audio formats to a dedicated target format.
</para></formalpara>
<formalpara><title>Source code</title>
<para>
<ulink url="AudioConcat.java.html
">AudioCon
cat.java</
ulink>,
<ulink url="SequenceAudioInputStr
eam.java.h
tml">Seque
nceAudioIn
putStream.
java</ulin
k>,
<ulink url="MixingAudioInputStrea
m.java.htm
l">MixingA
udioInputS
tream.java
</ulink>,
<ulink url="
http://www.urbanophile.com/arenn/hacking/download.html">gnu.getopt.Getopt</
ulink>
</para>
</formalpara>
*/
public class AudioConcat
{
private static final int MODE_NONE = 0;
private static final int MODE_MIXING = 1;
private static final int MODE_CONCATENATION = 2;
/** Flag for debugging messages.
* If true, some messages are dumped to the console
* during operation.
*/
private static boolean DEBUG = false;
public static void main(String[] args)
{
/** Mode of operation.
Determines what to do with the input files:
either mixing or concatenation.
*/
int nMode = MODE_NONE;
String strOutputFilename = null;
AudioFormat audioFormat = null;
List audioInputStreamList = new ArrayList();
// int nExternalBufferSize = DEFAULT_EXTERNAL_BUFFER_SI
ZE;
// int nInternalBufferSize = AudioSystem.NOT_SPECIFIED;
/*
* Parsing of command-line options takes place...
*/
Getopt g = new Getopt("AudioConcat", args, "hDcmo:");
int c;
while ((c = g.getopt()) != -1)
{
switch (c)
{
case 'h':
printUsageAndExit();
case 'o':
strOutputFilename = g.getOptarg();
if (DEBUG) { out("AudioConcat.main(): output filename: " + strOutputFilename); }
break;
case 'c':
nMode = MODE_CONCATENATION;
break;
case 'm':
nMode = MODE_MIXING;
break;
case 'D':
DEBUG = true;
break;
case '?':
printUsageAndExit();
default:
out("AudioConcat.main(): getopt() returned " + c);
break;
}
}
/*
* All remaining arguments are assumed to be filenames of
* soundfiles we want to play.
*/
String strFilename = null;
for (int i = g.getOptind(); i < args.length; i++)
{
strFilename = args[i];
File soundFile = new File(strFilename);
/*
* We have to read in the sound file.
*/
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputS
tream(soun
dFile);
}
catch (Exception e)
{
/*
* In case of an exception, we dump the exception
* including the stack trace to the console output.
* Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}
AudioFormat format = audioInputStream.getFormat
();
/*
The first input file determines the audio format. This stream's
AudioFormat is stored. All other streams are checked against
this format.
*/
if (audioFormat == null)
{
audioFormat = format;
if (DEBUG) { out("AudioConcat.main(): format: " + audioFormat); }
}
else if ( ! audioFormat.matches(format
))
{
// TODO: try to convert
out("AudioConcat.main(): WARNING: AudioFormats don't match");
out("AudioConcat.main(): master format: " + audioFormat);
out("AudioConcat.main(): this format: " + format);
}
audioInputStreamList.add(a
udioInputS
tream);
}
if (audioFormat == null)
{
out("No input filenames!");
printUsageAndExit();
}
AudioInputStream audioInputStream = null;
switch (nMode)
{
case MODE_CONCATENATION:
audioInputStream = new SequenceAudioInputStream(a
udioFormat
, audioInputStreamList);
break;
case MODE_MIXING:
audioInputStream = new MixingAudioInputStream(aud
ioFormat, audioInputStreamList);
break;
default:
out("you have to specify a mode (either -m or -c).");
printUsageAndExit();
}
if (strOutputFilename == null)
{
out("you have to specify an output filename (using -o <filename>).");
printUsageAndExit();
}
File outputFile = new File(strOutputFilename);
try
{
AudioSystem.write(audioInp
utStream, AudioFileFormat.Type.WAVE,
outputFile);
}
catch (IOException e)
{
e.printStackTrace();
}
if (DEBUG) { out("AudioConcat.main(): before exit"); }
System.exit(0);
}
private static void printUsageAndExit()
{
out("AudioConcat: usage:");
out("\tjava AudioConcat -h");
out("\tjava AudioConcat [-D] -c|-m -o <outputfile> <inputfile> ...");
System.exit(1);
}
private static void out(String strMessage)
{
System.out.println(strMess
age);
}
}
/*** AudioConcat.java ***/
Then to concat all your audio files into a single file
you have to use SequenceAudioInputStream all the small audio files as a collection which should be passed to the constructor of "SequenceAudioInputStream"
Am I missing somewhere? If that is the case, then let me know your exact requirement.
Regards,
Muruga.