Alejandro Acuña
2024-10-25 e51f4a713ed6e744c203c9493165584728a29c52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package testaudio;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
 
public class CaptureAudio implements Runnable
{
    // Capture Class
 
    final int bufSize = 16384;
    AudioInputStream audioInputStream;
    String errStr;
    double duration, seconds; 
 
    TargetDataLine line;
 
    Thread thread;
    String ip = "";
    int port = 15100;
    int tiempoEsperaServidorReceptorAudio = 5000;
    
    private float rate = 44100.0f;
    private int channels = 2;
    private int sampleSize = 16;
    private Boolean bigEndian = true;
  
    private List<AudioFormat> lFormat = null;
 
    public CaptureAudio(String ip, int port, int tiempoEspera)
    {
        this.ip = ip;
        this.port = port;
        this.tiempoEsperaServidorReceptorAudio = tiempoEspera;
    }
 
    public CaptureAudio()
    {
    }
    
    public CaptureAudio(float rate, int channels, int sampleSize, Boolean bigEndian)
    {
        this.rate = rate;
        this.channels = channels;
        this.sampleSize = sampleSize;
        this.bigEndian = bigEndian;
    }
 
    public void start()
    {
        errStr = null;
        thread = new Thread(this);
        thread.setName("Capture");
        thread.start();
    }
 
    public void stop()
    {
        thread = null;
    }
 
    private void shutDown(String message)
    {
        if ((errStr = message) != null && thread != null)
        {
            thread = null;
            System.err.println(errStr);
        }
    }
 
    public void run()
    {
        duration = 0;
        audioInputStream = null;
 
        // define the required attributes for our line,
        // and make sure a compatible line is supported.
        javax.sound.sampled.AudioFormat.Encoding encoding = javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
 
//        javax.sound.sampled.AudioFormat format = new javax.sound.sampled.AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8)
//                * channels, rate, bigEndian);
        javax.sound.sampled.AudioFormat format = new javax.sound.sampled.AudioFormat(8000.0f, 16, 1, true, true);
 
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
 
        if (!AudioSystem.isLineSupported(info))
        {
            System.out.println("Line matching " + info + " not supported.");
            shutDown("Line matching " + info + " not supported.");
            return;
        }
 
        // get and open the target data line for capture.
        try
        {
            line = AudioSystem.getTargetDataLine(format);
            line.open(format, line.getBufferSize());
        } catch (LineUnavailableException ex)
        {
            System.out.println("ERROR -> Line unavailable");
            shutDown("Unable to open the line: " + ex);
            return;
        } catch (SecurityException ex)
        {
            shutDown(ex.toString());
            //JavaSound.showInfoDialog();
            return;
        } catch (Exception ex)
        {
            shutDown(ex.toString());
            return;
        }
 
        // play back the captured audio data
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int frameSizeInBytes = format.getFrameSize();
        int bufferLengthInFrames = line.getBufferSize() / 8;
        int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
        byte[] data = new byte[bufferLengthInBytes];
        int numBytesRead;
 
        line.start();
 
        Socket socket = null;
 
        try
        {
            long i1 = System.currentTimeMillis();
            while (thread != null)
            {
                if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1)
                {
                    break;
                }
                out.write(data, 0, numBytesRead);
 
                if ((System.currentTimeMillis() - i1) > this.tiempoEsperaServidorReceptorAudio)
                {
                    i1 = System.currentTimeMillis();
                    byte[] stream = out.toByteArray();
                    System.out.println("SEND: " + stream.length);
                    socket = new Socket(this.ip, this.port);
                    socket.getOutputStream().write(stream);
                    out.reset();
                    socket.getOutputStream().flush();
                    try
                    {
                        socket.close();
                    } catch (Exception e)
                    {
                    };
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
 
        // we reached the end of the stream.
        // stop and close the line.
        line.stop();
        line.close();
        line = null;
 
        // stop and close the output stream
        try
        {
            out.flush();
            out.close();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        }
 
        // load bytes into the audio input stream for playback
        byte audioBytes[] = out.toByteArray();
 
        try
        {
            try
            {
                socket.close();
            } catch (Exception e)
            {
            };
        } catch (Exception e)
        {
            e.printStackTrace();
        }
 
        long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format
                .getFrameRate());
        duration = milliseconds / 1000.0;
 
        try
        {
            audioInputStream.reset();
        } catch (Exception ex)
        {
            ex.printStackTrace();
            return;
        }
 
    }
 
}