Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

SourceDataLine returns wierd sound

Thread view: 
jacksuyu@gmail.com - 02 May 2006 23:13 GMT
I read one wav file, and could play with Clip. But when I tried to use
SourceDataLine, I got wierd sound back, basically the sound is very
quick, 15 sec sound finishes in 1 sec or so.

Also, another question, how to get the pure sound data? the byte array
I read from wav file contains header and other meta information. Is it
possible to seperate wav files's meta data and real sound data?

Thank you.

Here is the code:

import javax.sound.sampled.*;
import javax.sound.sampled.Mixer.*;
import javax.sound.sampled.spi.*;
import java.io.*;
import java.util.*;

public class A {
    public static void main(String args[]) throws Exception {
        Info[] infos = AudioSystem.getMixerInfo();

        Info info = null;
        for (int i = 0; i < infos.length; i++) {
            if (infos[i].getName().equals("Java Sound Audio Engine")) {
                info = infos[i];
            }
        }
        Mixer mixer = AudioSystem.getMixer(info);

        Line.Info lInfos[] = mixer.getSourceLineInfo();
        Line clipLine = null;
        for (int i = 0; i < lInfos.length; i++) {
            if
(lInfos[i].getLineClass().toString().indexOf("javax.sound.sampled.SourceDataLine")
>= 0) {
                clipLine = mixer.getLine(lInfos[i]);
            }
        }

        AudioFileReader a = getReader();
        File f = new File("a.wav");
        AudioFileFormat fmt = a.getAudioFileFormat(f);
        AudioFormat afmt = fmt.getFormat();

        FileInputStream fIn = new FileInputStream(f);
        AudioInputStream fiFmt = new AudioInputStream(new FileInputStream(f),
afmt, f.length());
        Date d = new Date();
        SourceDataLine sLine = ((SourceDataLine)clipLine);
        sLine.open(afmt);

        byte buf[] = new byte[2048];
        int n ;
        sLine.start();

        while (( n = fIn.read(buf)) > 0) {
            sLine.write(buf, 0, n);
            sLine.drain();
        }
        sLine.stop();
    }

    static AudioFileReader getReader() {
     Iterator ps = sun.misc.Service.providers(AudioFileReader.class);
        AudioFileReader mi = null;

        while (ps.hasNext()) {
            mi = (AudioFileReader)ps.next();
            System.out.println(mi);
        }
       
        return mi;
 }
}
Oliver Wong - 03 May 2006 00:17 GMT
>I read one wav file, and could play with Clip. But when I tried to use
> SourceDataLine, I got wierd sound back, basically the sound is very
> quick, 15 sec sound finishes in 1 sec or so.

   Could it be that you've got the sampling rate wrong? A 15 to 1 ratio is
surprising to me though. If your original sound was at 8Khz (telephone
quality), and you played it at 96Khz (DVD-Audio quality), you'd only have a
12 to 1 ratio.

   - Oliver
jacksuyu@gmail.com - 03 May 2006 02:12 GMT
I got the AudioInputStream from wav, I didn't specify them by myself.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.