?
Solved

putting args into a class

Posted on 2004-04-13
11
Medium Priority
?
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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