Solved

putting args into a class

Posted on 2004-04-13
11
716 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Drop_of_Rain
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You can use the Same AudioTest.java to invoke the AudioConcat program.

Regards,
Muruga
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Thanks everyone
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 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

14 Experts available now in Live!

Get 1:1 Help Now