Tuesday, March 18, 2008
Saturday, March 15, 2008
How to write a Java Application without a main method
You can write a runnable
The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
Sunday, March 09, 2008
How to use System class to copy Array
arrayCopy() method of System
public static void arraycopy(Objectsource , int srcIndex,
Object dest, int destIndex, int length)
The two Object arguments indicate the source and destination array. The three integer arguments indicate the starting location in each the source and the destination array, and the number of elements to copy.
This method will copy ‘length’ number of elements of array ‘source’ starts from the location ‘srcIndex’ to the array ‘dest’ starts from ‘destIndex’.
public class ArrayCopyDemo {
public static void main(String[] args) {
char[] Src = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] dst = new char[7];
System.arraycopy(src, 2, dst, 0, 7);
System.out.println(new String(dst));
}
}
Tuesday, March 04, 2008
Capturing Audio with Java Sound API
This Tech Tip reprinted with permission by java.sun.com
The Java Sound API has been a part of the standard libraries of the Java 2 Platform since the 1.3 release. Found in the javax.sound.sampled package, the Java Sound API provides support for playing and capturing
The javax.sound.sampled package consists of eight interfaces, twelve top-level classes, twelve inner classes, and two exceptions. To record and play audio, you only need to deal with a total of seven parts of the package.
Let's examine recording first. The basic recording process is as follows:
Describe the audio format in which you want to record the data. This includes specifying the sampling rate and the number of channels (mono versus stereo) for the audio. You specify these properties using the aptly named AudioFormat class. There are two constructors for creating an AudioFormat object:
AudioFormat(AudioFormat.Encoding |
The first constructor lets you explicitly set the audio format encoding, while the latter uses a default. The available
float sampleRate = 8000; |
After you describe the audio format, you need to get a DataLine. This interface represents an audio feed from which you can capture the audio. You use a subinterface of DataLine to do the actual capturing. The subinterface is called TargetDataLine. To get the TargetDataLine, you ask the AudioSystem. However when you do that, you need to specify information about the line. You make the specification in the form of a DataLine.Info object. In particular, you need to create a DataLine.Info object that is specific to the DataLine type and audio format. Here are some lines of source that get the TargetDataLine.
DataLine.Info info = new DataLine.Info( |
If the TargetDataLine is unavailable, a LineUnavailableException is thrown.
At this point you have your input source. You can think of the TargetDataLine like an input stream. However, it requires some setup before you can read form it. Setup in this case means first opening the line using the open() method, and then initializing the line using the start() method:
line.open(format); |
Your data line is ready, so you can start recording from it as shown in the following lines of code. Here you save a captured audio stream to a byte array for later playing. You could also save the audio stream to a file. Notice that you have to manage when to stop outside the read-loop construct.
int bufferSize = (int)format.getSampleRate() * |
Now let's examine playing audio. There are two key differences in playing audio as compared to recording audio. First, when you play audio, the bytes come from an AudioInputStream instead of a TargetDataLine. Second, you write to a SourceDataLine instead of into a ByteArrayOutputStream. Besides that, the process is the same.
To get the AudioInputStream, you need to
byte audio[] = out.toByteArray(); |
Getting the DataLine is similar to the way you get it for
DataLine.Info info = new DataLine.Info( |
Setup for the line is identical to the setup for audio recording:
line.open(format); |
The last step is to play the audio as shown below. Notice that this step is similar to the last step in recording. However, here you read from the buffer and write to the data line. There is also an added drain operation that works like a flush on an output stream.
int bufferSize = (int) format.getSampleRate() |
The following program puts these steps together to demonstrate using the Java Sound the API to record and play audio. The program also presents a GUI. Press the Capture button to start recording the audio, the Stop button to stop recording, and the Play button to play back the audio.
Note: Depending on the audio support your platform provides, you might need to change the format returned by the getFormat method in the program. If you don't know what format is supported by your platform, download the demo program from the Java Sound Demo page, and run it. Click on the CapturePlayback tab, and find a set of format settings that work for you. You can load an audio file from the audio subdirectory, and then try various settings on the left until something works. Use those settings in the creation of the AudioFormat returned by getFormat().
import javax.swing.*; |
You can find a more complete example of using the Java Sound API at the Java Sound Demo page. That example also shows an oscilloscope of the sound wave as it is playing back.
Copyright (c) 2004-2005 Sun Microsystems, Inc.
All Rights Reserved.