1 Reply Latest reply: Mar 27, 2014 10:46 AM by mingsai
mingsai Level 1 Level 1 (30 points)

Problem

 

AppleScript produces an aiff file with no sound and zero (0) bytes.  I am looping through many lists of text and using three different voices. When I run my script I get many files with no sound. There does not appear t be any common reason for the failure. I've tried using:

 

  • delay n
  • stopping with current voice
  • wating for completion

 

Nothing has fixed the problem. Some of the files simply have no sound but if I say the text via my automator service then I do hear the output . So it seems that something is off in my repeat loop. Any thoughts on how to fix this (timing issue?) using AppleScript?

 

set alist to {}

set input to every paragraph of (readFile("textFileWithChineseWords"))

 

repeat with nextLine in input

          if first character of nextLine is not "#" then

  --^.*\s(.*)\s\[(.*)\]\s

  --^.*\s(.*)\s\[

                    set output to (find text "^.*\\s(.*)\\s\\[(.*)\\]" in nextLine with regexp and string result)

                    set pinyin to (find text "\\[(.*)\\]" in nextLine with regexp and string result)

                    set pinyin to change "[" into "" in pinyin

                    set pinyin to change "]" into "" in pinyin

                    set pinyin to change space into "" in pinyin

                    set pinyin to change "," into "" in pinyin

  --set pinyin to change "," into "" in pinyin

 

                    set alist to (split(output, space))

                    set simple to item 2 of alist

  log simple

 

  saveAIFFFile(simple, pinyin)

 

  --log simple

  --log alist

  --log pinyin

                    delay 0.3

          end if

end repeat

 

 

on readFile(unixPath)

          set foo to (open for access (POSIX file unixPath))

          set txt to (read foo for (get eof foo) as «class utf8»)

close access foo

          return txt

end readFile

 

on fileIsGood(filename)

 

          set theItem to quoted form of POSIX path of filename

          set output to false

 

--log theItem

 

          if exists (theItem) then

                    set fileSize to word 1 of (do shell script "du -sk " & theItem)

  --log fileSize

                    if (fileSize is equal to 0) then return false

          else

                    return false

          end if

 

          return output

 

end fileIsGood

 

 

on saveAIFFFile(msg, filename)

 

          set cnfilname to ("/tmp" & filename & ".aiff") -- quoted form of POSIX path of

 

delay 4

          set msg to change "" into ", " in msg

say msg using "Ting-Ting" saving to POSIX file cnfilname with stopping current speech and waiting until completion

--say "" with stopping current speech

 

          if (not fileIsGood(cnfilname)) then

                    repeat until (fileIsGood(cnfilname))

  --delay 4

                              set msg to change "" into ", " in msg

  say msg using "Ting-Ting" saving to cnfilname with stopping current speech and waiting until completion

                    end repeat

          end if

 

 

end saveAIFFFile

 

to split(input, delimiter)

          set AppleScript's text item delimiters to delimiter

          set someText to (every text item in input) as list

          set AppleScript's text item delimiters to {""} --> restore delimiters to default value

          return someText

end split



MacBook Air (13-inch Mid 2012), Mac OS X (10.7.5), Love the mac (OSX 10.9+)
  • mingsai Level 1 Level 1 (30 points)

    I thought I'd share the solution for this problem with the community.

     

    Changing my AppleScript line that was performing the say command to the shell script results in much faster execution and no loss of audio on the output. It appears that AppleScript was likely sending the same commands under the hood and the timing was thrown off as a result (delay between sig and ack).

     

    do shell script "say '" & msg & "' -o '" & cnfilname & "'  -v 'Ting-Ting'"

     

    looping in this way I was able to remove the delays and I'm on to the next problem.

     

    Hope this helps someone else.