Java sound processing method

Source: Internet
Author: User
Tags abstract definition
Java voice processing method (transferred from an Eid)
Java sound processing method

Author: BAT vs fallen Editor: Violet


Java sound processing

There are many digital audio formats. Their quality is related to the sampling frequency and sampling accuracy. The unit of frequency is Hz (HZ), which indicates the number of samples per second. The higher the sampling frequency, the better the sound quality. The sampling accuracy is the amount of data stored for each sampling. It determines the amount of discrete amplitude that each digital signal can represent. The more data each sample is stored, the better the sound quality. However, high-quality sound requires a large amount of memory and disk space. Considering the network bandwidth, it takes a long time to transmit data over the Internet. For the applet, it is extremely important to minimize the number of audio files.


JAVA supports the following four sound formats:

◆ Au
◆ AIFF
◆ Wave
◆ MIDI

The first sound format, AU, is the only sound format supported by the previous Java 1.1 version. The sampling frequency is 8000Hz and the sampling accuracy is 8 bits. The formats of AIFF and wave are the same as those of Au. They are used to represent digital sounds. Among them, the wave format provides a wider range of sound quality. The MIDI format is used to describe the sound with notes and instruments rather than digital sounds.

Sound creation: the Key sound tool is the standard application-recorder in Windows9x. The recording format is wave. There are also many professional applications, sampling and editing digital sounds, and format conversion.


Resource requirements:

■ Hardware Resource requirements:
(1) Sound Card
(2) Speaker
# If you need to record the sound, you need a microphone.

■ Software Resource requirements:
(1) Windows9x Operating System
(2) Web Browser
(3) Java Plug-in/Runtime Environment


Solution 1

Java language supports the above four sound formats. The simple method is to load the sound in Java through the audioclip () interface of the Applet Class. This interface is used to obtain the most useful method for playing audio clips.

This interface defines the following methods:
■ Play ()
Syntax: void play ()
Function: starts playing audio clips.
Whether or not the Sound clip is already playing, it is played from the beginning. Play once but not repeatedly.

■ Loop ()
Syntax: void loop ()
Function: loop playback of audio clips.
You can call this method to play a Sound clip from the beginning, no matter whether it is already playing.

■ Stop ()
Syntax: void stop ()
Function: stops playing audio clips.
You can use the getaudioclip () and getcodebase () methods to obtain audio clips and
URL. You can use this method to play a specified Sound clip on a web page.
# Requirement: A Development Environment Based on JDK or later. If you only play audio files in Au format, jdk1
. 1.

Appendix: soundtest.html source code
Html
Head
Title
HTML test page
/Title
/Head
Body
Applet
Codebase = "" code = "test. Class" name = "testapplet" wid
Th = 400 Height = 200 hspace = 0 vspace = 0 align = mi
Ddle
Param name = "clip" value = "sound. Au"
/Applet
/Body
/Html
Appendix: Test, Java source code
Package soundtest;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Applet .*;
Import javax. Swing .*;
Import java.net .*;
Public class test extends japplet {
Boolean isstandalone = false;
String sound;
Jbutton jbuttonplay = new jbutton ();
Jbutton jbuttonloop = new jbutton ();
Jbutton jbuttonstop = new jbutton ();
Jlabel jlabel1 = new jlabel ();
Audioclip clip;
// Get a parameter value
Public String getparameter (string key, string DEF ){
Return isstandalone? System. getproperty (Key, DEF ):
(Getparameter (key )! = NULL? Getparameter (key): DEF );
}
// Construct the Applet
Public test (){
}
// Initialize the Applet
Public void Init (){
Try {
Jbinit ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
// Component Initialization
Private void jbinit () throws exception {
Jbuttonplay. settext ("play ");
Jbuttonplay. setbounds (New rectangle (50, 85, 80, 40 ));
Jbuttonplay. addmouselistener (New java. AWT. event. mouseadapter (){
Public void mouseclicked (mouseevent e ){
Jbuttonplay_mouseclicked (E );
}
});
This. setsize (new dimension (400,200 ));
This. getcontentpane (). setlayout (null );
Jbuttonloop. settext ("loop ");
Jbuttonloop. setbounds (New rectangle (150, 85, 80, 40 ));
Jbuttonloop. addmouselistener (New java. AWT. event. mouseadapter (){
Public void mouseclicked (mouseevent e ){
Jbuttonloop_mouseclicked (E );
}
});
Jbuttonstop. settext ("stop ");
Jbuttonstop. setbounds (New rectangle (250, 85, 80, 40 ));
Jbuttonstop. addmouselistener (New java. AWT. event. mouseadapter (){

Public void mouseclicked (mouseevent e ){
Jbuttonstop_mouseclicked (E );
}
});
Jlabel1.settext ("Sound test Demo ");
Jlabel1.setbounds (New rectangle (109, 28,186, 28 ));
This. getcontentpane (). Add (jbuttonplay, null );
This. getcontentpane (). Add (jbuttonstop, null );
This. getcontentpane (). Add (jbuttonloop, null );
This. getcontentpane (). Add (jlabel1, null );
Try {sound = This. getparameter ("clip", "sound. Au");} catch (exce
Ption e) {e. printstacktrace ();}
If (sound! = NULL ){
Try {
Clip = japplet. newaudioclip (new URL (getcodebase (), sound ));

}
Catch (malformedurlexception e ){
System. Out. println ("Bad URL ");
}
}
}
// Get applet Information
Public String getappletinfo (){
Return "applet information ";
}
// Get parameter info
Public String [] [] getparameterinfo (){
String [] [] pinfo =
{
{"Clip", "string", "sound. Au "},
};
Return pinfo;
}
Void jbuttonplay_mouseclicked (mouseevent e ){
Clip. Play ();
}
Void jbuttonloop_mouseclicked (mouseevent e ){
Clip. Loop ();
}
Void jbuttonstop_mouseclicked (mouseevent e ){
Clip. Stop ();
}
}


Solution 2

Use the sound API in the java media framework to process sound fragments. Sound API is included in Sun's java media framework and also in JDK. To compile the source code, you must support the development environment and runtime environment of jdk1.3.

You can use the sound API to add an applet to a webpage. However, the browser must be supported by the Java Plug-in or runtime environment.

The sound API consists of the following four parts:

Packages
Javax. Sound. MIDI provides Midi (Musical Instrument Digital Interface) I/O,
Sequences, merged interfaces, and classes.
Javax. Sound. Midi. SPI supplies interfaces for service providers to impl
Ement when offering new MIDI devices, MIDI file readers and writers, O
R sound bank readers.
Javax. Sound. sampled provides interfaces and classes for capturing, processing, and replaying sampled digital audio.
Javax. Sound. sampled. SPI supplies abstract classes for service provider
S to subclass when offering new audio devices, sound file readers and
Writers, or audio format converters.


Solution 3

Use the sound class in Java 3D. This is an abstract definition of a sound resource. Each specific sound object can use the sound method.

The class relationship of the sound class is as follows:
Java. Lang. Object
+-Javax. Media. j3d. scenegraphobject
+-Javax. Media. j3d. Node
+-Javax. Media. j3d. Leaf
+-Javax. Media. j3d. Sound

The sub-classes of the sound class include:
⒈ Backgroundsound
⒉ Pointsound
⒊ Conesound

Follow these steps:

The volume defines and generates a mediacontainer object, and provides the audio files required by the object.
You must separately set a directory for storing Au/WAV audio files.

Define a backgroundsound/pointsound/conesound object.
Whether the audio is cyclic, intensity, and other parameters. set its function range and use setenable to enable it.

Configure the coordinate system based on the specific object.
# Java 3D applications can only play audio files in Au/WAV format. You cannot play a MIDI file.

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.