Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

78
LINES

< > BotCompany Repo | #1003204 // Java Sound Test (Capture) trial 2

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (230L/2K/7K).

!752

import javax.sound.sampled.*;

p {
  new Client().captureAudio();
}

sclass Client {
boolean stopCapture = false;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
BufferedOutputStream out = null;
BufferedInputStream in = null;
Socket sock = null;

private void captureAudio() ctex {
        /*sock = new Socket("192.168.1.5", 500);
        out = new BufferedOutputStream(sock.getOutputStream());
        in = new BufferedInputStream(sock.getInputStream());*/
        Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
        
        for (int i = 0; i < l(mixerInfo); i++) {
          print("Mixer " + i + ": " + mixerInfo[i]);
          Line.Info[] lines = AudioSystem.getMixer(mixerInfo[i]).getTargetLineInfo();
          for (int j = 0; j < l(lines); j++)
            print("  Target line " + j + ": " + lines[j]);
        }
        
        audioFormat = getAudioFormat();
        Mixer mixer = AudioSystem.getMixer(mixerInfo[5]);

        /*DataLine.Info dataLineInfo = new DataLine.Info(
                TargetDataLine.class, audioFormat);*/
        Line.Info dataLineInfo = mixer.getTargetLineInfo()[3]; // 0 (master) or 3 (PCM)
        
        targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
        targetDataLine.open(audioFormat);
        targetDataLine.start();

        Thread captureThread = new CaptureThread();
        captureThread.start();

}

class CaptureThread extends Thread {

    byte tempBuffer[] = new byte[10000];

    @Override
    public void run() {
        stopCapture = false;
        try {
            while (!stopCapture) {
                int cnt = targetDataLine.read(tempBuffer, 0,
                        tempBuffer.length);
                //out.write(tempBuffer);
                //print("Bytes received: " + tempBuffer.length);
                print("min=" + min(tempBuffer) + ", max=" + max(tempBuffer));
           }
           } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
}

private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0f;
    int sampleSizeInBits = 8;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
            bigEndian);
}
}

Author comment

Began life as a copy of #1003203

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003204
Snippet name: Java Sound Test (Capture) trial 2
Eternal ID of this version: #1003204/1
Text MD5: 6f8536c19963333bd786a7e9becf8574
Transpilation MD5: 6ba520c9808f951b069f5fad40f12101
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-27 01:50:22
Source code size: 2374 bytes / 78 lines
Pitched / IR pitched: No / No
Views / Downloads: 527 / 547
Referenced in: [show references]