MCPcopy Create free account
hub / github.com/dashscope/dashscope-sdk-java / AudioRecognitionUsage

Class AudioRecognitionUsage

samples/AudioRecognitionUsage.java:21–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19import io.reactivex.schedulers.Schedulers;
20
21public final class AudioRecognitionUsage {
22 private final AudioFormat format = buildAudioFormatInstance();
23 private volatile boolean stopped;
24
25 public TargetDataLine getTargetDataLineForRecord() throws LineUnavailableException {
26 TargetDataLine microphone = AudioSystem.getTargetDataLine(format);
27 System.out.println(microphone.getBufferSize());
28 microphone.open();
29 return microphone;
30 }
31
32 public Flowable<ByteBuffer> getMicrophoneStreaming() {
33 Flowable<ByteBuffer> audios =
34 Flowable.<ByteBuffer>create(
35 emitter -> {
36 try {
37 final TargetDataLine line = getTargetDataLineForRecord();
38 int frameSizeInBytes = format.getFrameSize();
39 System.out.println(
40 String.format(
41 "Frame size in bytes: %s, %s", frameSizeInBytes, format.getEncoding()));
42 final int bufferLengthInBytes = 1024 * frameSizeInBytes;
43 int numBytesRead;
44 line.start();
45 int c = 0;
46 byte[] data = new byte[bufferLengthInBytes];
47 while (!stopped && c < 100) {
48 if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
49 break;
50 } else {
51 System.out.println(String.format("Read microphone %s data", numBytesRead));
52 emitter.onNext(ByteBuffer.wrap(data, 0, numBytesRead));
53 }
54 if (c % 10 == 0) {
55 System.out.println(String.format("Sending %d packages", c));
56 }
57 ++c;
58 }
59 emitter.onComplete();
60 } catch (Exception ex) {
61 ex.printStackTrace();
62 emitter.onError(ex);
63 }
64 },
65 BackpressureStrategy.BUFFER);
66 return audios;
67 }
68
69 public AudioFormat buildAudioFormatInstance() {
70 final AudioFormat.Encoding ENCODING = AudioFormat.Encoding.PCM_SIGNED;
71 final float RATE = 16000.0f;
72 final int CHANNELS = 1;
73 final int SAMPLE_SIZE = 16;
74 final boolean BIG_ENDIAN = false;
75 return new AudioFormat(ENCODING, RATE, SAMPLE_SIZE, CHANNELS, 2, RATE, BIG_ENDIAN);
76 }
77
78 public Flowable<ByteBuffer> getStreamingDataFromFile(String filePath) {

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected