aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Audio in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Audio in Java" Watch "Audio in Java" New topic
Author

Audio in Java

Priya Kubher
Greenhorn

Joined: Jan 07, 2003
Posts: 10
I need to send audio from one computer to another in java. Which api will have the stuff that can help me out. Please advise and where would i go to get information about that api.
Thank you
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
If you're talking about streaming audio, look at the JMF (java media framework). They have APIs for that sort of thing. You can find it by searching on Sun's java site.
If you're just talking about referencing an audio clip via a url to play it, look at Applet.getAudioClip() and Applet.play().
If you're just tallking about doing binary file transfers, look at the java.io and java.nio packages to see how to work with streams and channels.


Rob
SCJP 1.4
Priya Kubher
Greenhorn

Joined: Jan 07, 2003
Posts: 10
Well I want to record with a microphone and transfer it into digital and then send it to another computer for playback. Which api should I use.
Please advise. I appreciate all help.
PMP Kannan
Greenhorn

Joined: Jan 23, 2003
Posts: 10
Hi Priya,
You should use javax.sound.sampled package to record and palyback.
actually this package does not provide any facilities for transmitting sound, but it does give u the API's for capture and play back.
as far as transmitting is concerned u should use sockets to transmit and receive.
If u would like more details do contact
cheers
kannan

In the country of the blind, one eye'd man is the king
Priya Kubher
Greenhorn

Joined: Jan 07, 2003
Posts: 10
Hi,
iam trying to record from microphone to a buffer like
byte []buffer;
The recording part works. Kannan I followed your advise and used java.sound.sampled package.
and then trying to send it over the net using udp.
I trying the the following code at client side
ds = new DatagramSocket(serverPort);
InetAddress Addr1 = InetAddress.getByName("c1635229-a");
ds.send(new DatagramPacket(abBuffer,pos,Addr1,clientPort));
and this code at server side
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
ds.receive(p);
abBuffer = p.getData();
This code worked fine when i was using to read and write from command line....but does not work for sound, My program does not deliver the sound to the server side. Also I am using TargetDataLine to read into the buffer and iam sending abBuffer to SourceDataLine to write output to the speakers.
Please tell me what I am doing wrong...
Thanks a million
PMP Kannan
Greenhorn

Joined: Jan 23, 2003
Posts: 10
hi Priya,
if u could give the full code to me, i will check it up and tell u where u went wrong. actually i have done a audio conference application and it works fine.

cheers
kannan

Working together works
Gulzar Hussain
Greenhorn

Joined: Jul 10, 2003
Posts: 4
hi
i m trying to do the same and having problems, can somebody post the code
NarendrA sINGH
Greenhorn

Joined: Jul 24, 2003
Posts: 2
Priya, Use java.nio.* in 1.4 package it has several classes which enable you to transmit big data
serge masse
Ranch Hand

Joined: Jul 23, 2003
Posts: 102
the project http://www.sourceforge.net/projects/c2h/ contains api Leafy, a generic api that does send audio from one machine to another via tcp sockets; it converts the audio voltages to frequency numbers prior to sending and reconverts the frequencies into audio in the speaker, all in Java.
"The wireless telegraph is not difficult to understand. The ordinary telegraph is like a very long cat. You pull the tail in New York, and it meows in Los Angeles. The wireless is the same, only without the cat." - Albert Einstein


serge - http://goo.gl/GgF7R - my android apps
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I am trying to write a voice chat using JMF. In JMF there is an implementation of the Real-Time Transport Protocol and RTCP (for control). They allow the streaming of audio and video over the Internet. If you are using just UDP, should'nt write yourself what RTP-RTCP already provide? for instance reordering the packets out of sequence.
Below is a test code that should work in theory.
I have been able to make two participants in different computers to talk to each other in a LAN. But the third one has problems. The difficulty I have found is that RTPManager seems not being issuing all the events. When a stream is received from a remote participant I listen for any of three events from which I could extract the stream to play. Sometimes none of these events occurr.
Another problem I have -initially- avoided is that if Participant A has started the program first, he will receive the stream from Participant B; but B will not receive the one from A unless both programs has been started within the same minute. In fact that lapse is even fewer, maybe 45 seconds.
I have found that closing and starting the sending stream from A makes B to receive it.
Here is the code you can test it and feedback please.


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Well its a long time since my last post but all is well if ends well. The following code is working nicely in a LAN.
sorimachi richard
Greenhorn

Joined: Feb 19, 2004
Posts: 1
Hi, im developing a real-time voice communication program. However, this program is designed to work in Internet environment. So is the LAN voice chatting program could be work fine as well in Internet environment? any motification of the source code must be made?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch sorimachi.
No modification should be needed for Internet as long as you have IP conectivity. If a participant is behind an ADSL router's NAT the ports 5004 and 5005 should be opened and directed to the proper computer.
I have tried to test this code in the Internet but the IPs given to us by Telefonica at Spain do not respond to ping.
Is there anyone kind enough to test it on the Internet?
yk long
Greenhorn

Joined: Feb 21, 2004
Posts: 4
hi Jose Botella,
i try your code that your post on above, but when i execute the error such as "assert test.sop("The processor has been configured");" the line code that have "assert test.sop" cannot detect in my PC. why. i'm the student that doing my education project for develop voice chat application, since i face one problem, the server only can send voice to client( more than two), but the client cannot send back the voice to the server. pls help me to solve the problem. all the code is working.
i would like to know why, and what should i do in my coding. pls reply email yklong81@hotmail.com or in this website i will refer it.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch yk long!
In order to compile the program you will need javac -source 1.4 and to run it java -ea
Notice that the use of the assertion facility is not very orthodox. The java.util.logging package would be likely more adecuate.
[ February 22, 2004: Message edited by: Jose Botella ]
yk long
Greenhorn

Joined: Feb 21, 2004
Posts: 4
hi Jose Botella,
i'm tried your code that you post on top. i find that have some echo in my PC. i never test with three PC together. i just want to request some information. Do you test more than three PC together. what is the result?
i would like to know about it. pls let me know. thank
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No I have never tested it with more than three computers.

In order to reduce the echo try using a headset with microphone and speakers built in, instead of a microphone and separate speakers.
If you have some buddy we could test it on the Internet.
yk long
Greenhorn

Joined: Feb 21, 2004
Posts: 4
hi Jose Botella,
i would like to request your advice about, how to develop the function of volume control and microphone volume control??? i mean to capture to microphone input and spreaker ouput by using JMF or other.
second, the system that i develop now is using multicast, is it the ip for multicast should be set like( e.g 224.12.12.32), cannot set in (e.g 10.2.34.34). the first number should in three digit and cannot in two digit???. furthermore, i found that most of the multicast ip adress is using like (224.12.23.45) this kind of way. my question in this part is, is it the multicast_IP should set in three digit???
from yklong
yk long
Greenhorn

Joined: Feb 21, 2004
Posts: 4
hi,
one question what is "SCJP2. Please Indent your code using UBB Code"???
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
With this program I played what the microphone was capturing. Unfortunately the implementation doesn't seem to provide a GainControl to adjust the volume. At least the control component displayed allows either stopping the playing or setting it to mute.

For more information please read the JMF 2.1.1 Software Documentation
An search in Google for Multicast overview yields a number of documents. for instance This one from Cisco:

IP Multicast Group Addressing
Unlike Class A, B, and C IP addresses, the last 28 bits of a Class D address have no structure. The multicast group address is the combination of the high-order 4 bits of 1110 and the multicast group ID. These are typically written as dotted-decimal numbers and are in the range 224.0.0.0 through 239.255.255.255. Note that the high-order bits are 1110. If the bits in the first octet are 0, this yields the 224 portion of the address.
The set of hosts that responds to a particular IP multicast address is called a host group. A host group can span multiple networks. Membership in a host group is dynamic-hosts can join and leave host groups. For a discussion of IP multicast registration, see the section called "Internet Group Management Protocol."
Some multicast group addresses are assigned as well-known addresses by the Internet Assigned Numbers Authority (IANA). These multicast group addresses are called permanent host groups and are similar in concept to the well-known TCP and UDP port numbers. Address 224.0.0.1 means "all systems on this subnet," and 224.0.0.2 means "all routers on this subnet." Groups in the range of 224.0.0.xxx are always sent with a TTL of 1. Groups in the range of 224.0.1.xxx are reserved for protocol operations and sent with normal TTLs.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You can have a signature added to the end of your posts by setting it up in the MyProfile URL, then click View/Update Profile.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I forgot to paste the output of the program:

[com.sun.media.protocol.dsound.DirectSoundStream$FC@5e3974, com.sun.media.protocol.dsound.DirectSoundStream$BC@df503]
1
LINEAR, 48000.0 Hz, 16-bit, Stereo, LittleEndian, Signed
com.sun.media.ui.DefaultControlPanel[,0,0,74x21,invalid,layout=java.awt.BorderLayout]
null
[com.sun.media.protocol.dsound.DirectSoundStream$FC@5e3974, com.sun.media.protocol.dsound.DirectSoundStream$BC@df503, com.sun.media.renderer.audio.DirectAudioRenderer$MCA@af993e, com.sun.media.renderer.audio.AudioRenderer$BC@75e4fc, com.sun.media.PlaybackEngine$BitRateA@c62c8, com.sun.media.BasicJMD[panel0,0,0,512x200,invalid,layout=java.awt.BorderLayout], com.sun.media.PlaybackEngine$PlayerTControl@12940b3]
com.sun.media.renderer.audio.DirectAudioRenderer$MCA@af993e

I hoped to have seen a GainControl somewhere.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Originally posted by Jose Botella:
In order to reduce the echo try using a headset with microphone and speakers built in, instead of a microphone and separate speakers.

Hi, Jose, could you tell me which brand / model of this headset it is ?
Thanks
Edward
Tedd Alevo
Greenhorn

Joined: Mar 18, 2004
Posts: 4
Hi Jose,
thank you for your code. Unfortunately I experience serious, buildung up echo, though all participants (two in fact) use a head set.
As far as I understood your code, you do the following:
1.) Get the capture devices (cd) and select the first
2.) Create a DataSource (ds) from the cd
3.) Put the ds in a processor to convert the format to smth. streamable
4.) Uppon receipt of the first recipient build the output stream and transmit only what leaves the processor, created in 3.)
Any idea where this echo could originate from?
TIA
Tedd Alevo
Greenhorn

Joined: Mar 18, 2004
Posts: 4
This is really weired... I tested it on a different network with two different PCs and it works absolutely correct. Is it possible that one of the two PCs in the non-working configuration have a bugged soundcard or something like that?
Ali Web
Greenhorn

Joined: Mar 13, 2004
Posts: 3
Hi
I would like to have your opinion guys especially of Jose.
I am trying to develop a chatting applet that will allow users to do voice and video chat.
I want to make sure that all all the browsers support my chat application without having to install the latest JVM. Most old browsers on Windows have MS JVM 1.1.4.
The problem with installing latest JVM which is 1.4 is that it is 15 MB in size and takes ages to download on a dialup connection.
For this reason I am sticking to AWT instead of Swing for GUI. If I use Swing then it won't work on browsers having MS JVM 1.1.4 which I don't want.
So far so good but now I am facing problems incorporating audio and video features as I will have to use Java Media which will require all the client browsers to update their JVM isn't it!?
Is there a way through which I make sure that the audio & video works on the older versions of JVM?
If you take a look at this chatting software called GlassRoom http://www.chatessentials.com you will find that it works on IE 5 with MS JVM 1.1.4. It has audio chat feature.
Please give me your opinions and suggestions.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Tedd.
My recommendation about using a headset intended to avoid that the microphone transmits what the loudspeakers play. When using the control componet provided by JMF we can stop our transmission at the time we are listening the remote peer. That would definitely stop echo!
Some general guidelines about echo:
In voice over IP the echo results from an analog leakage from the transmit path to the reception part. If the bits were to leak we weren't able to recognize our voice. Instead a noise woul be heard, maybe making impossible the comunication.
Echo is only audible with a certain delay and certain power in the signal.
That was the easy part. Now applying the above to our problem:
Yes maybe the sound card could originate echo, or maybe the headset if it has bad quality.
Echo cancellers can be built into the codecs. Maybe the ones used in a computers were not good enough.
The roundtrip delay in a network may cause that an echo that is not a problem becames audible. Try experimenting with BufferControl and PacketsizeControl to reduce the echo.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Ali.
The MVM 1.1.4 is not supported by JMF for Windows. This is one of the requirements published in the JMF site:
"JDK 1.1.3 or later for Windows (from Sun). JDK 1.1.6 or later is required for Y2K compatibility."
[ March 28, 2004: Message edited by: Jose Botella ]
German Andres
Greenhorn

Joined: Mar 29, 2004
Posts: 1
Hi Jose, can you please give a clue about howto to compile and test your code ?
Many thanks in advance.
Mancho.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch German.
You will need to change the addresses to match yours, and compile with:

[ April 01, 2004: Message edited by: Jose Botella ]
Jens Kallup
Greenhorn

Joined: Apr 25, 2004
Posts: 3
Hello Jose,
i have rename the package in the source as "test", compile it fine, but get this message:
C:\Programme\xampp\htdocs\java>java TestRTPManager3
Exception in thread "main" java.lang.NoClassDefFoundError: TestRTPManager3 (wron
g name: test/TestRTPManager3)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
what can i do ?
thanks
Jens
Jens Kallup
Greenhorn

Joined: Apr 25, 2004
Posts: 3
okay, have forgot to make directories and copy the files there ...
Vijay Arora
Greenhorn

Joined: May 11, 2004
Posts: 4
Hi Jose,
I want to design a voice recording applet
using JMF. can u plz send me the sample
code for that as i m new to JMF.
plz help me out.
thanx in advance
imran raza
Greenhorn

Joined: Jun 11, 2004
Posts: 1
hi guys,

i read whole thread, there was very good dicussion, i also want to pua a question , could we detect silence by java sound API not JMF.


any help???
Shyam Bhand
Greenhorn

Joined: Jun 28, 2004
Posts: 1
Hi guyes,

This is my first time tow ork on JMF. I am facing one starnge problem with the applet that i have developed using JFM and Swing. When i tried to access my Web based applet on a machine where JMF is installed it worked fine. But when i tried to access it on fresh machien where i have only Java Runtimes , it will not able to find any device. Can anyone guide me regarding the process.
Is it a security issue? or it is related with JMF installation?
 
wood burning stoves
 
subject: Audio in Java