Coder Social home page Coder Social logo

Change voice mid text about voices HOT 6 CLOSED

2V3EvG4LMJFdRe avatar 2V3EvG4LMJFdRe commented on July 2, 2024
Change voice mid text

from voices.

Comments (6)

mklement0 avatar mklement0 commented on July 2, 2024 1

@2V3EvG4LMJFdRe

You need to provide the text via the pipeline / stdin:

echo '[[voice Alex]] I am Alex. [[voice Samantha]] I am Samantha. [[voice default]] Back to default.' | say-multivoice

The original script was bare-bones, it was just a quick proof of concept. I've updated it to be a little friendlier, with command-line help and error checking - please see above.

from voices.

2V3EvG4LMJFdRe avatar 2V3EvG4LMJFdRe commented on July 2, 2024 1

Thanks a lot! I can finally do what I requested, so I'm gonna close the issue.

from voices.

mklement0 avatar mklement0 commented on July 2, 2024

That's an interesting idea, but note that voices just defers to say, which uses the speech synthesizer API - and it would have to be that API that supports commands to change the speaking voice.

I didn't actually know about "markup" such as [[slnc 1000]], which Apple calls embeddable speech commands.

I couldn't find current documentation, but here's an archived version that suggests that at least historically changing the voice wasn't supported: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/SpeechSynthesisProgrammingGuide/FineTuning/FineTuning.html#//apple_ref/doc/uid/TP40004365-CH5-SW12

You could ask a question at https://apple.stackexchange.com/ to see if someone has a definitive answer for the current macOS version.

Building such functionality on top of the API is conceivable, but I think is outside the mandate of voices.

If you wish to explore this further yourself, here's a quick PowerShell prototype, which wraps the say utility:

# Function definition
function say-multivoice {
    $voiceArg = @()
    $Input -split '(\[\[voice .+?\]\])' -ne '' | foreach {
        if ($_ -match '\[\[voice (.+?)\]\]') {
            $voice = $Matches[1]
            if ($voice -eq 'default') { $voiceArg = @() }
            else                      { $voiceArg = '-v', $voice }
        } else {
            $_ | say $voiceArg
        }

    }
}

Once the above function is defined (e.g. via your $PROFILE file), you could use commands such as the following:

'[[voice Alex]] I am Alex. [[voice Samantha]] I am Samantha. [[voice default]] Back to default.' | say-multivoice

from voices.

2V3EvG4LMJFdRe avatar 2V3EvG4LMJFdRe commented on July 2, 2024

Thanks a lot for the thoughtful response, it goes very in depth at what I've found is a rather obscure operation. Since I'm not a programmer, some of what you write goes over my head. From what I gather, I have to install PowerShell, and then create a function that calls for the voices to change on a $PROFILE file, which would link it for use system-wide. I've installed PowerShell with brew, but now I'm not sure where the $PROFILE file is located to put the above in.

from voices.

mklement0 avatar mklement0 commented on July 2, 2024

On second thought, I suggest creating an executable shell script, so that you can invoke it from any shell, without having to start PowerShell explicitly:

Assuming you have administrative privileges on your machine, open a terminal and run the following commands; you'll be prompted for your login password once:

  • Create script file /usr/local/bin/say-multivoice

    • sudo touch /usr/local/bin/say-multivoice
  • Make it executable:

    • sudo chmod +x /usr/local/bin/say-multivoice
  • Open the script file for interactive editing:

    • sudo nano /usr/local/bin/say-multivoice
  • Once the interactive editor (nano) is up, paste the following text:

#!/usr/bin/env pwsh -noprofile

# See https://github.com/mklement0/voices/issues/6#issuecomment-666714554

if ($args[0] -in '-?', '-h', '--help') {
@"
Wrapper for the `say` utility that supports text with embedded commands to
switch the speaking voice.

Usage: 
  <text> | say-multivoice
  say-multivoice < file              # not in PowerShell
  Get-Content file | say-multiveoice # PowerShell

Example:
   echo '[[voice Alex]] I am Alex. [[voice Samantha]] I am Samantha. [[voice default]] Back to default.' | say-multivoice
"@
  exit 0
}
elseif ($args.Count) {
  [Console]::Error.WriteLine(@"
ERROR: Unexpected argument(s) specified. Please provide the text to speak via the pipeline (stdin). Use -? for help.
"@)
  exit 1
}
elseif (-not $MyInvocation.ExpectingInput) {
  [Console]::Error.WriteLine(@"
ERROR: Missing input. Please provide the text to speak via the pipeline (stdin). Use -? for help.
"@)
  exit 1
}

$voiceArg = @()
$Input -split '(\[\[voice .+?\]\])' -ne '' | ForEach-Object {
    if ($_ -match '\[\[voice (.+?)\]\]') {
        $voice = $Matches[1]
        if ($voice -eq 'default') { $voiceArg = @() }
        else                      { $voiceArg = '-v', $voice }
    } else {
        $_ | say $voiceArg
    }
}

exit $LASTEXITCODE
  • Press Control-X, then y, then Return to save the file.

After that, you should be able to call say-multivoice from any terminal; e.g.:

echo '[[voice Alex]] I am Alex. [[voice Samantha]] I am Samantha. [[voice default]] Back to default.' | say-multivoice

However, note that all you can do is pipe text to it, none of say's other options are supported by this simple wrapper.

from voices.

2V3EvG4LMJFdRe avatar 2V3EvG4LMJFdRe commented on July 2, 2024

Having followed your instructions, I'm not sure how to put it to work. say-multivoice is indeed recognized as a terminal command, but I'm not sure what the syntax of it would be so that it can speak text out loud. For example, entering the line say-multivoice '[[voice Alex]] I am Alex. [[voice Samantha]] I am Samantha. [[voice default]] Back to default.' doesn't prompt any speech.

from voices.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.