-
Notifications
You must be signed in to change notification settings - Fork 0
Add audio tag #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
minhphu20
wants to merge
4
commits into
master
Choose a base branch
from
minhphu/textspeak
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add audio tag #42
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.google.codeu.data; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| import com.google.cloud.texttospeech.v1.AudioConfig; | ||
| import com.google.cloud.texttospeech.v1.AudioEncoding; | ||
| import com.google.cloud.texttospeech.v1.SsmlVoiceGender; | ||
| import com.google.cloud.texttospeech.v1.SynthesisInput; | ||
| import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; | ||
| import com.google.cloud.texttospeech.v1.TextToSpeechClient; | ||
| import com.google.cloud.texttospeech.v1.VoiceSelectionParams; | ||
| import com.google.protobuf.ByteString; | ||
| import java.io.FileOutputStream; | ||
| import java.io.OutputStream; | ||
| import java.lang.Exception; | ||
|
|
||
| public class QuickstartSample { | ||
|
|
||
| /** | ||
| * Demonstrates using the Text-to-Speech API. | ||
| */ | ||
| public static void main(String... args) throws Exception { | ||
| // Instantiates a client | ||
| try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { | ||
| // Set the text input to be synthesized | ||
| SynthesisInput input = SynthesisInput.newBuilder() | ||
| .setText("Hello this is really nice haha.") | ||
| .build(); | ||
|
|
||
| // Build the voice request, select the language code ("en-US") and the ssml voice gender | ||
| // ("neutral") | ||
| VoiceSelectionParams voice = VoiceSelectionParams.newBuilder() | ||
| .setLanguageCode("en-US") | ||
| // Try experimenting with the different voices | ||
| .setSsmlGender(SsmlVoiceGender.NEUTRAL) // Try experimenting with t | ||
| .build(); | ||
|
|
||
| // Select the type of audio file you want returned | ||
| AudioConfig audioConfig = AudioConfig.newBuilder() | ||
| .setAudioEncoding(AudioEncoding.MP3) | ||
| .build(); | ||
|
|
||
| // Perform the text-to-speech request on the text input with the selected voice parameters and | ||
| // audio file type | ||
| SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, | ||
| audioConfig); | ||
|
|
||
| // Get the audio contents from the response | ||
| ByteString audioContents = response.getAudioContent(); | ||
|
|
||
| // Write the response to the output file. | ||
| try (OutputStream out = new FileOutputStream("/Users/minhphu/Desktop/GoogleCode/codeu-starter-project/output.mp3")) { | ||
| out.write(audioContents.toByteArray()); | ||
| System.out.println("Audio content written to file \"/Users/minhphu/Desktop/GoogleCode/codeu-starter-project/output.mp3\""); | ||
| } | ||
| } | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
src/main/java/com/google/codeu/servlets/MessageServlet2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package com.google.codeu.servlets; | ||
|
|
||
| import com.google.appengine.api.users.UserService; | ||
| import com.google.appengine.api.users.UserServiceFactory; | ||
| import com.google.codeu.data.Message; | ||
| import java.io.IOException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.jsoup.Jsoup; | ||
| import org.jsoup.safety.Whitelist; | ||
| import com.google.cloud.texttospeech.v1.AudioConfig; | ||
| import com.google.cloud.texttospeech.v1.AudioEncoding; | ||
| import com.google.cloud.texttospeech.v1.SsmlVoiceGender; | ||
| import com.google.cloud.texttospeech.v1.SynthesisInput; | ||
| import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; | ||
| import com.google.cloud.texttospeech.v1.TextToSpeechClient; | ||
| import com.google.cloud.texttospeech.v1.VoiceSelectionParams; | ||
| import com.google.protobuf.ByteString; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** Handles fetching and saving {@link Message} instances. */ | ||
| @WebServlet("/a11y/tts") | ||
| public class MessageServlet2 extends HttpServlet { | ||
| /** Responses with a bytestream from the Google Cloud | ||
| /* Text-to-Speech API | ||
| **/ | ||
| @Override | ||
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
|
|
||
| UserService userService = UserServiceFactory.getUserService(); | ||
| if (!userService.isUserLoggedIn()) { | ||
| response.sendRedirect("/index.html"); | ||
| return; | ||
| } | ||
| // To get clean messages, removed dangerous things, html | ||
| String userMessage = Jsoup.clean(request.getReader().lines().collect(Collectors.joining(System.lineSeparator())), Whitelist.none()); | ||
|
|
||
| try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { | ||
| // Set the text input to be synthesized | ||
| SynthesisInput input = SynthesisInput.newBuilder() | ||
| .setText(userMessage) | ||
| .build(); | ||
|
|
||
| // Build the voice request, select the language code ("en-US") and the ssml voice gender | ||
| // ("neutral") | ||
| VoiceSelectionParams voice = VoiceSelectionParams.newBuilder() | ||
| .setLanguageCode("en-US") | ||
| .setSsmlGender(SsmlVoiceGender.NEUTRAL) | ||
| .build(); | ||
|
|
||
| // Select the type of audio file you want returned | ||
| AudioConfig audioConfig = AudioConfig.newBuilder() | ||
| .setAudioEncoding(AudioEncoding.MP3) | ||
| .build(); | ||
|
|
||
| // Perform the text-to-speech request on the text input with the selected voice parameters and | ||
| // audio file type | ||
| SynthesizeSpeechResponse apiResponse = textToSpeechClient.synthesizeSpeech(input, voice, | ||
| audioConfig); | ||
|
|
||
| // Get the audio contents from the response | ||
| ByteString audioContents = apiResponse.getAudioContent(); | ||
|
|
||
| response.setContentType("audio/mpeg"); | ||
| response.getOutputStream().write(audioContents.toByteArray()); | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/google/codeu/servlets/QuickstartServlet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.google.codeu.servlets; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import com.google.codeu.data.QuickstartSample; | ||
|
|
||
| /** | ||
| * Runs QuickstartSample class to demonstrate the text-to-speech API | ||
| */ | ||
| @WebServlet("/quickstart") | ||
| public class QuickstartServlet extends HttpServlet { | ||
|
|
||
| private QuickstartSample quickstartSample; | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| try { | ||
| quickstartSample = new QuickstartSample(); | ||
| quickstartSample.main(); | ||
| } catch (Exception e) { | ||
| System.out.println("An exception was thrown."); | ||
| } | ||
| response.setContentType("application/json"); | ||
|
|
||
| response.getWriter().println("Have printed the file"); | ||
| } | ||
|
|
||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment for the class.