Class SynthesisSession

java.lang.Object
com.alibaba.dashscope.resources.audio.SynthesisSession
All Implemented Interfaces:
AutoCloseable

public final class SynthesisSession extends Object implements AutoCloseable
TTS duplex session (audio design §1.2): ws task protocol, text up, binary audio down. Pull model — no SDK threads.

events() (mixed JsonNode + byte[]) and audioChunks() (audio only) share one single-use reader: whichever claims first (at iterator() time) wins, and any second claim throws SdkException immediately. Frames are consumed on delivery — a reader that starts late sees only the frames not yet consumed, e.g. buffered before stop(). close() is idempotent but the session is NOT restartable (Python parity — asymmetric with ASR). On a shared WsConnection the binary downlink is claimed at start() and released at task end.

  • Method Summary

    Modifier and Type
    Method
    Description
    Iterable<byte[]>
    Audio frames only.
    void
    finish-task with directive:cancel — once only, and never sent after a terminal event.
    void
    cancel(Duration timeout)
     
    void
    Idempotent; detaches from a shared connection — unless closed before the terminal event while owning the binary downlink, which tears the whole shared connection down.
    Mixed stream: JsonNode for text events (task-finished included), byte[] for audio frames.
    void
    continue-task {input:{flush:true}} — synthesize buffered text without ending.
    First text sent to first audio/result; null until both happened.
    finish-task to task-finished; null until both happened.
    com.fasterxml.jackson.databind.JsonNode
    The last text frame received (raw, header included) — recorded before error rethrow (§6).
     
     
     
    void
    continue-task carrying payload.input.text (2.x SpeechSynthesizerV2 parity).
    void
    Connect (unless shared), send run-task, block until task-started; claims binary downlink.
    void
    finish-task and wait for task-finished with the client receive timeout.
    void
    stop(Duration timeout)
    finish-task and wait for task-finished, bounded by timeout.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • start

      public void start()
      Connect (unless shared), send run-task, block until task-started; claims binary downlink.
    • sendText

      public void sendText(String text)
      continue-task carrying payload.input.text (2.x SpeechSynthesizerV2 parity).
    • flush

      public void flush()
      continue-task {input:{flush:true}} — synthesize buffered text without ending.
    • stop

      public void stop()
      finish-task and wait for task-finished with the client receive timeout.
    • stop

      public void stop(Duration timeout)
      finish-task and wait for task-finished, bounded by timeout.
    • cancel

      public void cancel()
      finish-task with directive:cancel — once only, and never sent after a terminal event.
    • cancel

      public void cancel(Duration timeout)
    • events

      public Iterable<Object> events()
      Mixed stream: JsonNode for text events (task-finished included), byte[] for audio frames. Single-use and mutually exclusive with audioChunks() (a second claim throws); frames are consumed on delivery; task-failed raises.
    • audioChunks

      public Iterable<byte[]> audioChunks()
      Audio frames only. Single-use, mutually exclusive with events().
    • getTaskId

      public String getTaskId()
    • getRequestId

      public String getRequestId()
    • getLastResponse

      public com.fasterxml.jackson.databind.JsonNode getLastResponse()
      The last text frame received (raw, header included) — recorded before error rethrow (§6).
    • getUsage

      public Usage getUsage()
    • getFirstPackageDelay

      public Duration getFirstPackageDelay()
      First text sent to first audio/result; null until both happened.
    • getLastPackageDelay

      public Duration getLastPackageDelay()
      finish-task to task-finished; null until both happened.
    • close

      public void close()
      Idempotent; detaches from a shared connection — unless closed before the terminal event while owning the binary downlink, which tears the whole shared connection down. NOT restartable.
      Specified by:
      close in interface AutoCloseable