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

84
LINES

< > BotCompany Repo | #1003203 // Java Sound Test (Capture)

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

Libraryless. Click here for Pure Java version (236L/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]);
            
          lines = AudioSystem.getMixer(mixerInfo[i]).getSourceLineInfo();
          for (int j = 0; j < l(lines); j++)
            print("  Source line " + j + ": " + lines[j]);
            
          print();
        }
        
        audioFormat = getAudioFormat();
        Mixer mixer = AudioSystem.getMixer(mixerInfo[5]);

        /*DataLine.Info dataLineInfo = new DataLine.Info(
                TargetDataLine.class, audioFormat);*/
        Line.Info dataLineInfo = mixer.getTargetLineInfo()[0];
        
        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);
}
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003203
Snippet name: Java Sound Test (Capture)
Eternal ID of this version: #1003203/1
Text MD5: 599592b6406a073804560ee684ec5969
Transpilation MD5: 18447d219168952179bf47405c051b2d
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-27 02:01:26
Source code size: 2578 bytes / 84 lines
Pitched / IR pitched: No / No
Views / Downloads: 580 / 678
Referenced in: #1003204 - Java Sound Test (Capture) trial 2