Java: Processing audio in real time from speakers

£可爱£侵袭症+ 提交于 2021-02-08 06:39:39

问题


I'm working on a project that will change the brightness of keyboard lights with PWM signal, according to output sound levels. That trouble is, I need the output stream in real time. So far I used javax.sound.sampled package and I never succeeded grabbing the audio output. However, what have I done was to use targetDataLine and sourceDataLine, which don't look like the way to go, I'm still trying though. What I need is an audio stream provider, which my java app can "hear" and process it. So far, I viewed many tutorials, videos, posts, some articles (like this one: https://cr.openjdk.java.net/~iris/se/12/latestSpec/api/java.desktop/javax/sound/sampled/class-use/Mixer.Info.html ) etc. but no results. Has somebody done it before ? Or is out there a library other than sampled library ? Any help would be appreciated.

The error I get for every single format:

java.lang.IllegalArgumentException: Line unsupported: interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
    at com.sun.media.sound.DirectAudioDevice.getLine(Unknown Source)
    at javax.sound.sampled.AudioSystem.getTargetDataLine(Unknown Source)

The code used:

package audio;
import javax.sound.sampled.*;

public class App1 {
    public static String getWord(String arr) {
        int i = 0;
        for(; i < arr.length(); i++) {
            if (arr.charAt(i) == ' ')
                break;
        }
        return arr.substring(0,i);
    }

    public static void wait(int ms) {
        try{
            Thread.sleep(ms);
        }
        catch(InterruptedException ex){
            Thread.currentThread().interrupt();
        }
    }

    public static AudioFormat getAudioFormat(){
        float sampleRate = 44100;
        //8000,11025,16000,22050,44100
        int sampleSizeInBits = 16;
        //8,16
        int channels = 1;
        //1,2
        boolean signed = true;
        //true,false
        boolean bigEndian = false;
        //true,false
        //return new AudioFormat(Encoding.PCM_SIGNED, sampleRate, 16, 1, 2, sampleRate, false);
        return new AudioFormat(sampleRate,
                               sampleSizeInBits,
                               channels,
                               signed,
                               bigEndian);
    }

    public static void main(String [] args) {
        try {
            //-----DECLARATIONS-----
            TargetDataLine targetDataLine = null;
            Mixer.Info[] mixers = AudioSystem.getMixerInfo();
            Mixer.Info m = null;
            String expectedMixer = "Speakers";      

            //-----MIXER FINDER-----
            System.out.println("Number of mixers: " + mixers.length);
            for(int i = 0; i < mixers.length; i++) {
                System.out.println(getWord(mixers[i].getName()));
                if(getWord(mixers[i].getName()).compareTo(expectedMixer) == 0){
                    m = mixers[i];
                }
            }
            if(m==null) throw new Exception("No such mixer found: " + expectedMixer);
            else System.out.println('\n'+"Device choosen: "+m.getName());

            //-----LINE TESTER-----
            boolean v = false, showError = true; // show error or keep trying several times
            int tries = 3, i = 0;
            while(v==false && i++ < tries){
                try {
                    //sourceDataLine = AudioSystem.getTargetDataLine(getAudioFormat(), m);
                    targetDataLine = AudioSystem.getTargetDataLine(getAudioFormat(), m);
                    targetDataLine.open(getAudioFormat());
                    v=true;
                    //System.out.println("Success!");
                } catch (IllegalArgumentException e){
                    if (showError) {
                        v = true;
                        e.printStackTrace();
                    }
                    else {
                        System.out.println("Error! Retrying...  "+i+'/'+tries);
                        v = false;
                    }
                    wait(2000);
                }
            }
            if(i-1==tries)
                //System.out.println("No success :(");
                throw new Exception("No success :(");
            else
                if(v==false)
                    System.out.println("SourceData line found and accepted !");

            //-----SIGNAL PROCESSING-----
            //nothing here because the rest isn't working

        } catch(Exception e) { e.printStackTrace();} 
    }
}

Lately, I've been searching for some piece of code to discover available formats, and I've made something between my code and edoloughlin's code:

package audio;
import javax.sound.sampled.*;

public class App2 {
    public static String getWord(String arr) {
        int i = 0;
        for(; i < arr.length(); i++) {
            if (arr.charAt(i) == ' ')
                break;
        }
        return arr.substring(0,i);
    }

    public static void wait(int ms) {
        try{
            Thread.sleep(ms);
        }
        catch(InterruptedException ex){
            Thread.currentThread().interrupt();
        }
    }

    public static AudioFormat getAudioFormat(){
        float sampleRate = 44100;
        //8000,11025,16000,22050,44100
        int sampleSizeInBits = 16;
        //8,16
        int channels = 1;
        //1,2
        boolean signed = true;
        //true,false
        boolean bigEndian = false;
        //true,false
        //return new AudioFormat(Encoding.PCM_SIGNED, sampleRate, 16, 1, 2, sampleRate, false);
        return new AudioFormat(sampleRate,
                               sampleSizeInBits,
                               channels,
                               signed,
                               bigEndian);
    }

    public static void main(String [] args) {
        try {
            String expectedMixer = "Speakers";
            Mixer.Info[] mixers = AudioSystem.getMixerInfo();
            Mixer.Info m = null;
            for(int i = 0; i < mixers.length; i++) {
                if(getWord(mixers[i].getName()).compareTo(expectedMixer) == 0){
                    m = mixers[i];
                }
            }

            int sampleRates[] = { 8000, 11025, 16000, 22050, 44100 };
            int channels[] = { 1, 2 };
            int bytesPerSample[] = { 1, 2 };
            boolean signature[] = {true, false};

            AudioFormat format;
            DataLine.Info lineInfo;


           //for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
                for (int a = 0; a < sampleRates.length; a++) {
                    for (int b = 0; b < channels.length; b++) {
                        for (int c = 0; c < bytesPerSample.length; c++) {
                            for(int d = 0; d < signature.length; d++) {
                                format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                                        (float)sampleRates[a], 8 * bytesPerSample[c], channels[b], bytesPerSample[c],
                                        (float)sampleRates[a], signature[d]);
                                lineInfo = new DataLine.Info(/*dataLineClass*/TargetDataLine.class, format);
                                if (AudioSystem.isLineSupported(lineInfo)) {
                                    /*
                                     * TODO: To perform an exhaustive search on supported lines, we should open
                                     * TODO: each Mixer and get the supported lines. Do this if this approach
                                     * TODO: doesn't give decent results. For the moment, we just work with whatever
                                     * TODO: the unopened mixers tell us.
                                     */
                                    if (AudioSystem.getMixer(/*mixerInfo*/m).isLineSupported(lineInfo)) {
                                        //formats.add(format);
                                        System.out.println(format);
                                    }
                                }
                            }
                        }
                    }
                }
           //}
        }catch(Exception e) {e.printStackTrace();}
    }
}

As shown above, I used many format combinations, but nothing gets printed out in the console. I wonder if my system/java app supports such a task. If not, is there a way to achieve the goal ? (read real-time output)

来源:https://stackoverflow.com/questions/59916365/java-processing-audio-in-real-time-from-speakers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!