## ----setup, include = FALSE---------------------------------------------------
# This vignette runs against recorded, credential-free API fixtures. The
# fixtures are recorded once by a maintainer with real Azure OpenAI credentials
# (data-raw/record-doc-outputs.R) and committed under vignettes/audio/. When the
# fixtures are present every foundry_*() call below executes and shows its real
# output; when they are absent the API chunks are not evaluated so the vignette
# still builds anywhere without credentials. No output on this page is fabricated.
fixture_dir <- "audio"
recording <- nzchar(Sys.getenv("FOUNDRY_RECORD_DOCS"))
have_fixtures <- dir.exists(fixture_dir) && length(list.files(fixture_dir)) > 0
run_api <- requireNamespace("httptest2", quietly = TRUE) &&
  (recording || have_fixtures)

# Attach foundryR before start_vignette(): httptest2 only sources the package's
# inst/httptest2/start-vignette.R (which sets replay placeholders) from attached
# packages.
library(foundryR)

if (run_api) {
  httptest2::start_vignette(fixture_dir)
}

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = run_api
)

# Embed a self-contained <audio> player holding the real bytes foundry_speak()
# produced, so readers can play the output (not just read its size). On replay
# the file holds the recorded audio, so this works offline with no credentials.
embed_audio <- function(path) {
  if (!requireNamespace("base64enc", quietly = TRUE)) {
    return(invisible(NULL))
  }
  uri <- paste0("data:audio/mpeg;base64,", base64enc::base64encode(path))
  knitr::asis_output(
    sprintf(
      '<audio controls src="%s">Your browser does not support audio playback.</audio>',
      uri
    )
  )
}

## ----library, eval = TRUE-----------------------------------------------------
library(foundryR)

## ----configure-speech, eval = FALSE-------------------------------------------
# # Only needed for MAI-Transcribe on a dedicated Speech resource:
# foundry_set_speech_endpoint(Sys.getenv("AZURE_FOUNDRY_SPEECH_ENDPOINT"))
# foundry_set_speech_key("your-speech-key")

## ----sample, eval = TRUE------------------------------------------------------
sample_audio <- system.file("extdata/samples/jfk.wav", package = "foundryR")
basename(sample_audio)

## ----transcribe---------------------------------------------------------------
transcript <- foundry_transcribe(
  sample_audio,
  service = "openai",
  model = "whisper",
  api = "deployment",
  response_format = "verbose_json"
)

transcript$text

## ----transcribe-phrases-------------------------------------------------------
head(transcript$phrases[[1]])

## ----speak--------------------------------------------------------------------
speech_path <- tempfile(fileext = ".mp3")
speech <- foundry_speak(
  "Hello, world.",
  model = "gpt-4o-mini-tts",
  voice = "alloy",
  path = speech_path
)

speech[, c("bytes", "model", "voice", "format")]

## ----speak-play, echo = FALSE-------------------------------------------------
embed_audio(speech$path)

## ----translate-synth----------------------------------------------------------
spanish_path <- tempfile(fileext = ".mp3")
spanish_clip <- foundry_speak(
  "La reunion fue muy util.",
  model = "gpt-4o-mini-tts",
  voice = "alloy",
  path = spanish_path
)

## ----translate-play, echo = FALSE---------------------------------------------
embed_audio(spanish_clip$path)

## ----translate----------------------------------------------------------------
translation <- foundry_translate_audio(
  spanish_clip$path,
  service = "openai",
  model = "whisper",
  api = "deployment"
)

translation$text

## ----cleanup, include = FALSE, eval = TRUE------------------------------------
if (run_api) {
  unlink(c(speech_path, spanish_path))
  httptest2::end_vignette()
}

