Solved

putting args into a class

Posted on 2004-04-13
11
718 Views
Last Modified: 2010-05-18
How would I load the files in this code into the next program AudioConcat.

public class AudioTest
{
      public static void main(String[] args)
      {
            args =
                  new String[] {
                        "-c",  // concatenation mode
                        "-o",  // output file specified
                        "c:/test/test.wav", // output file
                        "c:/test/wav/kp1.wav", // input file 1
                        "c:/test/wav/kp2.wav", // input file 2
                        "c:/test/wav/kp3.wav", // input file 3
                        "c:/test/wav/kp4.wav" // input file 4
                        };

            AudioConcat.main(args);
      }
}



import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;

/*      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</titleabbrev>
      <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</option></arg>
      <group choice="plain">
      <arg><option>-c</option></arg>
      <arg><option>-m</option></arg>
      </group>
      <arg choice="plain"><option>-o <replaceable>outputfile</replaceable></option></arg>
      <arg choice="plain" rep="repeat"><replaceable>inputfile</replaceable></arg>
      </cmdsynopsis>
      </para>
      </formalpara>

      <formalpara><title>Parameters</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</replaceable></option></term>
      <listitem><para>The filename of the output file</para></listitem>
      </varlistentry>
      <varlistentry>
      <term><replaceable>inputfile</replaceable></term>
      <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">AudioConcat.java</ulink>,
      <ulink url="SequenceAudioInputStream.java.html">SequenceAudioInputStream.java</ulink>,
      <ulink url="MixingAudioInputStream.java.html">MixingAudioInputStream.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_SIZE;
            // 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.getAudioInputStream(soundFile);
                  }
                  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(audioInputStream);
            }

            if (audioFormat == null)
            {
                  out("No input filenames!");
                  printUsageAndExit();
            }
            AudioInputStream      audioInputStream = null;
            switch (nMode)
            {
            case MODE_CONCATENATION:
                  audioInputStream = new SequenceAudioInputStream(audioFormat, audioInputStreamList);
                  break;

            case MODE_MIXING:
                  audioInputStream = new MixingAudioInputStream(audioFormat, 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(audioInputStream, 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(strMessage);
      }
}



/*** AudioConcat.java ***/
0
Comment
Question by:Drop_of_Rain
11 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 10815316
What is the problem ?

If the files are not found, replace / by \ in the file paths: "c:\\test\\test.wav"
0
 

Author Comment

by:Drop_of_Rain
ID: 10815393
I ran into a problem with an error exception previus question: Exception in thread "main" java.lang.NoC...

So I thoughI would work right from within the class.
0
 

Author Comment

by:Drop_of_Rain
ID: 10815596
I put this at the command line and got this info:

C:\>cd test

C:\test>java AudioConcat -c -o c:/test/wav/test.wav,c:/test/wav/kp1.wav,c:/test/
wav/kp2.wav
Exception in thread "main" java.lang.NoClassDefFoundError: AudioConcat

C:\test>java AudioConcat -c -o c:/test/wav/test.wav,c:/test/wav/kp1.wav.c:/test/
wav/kp2.wav
Exception in thread "main" java.lang.UnsupportedClassVersionError: AudioConcat (
Unsupported major.minor version 48.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Here the info on the program


Purpose. 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 -c) or containing a mixdown of all the other files (mixing mode, option -m). For concatenation, the input files must have the same audio format. They need not have the same file type.

Usage.  

java AudioConcat -h

java AudioConcat [ -D ] [ -c ] | [ -m ] -o outputfile inputfile ...


Parameters.

-c
selects concatenation mode

-m
selects mixing mode

-o outputfile
The filename of the output file

inputfile
the name(s) of input file(s)





I put this at the command line and got this info:

0
 
LVL 7

Expert Comment

by:searlas
ID: 10815692
Seems like AudioConcat was compiled with a different (newer) version of Java then the one you are using to run it.
0
 

Author Comment

by:Drop_of_Rain
ID: 10815735
I compiled it in a ide I only have one java that I know of. What do you think I shoud try.

Did I enter the args correctly?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Drop_of_Rain
ID: 10815846
I will try from the command line to compile it. I did get this from someone else already compiled.
0
 
LVL 92

Expert Comment

by:objects
ID: 10817735
C:\test>java AudioConcat -c -o c:/test/wav/test.wav,c:/test/wav/kp1.wav,c:/test/
wav/kp2.wav
Exception in thread "main" java.lang.NoClassDefFoundError: AudioConcat

C:\test>java AudioConcat -c -o c:/test/wav/test.wav,c:/test/wav/kp1.wav.c:/test/
wav/kp2.wav
Exception in thread "main" java.lang.UnsupportedClassVersionError: AudioConcat (
Unsupported major.minor version 48.0)


Which error is it u get when you run this?
0
 

Author Comment

by:Drop_of_Rain
ID: 10818555
I got all of that at the same time. I think I found the problem. I will be working on it a little later, I will go from there. I didn't have a class that was needed, I was going by so9mething I was told. Let me work on this for a few hours from now, and we can start from there.

I will have some other questions at that time as well!
0
 

Author Comment

by:Drop_of_Rain
ID: 10820430
I am having problems with this part of the program as well MixingAudioInputStream(audioFormat, audioInputStreamList); so I just want to let this go for now.
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 500 total points
ID: 10822036
You can use the Same AudioTest.java to invoke the AudioConcat program.

Regards,
Muruga
0
 

Author Comment

by:Drop_of_Rain
ID: 10823254
Thanks everyone
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 52
Error with Java/Cache JDBC Classpath 2 30
eclipse argument 14 53
map related example 6 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now