Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Test] spoken output from Stata on macOS

    This is a small test of spoken output from Stata using the built-in speech service in macOS.
    A possible use is announcing that an analysis has finished.

    Copy the code below into Stata's Do-file Editor and run it.
    You should hear “The analysis is complete.”
    This is a sample message; it does not run an analysis or change the data in memory.

    Requirements: Stata 16 or later on macOS. No additional Stata package is needed.
    The code block itself does not play audio in the browser.

    Code:
    version 16.0
    if !strpos("`c(machine_type)'", "Mac") {
        display as error "This example requires macOS."
        exit 198
    }
    display as text "The analysis is complete."
    shell /usr/bin/say "The analysis is complete."
    If you try it, did you hear the message?
    Please mention your Stata and macOS versions.
    The voice may differ with your system settings.

    This tests speech output only. It does not listen to the microphone.

  • #2
    A second small test: can changes in speaking rate or punctuation help convey the intended phrasing?

    These examples use the Korean expression "가가 가가?", repeating the syllable ga.
    They invite comparison of phrasing; interpretation can depend on dialect and context.
    Requirements: Stata 16 or later on macOS, with the Korean voice Yuna installed.
    Run the voice-list command first and check for Yuna before running the three speech commands. No dataset is needed.

    A is the original text at the default rate.
    B changes only the rate to 140 words per minute, using the speech service's rate setting.
    C returns to the default rate and adds a comma.
    The number 140 is an engine setting, not a measured Korean word rate.

    Code:
    version 16.0
    if !strpos("`c(machine_type)'", "Mac") {
        display as error "This example requires macOS."
        exit 198
    }
    * Check that the Korean voice Yuna is installed.
    shell /usr/bin/say -v '?'
    
    display as text "A: default rate; original punctuation"
    shell /usr/bin/say -v Yuna "가가 가가?"
    
    display as text "B: rate 140; original punctuation"
    shell /usr/bin/say -v Yuna -r 140 "가가 가가?"
    
    display as text "C: default rate; comma added"
    shell /usr/bin/say -v Yuna "가가, 가가?"
    Can you hear a difference in pacing or phrasing?
    If you know Korean, does any version convey the meaning you expected?
    A short report could include:

    Stata / macOS version:
    Played A, B, and C successfully: yes / no
    Difference between A and B:
    Difference between A and C:
    Korean / dialect familiarity (if applicable):
    Did a difference help convey the intended meaning, or only change the sound?

    This comparison does not specify a pitch contour or establish correct regional intonation.
    Hearing a difference is not, by itself, evidence that the intended meaning is clearer.
    Stata sends the text and options; the macOS speech service produces the voice.

    Comment


    • #3
      An update to the small rule-based exchange: an unrecognized answer now leads to another prompt within the same run. Enter exit or quit to stop the example; Stata itself remains open.

      Requirements: Stata 16 or later on macOS. No additional Stata package is needed.

      Test 1: typed input and spoken output

      Copy the complete code into Stata's Do-file Editor and run it together. At the response prompt, enter yes, no, exit, or quit and press Return. Stata displays and speaks each prompt and its final response. The example does not change the dataset; with no observations in memory, the count is zero.

      Code:
      version 16.0
      if !strpos("`c(machine_type)'", "Mac") {
          display as error "This example requires macOS."
          exit 198
      }
      tempfile speechtext
      tempname fh
      local choice 0
      local prompt "Would you like the number of observations? Say yes or no, or exit to stop."
      
      while `choice' == 0 {
          display as text "`prompt'"
          file open `fh' using "`speechtext'", write text replace
          file write `fh' ("`prompt'") _n
          file close `fh'
          shell /usr/bin/say -f "`speechtext'"
          display as text "yes / no / exit / quit: " _request(_answer)
      
          * Match whole responses as text; do not execute the input.
          mata: st_local("choice", strofreal( ///
              (strlower(strtrim(st_local("answer"))) == "yes") + ///
              2*(strlower(strtrim(st_local("answer"))) == "no") + ///
              3*((strlower(strtrim(st_local("answer"))) == "exit") | ///
                 (strlower(strtrim(st_local("answer"))) == "quit"))))
          local prompt "Please answer yes or no. Say exit to stop."
      }
      
      local response "Stopped."
      if "`choice'" == "1" {
          local n = strtrim(string(_N, "%18.0f"))
          local response "There are `n' observations in memory."
      }
      if "`choice'" == "2" {
          local response "OK. No count requested."
      }
      display as result "`response'"
      
      * Transfer the response to the speech service through a text file.
      file open `fh' using "`speechtext'", write text replace
      file write `fh' ("`response'") _n
      file close `fh'
      shell /usr/bin/say -f "`speechtext'"
      Try these short sequences, starting a new run for each sequence:
      1. maybe, then yes: Stata asks again without restarting the do-file, then reports the observation count.
      2. Press Return without an answer, then enter no: Stata asks again, then says "OK. No count requested."
      3. not yes, then exit: Stata asks again, then says "Stopped."
      You can also use quit instead of exit.

      The exchange follows a fixed rule. It does not interpret unrestricted natural language or execute the submitted text as a command.

      Test 2 (optional): operating-system Dictation

      After Test 1 works, try entering the same responses with macOS Dictation. Activate Dictation manually at each response prompt, inspect the text, end Dictation, and then press Return. The do-file does not switch between keyboard and voice input automatically. Yes and YES are accepted, but added punctuation such as "Yes." is not; that response leads to another prompt.

      Dictation is provided by macOS; this Stata code does not capture or recognize speech. The updated behavior concerns how Stata handles the text once it has been entered.

      The retry, yes/no, and exit/quit paths were checked with typed input in Stata/MP 16.1 console on macOS. The person conducting this trial reported successful Dictation input with the earlier single-answer version. The retry version needs the same practical check at successive Dictation prompts; compatibility across Stata/macOS combinations has not been established.

      A brief report could include:
      Stata / macOS version:
      Typed retry and exit/quit worked:
      Question and response were audible:
      Dictation entered text at both the first and repeated prompts (if tried):
      Exact dictated text, and whether it needed correction:

      References: Stata's display and _request(); macOS Dictation.
      Last edited by Choonjoo Lee; 23 Oct 2026, 21:05.

      Comment

      Working...
      X