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

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  
          lines = AudioSystem.getMixer(mixerInfo[i]).getSourceLineInfo();
30  
          for (int j = 0; j < l(lines); j++)
31  
            print("  Source line " + j + ": " + lines[j]);
32  
            
33  
          print();
34  
        }
35  
        
36  
        audioFormat = getAudioFormat();
37  
        Mixer mixer = AudioSystem.getMixer(mixerInfo[5]);
38  
39  
        /*DataLine.Info dataLineInfo = new DataLine.Info(
40  
                TargetDataLine.class, audioFormat);*/
41  
        Line.Info dataLineInfo = mixer.getTargetLineInfo()[0];
42  
        
43  
        targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
44  
        targetDataLine.open(audioFormat);
45  
        targetDataLine.start();
46  
47  
        Thread captureThread = new CaptureThread();
48  
        captureThread.start();
49  
50  
}
51  
52  
class CaptureThread extends Thread {
53  
54  
    byte tempBuffer[] = new byte[10000];
55  
56  
    @Override
57  
    public void run() {
58  
        stopCapture = false;
59  
        try {
60  
            while (!stopCapture) {
61  
                int cnt = targetDataLine.read(tempBuffer, 0,
62  
                        tempBuffer.length);
63  
                //out.write(tempBuffer);
64  
                //print("Bytes received: " + tempBuffer.length);
65  
                print("min=" + min(tempBuffer) + ", max=" + max(tempBuffer));
66  
           }
67  
           } catch (Exception e) {
68  
            System.out.println(e);
69  
            System.exit(0);
70  
        }
71  
    }
72  
}
73  
74  
private AudioFormat getAudioFormat() {
75  
    float sampleRate = 8000.0f;
76  
    int sampleSizeInBits = 8;
77  
    int channels = 1;
78  
    boolean signed = true;
79  
    boolean bigEndian = false;
80  
    
81  
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
82  
            bigEndian);
83  
}
84  
}

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