Skip to content

Commit 07ff6cc

Browse files
committed
Speech: Update API to reflect documentation.
1 parent e66e1c3 commit 07ff6cc

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

inc/genhdr/qstrdefs.generated.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,9 @@ QDEF(MP_QSTR_sing, (const byte*)"\xb6\x04" "sing")
703703
QDEF(MP_QSTR_throat, (const byte*)"\x31\x06" "throat")
704704
QDEF(MP_QSTR_mouth, (const byte*)"\x6e\x05" "mouth")
705705
QDEF(MP_QSTR_speed, (const byte*)"\x62\x05" "speed")
706-
QDEF(MP_QSTR_radio, (const byte*)"\xd4\x05" "radio")
707706
QDEF(MP_QSTR_debug, (const byte*)"\xd4\x05" "debug")
707+
QDEF(MP_QSTR_translate, (const byte*)"\x43\x09" "translate")
708+
QDEF(MP_QSTR_radio, (const byte*)"\xd4\x05" "radio")
708709
QDEF(MP_QSTR_config, (const byte*)"\x4f\x06" "config")
709710
QDEF(MP_QSTR_send_bytes, (const byte*)"\xbf\x0a" "send_bytes")
710711
QDEF(MP_QSTR_receive_bytes, (const byte*)"\x88\x0d" "receive_bytes")

inc/microbit/qstrdefsport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ Q(throat)
429429
Q(mouth)
430430
Q(speed)
431431
Q(debug)
432+
Q(translate)
433+
432434
Q(radio)
433435
Q(reset)
434436
Q(config)

source/microbit/modspeech.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static mp_obj_t make_speech_iter(void) {
118118
return result;
119119
}
120120

121-
static mp_obj_t pronounce(mp_obj_t words) {
121+
static mp_obj_t translate(mp_obj_t words) {
122122
mp_uint_t len, outlen;
123123
const char *txt = mp_obj_str_get_data(words, &len);
124124
// Reciter truncates *output* at about 120 characters.
@@ -145,11 +145,11 @@ static mp_obj_t pronounce(mp_obj_t words) {
145145
// Prevent input becoming invisible to GC due to tail-call optimisation.
146146
MP_STATE_PORT(speech_data) = NULL;
147147
return res;
148-
}MP_DEFINE_CONST_FUN_OBJ_1(pronounce_obj, pronounce);
148+
}MP_DEFINE_CONST_FUN_OBJ_1(translate_obj, translate);
149149

150150
extern int debug;
151151

152-
static mp_obj_t articulate(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool sing) {
152+
static mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool sing) {
153153

154154
static const mp_arg_t allowed_args[] = {
155155
{ MP_QSTR_pitch, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_PITCH} },
@@ -161,7 +161,7 @@ static mp_obj_t articulate(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
161161

162162
// parse args
163163
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
164-
mp_arg_parse_all(n_args-1, pos_args+1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
164+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
165165

166166
sam_memory *sam = m_new(sam_memory, 1);
167167
MP_STATE_PORT(speech_data) = sam;
@@ -175,7 +175,7 @@ static mp_obj_t articulate(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
175175
debug = args[4].u_bool;
176176

177177
mp_uint_t len;
178-
const char *input = mp_obj_str_get_data(pos_args[0], &len);
178+
const char *input = mp_obj_str_get_data(phonemes, &len);
179179
buf_start_pos = 0;
180180
speech_iterator_t *src = make_speech_iter();
181181
buf = src->buf;
@@ -203,12 +203,18 @@ static mp_obj_t articulate(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
203203
}
204204

205205
static mp_obj_t say(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
206-
return articulate(n_args, pos_args, kw_args, false);
206+
mp_obj_t phonemes = translate(pos_args[0]);
207+
return articulate(phonemes, n_args-1, pos_args+1, kw_args, false);
207208
}
208209
MP_DEFINE_CONST_FUN_OBJ_KW(say_obj, 1, say);
209210

211+
static mp_obj_t pronounce(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
212+
return articulate(pos_args[0], n_args-1, pos_args+1, kw_args, false);
213+
}
214+
MP_DEFINE_CONST_FUN_OBJ_KW(pronounce_obj, 1, pronounce);
215+
210216
static mp_obj_t sing(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
211-
return articulate(n_args, pos_args, kw_args, true);
217+
return articulate(pos_args[0], n_args-1, pos_args+1, kw_args, true);
212218
}
213219
MP_DEFINE_CONST_FUN_OBJ_KW(sing_obj, 1, sing);
214220

@@ -217,6 +223,7 @@ static const mp_map_elem_t _globals_table[] = {
217223
{ MP_OBJ_NEW_QSTR(MP_QSTR_say), (mp_obj_t)&say_obj },
218224
{ MP_OBJ_NEW_QSTR(MP_QSTR_sing), (mp_obj_t)&sing_obj },
219225
{ MP_OBJ_NEW_QSTR(MP_QSTR_pronounce), (mp_obj_t)&pronounce_obj },
226+
{ MP_OBJ_NEW_QSTR(MP_QSTR_translate), (mp_obj_t)&translate_obj },
220227
};
221228
static MP_DEFINE_CONST_DICT(_globals, _globals_table);
222229

0 commit comments

Comments
 (0)