Guest User

Untitled

a guest
Jan 27th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import json
  2. from watson_developer_cloud import TextToSpeechV1
  3. import time
  4. from datetime import timedelta
  5. import sys
  6. import os
  7. import argparse
  8.  
  9.  
  10. #We need to get our API credentials in the code for authentication that we have stored as Environment Variables locally
  11. TTS_USER_WATSON = os.environ.get("TTS_USER_WATSON")
  12. TTS_PASS_WATSON = os.environ.get("TTS_PASS_WATSON")
  13.  
  14. #Following line is used to save all the console output into a text file
  15. sys.stdout = open('speech_api_test_text_tts_output.txt', 'w')
  16.  
  17. start_time = time.monotonic()
  18.  
  19.  
  20. def input_file(text_file_path):
  21. global text
  22. if os.path.isfile(text_file_path):
  23. with open(text_file_path, 'r') as text_file:
  24. text = text_file.read()
  25. else:
  26. print("File doesn't exist in the directory!")
  27.  
  28.  
  29. def speech_tts():
  30. #Initialize TextToSpeech function using the API credentials
  31. tts = TextToSpeechV1(
  32. username = TTS_USER_WATSON,
  33. password = TTS_PASS_WATSON
  34. )
  35. #Parameters required to send the API request
  36. voice = 'en-US_AllisonVoice'
  37. accept = 'audio/wav'
  38.  
  39. with open('watson_test_text_tts.wav', 'wb') as audio_file:
  40. audio_file.write(
  41. tts.synthesize(text, voice = voice, accept = accept)) #This line will synthesize the text file and convert it into audio format and generate a '.wav' format audio file
  42.  
  43.  
  44. if __name__ == '__main__':
  45. parser = argparse.ArgumentParser(
  46. description = __doc__,
  47. formatter_class = argparse.RawDescriptionHelpFormatter)
  48. parser.add_argument(
  49. 'text_file_path',
  50. help = 'The complete file path of the text file you want to convert from text to speech.')
  51. args = parser.parse_args()
  52.  
  53. input_file(args.text_file_path)
  54. speech_tts()
  55.  
  56.  
  57. end_time = time.monotonic()
  58. print("Execution_Time:", timedelta(seconds = end_time - start_time))
Add Comment
Please, Sign In to add comment