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

43
LINES

< > BotCompany Repo | #1007032 // captureAudioFromLine2

JavaX fragment (include)

1  
sinterface Capturer {
2  
  void stop();
3  
  byte[] getPCM();
4  
  AudioFormat format();
5  
}
6  
7  
static Capturer captureAudioFromLine2(DataLine line, final AudioFormat format) ctex {
8  
  if (!line instanceof TargetDataLine)
9  
    fail(getClassName(line));
10  
  final TargetDataLine tdl = cast line;
11  
  tdl.open(format);
12  
  tdl.start();
13  
  ret new Capturer {
14  
    volatile bool stopped;
15  
    new ByteArrayOutputStream baos;
16  
    
17  
    public void stop() { stopped = true; }
18  
    
19  
    {
20  
      thread "Audio Capture" {
21  
        try {
22  
          int total = 0, n;
23  
          byte[] buf = new byte[1024];
24  
          while (!stopped && licensed() && (n = tdl.read(buf, 0, l(buf))) > 0) {
25  
            total += n;
26  
            baos.write(buf, 0, n);
27  
            //print("Bytes read from line: " + total);
28  
          }
29  
        } finally {
30  
          tdl.close();
31  
        }
32  
      }
33  
    }
34  
    
35  
    public byte[] getPCM() {
36  
      ret baos.toByteArray();
37  
    }
38  
    
39  
    public AudioFormat format() {
40  
      ret format;
41  
    }
42  
  };
43  
}

Author comment

Began life as a copy of #1007026

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: #1007032
Snippet name: captureAudioFromLine2
Eternal ID of this version: #1007032/7
Text MD5: b8d0ee5797a90281e128f4c1122cd486
Author: stefan
Category: javax / audio
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-26 00:28:02
Source code size: 1023 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 576 / 565
Version history: 6 change(s)
Referenced in: [show references]