|
| 1 | +/** |
| 2 | + * Copyright 2023 Github.com/Barqawiz/IntelliJava |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.intellijava.core.controller; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import com.intellijava.core.model.AudioResponse; |
| 24 | +import com.intellijava.core.model.SpeechModels; |
| 25 | +import com.intellijava.core.model.input.SpeechInput; |
| 26 | +import com.intellijava.core.model.input.SpeechInput.Gender; |
| 27 | +import com.intellijava.core.utils.AudioHelper; |
| 28 | +import com.intellijava.core.wrappers.GoogleAIWrapper; |
| 29 | + |
| 30 | +/** |
| 31 | + * RemoteSpeechModel class provides a remote speech model implementation. |
| 32 | + * It generates speech from text using the Wrapper classes. |
| 33 | + * |
| 34 | + * This version support google speech models only. |
| 35 | + * |
| 36 | + * To use Google speech services: |
| 37 | + * 1- Go to console.cloud.google.com. |
| 38 | + * 2- Enable "Cloud Text-to-Speech API". |
| 39 | + * 3- Generate API key from "Credentials" page. |
| 40 | + * |
| 41 | + * @author github.com/Barqawiz |
| 42 | + */ |
| 43 | +public class RemoteSpeechModel { |
| 44 | + |
| 45 | + private SpeechModels keyType; |
| 46 | + private GoogleAIWrapper wrapper; |
| 47 | + |
| 48 | + /** |
| 49 | + * |
| 50 | + * Constructs a new RemoteSpeechModel object with the specified key value and key type string. |
| 51 | + * If keyTypeString is empty, it is set to "google" by default. |
| 52 | + * |
| 53 | + * @param keyValue the API key value to use. |
| 54 | + * @param keyTypeString the string representation of the key type. |
| 55 | + */ |
| 56 | + public RemoteSpeechModel(String keyValue, String keyTypeString) { |
| 57 | + |
| 58 | + if (keyTypeString.isEmpty()) { |
| 59 | + keyTypeString = SpeechModels.google.toString(); |
| 60 | + } |
| 61 | + |
| 62 | + List<String> supportedModels = this.getSupportedModels(); |
| 63 | + |
| 64 | + |
| 65 | + if (supportedModels.contains(keyTypeString)) { |
| 66 | + this.initiate(keyValue, SpeechModels.valueOf(keyTypeString)); |
| 67 | + } else { |
| 68 | + String models = String.join(" - ", supportedModels); |
| 69 | + throw new IllegalArgumentException("The received keyValue not supported. Send any model from: " + models); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * |
| 75 | + * Constructs a new RemoteSpeechModel object with the specified key value and key type. |
| 76 | + * |
| 77 | + * @param keyValue The API key value to use. |
| 78 | + * @param keyType The SpeechModels enum value representing the key type. |
| 79 | + */ |
| 80 | + public RemoteSpeechModel(String keyValue, SpeechModels keyType) { |
| 81 | + this.initiate(keyValue, keyType); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Initiate the object with the specified key value and key type. |
| 86 | + * |
| 87 | + * @param keyValue the API key value to use. |
| 88 | + * @param keyType the SpeechModels enum value representing the key type. |
| 89 | + */ |
| 90 | + private void initiate(String keyValue, SpeechModels keyType) { |
| 91 | + |
| 92 | + this.keyType = keyType; |
| 93 | + wrapper = new GoogleAIWrapper(keyValue); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get a list of supported key type models. |
| 98 | + * |
| 99 | + * @return list of the supported SpeechModels enum values. |
| 100 | + */ |
| 101 | + public List<String> getSupportedModels() { |
| 102 | + SpeechModels[] values = SpeechModels.values(); |
| 103 | + List<String> enumValues = new ArrayList<>(); |
| 104 | + |
| 105 | + for (int i = 0; i < values.length; i++) { |
| 106 | + enumValues.add(values[i].name()); |
| 107 | + } |
| 108 | + |
| 109 | + return enumValues; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Generates speech from text using the support models. |
| 114 | + * |
| 115 | + * You can save the returned byte to audio file using FileOutputStream("path/audio.mp3"). |
| 116 | + * |
| 117 | + * @param input SpeechInput object containing the text and gender to use. |
| 118 | + * @return byte array of the decoded audio content. |
| 119 | + * @throws IOException in case of communication error. |
| 120 | + */ |
| 121 | + public byte[] generateEnglishText(SpeechInput input) throws IOException { |
| 122 | + |
| 123 | + if (this.keyType == SpeechModels.google) { |
| 124 | + return this.generateGoogleText(input.getText(), input.getGender(), "en-gb"); |
| 125 | + } else { |
| 126 | + throw new IllegalArgumentException("the keyType not supported"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Generates speech from text using the Google Speech service API. |
| 132 | + * |
| 133 | + * @param text text to generate the speech. |
| 134 | + * @param gender gender to use (male or female). |
| 135 | + * @param language en-gb. |
| 136 | + * @return |
| 137 | + * @throws IOException in case of communication error. |
| 138 | + */ |
| 139 | + private byte[] generateGoogleText(String text, Gender gender, String language) throws IOException { |
| 140 | + byte[] decodedAudio = null; |
| 141 | + |
| 142 | + Map<String, Object> params = new HashMap<>(); |
| 143 | + params.put("text", text); |
| 144 | + params.put("languageCode", language); |
| 145 | + |
| 146 | + if (gender == Gender.FEMALE) { |
| 147 | + params.put("name", "en-GB-Standard-A"); |
| 148 | + params.put("ssmlGender", "FEMALE"); |
| 149 | + } else { |
| 150 | + params.put("name", "en-GB-Standard-B"); |
| 151 | + params.put("ssmlGender", "MALE"); |
| 152 | + } |
| 153 | + |
| 154 | + AudioResponse resModel = (AudioResponse) wrapper.generateSpeech(params); |
| 155 | + decodedAudio = AudioHelper.decode(resModel.getAudioContent()); |
| 156 | + |
| 157 | + return decodedAudio; |
| 158 | + } |
| 159 | +} |
0 commit comments