Skip to content

Commit 761547e

Browse files
committed
Get SAM working on the microbit. Fix up signed-ness, make read-only arrays const and add minimal iterator to get SAM working with audio.
1 parent 268623b commit 761547e

File tree

11 files changed

+196
-49
lines changed

11 files changed

+196
-49
lines changed

inc/genhdr/qstrdefs.generated.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ QDEF(MP_QSTR_listdir, (const byte*)"\x98\x07" "listdir")
696696
QDEF(MP_QSTR_machine, (const byte*)"\x60\x07" "machine")
697697
QDEF(MP_QSTR_size, (const byte*)"\x20\x04" "size")
698698
QDEF(MP_QSTR_is_playing, (const byte*)"\x04\x0a" "is_playing")
699+
QDEF(MP_QSTR_speech, (const byte*)"\x6d\x06" "speech")
700+
QDEF(MP_QSTR_say, (const byte*)"\xae\x03" "say")
699701
QDEF(MP_QSTR_radio, (const byte*)"\xd4\x05" "radio")
700702
QDEF(MP_QSTR_config, (const byte*)"\x4f\x06" "config")
701703
QDEF(MP_QSTR_send_bytes, (const byte*)"\xbf\x0a" "send_bytes")

inc/microbit/modaudio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ typedef struct _microbit_audio_frame_obj_t {
2020

2121
extern const mp_obj_type_t microbit_audio_frame_type;
2222

23+
bool microbit_audio_is_playing(void);
24+
2325
microbit_audio_frame_obj_t *new_microbit_audio_frame(void);
2426

2527
#endif // __MICROPY_INCLUDED_MICROBIT_AUDIO_H__

inc/microbit/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extern const struct _mp_obj_module_t random_module;
9696
extern const struct _mp_obj_module_t os_module;
9797
extern const struct _mp_obj_module_t radio_module;
9898
extern const struct _mp_obj_module_t audio_module;
99+
extern const struct _mp_obj_module_t speech_module;
99100

100101
#define MICROPY_PORT_BUILTIN_MODULES \
101102
{ MP_OBJ_NEW_QSTR(MP_QSTR_microbit), (mp_obj_t)&microbit_module }, \
@@ -108,6 +109,7 @@ extern const struct _mp_obj_module_t audio_module;
108109
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
109110
{ MP_OBJ_NEW_QSTR(MP_QSTR_radio), (mp_obj_t)&radio_module }, \
110111
{ MP_OBJ_NEW_QSTR(MP_QSTR_audio), (mp_obj_t)&audio_module }, \
112+
{ MP_OBJ_NEW_QSTR(MP_QSTR_speech), (mp_obj_t)&speech_module }, \
111113
\
112114
/* the following provide aliases for existing modules */ \
113115
{ MP_OBJ_NEW_QSTR(MP_QSTR_collections), (mp_obj_t)&mp_module_collections }, \

inc/microbit/qstrdefsport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ Q(machine)
419419
Q(size)
420420

421421
Q(is_playing)
422+
423+
Q(speech)
424+
Q(say)
425+
422426
Q(radio)
423427
Q(reset)
424428
Q(config)

source/lib/sam/ReciterTabs.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define RECITERTABS_H
33

44
//some flags
5-
unsigned char tab36376[] =
5+
const unsigned char tab36376[] =
66
{
77
0, 0, 0, 0, 0, 0, 0, 0, // 0-7
88
0, 0, 0, 0, 0, 0, 0, 0, // 8-15
@@ -20,7 +20,7 @@ unsigned char tab36376[] =
2020
163, 76, 138, 142
2121
};
2222

23-
char rules[] =
23+
const char rules[] =
2424
{
2525
']','A'|0x80,
2626
' ','(','A','.',')', '=','E','H','4','Y','.',' '|0x80,
@@ -478,7 +478,7 @@ char rules[] =
478478
'j'|0x80
479479
};
480480

481-
char rules2[] =
481+
const char rules2[] =
482482
{
483483
'(','A',')', '='|0x80,
484484
'(','!',')', '=','.'|0x80,
@@ -527,15 +527,15 @@ char rules2[] =
527527

528528
//26 items. From 'A' to 'Z'
529529
// positions for mem62 and mem63 for each character
530-
unsigned char tab37489[] =
530+
const unsigned char tab37489[] =
531531
{
532532
0, 149, 247, 162, 57, 197, 6, 126,
533533
199, 38, 55, 78, 145, 241, 85, 161,
534534
254, 36, 69, 45, 167, 54, 83, 46,
535535
71, 218
536536
};
537537

538-
unsigned char tab37515[] =
538+
const unsigned char tab37515[] =
539539
{
540540
125, 126, 126, 127, 128, 129, 130, 130,
541541
130, 132, 132, 132, 132, 132, 133, 135,

source/lib/sam/RenderTabs.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#ifndef RENDERTABS_H
22
#define RENDERTABS_H
33

4-
unsigned char tab48426[5] = { 0x18, 0x1A, 0x17, 0x17, 0x17 };
4+
const unsigned char tab48426[5] = { 0x18, 0x1A, 0x17, 0x17, 0x17 };
55

6-
unsigned char tab47492[] =
6+
const unsigned char tab47492[] =
77
{
88
0 , 0 , 0xE0 , 0xE6 , 0xEC , 0xF3 , 0xF9 , 0 ,
99
6 , 0xC , 6
1010
};
1111

1212

13-
unsigned char amplitudeRescale[] =
13+
const unsigned char amplitudeRescale[] =
1414
{
1515
0 , 1 , 2 , 2 , 2 , 3 , 3 , 4 ,
1616
4 , 5 , 6 , 8 , 9 ,0xB ,0xD ,0xF, 0 //17 elements?
1717
};
1818

1919
// Used to decide which phoneme's blend lengths. The candidate with the lower score is selected.
2020
// tab45856
21-
unsigned char blendRank[] =
21+
const unsigned char blendRank[] =
2222
{
2323
0 , 0x1F , 0x1F , 0x1F , 0x1F , 2 , 2 , 2 ,
2424
2 , 2 , 2 , 2 , 2 , 2 , 5 , 5 ,
@@ -35,7 +35,7 @@ unsigned char blendRank[] =
3535

3636
// Number of frames at the end of a phoneme devoted to interpolating to next phoneme's final value
3737
//tab45696
38-
unsigned char outBlendLength[] =
38+
const unsigned char outBlendLength[] =
3939
{
4040
0 , 2 , 2 , 2 , 2 , 4 , 4 , 4 ,
4141
4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 ,
@@ -52,7 +52,7 @@ unsigned char outBlendLength[] =
5252

5353
// Number of frames at beginning of a phoneme devoted to interpolating to phoneme's final value
5454
// tab45776
55-
unsigned char inBlendLength[] =
55+
const unsigned char inBlendLength[] =
5656
{
5757
0 , 2 , 2 , 2 , 2 , 4 , 4 , 4 ,
5858
4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 ,
@@ -85,7 +85,7 @@ unsigned char inBlendLength[] =
8585
// 67: ** 27 00011011
8686
// 70: ** 25 00011001
8787
// tab45936
88-
unsigned char sampledConsonantFlags[] =
88+
const unsigned char sampledConsonantFlags[] =
8989
{
9090
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
9191
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -131,7 +131,7 @@ unsigned char freq2data[]=
131131
};
132132

133133
//tab45216
134-
unsigned char freq3data[]=
134+
const unsigned char freq3data[]=
135135
{
136136
0x00 , 0x5B , 0x5B , 0x5B , 0x5B , 0x6E , 0x5D , 0x5B ,
137137
0x58 , 0x59 , 0x57 , 0x58 , 0x52 , 0x59 , 0x5D , 0x3E ,
@@ -145,7 +145,7 @@ unsigned char freq3data[]=
145145
0x65 , 0x65 , 0x70 , 0x5E , 0x5E , 0x5E , 0x08 , 0x01
146146
};
147147

148-
unsigned char ampl1data[] =
148+
const unsigned char ampl1data[] =
149149
{
150150
0 , 0 , 0 , 0 , 0 ,0xD ,0xD ,0xE ,
151151
0xF ,0xF ,0xF ,0xF ,0xF ,0xC ,0xD ,0xC ,
@@ -159,7 +159,7 @@ unsigned char ampl1data[] =
159159
0 ,0xC , 0 , 0 , 0 , 0 ,0xF ,0xF
160160
};
161161

162-
unsigned char ampl2data[] =
162+
const unsigned char ampl2data[] =
163163
{
164164
0 , 0 , 0 , 0 , 0 ,0xA ,0xB ,0xD ,
165165
0xE ,0xD ,0xC ,0xC ,0xB , 9 ,0xB ,0xB ,
@@ -173,7 +173,7 @@ unsigned char ampl2data[] =
173173
0 ,0xA , 0 , 0 ,0xA , 0 , 0 , 0
174174
};
175175

176-
unsigned char ampl3data[] =
176+
const unsigned char ampl3data[] =
177177
{
178178
0 , 0 , 0 , 0 , 0 , 8 , 7 , 8 ,
179179
8 , 1 , 1 , 0 , 1 , 0 , 7 , 5 ,
@@ -190,7 +190,7 @@ unsigned char ampl3data[] =
190190

191191

192192
//tab42240
193-
unsigned char sinus[] =
193+
const unsigned char sinus[] =
194194
{
195195
0x00 , 0x00 , 0x00 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 ,
196196
0x10 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x30 ,
@@ -226,7 +226,7 @@ unsigned char sinus[] =
226226
0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0x00 , 0x00 };
227227

228228
//tab42496
229-
unsigned char rectangle[] =
229+
const unsigned char rectangle[] =
230230
{
231231
0x90 , 0x90 , 0x90 , 0x90 , 0x90 , 0x90 , 0x90 , 0x90 ,
232232
0x90 , 0x90 , 0x90 , 0x90 , 0x90 , 0x90 , 0x90 , 0x90 ,
@@ -264,7 +264,7 @@ unsigned char rectangle[] =
264264

265265

266266
//tab42752
267-
unsigned char multtable[] =
267+
const unsigned char multtable[] =
268268
{
269269
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
270270
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -301,7 +301,7 @@ unsigned char multtable[] =
301301
};
302302

303303
//random data ?
304-
unsigned char sampleTable[0x500] =
304+
const unsigned char sampleTable[0x500] =
305305
{
306306
//00
307307

source/lib/sam/main.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ int sam_main(int argc, char **argv)
6060
int i;
6161
int phonetic = 0;
6262

63-
char* wavfilename = NULL;
6463
char input[256];
6564

6665
for(i=0; i<256; i++) input[i] = 0;
@@ -80,11 +79,6 @@ int sam_main(int argc, char **argv)
8079
strncat(input, " ", 256);
8180
} else
8281
{
83-
if (strcmp(&argv[i][1], "wav")==0)
84-
{
85-
wavfilename = argv[i+1];
86-
i++;
87-
} else
8882
if (strcmp(&argv[i][1], "sing")==0)
8983
{
9084
EnableSingmode();

source/lib/sam/render.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,9 @@ unsigned char trans(unsigned char mem39212, unsigned char mem39213);
4949

5050
// contains the final soundbuffer
5151
extern int bufferpos;
52-
extern char *buffer;
53-
54-
/** Scaling c64 rate to sample rate */
55-
// Rate for 22.05kHz
56-
// #define SCALE_RATE(x) (((x)*1310)>>16)
57-
// Rate for 7.8125KHz
58-
#define SCALE_RATE(x) (((x)*464)>>16)
5952

6053
//timetable for more accurate c64 simulation
61-
int timetable[5][5] =
54+
const int timetable[5][5] =
6255
{
6356
{162, 167, 167, 127, 128},
6457
{226, 60, 60, 0, 0},
@@ -67,23 +60,18 @@ int timetable[5][5] =
6760
{199, 0, 0, 54, 54}
6861
};
6962

63+
extern void SamOutputByte(unsigned int pos, unsigned char b);
64+
7065
void Output(int index, unsigned char A)
7166
{
72-
static unsigned oldtimetableindex = 0;
73-
int k;
74-
bufferpos += timetable[oldtimetableindex][index];
75-
oldtimetableindex = index;
76-
// write a little bit in advance
77-
for(k=0; k<5; k++)
78-
buffer[SCALE_RATE(bufferpos) + k] = (A & 15)*16;
67+
static unsigned oldtimetableindex = 0;
68+
bufferpos += timetable[oldtimetableindex][index];
69+
oldtimetableindex = index;
70+
SamOutputByte(bufferpos, (A & 15)*16);
7971
}
8072

8173

8274

83-
84-
85-
86-
8775
//written by me because of different table positions.
8876
// mem[47] = ...
8977
// 168=pitches
@@ -677,9 +665,9 @@ do
677665
// ML : Code47503 is division with remainder, and mem50 gets the sign
678666

679667
// calculate change per frame
680-
mem50 = (((char)(mem53) < 0) ? 128 : 0);
681-
mem51 = abs((char)mem53) % mem40;
682-
mem53 = (unsigned char)((char)(mem53) / mem40);
668+
mem50 = (((signed char)(mem53) < 0) ? 128 : 0);
669+
mem51 = abs((signed char)mem53) % mem40;
670+
mem53 = (unsigned char)((signed char)(mem53) / mem40);
683671

684672
// interpolation range
685673
X = mem40; // number of frames to interpolate over

source/lib/sam/render.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
void Render();
55
void SetMouthThroat(unsigned char mouth, unsigned char throat);
66

7+
/** Scaling c64 rate to sample rate */
8+
// Rate for 22.05kHz
9+
// #define SCALE_RATE(x) (((x)*1310)>>16)
10+
// Rate for 7.8125KHz
11+
#define SCALE_RATE(x) (((x)*420)>>16)
12+
713
#endif

source/microbit/modaudio.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,11 @@ STATIC mp_obj_t play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
446446
}
447447
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_audio_play_obj, 0, play);
448448

449-
STATIC mp_obj_t is_playing(void) {
449+
bool microbit_audio_is_playing(void) {
450+
return running;
451+
}
452+
453+
mp_obj_t is_playing(void) {
450454
return mp_obj_new_bool(running);
451455
}
452456
MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_is_playing_obj, is_playing);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy