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).

1  
!752
2  
3  
import javax.sound.sampled.*;
4  
5  
p {
6  
  new Client().captureAudio();
7  
}
8  
9  
sclass Client {
10  
boolean stopCapture = false;
11  
AudioFormat audioFormat;
12  
TargetDataLine targetDataLine;
13  
BufferedOutputStream out = null;
14  
BufferedInputStream in = null;
15  
Socket sock = null;
16  
17  
private void captureAudio() ctex {
18  
        /*sock = new Socket("192.168.1.5", 500);
19  
        out = new BufferedOutputStream(sock.getOutputStream());
20  
        in = new BufferedInputStream(sock.getInputStream());*/
21  
        Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
22  
        
23  
        for (int i = 0; i < l(mixerInfo); i++) {
24  
          print("Mixer " + i + ": " + mixerInfo[i]);
25  
          Line.Info[] lines = AudioSystem.getMixer(mixerInfo[i]).getTargetLineInfo();
26  
          for (int j = 0; j < l(lines); j++)
27  
            print("  Target line " + j + ": " + lines[j]);
28  
        }
29  
        
30  
        audioFormat = getAudioFormat();
31  
        Mixer mixer = AudioSystem.getMixer(mixerInfo[5]);
32  
33  
        /*DataLine.Info dataLineInfo = new DataLine.Info(
34  
                TargetDataLine.class, audioFormat);*/
35  
        Line.Info dataLineInfo = mixer.getTargetLineInfo()[3]; // 0 (master) or 3 (PCM)
36  
        
37  
        targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
38  
        targetDataLine.open(audioFormat);
39  
        targetDataLine.start();
40  
41  
        Thread captureThread = new CaptureThread();
42  
        captureThread.start();
43  
44  
}
45  
46  
class CaptureThread extends Thread {
47  
48  
    byte tempBuffer[] = new byte[10000];
49  
50  
    @Override
51  
    public void run() {
52  
        stopCapture = false;
53  
        try {
54  
            while (!stopCapture) {
55  
                int cnt = targetDataLine.read(tempBuffer, 0,
56  
                        tempBuffer.length);
57  
                //out.write(tempBuffer);
58  
                //print("Bytes received: " + tempBuffer.length);
59  
                print("min=" + min(tempBuffer) + ", max=" + max(tempBuffer));
60  
           }
61  
           } catch (Exception e) {
62  
            System.out.println(e);
63  
            System.exit(0);
64  
        }
65  
    }
66  
}
67  
68  
private AudioFormat getAudioFormat() {
69  
    float sampleRate = 8000.0f;
70  
    int sampleSizeInBits = 8;
71  
    int channels = 1;
72  
    boolean signed = true;
73  
    boolean bigEndian = false;
74  
    
75  
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
76  
            bigEndian);
77  
}
78  
}

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: 597 / 635
Referenced in: [show references]