|
1 | 1 | package lemmesay.voice; |
2 | 2 |
|
3 | 3 | import arc.util.Log; |
| 4 | +import arc.util.Threads; |
4 | 5 | import lemmesay.voice.microphone.JavaxMicrophone; |
5 | 6 | import mindustry.Vars; |
6 | 7 |
|
7 | | -public class VoiceManager implements Runnable { |
| 8 | +public class VoiceManager{ |
8 | 9 |
|
9 | 10 | // WARNING: LAZY Implement. |
10 | 11 | private static final int SAMPLE_RATE = 48000; |
11 | 12 | private static final int CHANNELS = 1; |
12 | 13 | private static final int BUFFER_SIZE = 960; |
13 | 14 |
|
14 | | - @Override |
15 | | - public void run() { |
16 | | - JavaxMicrophone mic = new JavaxMicrophone(SAMPLE_RATE, BUFFER_SIZE, ""); |
17 | | - mic.open(); |
18 | | - mic.start(); |
19 | | - Log.info("Microphone capture started for voice chat."); |
20 | | - while (true) { |
21 | | - if (!Vars.net.client()) { |
22 | | - Log.info("Voice chat disconnected, stopping microphone capture."); |
23 | | - mic.close(); |
24 | | - VoiceProcessing.getInstance().close(); |
25 | | - Log.info("Voice processing resources released."); |
26 | | - break; |
27 | | - } |
28 | | - try { |
29 | | - short[] audioData = mic.read(); |
30 | | - VoiceProcessing.getInstance().handleAudio(audioData); |
31 | | - Thread.sleep(20); |
32 | | - } catch (IllegalStateException e) { |
33 | | - Log.warn("Failed to read from microphone: " + e.getMessage()); |
34 | | - } catch (InterruptedException e) { |
35 | | - Thread.currentThread().interrupt(); |
36 | | - mic.close(); |
37 | | - Log.info("Voice chat thread interrupted, stopping microphone capture."); |
38 | | - break; |
| 15 | + public static void startVoiceThread() { |
| 16 | + Log.info("Starting voice communication thread."); |
| 17 | + Threads.daemon(VoiceThread::new); |
| 18 | + Vars.player.usid(); |
| 19 | + } |
| 20 | + |
| 21 | + public static class VoiceThread implements Runnable { |
| 22 | + @Override |
| 23 | + public void run() { |
| 24 | + JavaxMicrophone mic = new JavaxMicrophone(SAMPLE_RATE, BUFFER_SIZE, ""); |
| 25 | + mic.open(); |
| 26 | + mic.start(); |
| 27 | + Log.info("Microphone capture started for voice chat."); |
| 28 | + while (true) { |
| 29 | + if (!Vars.net.client()) { |
| 30 | + Log.info("Voice chat disconnected, stopping microphone capture."); |
| 31 | + mic.close(); |
| 32 | + VoiceProcessing.getInstance().close(); |
| 33 | + Log.info("Voice processing resources released."); |
| 34 | + break; |
| 35 | + } |
| 36 | + try { |
| 37 | + short[] audioData = mic.read(); |
| 38 | + VoiceProcessing.getInstance().handleAudio(audioData); |
| 39 | + Thread.sleep(20); |
| 40 | + } catch (IllegalStateException e) { |
| 41 | + Log.warn("Failed to read from microphone: " + e.getMessage()); |
| 42 | + } catch (InterruptedException e) { |
| 43 | + Thread.currentThread().interrupt(); |
| 44 | + mic.close(); |
| 45 | + Log.info("Voice chat thread interrupted, stopping microphone capture."); |
| 46 | + break; |
| 47 | + } |
39 | 48 | } |
40 | 49 | } |
41 | 50 | } |
|
0 commit comments