Skip to content

Fix Google Speech #10

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ public void onVoiceStart() {

@Override
public void onVoice(byte[] data, int size) {



RecognitionConfig.AudioEncoding encoding =
RecognitionConfig.AudioEncoding.LINEAR16;
RecognitionConfig config = RecognitionConfig.newBuilder()
//.setMaxAlternatives(1)
//.setProfanityFilter(true)
.setEncoding(encoding)
.setSampleRateHertz(mAudioRecorder.getSampleRate())
.setLanguageCode(getDefaultLanguageCode())
.setLanguageCode("en-US")
.build();

RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(ByteString.copyFrom(data))
.setContent(ByteString.copyFrom(data, 0, size))
.build();

try (SpeechClient speech = generateFromRawFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public final class GoogleSpeechSynthesizer extends BaseSpeechSynthesizer {
private AudioConfig audioConfig;
private MediaPlayer mediaPlayer;
private final ConditionVariable mCondVar = new ConditionVariable();
private boolean completed; //released
private int extraCode; //released

GoogleSpeechSynthesizer(Application application,
ComponentConfig configuration,
Expand All @@ -75,11 +73,10 @@ public final class GoogleSpeechSynthesizer extends BaseSpeechSynthesizer {
@Override
public void setup(SynthesizerListener.OnSetup onSynthesizerSetup) {
logger.i(TAG, "GOOGLE TTS - setup and check language");
// At this stage, we only want to check whether the api works and the language is available
try (TextToSpeechClient ttsClient = generateFromRawFile(
application, getConfiguration().getGoogleCredentialsResourceFile())) {
ListVoicesResponse response = ttsClient.listVoices(getDefaultLanguageCode());
ttsClient.shutdownNow();
ttsClient.awaitTermination(2, TimeUnit.SECONDS);
if (response.getVoicesCount() > 0) {
onSynthesizerSetup.execute(SynthesizerListener.Status.AVAILABLE);
} else {
Expand Down Expand Up @@ -158,7 +155,7 @@ private void finishPlayer() {
if (mediaPlayer != null) mediaPlayer.stop();
logger.v(TAG, "GOOGLE TTS - stopped");
} catch (IllegalStateException ex) {
// Do nothing, the player is already stopped
// Does nothing, the player is already stopped
}
mCondVar.open();
if (mediaPlayer != null) {
Expand All @@ -167,33 +164,31 @@ private void finishPlayer() {
}
}

@Override
public void shutdown() {
logger.w(TAG, "GOOGLE TTS - shutting down");
this.stop();
release();
}

private void destroyTts() {
if (!isTtsNull()) {
try {
tts.close();
tts.shutdown();
tts.awaitTermination(2 ,TimeUnit.SECONDS);
tts.awaitTermination(2, TimeUnit.SECONDS);
logger.v(TAG, "GOOGLE TTS - destroyed");
} catch (Exception ignored) {}
tts = null;
}
}

@Override
public void shutdown() {
logger.w(TAG, "GOOGLE TTS - shutting down");
this.stop();
release();
}

@Override
public void release() {
super.release();
tts = null;
voice = null;
audioConfig = null;
completed = true;
extraCode = 0;
}

@Override
Expand All @@ -206,11 +201,11 @@ void initTts(SynthesizerListener.OnInitialised onSynthesizerInitialised) {
if (isTtsNull()) {
setReady(false);
logger.i(TAG, "GOOGLE TTS - creating new instance of TextToSpeechClient.class");
try (TextToSpeechClient ttsClient = generateFromRawFile(
application, getConfiguration().getGoogleCredentialsResourceFile())) {
try {
logger.i(TAG, "GOOGLE TTS - new instance created");
setReady(true);
this.tts = ttsClient;
this.tts = generateFromRawFile(
application, getConfiguration().getGoogleCredentialsResourceFile());
this.audioConfig = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
setupLanguage();
setSynthesizerUtteranceListener(createUtterancesListener());
Expand Down Expand Up @@ -278,7 +273,7 @@ public void run() {
public void onStart(String utteranceId) {
logger.v(getTag(), "GOOGLE TTS[%s] - on start", utteranceId);

startTimeout(utteranceId);
//startTimeout(utteranceId);
timestamp = System.currentTimeMillis();

if (getListenersMap().size() > 0) {
Expand Down Expand Up @@ -376,23 +371,19 @@ private void play(String utteranceId, String text, HashMap<String, String> param
getSynthesizerUtteranceListener().onStart(utteranceId);
initTts(status -> {
if (status == SynthesizerListener.Status.SUCCESS) {
mCondVar.close();
completed = false;

SynthesisInput input = SynthesisInput.newBuilder()
.setText(text)
.build();
SynthesizeSpeechResponse response = tts.synthesizeSpeech(input, voice, audioConfig);
destroyTts();
//destroyTts();

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();

logger.d(getTag(), "audio: %s", audioContents);
logger.d(getTag(), "audio string: %s", audioContents.toStringUtf8());

extraCode = -1;

try {
File tempMp3 = File.createTempFile("output", "mp3",
application.getCacheDir());
Expand All @@ -409,29 +400,20 @@ private void play(String utteranceId, String text, HashMap<String, String> param
return;
}

mediaPlayer.setOnCompletionListener(mp -> {
completed = true;
mCondVar.open();
mediaPlayer.setOnCompletionListener(mediaPlayer -> {
finishPlayer();
getSynthesizerUtteranceListener().onDone(utteranceId);
});
mediaPlayer.setOnErrorListener((mp, what, extra) -> {
extraCode = extra;
mCondVar.open();
finishPlayer();
// TODO: When I release the timeout, since I already run onError, it might be called twice
getSynthesizerUtteranceListener().onError(utteranceId, extra);
return true;
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();
mCondVar.block();
finishPlayer();
} catch (Exception ex) {
logger.logException(ex);
mCondVar.open();
}

if (completed) {
getSynthesizerUtteranceListener().onDone(utteranceId);
} else {
// TODO: When I release the timeout, since I already run onError, it might be called twice
getSynthesizerUtteranceListener().onError(utteranceId, extraCode);
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private AudioRecord createAudioRecord() {
if (sizeInBytes == AudioRecord.ERROR_BAD_VALUE) {
continue;
}
final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate, CHANNEL, ENCODING, sizeInBytes);
if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
mBuffer = new byte[sizeInBytes];
Expand Down