Class Radio

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class Radio extends AnahataToolkit
A stateful toolkit for playing internet radio streams.

This toolkit maintains its own playback state and hardware routing, allowing for independent audio management within an AGI session. It features a custom JLayer bridge to route MP3 streams to selected hardware.

Author:
anahata
  • Field Details

    • STATIONS

      public static final Map<String,String> STATIONS
      The curated list of radio stations.
    • currentStationUrl

      private String currentStationUrl
      The URL of the currently playing station.
    • playing

      private boolean playing
      Whether the radio is currently playing.
    • selectedOutputDevice

      private AudioDevice selectedOutputDevice
      The selected output device for radio playback.
    • player

      private transient javazoom.jl.player.Player player
      The active JLayer player instance.

      Marked as transient because the native audio stream and decoder state cannot be serialized across sessions.

    • playbackTask

      private transient Future<?> playbackTask
      The future representing the active background playback task.

      Marked as transient as the thread state is not persistent. Used to monitor and cancel the stream during stop/restart cycles.

  • Constructor Details

    • Radio

      public Radio()
  • Method Details

    • initialize

      public void initialize()
      Performs initial setup for a newly created toolkit instance.

      This method is called exactly once by the ToolManager after the toolkit has been instantiated and registered. It provides a clean hook for toolkits to perform one-time initialization logic that requires the tool environment to be fully configured, such as scanning for project instructions or registering default resources.

      Implementation details: Initializes the default hardware routing and selects a random radio station from the curated list to provide immediate ambient audio upon first activation.

      Overrides:
      initialize in class AnahataToolkit
    • postActivate

      public void postActivate()
      Callback method triggered by the Asi Container once the Agi session has been fully deserialized and associated to the ASI container.

      Implementation details: Resumes the audio stream by submitting a new playback task to the executor service. This cannot be done in rebind() as the Agi executor might not be ready.

      Overrides:
      postActivate in class AnahataToolkit
    • initOutputLineFromDefault

      private void initOutputLineFromDefault()
      Selects the system's default audio output line and binds it to this toolkit. This ensures that audio is routed to the user's preferred device unless explicitly overridden.
    • setSelectedOutputDevice

      public void setSelectedOutputDevice(AudioDevice device)
      Sets the selected output device and fires a property change event.
      Parameters:
      device - The device to select.
    • populateMessage

      public void populateMessage(RagMessage ragMessage) throws Exception
      Populates the given RagMessage with dynamic, just-in-time context parts. These parts are appended to the end of the user's prompt (RAG).

      Implementation details: Injects the current playback status, the active station metadata, and the full list of available curated streams into the RAG message for model awareness.

      Parameters:
      ragMessage - The message to be augmented with context.
      Throws:
      Exception - if an error occurs during context generation.
    • start

      public String start(String url)
      Starts playing a radio station.
      Parameters:
      url - The station URL.
      Returns:
      Status message.
    • stop

      public String stop()
      Stops the radio.
      Returns:
      Status message.
    • selectOutputDevice

      public String selectOutputDevice(String deviceId)
      Selects the output device for the radio.
      Parameters:
      deviceId - The unique ID of the output device.
      Returns:
      Status message.