def test_list_voices(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = cloud_tts_pb2.ListVoicesResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = texttospeech_v1beta1.TextToSpeechClient(channel=channel)

        response = client.list_voices()
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloud_tts_pb2.ListVoicesRequest()
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        if request.method == 'POST':
            post_data = request.POST.copy()
            form = forms.BaseForm(post_data, request.FILES)
            print('get post req')
            print(form.errors)
            if form.is_valid():
                print('form is valid')
                obj = form
                obj.textFile = form.cleaned_data['textFile']
                obj.gender = form.cleaned_data['gender']
                obj.pitch = form.cleaned_data['pitch']
                obj.speed = form.cleaned_data['speed']
                obj.voice_type = form.cleaned_data['voice_type']
                obj.save()
                text_path = os.path.join(settings.MEDIA_ROOT, 'texts/', obj.textFile.name)
                text = text_path
                txt = Path(text).read_text(encoding='cp1252')
                fileName = obj.textFile.name[:-4]
                print('File Name is: {}'.format(fileName))
                input_text = {'text': txt}
                voice = {
                    'language_code': 'en-US',
                    'ssml_gender': obj.gender,
                }
                audio_config = {
                    'audio_encoding': texttospeech_v1beta1.enums.AudioEncoding.MP3,
                    'pitch': float(obj.pitch),
                    'speaking_rate': float(obj.speed),
                }
                client = texttospeech_v1beta1.TextToSpeechClient(credentials=credentials)
                response = client.synthesize_speech(input_=input_text,
                                                    voice=voice,
                                                    audio_config=audio_config)
                with open('media/audios/' + str(fileName) + '.mp3', 'wb') as out:
                    out.write(response.audio_content)
                    print('Audio content written to file "output.mp3"')
                
                file = open('media/audios/' + str(fileName) + '.mp3', "rb").read()
                rea_response = HttpResponse(file, content_type='audio/mpeg')
                rea_response['Content-Disposition'] = 'attachment; filename={}'.format(fileName + '.mp3')

                return rea_response
            else:
                return HttpResponse('Something went wrong', status=400)
    def test_list_voices(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = cloud_tts_pb2.ListVoicesResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = texttospeech_v1beta1.TextToSpeechClient()

        response = client.list_voices()
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloud_tts_pb2.ListVoicesRequest()
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 4
0
    def __init__(
        self,
        gender="female",
        speaking_rate=1.0,
        pitch=0,
        sample_rate=16000,
        language_code="en-US",
        name="en-US-Wavenet-C",
    ):
        self.gender = gender
        self.speaking_rate = speaking_rate
        self.pitch = pitch
        self.sample_rate = sample_rate
        self.language_code = language_code
        self.name = name

        # tts
        self.client = texttospeech.TextToSpeechClient()
        self.voices = self._list_voices()
        self.voice_parameters = self._voice_parameters()
        self.audio_config = self._audio_config()
Exemplo n.º 5
0
def synthesize_text(text):
    """Synthesizes speech from the input string of text."""
    from google.cloud import texttospeech_v1beta1
    client = texttospeech_v1beta1.TextToSpeechClient()

    input_text = texttospeech_v1beta1.types.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech_v1beta1.types.VoiceSelectionParams(
        language_code='en-US',
        ssml_gender=texttospeech_v1beta1.enums.SsmlVoiceGender.FEMALE)

    audio_config = texttospeech_v1beta1.types.AudioConfig(
        audio_encoding=texttospeech_v1beta1.enums.AudioEncoding.MP3)

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    with open('output.mp3', 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "output.mp3"')
Exemplo n.º 6
0
def synthesize_text(text, output_filename, output_dir, voice=None):
    """
    Synthesizes speech from the input string of text.
    Female voice = 0, Male voice = 1
    output filename doesn't need extension
    """
    from google.cloud import texttospeech_v1beta1 as texttospeech
    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(text=text)

    genders = (texttospeech.enums.SsmlVoiceGender.FEMALE, texttospeech.enums.SsmlVoiceGender.MALE)
    if not voice:
        gender = genders[random.randrange(0, 2)]
    else:
        gender = genders[voice]

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.types.VoiceSelectionParams(
        language_code='en-US',
        ssml_gender=gender)

    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    mp3_filepath = os.path.join(output_dir, "%s.mp3" % output_filename)
    with open(mp3_filepath, 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file %s' % mp3_filepath)
    
    wav_name = os.path.join(output_dir, "%s.wav" % output_filename)
    print('Audio content re-written to file %s' % wav_name)
    os.system("mpg321 -w %s %s" % (wav_name, mp3_filepath))
    print('Deleting mp3')
    os.remove(mp3_filepath)
Exemplo n.º 7
0
    def google_cloud_tts(self, text, folder):
        try:
            client = texttospeech_v1beta1.TextToSpeechClient()
        except Exception as e:
            print('\n')
            print('{}'.format('- ' * 30))
            print(
                '  Error, client = texttospeech_v1beta1.TextToSpeechClient()\n  {}'
                .format(e))
            print('{}'.format('- ' * 30))
            return

        input_text = texttospeech_v1beta1.types.SynthesisInput(text=text)

        # Note: the voice can also be specified by name.
        # Names of voices can be retrieved with client.list_voices().
        voice = texttospeech_v1beta1.types.VoiceSelectionParams(
            language_code='ko-KR',
            ssml_gender=texttospeech_v1beta1.enums.SsmlVoiceGender.FEMALE)

        audio_config = texttospeech_v1beta1.types.AudioConfig(
            audio_encoding=texttospeech_v1beta1.enums.AudioEncoding.MP3
            # speaking_rate = 말 하는 속도를 적어놓은 것이다, double 형태로 지정할 수 있고 범위가 있었는데.. document 에 있으니 참고바람..
            # 삭제하면 1.0배속으로 된다.
            # speaking_rate=0.4)
        )

        response = client.synthesize_speech(input_text, voice, audio_config)

        # The response's audio_content is binary.
        if folder is not None:
            output_gcltts = self.DEFAULT_FOLDER_PATH + folder + '/' + text + '.mp3'
        else:
            output_gcltts = self.DEFAULT_FOLDER_PATH + text + '.mp3'

        with open(output_gcltts, 'wb') as out:
            out.write(response.audio_content)
            print('Audio content written to file {}'.format(output_gcltts))
Exemplo n.º 8
0
    def test_synthesize_speech(self):
        # Setup Expected Response
        audio_content = b'16'
        expected_response = {'audio_content': audio_content}
        expected_response = cloud_tts_pb2.SynthesizeSpeechResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = texttospeech_v1beta1.TextToSpeechClient(channel=channel)

        # Setup Request
        input_ = {}
        voice = {}
        audio_config = {}

        response = client.synthesize_speech(input_, voice, audio_config)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = cloud_tts_pb2.SynthesizeSpeechRequest(
            input=input_, voice=voice, audio_config=audio_config)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 9
0
def list_voices():
    """Lists the available voices."""
    from google.cloud import texttospeech_v1beta1

    client = texttospeech_v1beta1.TextToSpeechClient()

    # Performs the list voices request
    voices = client.list_voices()

    for voice in voices.voices:
        # Display the voice's name. Example: tpc-vocoded
        print(f"Name: {voice.name}")

        # Display the supported language codes for this voice. Example: "en-US"
        for language_code in voice.language_codes:
            print(f"Supported language: {language_code}")

        ssml_gender = texttospeech_v1beta1.SsmlVoiceGender(voice.ssml_gender)

        # Display the SSML Voice Gender
        print(f"SSML Voice Gender: {ssml_gender.name}")

        # Display the natural sample rate hertz for this voice. Example: 24000
        print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n")
def synthesize_text_with_audio_profile(text, output, effects_profile_id):
    """Synthesizes speech from the input string of text."""
    from google.cloud import texttospeech_v1beta1 as texttospeech

    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.types.VoiceSelectionParams(language_code='en-US')

    # Note: you can pass in multiple effects_profile_id. They will be applied
    # in the same order they are provided.
    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3,
        effects_profile_id=[effects_profile_id])

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    with open(output, 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "%s"' % output)
Exemplo n.º 11
0
def goog_text2speech():
    text = request.args.get(
        'text',
        "Oh No! There seems to be something wrong with my ram. Can you try calling back a little later after i talk to my friends in IT"
    )

    # Pre-process the text
    #if len(text) == 0:
    #text = "We are experiencing technical difficulties at the moment. Please call back later."

    # Adding space between numbers for better synthesis
    #if re.search(r'\b\d{1,16}\b', text):
    #text = re.sub('(?<=\d)(?=\d)', ' ', text)
    #print "Changed input: " + text

    # Setting profile id
    effects_profile_id = 'telephony-class-application'

    #Setting credentials -  Read env data
    credentials_raw = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')

    #Generate Google TTS Credentials
    service_account_info = json.loads(credentials_raw)
    credentials = service_account.Credentials.from_service_account_info(
        service_account_info)

    # Create Google Text-To-Speech client
    client = texttospeech.TextToSpeechClient(credentials=credentials)

    #pass the text to be synthesized by Google Text-To-Speech
    input_text = texttospeech.types.SynthesisInput(text=text)

    #Set the Google Text-To-Speech voice parameters
    voice = texttospeech.types.VoiceSelectionParams(
        language_code='en-AU',
        name='en-AU-Wavenet-B',
        ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE)

    #Set Google Text-To-Speech audio configuration parameters
    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3,
        effects_profile_id=[effects_profile_id])

    # Request speech synthesis from Google Text-To-Speech
    response = client.synthesize_speech(input_text, voice, audio_config)

    # Write the output to a temp file
    with open('output.mp3', 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "output.mp3"')

    if response.audio_content:
        # Read the audio stream from the response
        def generate():
            print 'inside google tts generate method'
            with open('output.mp3', 'rb') as dmp3:
                data = dmp3.read(1024)
                while data:
                    yield data
                    data = dmp3.read(1024)
            print 'generate complete for google tts'

        return Response(generate(), mimetype="audio/mpeg")
    else:
        # The response didn't contain audio data, exit gracefully
        print("Could not stream audio")
        return "Error"
    def test_list_voices(self):
        client = texttospeech_v1beta1.TextToSpeechClient()

        voices = client.list_voices()
        assert len(voices.voices) > 0
Exemplo n.º 13
0
from google.cloud import texttospeech_v1beta1
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    "OAuth/JING-704cd29711ee.json",
    scopes=["https://www.googleapis.com/auth/cloud-platform"],
)

client = texttospeech_v1beta1.TextToSpeechClient(credentials=credentials)

voice = texttospeech_v1beta1.types.VoiceSelectionParams(
    language_code="en-US",
    ssml_gender=texttospeech_v1beta1.enums.SsmlVoiceGender.FEMALE)

audio_config = texttospeech_v1beta1.types.AudioConfig(
    audio_encoding=texttospeech_v1beta1.enums.AudioEncoding.LINEAR16)


def textToSpeech(text):
    synthesis_input = texttospeech_v1beta1.types.SynthesisInput(text=text)
    response = client.synthesize_speech(synthesis_input, voice, audio_config)
    '''
    with open(file_path, "wb") as out:
        out.write(response.audio_content)
        print("Audio content written to file %s" % file_path)
        '''
    return response.audio_content