| 63 | Files.newOutputStream(output, StandardOpenOption.CREATE_NEW))) { |
| 64 | QwenLiveConversation conversation = new QwenLiveConversation(param, new QwenLiveCallback() { |
| 65 | @Override public void onEvent(JsonObject event) { |
| 66 | synchronized (audioFile) { |
| 67 | if (!accepting.get()) return; |
| 68 | try { |
| 69 | String type = event.has(FIELD_TYPE) ? event.get(FIELD_TYPE).getAsString() : ""; |
| 70 | if (EVENT_SESSION_UPDATED.equals(type)) configured.countDown(); |
| 71 | if (EVENT_SESSION_CLOSED.equals(type)) { |
| 72 | onError(new IOException("Session ended before audio idle window.")); |
| 73 | } |
| 74 | if (EVENT_SESSION_OUTPUT_AUDIO_DELTA.equals(type)) { |
| 75 | JsonElement audio = event.get(FIELD_AUDIO); |
| 76 | if (audio == null || audio.isJsonNull()) audio = event.get(FIELD_DELTA); |
| 77 | if (audio == null || audio.isJsonNull()) throw new IOException("Audio event has no audio/delta data."); |
| 78 | byte[] bytes = Base64.getDecoder().decode(audio.getAsString()); |
| 79 | if (bytes.length > 0) { |
| 80 | audioFile.write(bytes); |
| 81 | lastAudio.set(System.nanoTime()); |
| 82 | } |
| 83 | } |
| 84 | } catch (IOException | RuntimeException e) { onError(e); } |
| 85 | audioFile.notifyAll(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override public void onClose(int code, String reason) { |
| 90 | onError(new IOException("WebSocket closed before output settled: " + code)); |