-
Notifications
You must be signed in to change notification settings - Fork 214
add Qwen tts model support. #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flystar32
wants to merge
20
commits into
agentscope-ai:main
Choose a base branch
from
flystar32:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b1cb244
add tts model support.
flystar32 632bb2c
add qwen tts doc.
flystar32 9212a6f
Merge branch 'main' into main
flystar32 42172b3
Update agentscope-core/src/main/java/io/agentscope/core/model/tts/Das…
flystar32 e30cbea
Update agentscope-core/src/main/java/io/agentscope/core/model/tts/Das…
flystar32 1e0f8a5
Update agentscope-core/src/main/java/io/agentscope/core/model/tts/Aud…
flystar32 d51e076
add unit test
flystar32 f144ead
add java doc and check emit result
flystar32 c46d9de
add java doc
flystar32 59d0f63
add license header
flystar32 02d4aec
unit test support no audio hardware environment
flystar32 bdcc053
add unit test
flystar32 4e9d9f6
add realtime tts support
flystar32 a1361fa
unit test support no audio hardware environment
flystar32 b6f9b71
add unit test
flystar32 1e08adb
update unit test
flystar32 56730c0
Merge branch 'main' into main
flystar32 7bbc67f
update DashScopeRealtimeTTSModel's webscoket
flystar32 c2f31b0
add unit test
flystar32 e3d8b8b
update DashScopeRealtimeTTSModel, change objectmapper to record
flystar32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
456 changes: 456 additions & 0 deletions
456
agentscope-core/src/main/java/io/agentscope/core/hook/TTSHook.java
Large diffs are not rendered by default.
Oops, something went wrong.
326 changes: 326 additions & 0 deletions
326
agentscope-core/src/main/java/io/agentscope/core/model/tts/AudioPlayer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,326 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.agentscope.core.model.tts; | ||
|
|
||
| import io.agentscope.core.message.AudioBlock; | ||
| import io.agentscope.core.message.Base64Source; | ||
| import java.util.Base64; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import javax.sound.sampled.AudioFormat; | ||
| import javax.sound.sampled.AudioSystem; | ||
| import javax.sound.sampled.DataLine; | ||
| import javax.sound.sampled.LineUnavailableException; | ||
| import javax.sound.sampled.SourceDataLine; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Audio player for playing PCM audio data in real-time. | ||
| * | ||
| * <p>This player uses Java Sound API (javax.sound.sampled) to play | ||
| * audio data. It supports both synchronous and asynchronous playback. | ||
| * | ||
| * <p>Example usage: | ||
| * <pre>{@code | ||
| * AudioPlayer player = AudioPlayer.builder() | ||
| * .sampleRate(24000) | ||
| * .build(); | ||
| * | ||
| * player.start(); | ||
| * | ||
| * // Play audio chunks as they arrive | ||
| * ttsModel.push("hello").subscribe(player::play); | ||
| * | ||
| * player.stop(); | ||
| * }</pre> | ||
| */ | ||
| public class AudioPlayer { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(AudioPlayer.class); | ||
|
|
||
| private final int sampleRate; | ||
| private final int sampleSizeInBits; | ||
| private final int channels; | ||
| private final boolean signed; | ||
| private final boolean bigEndian; | ||
|
|
||
| private SourceDataLine line; | ||
| private final AtomicBoolean running = new AtomicBoolean(false); | ||
| private final BlockingQueue<byte[]> audioQueue = new LinkedBlockingQueue<>(); | ||
| private Thread playbackThread; | ||
|
|
||
| private AudioPlayer(Builder builder) { | ||
| this.sampleRate = builder.sampleRate; | ||
| this.sampleSizeInBits = builder.sampleSizeInBits; | ||
| this.channels = builder.channels; | ||
| this.signed = builder.signed; | ||
| this.bigEndian = builder.bigEndian; | ||
| } | ||
|
|
||
| /** | ||
| * Starts the audio player and opens the audio line. | ||
| * | ||
| * @throws TTSException if audio line cannot be opened | ||
| */ | ||
| public void start() { | ||
| if (running.get()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| AudioFormat format = | ||
| new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); | ||
| DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); | ||
|
|
||
| if (!AudioSystem.isLineSupported(info)) { | ||
| throw new TTSException("Audio line not supported: " + format); | ||
| } | ||
|
|
||
| line = (SourceDataLine) AudioSystem.getLine(info); | ||
| line.open(format); | ||
| line.start(); | ||
| running.set(true); | ||
|
|
||
| // Start background playback thread | ||
| playbackThread = new Thread(this::playbackLoop, "audio-player"); | ||
| playbackThread.setDaemon(true); | ||
| playbackThread.start(); | ||
|
|
||
| log.debug("AudioPlayer started with format: {}", format); | ||
| } catch (LineUnavailableException e) { | ||
| throw new TTSException("Failed to open audio line", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Plays audio data synchronously. | ||
| * | ||
| * @param audioData PCM audio data | ||
| */ | ||
| public void playSync(byte[] audioData) { | ||
| if (!running.get()) { | ||
| start(); | ||
| } | ||
| if (line != null && audioData != null && audioData.length > 0) { | ||
| line.write(audioData, 0, audioData.length); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Queues audio data for asynchronous playback. | ||
| * | ||
| * @param audioData PCM audio data | ||
| */ | ||
| public void play(byte[] audioData) { | ||
| if (!running.get()) { | ||
| start(); | ||
| } | ||
| if (audioData != null && audioData.length > 0) { | ||
| boolean enqueued = audioQueue.offer(audioData); | ||
| if (!enqueued) { | ||
| log.warn( | ||
| "Failed to enqueue audio data for asynchronous playback; audio data may be" | ||
| + " dropped."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Plays an AudioBlock. | ||
| * | ||
| * @param audioBlock the audio block to play | ||
| */ | ||
| public void play(AudioBlock audioBlock) { | ||
| if (audioBlock == null || audioBlock.getSource() == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (audioBlock.getSource() instanceof Base64Source) { | ||
| Base64Source source = (Base64Source) audioBlock.getSource(); | ||
| if (source.getData() != null && !source.getData().isEmpty()) { | ||
| byte[] audioData = Base64.getDecoder().decode(source.getData()); | ||
| play(audioData); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Interrupts current playback and clears the queue, but keeps the audio line open. | ||
| * | ||
| * <p>This is useful when you want to immediately stop current playback and start | ||
| * playing new audio without closing and reopening the audio line. Unlike {@link #stop()}, | ||
| * this method keeps the audio line open so new audio can be played immediately. | ||
| */ | ||
| public void interrupt() { | ||
| // Clear the queue immediately | ||
| audioQueue.clear(); | ||
|
|
||
| // Stop and flush the current line | ||
| if (line != null) { | ||
| line.stop(); | ||
| line.flush(); | ||
| line.start(); // Restart to accept new audio | ||
| } | ||
|
|
||
| log.debug("AudioPlayer interrupted (queue cleared, line kept open)"); | ||
| } | ||
|
|
||
| /** | ||
| * Stops the audio player and closes the audio line. | ||
| */ | ||
| public void stop() { | ||
| running.set(false); | ||
|
|
||
| if (playbackThread != null) { | ||
| playbackThread.interrupt(); | ||
| try { | ||
| playbackThread.join(1000); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
|
|
||
| if (line != null) { | ||
| line.drain(); | ||
| line.stop(); | ||
| line.close(); | ||
| line = null; | ||
| } | ||
|
|
||
| audioQueue.clear(); | ||
| log.debug("AudioPlayer stopped"); | ||
| } | ||
|
|
||
| /** | ||
| * Waits for all queued audio to finish playing. | ||
| */ | ||
| public void drain() { | ||
| // Wait for queue to empty | ||
| while (!audioQueue.isEmpty() && running.get()) { | ||
| try { | ||
| Thread.sleep(100); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Drain the audio line | ||
| if (line != null) { | ||
| line.drain(); | ||
| } | ||
| } | ||
|
|
||
| private void playbackLoop() { | ||
| while (running.get()) { | ||
| try { | ||
| byte[] audioData = audioQueue.poll(100, java.util.concurrent.TimeUnit.MILLISECONDS); | ||
| if (audioData != null && line != null) { | ||
| line.write(audioData, 0, audioData.length); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new builder for AudioPlayer. | ||
| * | ||
| * @return a new Builder instance | ||
| */ | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| /** | ||
| * Builder for constructing AudioPlayer instances. | ||
| */ | ||
| public static class Builder { | ||
| private int sampleRate = 24000; | ||
| private int sampleSizeInBits = 16; | ||
| private int channels = 1; | ||
| private boolean signed = true; | ||
| private boolean bigEndian = false; | ||
|
|
||
| /** | ||
| * Sets the sample rate in Hz. | ||
| * | ||
| * @param sampleRate sample rate (e.g., 16000, 24000, 48000) | ||
| * @return this builder | ||
| */ | ||
| public Builder sampleRate(int sampleRate) { | ||
| this.sampleRate = sampleRate; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the sample size in bits. | ||
| * | ||
| * @param sampleSizeInBits bits per sample (e.g., 8, 16) | ||
| * @return this builder | ||
| */ | ||
| public Builder sampleSizeInBits(int sampleSizeInBits) { | ||
| this.sampleSizeInBits = sampleSizeInBits; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the number of audio channels. | ||
| * | ||
| * @param channels 1 for mono, 2 for stereo | ||
| * @return this builder | ||
| */ | ||
| public Builder channels(int channels) { | ||
| this.channels = channels; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets whether the audio data is signed. | ||
| * | ||
| * @param signed true for signed, false for unsigned | ||
| * @return this builder | ||
| */ | ||
| public Builder signed(boolean signed) { | ||
| this.signed = signed; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the byte order of the audio data. | ||
| * | ||
| * @param bigEndian true for big-endian, false for little-endian | ||
| * @return this builder | ||
| */ | ||
| public Builder bigEndian(boolean bigEndian) { | ||
| this.bigEndian = bigEndian; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the AudioPlayer instance. | ||
| * | ||
| * @return a configured AudioPlayer | ||
| */ | ||
| public AudioPlayer build() { | ||
| return new AudioPlayer(this); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
playbackThreadis started as a daemon thread, which means it will be abruptly terminated when the JVM exits, potentially cutting off audio playback mid-stream. Consider implementing graceful shutdown or documenting this behavior, especially since thestop()method exists but may not always be called.