Skip to content

Commit

Permalink
add TTS audio format (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Mar 21, 2024
1 parent 8e1651b commit beb4656
Show file tree
Hide file tree
Showing 28 changed files with 223 additions and 624 deletions.
2 changes: 2 additions & 0 deletions lib/content/audio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ defprotocol Content.Audio do
@doc "Converts an audio struct to the mid/vars params for the PA system"
@spec to_params(Content.Audio.t()) :: value()
def to_params(audio)
@spec to_tts(Content.Audio.t()) :: String.t()
def to_tts(audio)
end
23 changes: 15 additions & 8 deletions lib/content/audio/approaching.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ defmodule Content.Audio.Approaching do
case {destination_var(audio.destination, audio.platform, audio.route_id),
crowding_description} do
{nil, _} ->
case Utilities.ad_hoc_trip_description(audio.destination, audio.route_id) do
{:ok, trip_description} ->
text = "Attention passengers: The next #{trip_description} is now approaching."
{:ad_hoc, {text, :audio_visual}}

{:error, :unknown} ->
handle_unknown_destination(audio)
end
{:ad_hoc, {Content.Audio.to_tts(audio), :audio_visual}}

{var, nil} ->
Utilities.take_message([var], :audio_visual)
Expand Down Expand Up @@ -88,6 +81,20 @@ defmodule Content.Audio.Approaching do
end
end

def to_tts(%Content.Audio.Approaching{} = audio) do
train = Utilities.train_description(audio.destination, audio.route_id)
crowding = PaEss.Utilities.crowding_text(audio.crowding_description)

new_cars =
if audio.new_cars? && audio.route_id == "Red" do
", with all new Red Line cars."
else
"."
end

"Attention passengers: The next #{train} is now approaching#{new_cars}#{crowding}"
end

@spec handle_unknown_destination(Content.Audio.Approaching.t()) :: nil
defp handle_unknown_destination(audio) do
Logger.info(
Expand Down
4 changes: 4 additions & 0 deletions lib/content/audio/boarding_button.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ defmodule Content.Audio.BoardingButton do
def to_params(_audio) do
Utilities.take_message([@boarding_button_message], :audio_visual)
end

def to_tts(%Content.Audio.BoardingButton{}) do
"Attention Passengers: To board the next train, please push the button on either side of the door."
end
end
end
15 changes: 13 additions & 2 deletions lib/content/audio/closure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ defmodule Content.Audio.Closure do
end

# Hardcoded for Union Square
def to_params(%Content.Audio.Closure{alert: :use_routes_alert, routes: []}) do
{:ad_hoc, {"No Train Service. Use routes 86, 87, or 91", :audio}}
def to_params(%Content.Audio.Closure{alert: :use_routes_alert, routes: []} = audio) do
{:ad_hoc, {Content.Audio.to_tts(audio), :audio}}
end

def to_tts(%Content.Audio.Closure{} = audio) do
if audio.alert == :use_routes_alert do
# Hardcoded for Union Square
"No Train Service. Use routes 86, 87, or 91"
else
shuttle = if(audio.alert == :shuttles_closed_station, do: " Use shuttle.", else: "")
line = PaEss.Utilities.get_line_from_routes_list(audio.routes)
"There is no #{line} service at this station.#{shuttle}"
end
end
end
end
4 changes: 4 additions & 0 deletions lib/content/audio/custom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ defmodule Content.Audio.Custom do
def to_params(%Content.Audio.Custom{message: message}) do
{:ad_hoc, {message, :audio}}
end

def to_tts(%Content.Audio.Custom{} = audio) do
audio.message
end
end
end
6 changes: 6 additions & 0 deletions lib/content/audio/first_train_scheduled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ defmodule Content.Audio.FirstTrainScheduled do

PaEss.Utilities.take_message(vars, :audio)
end

def to_tts(%Content.Audio.FirstTrainScheduled{} = audio) do
train = PaEss.Utilities.train_description(audio.destination, nil)
time = Content.Utilities.render_datetime_as_time(audio.scheduled_time)
"The first #{train} is scheduled to arrive at #{time}"
end
end
end
24 changes: 8 additions & 16 deletions lib/content/audio/following_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,15 @@ defmodule Content.Audio.FollowingTrain do
end
end

defp do_ad_hoc_message(audio) do
case Utilities.ad_hoc_trip_description(audio.destination, audio.route_id) do
{:ok, trip_description} ->
min_or_mins = if audio.minutes == 1, do: "minute", else: "minutes"

text =
"The following #{trip_description} #{audio.verb} in #{audio.minutes} #{min_or_mins}"

{:ad_hoc, {text, :audio}}

{:error, :unknown} ->
Logger.error(
"FollowingTrain.to_params unknown destination: #{inspect(audio.destination)}"
)
def to_tts(%Content.Audio.FollowingTrain{} = audio) do
train = Utilities.train_description(audio.destination, audio.route_id)
arrives_or_departs = if audio.verb == :arrives, do: "arrives", else: "departs"
min_or_mins = if audio.minutes == 1, do: "minute", else: "minutes"
"The following #{train} #{arrives_or_departs} in #{audio.minutes} #{min_or_mins}"
end

nil
end
defp do_ad_hoc_message(audio) do
{:ad_hoc, {Content.Audio.to_tts(audio), :audio}}
end

@spec green_line_with_branch_params(
Expand Down
44 changes: 27 additions & 17 deletions lib/content/audio/next_train_countdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,37 @@ defmodule Content.Audio.NextTrainCountdown do
end
end

defp do_ad_hoc_message(audio) do
case Utilities.ad_hoc_trip_description(audio.destination, audio.route_id) do
{:ok, trip_description} ->
min_or_mins = if audio.minutes == 1, do: "minute", else: "minutes"

text = "The next #{trip_description} #{audio.verb} in #{audio.minutes} #{min_or_mins}"

text =
if audio.track_number do
text <> " from track #{audio.track_number}"
else
text
def to_tts(%Content.Audio.NextTrainCountdown{} = audio) do
train = Utilities.train_description(audio.destination, audio.route_id)
arrives_or_departs = if audio.verb == :arrives, do: "arrives", else: "departs"
min_or_mins = if audio.minutes == 1, do: "minute", else: "minutes"

suffix =
cond do
audio.track_number ->
" on track #{audio.track_number}."

audio.platform ->
cond do
audio.minutes <= 5 ->
" on the #{if(audio.platform == :ashmont, do: "ashmont", else: "braintree")} platform"

audio.minutes <= 11 ->
". We will announce the platform for boarding soon."

true ->
". We will announce the platform for boarding when the train is closer."
end

{:ad_hoc, {text, :audio}}
true ->
"."
end

{:error, :unknown} ->
Logger.error("NextTrainCountdown unknown destination: #{inspect(audio.destination)}")
"The next #{train} #{arrives_or_departs} in #{audio.minutes} #{min_or_mins}#{suffix}"
end

nil
end
defp do_ad_hoc_message(audio) do
{:ad_hoc, {Content.Audio.to_tts(audio), :audio}}
end

@spec green_line_with_branch_params(
Expand Down
49 changes: 15 additions & 34 deletions lib/content/audio/no_service_to_destination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,31 @@ defmodule Content.Audio.NoServiceToDestination do

require Logger

@enforce_keys [:message]
@enforce_keys [:destination, :use_shuttle]
defstruct @enforce_keys

@type t :: %__MODULE__{
message: String.t()
destination: PaEss.destination(),
use_shuttle: boolean()
}

def from_message(
%Content.Message.Alert.DestinationNoService{destination: destination} = message
) do
case PaEss.Utilities.destination_to_ad_hoc_string(destination) do
{:ok, destination} ->
audio = "No service to #{destination}"

[%__MODULE__{message: audio}]

{:error, :unknown} ->
Logger.error(
"NoServiceToDestination.from_message unknown destination: #{inspect(message)}"
)

[]
end
def from_message(%Content.Message.Alert.DestinationNoService{destination: destination}) do
[%__MODULE__{destination: destination, use_shuttle: false}]
end

def from_message(%Content.Message.Alert.NoServiceUseShuttle{destination: destination} = message) do
case PaEss.Utilities.destination_to_ad_hoc_string(destination) do
{:ok, destination} ->
audio = "No service to #{destination} use shuttle"

[%__MODULE__{message: audio}]

{:error, :unknown} ->
Logger.error(
"NoServiceToDestination.from_message unknown destination: #{inspect(message)}"
)

[]
end
def from_message(%Content.Message.Alert.NoServiceUseShuttle{destination: destination}) do
[%__MODULE__{destination: destination, use_shuttle: true}]
end

defimpl Content.Audio do
def to_params(%Content.Audio.NoServiceToDestination{message: message}) do
{:ad_hoc, {message, :audio}}
def to_params(%Content.Audio.NoServiceToDestination{} = audio) do
{:ad_hoc, {Content.Audio.to_tts(audio), :audio}}
end

def to_tts(%Content.Audio.NoServiceToDestination{} = audio) do
{:ok, destination_text} = PaEss.Utilities.destination_to_ad_hoc_string(audio.destination)
shuttle = if(audio.use_shuttle, do: " Use shuttle.", else: "")
"No #{destination_text} service.#{shuttle}"
end
end
end
16 changes: 6 additions & 10 deletions lib/content/audio/passthrough.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ defmodule Content.Audio.Passthrough do
def to_params(audio) do
case destination_var(audio.destination, audio.route_id) do
nil ->
case PaEss.Utilities.ad_hoc_trip_description(audio.destination, audio.route_id) do
{:ok, trip_description} ->
text =
"The next #{trip_description} does not take customers. Please stand back from the yellow line."

{:ad_hoc, {text, :audio}}

{:error, :unknown} ->
handle_unknown_destination(audio)
end
{:ad_hoc, {Content.Audio.to_tts(audio), :audio_visual}}

var ->
{:canned, {"103", [var], :audio_visual}}
end
end

def to_tts(%Content.Audio.Passthrough{} = audio) do
train = PaEss.Utilities.train_description(audio.destination, audio.route_id)
"The next #{train} does not take customers. Please stand back from the yellow line."
end

@spec handle_unknown_destination(Content.Audio.Passthrough.t()) :: nil
defp handle_unknown_destination(audio) do
Logger.info(
Expand Down
34 changes: 15 additions & 19 deletions lib/content/audio/stopped_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ defmodule Content.Audio.StoppedTrain do
require Logger
alias PaEss.Utilities

@enforce_keys [:destination, :stops_away]
@enforce_keys [:destination, :route_id, :stops_away]
defstruct @enforce_keys

@type t :: %__MODULE__{
destination: PaEss.destination(),
route_id: String.t(),
stops_away: non_neg_integer()
}

@spec from_message(Content.Message.t()) :: [t()]
def from_message(%Content.Message.StoppedTrain{destination: destination, stops_away: stops_away})
def from_message(%Content.Message.StoppedTrain{
destination: destination,
stops_away: stops_away,
route_id: route_id
})
when stops_away > 0 do
[%__MODULE__{destination: destination, stops_away: stops_away}]
[%__MODULE__{destination: destination, route_id: route_id, stops_away: stops_away}]
end

def from_message(%Content.Message.StoppedTrain{stops_away: 0}) do
Expand Down Expand Up @@ -59,23 +64,14 @@ defmodule Content.Audio.StoppedTrain do
end
end

defp do_ad_hoc_message(audio) do
case Utilities.ad_hoc_trip_description(audio.destination) do
{:ok, trip_description} ->
stop_or_stops = if audio.stops_away == 1, do: "stop", else: "stops"

text =
"The next #{trip_description} is stopped #{audio.stops_away} #{stop_or_stops} away"

{:ad_hoc, {text, :audio}}

{:error, :unknown} ->
Logger.error(
"StoppedTrain.to_params unknown destination: #{inspect(audio.destination)}"
)
def to_tts(%Content.Audio.StoppedTrain{} = audio) do
train = Utilities.train_description(audio.destination, audio.route_id)
stop_or_stops = if audio.stops_away == 1, do: "stop", else: "stops"
"The next #{train} is stopped #{audio.stops_away} #{stop_or_stops} away"
end

nil
end
defp do_ad_hoc_message(audio) do
{:ad_hoc, {Content.Audio.to_tts(audio), :audio}}
end

defp stops_away_var(1), do: "535"
Expand Down
14 changes: 14 additions & 0 deletions lib/content/audio/track_change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ defmodule Content.Audio.TrackChange do
end
end

def to_tts(%Content.Audio.TrackChange{} = audio) do
train = PaEss.Utilities.train_description(audio.destination, audio.route_id)

platform =
case audio.berth do
"70196" -> "B"
"70197" -> "C"
"70198" -> "D"
"70199" -> "E"
end

"Track change: The next #{train} is now boarding on the #{platform} platform"
end

defp track_change_message(msg_id) do
vars = [@track_change, msg_id]
PaEss.Utilities.take_message(vars, :audio_visual)
Expand Down
16 changes: 7 additions & 9 deletions lib/content/audio/train_is_arriving.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ defmodule Content.Audio.TrainIsArriving do
) do
case {dest_param(audio), crowding_description} do
{nil, _} ->
case Utilities.ad_hoc_trip_description(audio.destination, audio.route_id) do
{:ok, trip_description} ->
text = "Attention passengers: The next #{trip_description} is now arriving."
{:ad_hoc, {text, :audio_visual}}

{:error, :unknown} ->
Logger.error("TrainIsArriving.to_params unknown params for #{inspect(audio)}")
nil
end
{:ad_hoc, {Content.Audio.to_tts(audio), :audio_visual}}

{var, nil} ->
Utilities.take_message([var], :audio_visual)
Expand All @@ -57,6 +49,12 @@ defmodule Content.Audio.TrainIsArriving do
end
end

def to_tts(%Content.Audio.TrainIsArriving{} = audio) do
train = Utilities.train_description(audio.destination, audio.route_id)
crowding = PaEss.Utilities.crowding_text(audio.crowding_description)
"Attention passengers: The next #{train} is now arriving.#{crowding}"
end

@spec dest_param(Content.Audio.TrainIsArriving.t()) :: String.t() | nil
defp dest_param(%{destination: :alewife, platform: :ashmont}), do: "32105"
defp dest_param(%{destination: :alewife, platform: :braintree}), do: "32106"
Expand Down
Loading

0 comments on commit beb4656

Please sign in to comment.