Week Five Notes

Source: Internet
Author: User

Import java.io.*;
Import javax.sound.sampled.*;

/**
* Class Soundengine provides functionality to process sound files.
* The Soundengine class provides the ability to handle sound files.
* Sounds can be loaded, played, paused, resumed, and stopped.
* Sound can be loaded, played, paused, resumed, and stopped.
*
* The sound engine can play multiple sounds simultaniously
* The last sound loaded can controlled (pased, resumed, etc).
* Music player sounds can play a variety of sound simultaniously, but only the last sound load can be controlled (obsolete, recovery, etc.).
*
* The sound engine accepts files in WAV, AIFF, and AU formats (although
* Not all WAV files-it depends on the encoding in the file).
* Music player accepts WAV files, Aiff,au format (although not all WAV files, depending on the encoded file).
*
* @author Michael kolling and David J Barnes
* @version 1.0
*/
public class Soundengine
{
The following three fields hold information about the sound clip
The following three fields hold information about the sound clip
Currently loaded in this engine
Private Clip currentsoundclip = null;
private int currentsoundduration = 0;
private int currentsoundframelength = 0;

/**
* Create a soundengine that can play sound files.
*/
Public Soundengine ()
{
}

/**
* Load and play a specified sound file. If the file is not in a
* Recognised file format, False is returned. Otherwise The Sound
* is started and true is returned. The method returns immediately
* After the sound starts (not after the sound finishes).
* Load and play the specified sound file. If the file is not in the Confirm file format, the error is returned. Otherwise the sound starts and returns correctly.
* Method immediately returns after the sound has started (after the sound has ended).
* @param soundfile the file to be loaded.
* @return True If the sound file could is loaded, false otherwise.
*/
public boolean play (File soundfile)
{
if (Loadsound (soundfile)) {
Currentsoundclip.start ();
return true;
}
else {
return false;
}
}

/**
* Stop the currently playing sound (if there is one playing). If No
* Sound was playing, this method has no effect.
*/
public void Stop ()
{
if (currentsoundclip! = null) {
Currentsoundclip.stop ();
Currentsoundclip = null;
}
}

/**
* Pause the currently playing sound. If no sound is currently playing,
* Calling this method has no effect.
*/
public void Pause ()
{
if (currentsoundclip! = null) {
Currentsoundclip.stop ();
}
}

/**
* Resume the currently paused sound. If no sound is currently paused,
* This method has no effect.
*/
public void Resume ()
{
if (currentsoundclip! = null) {
Currentsoundclip.start ();
}
}

/**
* Set the current playing position in the currently playing sound to ' value '.
* ' Value ' is a percentage value (0 to 100). If There is no sound currently
* Playing, this method has no effect.
*
* @param value the target position in the sound file, as a percentage.
*/
public void Seek (int value)
{
if (currentsoundclip! = null) {
int seekposition = currentsoundframelength/100 * value;
Currentsoundclip.setframeposition (seekposition);
}
}

/**
* Set the playback volume of the current sound. If no sound is currently
* Loaded, this method has no effect.
*
* @param vol Volume level as a percentage (0..100).
*/
public void SetVolume (int vol)
{
if (Currentsoundclip = = null) {
Return
}
if (Vol < 0 | | Vol > 100) {
vol = 100;
}

Double val = vol/100.0;

try {
Floatcontrol Volcontrol =
(Floatcontrol) Currentsoundclip.getcontrol (FloatControl.Type.MASTER_GAIN);
float DB = (float) (Math.log (val = = 0.0 0.0001:val)/Math.log (10.0) * 20.0);
Volcontrol.setvalue (DB);
} catch (Exception ex) {
System.out.println ("Could not set volume");
}
}

/**
* Return the duration of the currently loaded sound clip.
*
* @return The duration of the current sound, or 0 if no sound has been loaded.
*/
public int getduration ()
{
return currentsoundduration;
}

/**
* Load the sound file supplied by the parameter to this sound engine.
*
* @return True If successful, False if the file could not is decoded.
*/
Private Boolean loadsound (file file)
{
currentsoundduration = 0;

try {
Audioinputstream stream = audiosystem.getaudioinputstream (file);
Audioformat format = Stream.getformat ();

//We cannot play alaw/ulaw, so we convert them to PCM
//
if ((format.getencoding () = = AudioFormat.Encoding.UL AW) | |
(format.getencoding () = = AudioFormat.Encoding.ALAW))
{
Audioformat tmp = new Audioformat (
AudioFormat.Encoding.PCM_SIGNED,
Format.getsamplerate (),
Format.getsamplesizeinbits () * 2,
Format.getchannels (),
Format.getframesize () * 2,
Format.getframerate (),
true);
Stream = Audiosystem.getaudioinputstream (tmp, stream);
format = tmp;
}
Dataline.info Info = new Dataline.info (Clip.class,
Stream.getformat (),
(int) stream.getframelength ( ) *
Format.getframesize ()));

Currentsoundclip = (Clip) audiosystem.getline (info);
Currentsoundclip.open (stream);
Currentsoundframelength = (int) stream.getframelength ();
currentsoundduration = (int) (Currentsoundclip.getbuffersize ()/
(Currentsoundclip.getformat (). Getframesize () *
Currentsoundclip.getformat (). Getframerate ());
return true;
} catch (Exception ex) {
Currentsoundclip = null;
return false;
}
}
}

Week Five Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.