-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the sleep logic for streaming as per sample rate (#279)
* sleep 100ms instead of sleeping as function of rate * added sleep as inverse of sampling rate * added LINEAR16 bytes per sample constant * added few variables * added truth and log4j * added 32khz audio * added Streaming Test * added unit tests for 16 and 32 khz audio * checkstyle fixes * renamed to KHz * added comment
- Loading branch information
Showing
4 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
This file contains 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
87 changes: 87 additions & 0 deletions
87
speech/grpc/src/test/java/com/examples/cloud/speech/StreamingRecognizeClientTest.java
This file contains 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,87 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.examples.cloud.speech; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import io.grpc.ManagedChannel; | ||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.SimpleLayout; | ||
import org.apache.log4j.WriterAppender; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
|
||
/** | ||
* Unit tests for {@link StreamingRecognizeClient }. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class StreamingRecognizeClientTest { | ||
private Writer writer; | ||
private WriterAppender appender; | ||
|
||
@Before | ||
public void setUp() { | ||
writer = new StringWriter(); | ||
appender = new WriterAppender(new SimpleLayout(), writer); | ||
Logger.getRootLogger().addAppender(appender); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
Logger.getRootLogger().removeAppender(appender); | ||
} | ||
|
||
@Test | ||
public void test16KHzAudio() throws InterruptedException, IOException { | ||
URI uri = new File("resources/audio.raw").toURI(); | ||
Path path = Paths.get(uri); | ||
|
||
String host = "speech.googleapis.com"; | ||
int port = 443; | ||
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port); | ||
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 16000); | ||
|
||
client.recognize(); | ||
assertThat(writer.toString()).contains("transcript: \"how old is the Brooklyn Bridge\""); | ||
} | ||
|
||
@Test | ||
public void test32KHzAudio() throws InterruptedException, IOException { | ||
URI uri = new File("resources/audio32KHz.raw").toURI(); | ||
Path path = Paths.get(uri); | ||
|
||
String host = "speech.googleapis.com"; | ||
int port = 443; | ||
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port); | ||
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 32000); | ||
|
||
client.recognize(); | ||
assertThat(writer.toString()).contains("transcript: \"how old is the Brooklyn Bridge\""); | ||
} | ||
} |