Skip to content

Commit ee98aad

Browse files
author
Devota Aabel
authored
Added API voice (#743)
1 parent e096b0c commit ee98aad

11 files changed

Lines changed: 395 additions & 4 deletions

File tree

samples/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies {
1010
implementation project(":services-geojson")
1111
implementation project(":services-matching")
1212
implementation project(":services-staticmap")
13+
implementation project(":services-speech")
1314
}
1415

1516
buildConfig {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mapbox.samples;
2+
3+
import com.mapbox.api.speech.v1.MapboxSpeech;
4+
import com.mapbox.sample.BuildConfig;
5+
6+
import okhttp3.ResponseBody;
7+
import retrofit2.Call;
8+
import retrofit2.Callback;
9+
import retrofit2.Response;
10+
11+
public class BasicSpeech {
12+
13+
public static void main(String[] args) {
14+
MapboxSpeech mapboxSpeech = MapboxSpeech.builder()
15+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
16+
.instruction("turn right")
17+
.build();
18+
19+
mapboxSpeech.enqueueCall(new Callback<ResponseBody>() {
20+
@Override
21+
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
22+
System.out.println(response.body().contentType());
23+
}
24+
25+
@Override
26+
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
27+
System.out.println(throwable);
28+
}
29+
});
30+
}
31+
}

services-core/src/main/java/com/mapbox/core/MapboxService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class MapboxService<T, S> {
2424

2525
private final Class<S> serviceType;
2626
private boolean enableDebug;
27-
private OkHttpClient okHttpClient;
27+
protected OkHttpClient okHttpClient;
2828
private okhttp3.Call.Factory callFactory;
2929
private Retrofit retrofit;
3030
private Call<T> call;
@@ -209,7 +209,7 @@ public void setCallFactory(okhttp3.Call.Factory callFactory) {
209209
* @return OkHttpClient
210210
* @since 1.0.0
211211
*/
212-
public synchronized OkHttpClient getOkHttpClient() {
212+
protected synchronized OkHttpClient getOkHttpClient() {
213213
if (okHttpClient == null) {
214214
if (isEnableDebug()) {
215215
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
@@ -221,7 +221,6 @@ public synchronized OkHttpClient getOkHttpClient() {
221221
okHttpClient = new OkHttpClient();
222222
}
223223
}
224-
225224
return okHttpClient;
226225
}
227226
}

services-speech/build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: 'java-library'
2+
3+
dependencies {
4+
api project(":services-core")
5+
6+
// Annotations
7+
compileOnly dependenciesList.supportAnnotation
8+
9+
// AutoValue
10+
compileOnly dependenciesList.autoValue
11+
12+
// Test Dependencies
13+
testImplementation dependenciesList.okhttp3Mockwebserver
14+
testImplementation project(path: ':services-core', configuration: 'testOutput')
15+
}
16+
17+
apply from: "${rootDir}/gradle/checkstyle.gradle"
18+
apply from: "${rootDir}/gradle/jacoco.gradle"
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.mapbox.api.speech.v1;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.google.auto.value.AutoValue;
7+
import com.mapbox.core.MapboxService;
8+
import com.mapbox.core.constants.Constants;
9+
import com.mapbox.core.exceptions.ServicesException;
10+
import com.mapbox.core.utils.TextUtils;
11+
12+
import java.util.logging.Logger;
13+
14+
import okhttp3.Cache;
15+
import okhttp3.OkHttpClient;
16+
import okhttp3.ResponseBody;
17+
import okhttp3.logging.HttpLoggingInterceptor;
18+
import retrofit2.Call;
19+
20+
/**
21+
* The Speech API is a text-to-speech APi with a server-side caching layer in front of AWS Polly.
22+
* The only requirements are text to dictate, and a Mapbox access token. For 3-step-ahead
23+
* client-side caching, cache directory is required.
24+
*
25+
* @since 3.0.0
26+
*/
27+
@AutoValue
28+
public abstract class MapboxSpeech extends MapboxService<ResponseBody, SpeechService> {
29+
private static final Logger LOGGER = Logger.getLogger(MapboxSpeech.class.getName());
30+
31+
protected MapboxSpeech() {
32+
super(SpeechService.class);
33+
}
34+
35+
@Override
36+
protected Call<ResponseBody> initializeCall() {
37+
return getService().getCall(
38+
instruction(),
39+
textType(),
40+
language(),
41+
outputType(),
42+
accessToken());
43+
}
44+
45+
@Nullable
46+
abstract String language();
47+
48+
@Nullable
49+
abstract String textType();
50+
51+
@Nullable
52+
abstract String outputType();
53+
54+
@Nullable
55+
abstract Cache cache();
56+
57+
@NonNull
58+
abstract String accessToken();
59+
60+
@NonNull
61+
abstract String instruction();
62+
63+
@Override
64+
protected abstract String baseUrl();
65+
66+
@Override
67+
public synchronized OkHttpClient getOkHttpClient() {
68+
if (okHttpClient == null) {
69+
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
70+
if (isEnableDebug()) {
71+
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
72+
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
73+
httpClient.addInterceptor(logging);
74+
}
75+
if (cache() != null) {
76+
httpClient.cache(cache());
77+
}
78+
79+
okHttpClient = httpClient.build();
80+
}
81+
return okHttpClient;
82+
}
83+
84+
/**
85+
* Creates a builder for a MapboxSpeech object with a default cache size of 10 MB.
86+
*
87+
* @return a builder to create a MapboxSpeech object
88+
* @since 3.0.0
89+
*/
90+
public static Builder builder() {
91+
return new AutoValue_MapboxSpeech.Builder()
92+
.baseUrl(Constants.BASE_API_URL);
93+
}
94+
95+
/**
96+
* This builder is used to create a MapboxSpeech instance, with details about how the API calls
97+
* should be made (input/output format, language, etc.). To use caching, specify a cache
98+
* directory. Access token and instruction are required, along with cache directory if you choose
99+
* to use caching.
100+
*
101+
* @since 3.0.0
102+
*/
103+
@AutoValue.Builder
104+
public abstract static class Builder {
105+
/**
106+
* Language of which to request the instructions be spoken. Default is "en-us"
107+
*
108+
* @param language as a string, i.e., "en-us"
109+
* @return this builder for chaining options together
110+
* @since 3.0.0
111+
*/
112+
public abstract Builder language(String language);
113+
114+
/**
115+
* Format which the input is specified. If not specified, default is text
116+
*
117+
* @param textType either text or ssml
118+
* @return this builder for chaining options together
119+
* @since 3.0.0
120+
*/
121+
public abstract Builder textType(String textType);
122+
123+
/**
124+
* Output format for spoken instructions. If not specified, default is mp3
125+
*
126+
* @param outputType either mp3 or json
127+
* @return this builder for chaining options together
128+
* @since 3.0.0
129+
*/
130+
public abstract Builder outputType(String outputType);
131+
132+
/**
133+
* Required to call when this is being built. If no access token provided,
134+
* {@link ServicesException} will be thrown.
135+
*
136+
* @param accessToken Mapbox access token, You must have a Mapbox account in order to use
137+
* the Optimization API
138+
* @return this builder for chaining options together
139+
* @since 3.0.0
140+
*/
141+
public abstract Builder accessToken(@NonNull String accessToken);
142+
143+
/**
144+
* Add the instruction text to dictate, either as plain text or ssml.
145+
*
146+
* @param instruction to dictate
147+
* @return this builder for chaining options together
148+
* @since 3.0.0
149+
*/
150+
public abstract Builder instruction(@NonNull String instruction);
151+
152+
/**
153+
* Optionally change the APIs base URL to something other then the default Mapbox one.
154+
*
155+
* @param baseUrl base url used as end point
156+
* @return this builder for chaining options together
157+
* @since 2.1.0
158+
*/
159+
public abstract Builder baseUrl(@NonNull String baseUrl);
160+
161+
/**
162+
* Adds an optional cache to set in the OkHttp client.
163+
*
164+
* @param cache to set for OkHttp
165+
* @return this builder for chaining options together
166+
* @since 3.0.0
167+
*/
168+
public abstract Builder cache(Cache cache);
169+
170+
abstract MapboxSpeech autoBuild();
171+
172+
/**
173+
* This uses the provided parameters set using the {@link Builder} and first checks that all
174+
* values are valid, formats the values as strings for easier consumption by the API, and lastly
175+
* creates a new {@link MapboxSpeech} object with the values provided.
176+
*
177+
* @return a new instance of Mapbox Speech
178+
* @throws ServicesException when a provided parameter is detected to be incorrect
179+
* @since 3.0.0
180+
*/
181+
public MapboxSpeech build() {
182+
MapboxSpeech mapboxSpeech = autoBuild();
183+
184+
if (TextUtils.isEmpty(mapboxSpeech.instruction())) {
185+
throw new ServicesException("Non-null, non-empty instruction text is required.");
186+
}
187+
188+
return mapboxSpeech;
189+
}
190+
}
191+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mapbox.api.speech.v1;
2+
3+
import okhttp3.ResponseBody;
4+
import retrofit2.Call;
5+
import retrofit2.http.GET;
6+
import retrofit2.http.Path;
7+
import retrofit2.http.Query;
8+
9+
/**
10+
* Interface that defines the speech service.
11+
*
12+
* @since 3.0.0
13+
*/
14+
public interface SpeechService {
15+
16+
/**
17+
* Constructs the html call using the information passed in through the
18+
* {@link MapboxSpeech.Builder}.
19+
*
20+
* @param text text to dictate
21+
* @param textType text type, either "text" or "ssml" (default is "text")
22+
* @param language language locale, default is "en-us"
23+
* @param outputFormat output format, either "mp3" or "json", default is "mp3"
24+
* @param accessToken Mapbox access token
25+
* @return the MapboxSpeech response in a Call wrapper
26+
* @since 3.0.0
27+
*/
28+
@GET("/voice/v1/speak/{text}")
29+
Call<ResponseBody> getCall(
30+
@Path("text") String text,
31+
@Query("textType") String textType,
32+
@Query("language") String language,
33+
@Query("outputFormat") String outputFormat,
34+
@Query("access_token") String accessToken);
35+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Contains the Mapbox Java Services wrapper for the Speech API.
3+
*
4+
* @since 3.0.0
5+
*/
6+
package com.mapbox.api.speech.v1;

0 commit comments

Comments
 (0)