summaryrefslogtreecommitdiffstats
path: root/pico/lib/picoacph.c
blob: d59b03d335c3c4f0a08f743db5f8e0a3554d7789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
/*
 * Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
 *
 * 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.
 */
/**
 * @file picoacph.c
 *
 * accentuation and phrasing
 *
 * Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
 * All rights reserved.
 *
 * History:
 * - 2009-04-20 -- initial version
 *
 */

#include "picoos.h"
#include "picodbg.h"
#include "picobase.h"
#include "picodata.h"
#include "picoacph.h"
#include "picokdt.h"
#include "picoklex.h"
#include "picoktab.h"

#ifdef __cplusplus
extern "C" {
#endif
#if 0
}
#endif

/* PU acphStep states */
#define SA_STEPSTATE_COLLECT       0
#define SA_STEPSTATE_PROCESS_PHR  12
#define SA_STEPSTATE_PROCESS_ACC  13
#define SA_STEPSTATE_FEED          2


/* boundary strength state */
#define SA_BOUNDSTRENGTH_SSEP      0 /* sentence separator */
#define SA_BOUNDSTRENGTH_PPHR      1 /* primary phrase separator */


/*  subobject    : AccPhrUnit
 *  shortcut     : acph
 *  context size : one phrase, max. 30 non-PUNC items, for non-processed items
 *                 one item if internal input empty
 */

/**
 * @addtogroup picoacph
 *
 * <b> Pico Accentuation and Phrasing </b>\n
 *
  internal buffers:

  - headx : array for extended item heads of fixed size (head plus
    index for content, plus two fields for boundary strength/type)
  - cbuf : buffer for item contents (referenced by index in
    headx).

  0. bottom up filling of items in headx and cbuf

  1. phrasing (right-to-left):

     e.g. from      WP WP WP       WP WP PUNC  WP WP PUNC        WP WP WP PUNC  FLUSH    \n
     e.g. to  BSBEG WP WP WP BPHR3 WP WP BPHR1 WP WP BSEND BSBEG WP WP WP BSEND BTERM    \n
              |1                         |2                |3                   |4        \n

     2-level bound state: The internal buffer contains one primary phrase (sometimes forced, if buffer
     allmost full), with the trailing PUNCT item included (last item).\n
     If the trailing PUNC is a a primary phrase separator, the
       item is not output, but instead, the bound state is set to PPHR, so that the correct BOUND can
       be output at the start of the next primary phrase.\n
     Otherwise,
       the item is converted to the corresponding BOUND and output. the bound state is set to SSEP,
       so that a BOUND of type SBEG is output at the start of the next primary phrase.

     trailing PUNC item       bound states                                    \n
                              SSEP           PPHR                            \n
       PUNC(SENTEND, X)       B(B,X)>SSEP    B(P1,X)>SSEP  (X = T | Q | E)    \n
       PUNC(FLUSH, T)         B(B,T)>SSEP*    B(P1,T)>SSEP                    \n
       PUNC(PHRASEEND, P)     B(B,P)>PPHR    B(P1,P)>PPHR                    \n
       PUNC(PHRASEEND, FORC)  B(B,P)>PPHR    B(P1,P)>PPHR                    \n

    If more than one sentence separators follow each other (e.g. SEND-FLUSH, SEND-SEND) then
     all but the first will be treated as an (empty) phrase containing just this item.
     If this (single) item is a flush, creation of SBEG is suppressed.


  - dtphr phrasing tree ("subphrasing")
    determines
      - BOUND_PHR2
      - BOUND_PHR3
  - boundary strenghts are determined for every word (except the
    first one) from right-to-left. The boundary types mark the phrase
    type of the phrase following the boundary.
  - number of items actually changed (new BOUND items added): because
    of fixed size without content, two fields are contained in headx
    to indicate if a BOUND needs to be added to the LEFT of the item.
    -> headx further extended with boundary strength and type info to
    indicate that to the left of the headx ele a BOUND needs to be
    inserted when outputting.

  2. accentuation:
  - number of items unchanged, content unchanged, only head info changes
  -> changed in place in headx
*/


typedef struct {
    picodata_itemhead_t head;
    picoos_uint16 cind;
    picoos_uint8 boundstrength;  /* bstrength to the left, 0 if not set */
    picoos_uint8 boundtype;      /* btype for following phrase, 0 if not set */
} picoacph_headx_t;


typedef struct acph_subobj {
    picoos_uint8 procState; /* for next processing step decision */
    picoos_uint8 boundStrengthState;    /* boundary strength state */

    picoos_uint8 inspaceok;      /* flag: headx/cbuf has space for an item */
    picoos_uint8 needsmoreitems; /* flag: need more items */

    picoos_uint8 tmpbuf[PICODATA_MAX_ITEMSIZE];  /* tmp. location for an item */

    picoacph_headx_t headx[PICOACPH_MAXNR_HEADX];
    picoos_uint16 headxBottom; /* bottom */
    picoos_uint16 headxLen;    /* length, 0 if empty */

    picoos_uint8 cbuf[PICOACPH_MAXSIZE_CBUF];
    picoos_uint16 cbufBufSize; /* actually allocated size */
    picoos_uint16 cbufLen;     /* length, 0 if empty */

    /* tab knowledge base */
    picoktab_Phones tabphones;

    /* dtphr knowledge base */
    picokdt_DtPHR dtphr;

    /* dtacc knowledge base */
    picokdt_DtACC dtacc;
} acph_subobj_t;


static pico_status_t acphInitialize(register picodata_ProcessingUnit this, picoos_int32 resetMode) {
    acph_subobj_t * acph;
    picoos_uint16 i;

    PICODBG_DEBUG(("calling"));

    if (NULL == this || NULL == this->subObj) {
        return picoos_emRaiseException(this->common->em,
                                       PICO_ERR_NULLPTR_ACCESS, NULL, NULL);
    }
    acph = (acph_subobj_t *) this->subObj;
    acph->procState = SA_STEPSTATE_COLLECT;
    acph->boundStrengthState = SA_BOUNDSTRENGTH_SSEP;

    acph->inspaceok = TRUE;
    acph->needsmoreitems = TRUE;

    acph->headxBottom = 0;
    acph->headxLen = 0;
    acph->cbufBufSize = PICOACPH_MAXSIZE_CBUF;
    acph->cbufLen = 0;

    /* init headx, cbuf */
    for (i = 0; i < PICOACPH_MAXNR_HEADX; i++){
        acph->headx[i].head.type = 0;
        acph->headx[i].head.info1 = 0;
        acph->headx[i].head.info2 = 0;
        acph->headx[i].head.len = 0;
        acph->headx[i].cind = 0;
        acph->headx[i].boundstrength = 0;
        acph->headx[i].boundtype = 0;
    }
    for (i = 0; i < PICOACPH_MAXSIZE_CBUF; i++) {
        acph->cbuf[i] = 0;
    }

    if (resetMode == PICO_RESET_SOFT) {
        /*following initializations needed only at startup or after a full reset*/
        return PICO_OK;
    }

    /* kb tabphones */
    acph->tabphones =
        picoktab_getPhones(this->voice->kbArray[PICOKNOW_KBID_TAB_PHONES]);
    if (acph->tabphones == NULL) {
        return picoos_emRaiseException(this->common->em, PICO_EXC_KB_MISSING,
                                       NULL, NULL);
    }
    PICODBG_DEBUG(("got tabphones"));

#ifdef PICO_DEBUG_1
    {
        picoos_uint16 itmp;
        for (itmp = 0; itmp < 256; itmp++) {
            if (picoktab_hasVowelProp(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones hasVowel: %d", itmp));
            }
            if (picoktab_hasDiphthProp(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones hasDiphth: %d", itmp));
            }
            if (picoktab_hasGlottProp(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones hasGlott: %d", itmp));
            }
            if (picoktab_hasNonsyllvowelProp(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones hasNonsyllvowel: %d", itmp));
            }
            if (picoktab_hasSyllconsProp(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones hasSyllcons: %d", itmp));
            }

            if (picoktab_isPrimstress(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones isPrimstress: %d", itmp));
            }
            if (picoktab_isSecstress(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones isSecstress: %d", itmp));
            }
            if (picoktab_isSyllbound(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones isSyllbound: %d", itmp));
            }
            if (picoktab_isPause(acph->tabphones, itmp)) {
                PICODBG_DEBUG(("tabphones isPause: %d", itmp));
            }
        }

        PICODBG_DEBUG(("tabphones primstressID: %d",
                       picoktab_getPrimstressID(acph->tabphones)));
        PICODBG_DEBUG(("tabphones secstressID: %d",
                       picoktab_getSecstressID(acph->tabphones)));
        PICODBG_DEBUG(("tabphones syllboundID: %d",
                       picoktab_getSyllboundID(acph->tabphones)));
        PICODBG_DEBUG(("tabphones pauseID: %d",
                       picoktab_getPauseID(acph->tabphones)));
    }
#endif


    /* kb dtphr */
    acph->dtphr = picokdt_getDtPHR(this->voice->kbArray[PICOKNOW_KBID_DT_PHR]);
    if (acph->dtphr == NULL) {
        return picoos_emRaiseException(this->common->em, PICO_EXC_KB_MISSING,
                                       NULL, NULL);
    }
    PICODBG_DEBUG(("got dtphr"));

    /* kb dtacc */
    acph->dtacc = picokdt_getDtACC(this->voice->kbArray[PICOKNOW_KBID_DT_ACC]);
    if (acph->dtacc == NULL) {
        return picoos_emRaiseException(this->common->em, PICO_EXC_KB_MISSING,
                                       NULL, NULL);
    }
    PICODBG_DEBUG(("got dtacc"));

    return PICO_OK;
}

static picodata_step_result_t acphStep(register picodata_ProcessingUnit this,
                                     picoos_int16 mode,
                                     picoos_uint16 *numBytesOutput);

static pico_status_t acphTerminate(register picodata_ProcessingUnit this)
{
    return PICO_OK;
}

static pico_status_t acphSubObjDeallocate(register picodata_ProcessingUnit this,
                                        picoos_MemoryManager mm) {
    mm = mm;        /* avoid warning "var not used in this function"*/
    if (NULL != this) {
        picoos_deallocate(this->common->mm, (void *) &this->subObj);
    }
    return PICO_OK;
}


picodata_ProcessingUnit picoacph_newAccPhrUnit(picoos_MemoryManager mm,
                                              picoos_Common common,
                                              picodata_CharBuffer cbIn,
                                              picodata_CharBuffer cbOut,
                                              picorsrc_Voice voice) {
    picodata_ProcessingUnit this;

    this = picodata_newProcessingUnit(mm, common, cbIn, cbOut, voice);
    if (this == NULL) {
        return NULL;
    }

    this->initialize = acphInitialize;
    PICODBG_DEBUG(("set this->step to acphStep"));
    this->step = acphStep;
    this->terminate = acphTerminate;
    this->subDeallocate = acphSubObjDeallocate;
    this->subObj = picoos_allocate(mm, sizeof(acph_subobj_t));
    if (this->subObj == NULL) {
        picoos_deallocate(mm, (void *)&this);
        picoos_emRaiseException(common->em, PICO_EXC_OUT_OF_MEM, NULL, NULL);
        return NULL;
    }

    acphInitialize(this, PICO_RESET_FULL);
    return this;
}


/* ***********************************************************************/
/* PROCESS_PHR/ACC support functions */
/* ***********************************************************************/


static picoos_uint8 acphGetNrSylls(register picodata_ProcessingUnit this,
                                 register acph_subobj_t *acph,
                                 const picoos_uint16 ind) {
    picoos_uint8 i;
    picoos_uint8 ch;
    picoos_uint8 count;

    count = 1;
    for (i = 0; i < acph->headx[ind].head.len; i++) {
        ch = acph->cbuf[acph->headx[ind].cind + i];
        if (picoktab_isSyllbound(acph->tabphones, ch)) {
            count++;
        }
    }
    return count;
}


/* ***********************************************************************/
/* PROCESS_PHR functions */
/* ***********************************************************************/


/* find next POS to the left of 'ind' and return its POS and index */
static picoos_uint8 acphPhrItemSeqGetPosLeft(register picodata_ProcessingUnit this,
                                           register acph_subobj_t *acph,
                                           const picoos_uint16 ind,
                                           picoos_uint16 *leftind) {
    picoos_uint8 val;
    picoos_int32 i;

    val = PICOKDT_EPSILON;
    for (i = ind - 1; ((val == PICOKDT_EPSILON) && (i >= 0)); i--) {
        if ((acph->headx[i].head.type == PICODATA_ITEM_WORDPHON)) {
            val = acph->headx[i].head.info1;
        }
    }
    *leftind = i + 1;
    return val;
}


/* right-to-left, for each WORDPHON do phr */
static pico_status_t acphSubPhrasing(register picodata_ProcessingUnit this,
                                   register acph_subobj_t *acph) {
    picokdt_classify_result_t dtres;
    picoos_uint8 valbuf[5];
    picoos_uint16 nrwordspre;
    picoos_uint16 nrwordsfol;
    picoos_uint16 nrsyllsfol;
    picoos_uint16 lastprev2; /* last index of POS(es) found to the left */
    picoos_uint8 curpos;     /* POS(es) of current word */
    picoos_uint16 upbound;   /* index of last WORDPHON item (with POS) */
    picoos_uint8 okay;
    picoos_uint8 nosubphrases;
    picoos_int32 i;

    /* set initial values */
    okay = TRUE;
    nosubphrases = TRUE;
    curpos = PICOKDT_EPSILON;   /* needs to be in 2^8 */

    /* set upbound to last WORDPHON, don't worry about first one */
    upbound = acph->headxLen - 1;
    while ((upbound > 0) &&
           (acph->headx[upbound].head.type != PICODATA_ITEM_WORDPHON)) {
        upbound--;
    }

    /* zero or one WORDPHON, no subphrasing needed, but handling of
       BOUND strength state is needed */
    if (upbound <= 0) {
        /* phrase not containing more than one WORDPHON */
        PICODBG_DEBUG(("less than two WORDPHON in phrase -> no subphrasing"));
    }

    lastprev2 = upbound;

    /* set initial nr pre/fol words/sylls, upbound is ind of last WORDPHON */
    nrwordsfol = 0;
    nrsyllsfol = 0;
    nrwordspre = 0;
    for (i = 0; i < upbound; i++) {
        if (acph->headx[i].head.type == PICODATA_ITEM_WORDPHON) {
            nrwordspre++;
        }
    }

    nrwordspre++;    /* because we later have a decrement before being used */


    /* set POS of current word in valbuf[1], will be shifted right afterwards */
    valbuf[1] = acph->headx[upbound].head.info1;
    /* find first POS to the left and set valbuf[0] */
    valbuf[0] = acphPhrItemSeqGetPosLeft(this, acph, lastprev2, &lastprev2);
    for (i = 2; i < 5; i++) {
        valbuf[i] = PICOKDT_EPSILON;
    }

    PICODBG_TRACE(("headxLen: %d", acph->headxLen));

    /* at least two WORDPHON items */
    /* process from right-to-left all items in headx, except for 1st WORDPHON */
    for (i = upbound; (i > 0) && (nrwordspre > 1); i--) {
        okay = TRUE;

        PICODBG_TRACE(("iter: %d, type: %c", i, acph->headx[i].head.type));

        /* if not (WORDPHON) */
        if ((acph->headx[i].head.type != PICODATA_ITEM_WORDPHON)) {
            continue;
        }

        PICODBG_TRACE(("iter: %d, curpos: %d", i, acph->headx[i].head.info1));

        /* get and set POS of current item, must be WORDPHON */
        curpos = acph->headx[i].head.info1;

        /* no continue so far => at [i] we have a WORDPHON item */
        /* shift all POS elements one position to the right */
        valbuf[4] = valbuf[3];
        valbuf[3] = valbuf[2];
        valbuf[2] = valbuf[1];
        valbuf[1] = valbuf[0];
        /* find next POS to the left and set valbuf[0] */
        valbuf[0] = acphPhrItemSeqGetPosLeft(this, acph, lastprev2, &lastprev2);

        /* better check double than never */
        if (curpos != valbuf[2]) {
            PICODBG_WARN(("syncing POS"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_INVECTOR,
                                  NULL, NULL);
            valbuf[2] = curpos;
        }

        nrwordsfol++;
        nrsyllsfol += acphGetNrSylls(this, acph, i);
        nrwordspre--;

        PICODBG_TRACE(("%d: [%d,%d|%d|%d,%d|%d,%d,%d]",
                       i, valbuf[0], valbuf[1], valbuf[2], valbuf[3],
                       valbuf[4], nrwordspre, nrwordsfol, nrsyllsfol));

        /* no continue so far => subphrasing needed */
        /* construct input vector, which is set in dtphr */
        if (!picokdt_dtPHRconstructInVec(acph->dtphr, valbuf[0], valbuf[1],
                                         valbuf[2], valbuf[3], valbuf[4],
                                         nrwordspre, nrwordsfol, nrsyllsfol)) {
            /* error constructing invec */
            PICODBG_WARN(("problem with invec"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_INVECTOR,
                                  NULL, NULL);
            okay = FALSE;
        }
        /* classify */
        if (okay && (!picokdt_dtPHRclassify(acph->dtphr))) {
            /* error doing classification */
            PICODBG_WARN(("problem classifying"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_CLASSIFICATION,
                                  NULL, NULL);
            okay = FALSE;
        }
        /* decompose */
        if (okay && (!picokdt_dtPHRdecomposeOutClass(acph->dtphr, &dtres))) {
            /* error decomposing */
            PICODBG_WARN(("problem decomposing"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_OUTVECTOR,
                                  NULL, NULL);
            okay = FALSE;
        }

        if (okay && dtres.set) {
            PICODBG_DEBUG(("%d - inpos: %d, out: %d", i,valbuf[2],dtres.class));
        } else {
            PICODBG_WARN(("problem determining subphrase boundary strength"));
            dtres.class = PICODATA_ITEMINFO1_ERR;
        }

        if (dtres.class > 255) {
            PICODBG_WARN(("dt class outside valid range, setting to PHR0"));
            dtres.class = PICODATA_ITEMINFO1_BOUND_PHR0;
        }
        acph->headx[i].boundstrength = (picoos_uint8)dtres.class;
        if ((dtres.class == PICODATA_ITEMINFO1_BOUND_PHR2) ||
            (dtres.class == PICODATA_ITEMINFO1_BOUND_PHR3)) {
            if (nosubphrases) {
                /* it's the last secondary phrase in the primary phrase */
                /* add type info */
                switch (acph->headx[acph->headxLen - 1].head.info2) {
                    case PICODATA_ITEMINFO2_PUNC_SENT_T:
                        acph->headx[i].boundtype =
                            PICODATA_ITEMINFO2_BOUNDTYPE_T;
                        break;
                    case PICODATA_ITEMINFO2_PUNC_SENT_Q:
                        acph->headx[i].boundtype =
                            PICODATA_ITEMINFO2_BOUNDTYPE_Q;
                        break;
                    case PICODATA_ITEMINFO2_PUNC_SENT_E:
                        acph->headx[i].boundtype =
                            PICODATA_ITEMINFO2_BOUNDTYPE_E;
                        break;
                    case PICODATA_ITEMINFO2_PUNC_PHRASE:
                    case PICODATA_ITEMINFO2_PUNC_PHRASE_FORCED:
                        acph->headx[i].boundtype =
                            PICODATA_ITEMINFO2_BOUNDTYPE_P;
                        break;
                    default:
                        PICODBG_WARN(("invalid boundary type, not set"));
                        break;
                }
                nosubphrases = FALSE;

            } else {
                acph->headx[i].boundtype =
                    PICODATA_ITEMINFO2_BOUNDTYPE_P;
            }
            /* reset nr following words and sylls counters */
            nrwordsfol = 0;
            nrsyllsfol = 0;
        }
    }

    /* process first item, add bound-info */
    switch (acph->boundStrengthState) {
        case SA_BOUNDSTRENGTH_SSEP:
            acph->headx[0].boundstrength =
                PICODATA_ITEMINFO1_BOUND_SBEG;
            break;
        case SA_BOUNDSTRENGTH_PPHR:
            acph->headx[0].boundstrength =
                PICODATA_ITEMINFO1_BOUND_PHR1;
            break;
        default:
            PICODBG_WARN(("invalid boundary strength, not set"));
            break;
    }

    /* set boundary strength state */
    switch (acph->headx[acph->headxLen - 1].head.info1) {
        case PICODATA_ITEMINFO1_PUNC_SENTEND:
        case PICODATA_ITEMINFO1_PUNC_FLUSH:
            acph->boundStrengthState = SA_BOUNDSTRENGTH_SSEP;
            break;
        case PICODATA_ITEMINFO1_PUNC_PHRASEEND:
            acph->boundStrengthState = SA_BOUNDSTRENGTH_PPHR;
            break;
        default:
            PICODBG_WARN(("invalid boundary strength state, not changed"));
            break;
    }

    if (nosubphrases) {
        /* process first item, add type info */
        switch (acph->headx[acph->headxLen - 1].head.info2) {
            case PICODATA_ITEMINFO2_PUNC_SENT_T:
                acph->headx[0].boundtype =
                    PICODATA_ITEMINFO2_BOUNDTYPE_T;
                break;
            case PICODATA_ITEMINFO2_PUNC_SENT_Q:
                acph->headx[0].boundtype =
                    PICODATA_ITEMINFO2_BOUNDTYPE_Q;
                break;
            case PICODATA_ITEMINFO2_PUNC_SENT_E:
                acph->headx[0].boundtype =
                    PICODATA_ITEMINFO2_BOUNDTYPE_E;
                break;
            case PICODATA_ITEMINFO2_PUNC_PHRASE:
            case PICODATA_ITEMINFO2_PUNC_PHRASE_FORCED:
                acph->headx[0].boundtype =
                    PICODATA_ITEMINFO2_BOUNDTYPE_P;
                break;
            default:
                PICODBG_WARN(("invalid boundary type, not set"));
                break;
        }
    } else {
        acph->headx[0].boundtype =
            PICODATA_ITEMINFO2_BOUNDTYPE_P;
    }

    return PICO_OK;
}


/* ***********************************************************************/
/* PROCESS_ACC functions */
/* ***********************************************************************/

/* find next POS to the left of 'ind' and return its POS and index */
static picoos_uint8 acphAccItemSeqGetPosLeft(register picodata_ProcessingUnit this,
                                           register acph_subobj_t *acph,
                                           const picoos_uint16 ind,
                                           picoos_uint16 *leftind) {
    picoos_uint8 val;
    picoos_int32 i;

    val = PICOKDT_EPSILON;
    for (i = ind - 1; ((val == PICOKDT_EPSILON) && (i >= 0)); i--) {
        if ((acph->headx[i].head.type == PICODATA_ITEM_WORDPHON)) {
            val = acph->headx[i].head.info1;
        }
    }
    *leftind = i + 1;
    return val;
}


/* s1: nr sylls in word before the first primary stressed syll,
   s2: nr sylls in word after (but excluding) the first primary stressed syll */
static picoos_uint8 acphAccNrSyllParts(register picodata_ProcessingUnit this,
                                     register acph_subobj_t *acph,
                                     const picoos_uint16 ind,
                                     picoos_uint8 *s1,
                                     picoos_uint8 *s2) {
    picoos_uint16 pind;
    picoos_uint16 pend;    /* phone string start+len */
    picoos_uint8 afterprim;

    /* check ind is in valid range */
    if (ind >= acph->headxLen) {
        return FALSE;
    }

    *s1 = 0;
    *s2 = 0;
    afterprim = FALSE;
    pend = acph->headx[ind].cind + acph->headx[ind].head.len;
    for (pind = acph->headx[ind].cind; pind < pend; pind++) {
        if (picoktab_isPrimstress(acph->tabphones, acph->cbuf[pind])) {
            afterprim = TRUE;
        } else if (picoktab_isSyllbound(acph->tabphones, acph->cbuf[pind])) {
            if (afterprim) {
                (*s2)++;
            } else {
                (*s1)++;
            }
        }
    }
    if (afterprim) {
        (*s2)++;
    } else {
        (*s1)++;
    }

    /* exclude the stressed syllable */
    if ((*s2) > 0) {
        (*s2)--;
    }
    /* handle the case when there is no primstress */
    if (!afterprim) {
        (*s2) = (*s1);
    }
    return TRUE;
}


static picoos_uint8 acphAccGetNrsRight(register picodata_ProcessingUnit this,
                                     register acph_subobj_t *acph,
                                     const picoos_uint16 ind,
                                     picoos_uint16 *nrwordsfol,
                                     picoos_uint16 *nrsyllsfol,
                                     picoos_uint16 *footwordsfol,
                                     picoos_uint16 *footsyllsfol) {
    picoos_uint16 i;
    picoos_uint8 s1;
    picoos_uint8 s2;

    if (!acphAccNrSyllParts(this, acph, ind, &s1, &s2)) {
        return FALSE;
    }

    *nrwordsfol = 0;
    *nrsyllsfol = s2;
    i = ind + 1;
    while ((i < acph->headxLen) &&
           (acph->headx[i].boundstrength == PICODATA_ITEMINFO1_BOUND_PHR0)) {
        if (acph->headx[i].head.type == PICODATA_ITEM_WORDPHON) {
            (*nrwordsfol)++;
            *nrsyllsfol += acphGetNrSylls(this, acph, i);
        }
        i++;
    }

    *footwordsfol = 0;
    *footsyllsfol = s2;
    i = ind + 1;
    while ((i < acph->headxLen) &&
           (acph->headx[i].head.info2 != PICODATA_ACC1)) {
        if (acph->headx[i].head.type == PICODATA_ITEM_WORDPHON) {
            (*footwordsfol)++;
            *footsyllsfol += acphGetNrSylls(this, acph, i);
        }
        i++;
    }
    if ((i < acph->headxLen) && (acph->headx[i].head.info2 == PICODATA_ACC1)) {
        if (!acphAccNrSyllParts(this, acph, i, &s1, &s2)) {
            return FALSE;
        }
        *footsyllsfol += s1;
    }
    return TRUE;
}


static picoos_uint8 acphAccGetNrsLeft(register picodata_ProcessingUnit this,
                                    register acph_subobj_t *acph,
                                    const picoos_uint16 ind,
                                    picoos_uint16 *nrwordspre,
                                    picoos_uint16 *nrsyllspre) {
    picoos_int32 i;
    picoos_uint8 s1;
    picoos_uint8 s2;

    if (!acphAccNrSyllParts(this, acph, ind, &s1, &s2)) {
        return FALSE;
    }

    *nrwordspre = 0;
    *nrsyllspre = s1;
    i = ind - 1;
    while ((i >= 0) &&
           (acph->headx[i].boundstrength == PICODATA_ITEMINFO1_BOUND_PHR0)) {
        if (acph->headx[i].head.type == PICODATA_ITEM_WORDPHON) {
            (*nrwordspre)++;
            *nrsyllspre += acphGetNrSylls(this, acph, i);
        }
        i--;
    }

    if ((acph->headx[i].boundstrength != PICODATA_ITEMINFO1_BOUND_PHR0) &&
        (acph->headx[i].head.type == PICODATA_ITEM_WORDPHON)) {
        (*nrwordspre)++;
        *nrsyllspre += acphGetNrSylls(this, acph, i);
    }
    return TRUE;
}


/* return TRUE if wordphon contains no stress, FALSE otherwise */
static picoos_uint8 acphIsWordWithoutStress(register picodata_ProcessingUnit this,
                                          register acph_subobj_t *acph,
                                          const picoos_uint16 ind) {
    picoos_uint8 i;
    picoos_uint16 pos;

    pos = acph->headx[ind].cind;
    for (i = 0; i < acph->headx[ind].head.len; i++) {
        if (picoktab_isPrimstress(acph->tabphones, acph->cbuf[pos + i]) ||
            picoktab_isSecstress(acph->tabphones, acph->cbuf[pos + i])) {
            return FALSE;
        }
    }
    return TRUE;
}


/* right-to-left, for each WORDPHON do acc */
static pico_status_t acphAccentuation(register picodata_ProcessingUnit this,
                                    register acph_subobj_t *acph) {
    picokdt_classify_result_t dtres;
    picoos_uint8 valbuf[5];
    picoos_uint16 hist1;
    picoos_uint16 hist2;
    picoos_uint16 nrwordspre;
    picoos_uint16 nrsyllspre;
    picoos_uint16 nrwordsfol;
    picoos_uint16 nrsyllsfol;
    picoos_uint16 footwordsfol;
    picoos_uint16 footsyllsfol;
    picoos_uint16 lastprev2; /* last index of POS(es) found to the left */
    picoos_uint8 curpos;     /* POS(es) of current word */
    picoos_uint16 prevout;
    picoos_uint8 okay;
    picoos_int32 upbound;   /* index of last WORDPHON item (with POS) */
    picoos_uint16 i;

    /* set initial values */
    okay = TRUE;
    curpos = PICOKDT_EPSILON;    /* needs to be < 2^8 */

    /* set upbound to last WORDPHON */
    upbound = acph->headxLen - 1;
    while ((upbound >= 0) &&
           (acph->headx[upbound].head.type != PICODATA_ITEM_WORDPHON)) {
        upbound--;
    }

    if (upbound < 0) {
        /* phrase containing zero WORDPHON */
        PICODBG_DEBUG(("no WORDPHON in phrase -> no accentuation"));
        return PICO_OK;
    }

    lastprev2 = upbound;

    /* set initial history values */
    prevout = PICOKDT_HISTORY_ZERO;
    hist1 = PICOKDT_HISTORY_ZERO;
    hist2 = PICOKDT_HISTORY_ZERO;

    /* set initial nr pre/fol words/sylls, upbound is ind of last WORDPHON */
    nrwordsfol = 0;
    nrsyllsfol = 0;
    footwordsfol = 0;
    footsyllsfol = 0;
    nrwordspre = 0;
    nrsyllspre = 0;

    /* set POS of current word in valbuf[1], will be shifted right afterwards */
    valbuf[1] = acph->headx[upbound].head.info1;
    /* find first POS to the left and set valbuf[0] */
    valbuf[0] = acphAccItemSeqGetPosLeft(this, acph, lastprev2, &lastprev2);
    for (i = 2; i < 5; i++) {
        valbuf[i] = PICOKDT_EPSILON;
    }

    PICODBG_TRACE(("headxLen: %d", acph->headxLen));

    /* process from right-to-left all items in headx */
    for (i = upbound+1; i > 0; ) {
        i--;

        okay = TRUE;

        PICODBG_TRACE(("iter: %d, type: %c", i, acph->headx[i].head.type));

        /* if not (WORDPHON) */
        if ((acph->headx[i].head.type != PICODATA_ITEM_WORDPHON)) {
            continue;
        }

        PICODBG_TRACE(("iter: %d, curpos: %d", i, acph->headx[i].head.info1));

        /* get and set POS of current item, must be WORDPHON */
        curpos = acph->headx[i].head.info1;

        /* no continue so far => at [i] we have a WORDPHON item */
        /* shift all POS elements one position to the right */
        valbuf[4] = valbuf[3];
        valbuf[3] = valbuf[2];
        valbuf[2] = valbuf[1];
        valbuf[1] = valbuf[0];
        /* find next POS to the left and set valbuf[0] */
        valbuf[0] = acphAccItemSeqGetPosLeft(this, acph, lastprev2, &lastprev2);

        /* better check double than never */
        if (curpos != valbuf[2]) {
            PICODBG_WARN(("syncing POS"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_INVECTOR,
                                  NULL, NULL);
            valbuf[2] = curpos;
        }

        /* set history values */
        hist2 = hist1;
        hist1 = prevout;

        /* ************************************************************ */
        /* many speedups possible by avoiding double calc of attribtues */
        /* ************************************************************ */

        /* get distances */
        if ((!acphAccGetNrsRight(this, acph, i, &nrwordsfol, &nrsyllsfol,
                               &footwordsfol, &footsyllsfol)) ||
            (!acphAccGetNrsLeft(this, acph, i, &nrwordspre, &nrsyllspre))) {
            PICODBG_WARN(("problem setting distances in invec"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_INVECTOR,
                                  NULL, NULL);
            okay = FALSE;
        }

        PICODBG_TRACE(("%d: [%d,%d,%d,%d,%d|%d,%d|%d,%d,%d,%d|%d,%d]", i,
                       valbuf[0], valbuf[1], valbuf[2], valbuf[3], valbuf[4],
                       hist1, hist2, nrwordspre, nrsyllspre,
                       nrwordsfol, nrsyllsfol, footwordsfol, footsyllsfol));

        /* no continue so far => accentuation needed */
        /* construct input vector, which is set in dtacc */
        if (!picokdt_dtACCconstructInVec(acph->dtacc, valbuf[0], valbuf[1],
                                         valbuf[2], valbuf[3], valbuf[4],
                                         hist1, hist2, nrwordspre, nrsyllspre,
                                         nrwordsfol, nrsyllsfol, footwordsfol,
                                         footsyllsfol)) {
            /* error constructing invec */
            PICODBG_WARN(("problem with invec"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_INVECTOR,
                                  NULL, NULL);
            okay = FALSE;
        }
        /* classify */
        if (okay && (!picokdt_dtACCclassify(acph->dtacc, &prevout))) {
            /* error doing classification */
            PICODBG_WARN(("problem classifying"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_CLASSIFICATION,
                                  NULL, NULL);
            okay = FALSE;
        }
        /* decompose */
        if (okay && (!picokdt_dtACCdecomposeOutClass(acph->dtacc, &dtres))) {
            /* error decomposing */
            PICODBG_WARN(("problem decomposing"));
            picoos_emRaiseWarning(this->common->em, PICO_WARN_OUTVECTOR,
                                  NULL, NULL);
            okay = FALSE;
        }

        if (dtres.class > 255) {
            PICODBG_WARN(("dt class outside valid range, setting to ACC0"));
            dtres.class = PICODATA_ACC0;
        }

        if (okay && dtres.set) {
            PICODBG_DEBUG(("%d - inpos: %d, out: %d", i,valbuf[2],dtres.class));
            if (acphIsWordWithoutStress(this, acph, i)) {
                if (dtres.class != PICODATA_ACC0) {
                    acph->headx[i].head.info2 = PICODATA_ACC3;
                } else {
                    acph->headx[i].head.info2 = (picoos_uint8)dtres.class;
                }
            } else {
                acph->headx[i].head.info2 = (picoos_uint8)dtres.class;
            }
            PICODBG_DEBUG(("%d - after-nostress-corr: %d",
                           i, acph->headx[i].head.info2));
        } else {
            PICODBG_WARN(("problem determining accentuation level"));
            dtres.class = PICODATA_ITEMINFO1_ERR;
        }
    }
    return PICO_OK;
}



/* ***********************************************************************/
/* acphStep support functions */
/* ***********************************************************************/

static picoos_uint8 acphPutBoundItem(register picodata_ProcessingUnit this,
                                   register acph_subobj_t *acph,
                                   const picoos_uint8 strength,
                                   const picoos_uint8 type,
                                   picoos_uint8 *dopuoutfull,
                                   picoos_uint16 *numBytesOutput) {
    pico_status_t rv = PICO_OK;
    picoos_uint16 blen = 0;
    picodata_itemhead_t tmphead;

    *dopuoutfull = FALSE;

    /* construct BOUND item in tmpbuf and put item */
    tmphead.type = PICODATA_ITEM_BOUND;
    tmphead.info1 = strength;
    tmphead.info2 = type;
    tmphead.len = 0;
    rv = picodata_put_itemparts(&tmphead, NULL, 0, acph->tmpbuf,
                                PICODATA_MAX_ITEMSIZE, &blen);
    if (rv != PICO_OK) {
        PICODBG_ERROR(("problem creating BOUND item"));
        picoos_emRaiseException(this->common->em, rv, NULL, NULL);
        return FALSE;
    }
    /* put constructed item to ext. charbuf */
    rv = picodata_cbPutItem(this->cbOut, acph->tmpbuf, blen, &blen);

    *numBytesOutput += blen;
    if (rv == PICO_EXC_BUF_OVERFLOW) {
        PICODBG_DEBUG(("overflow in cb output buffer"));
        *dopuoutfull = TRUE;    /* ie. do PU_OUT_FULL later */
        return FALSE;
    } else if (rv != PICO_OK) {
        PICODBG_ERROR(("problem putting BOUND item"));
        picoos_emRaiseException(this->common->em, rv, NULL, NULL);
        return FALSE;
    }

    PICODATA_INFO_ITEM(this->voice->kbArray[PICOKNOW_KBID_DBG],
                       (picoos_uint8 *)"acph: ", acph->tmpbuf, blen);

    return TRUE;
}



/* ***********************************************************************/
/*                          acphStep function                              */
/* ***********************************************************************/

/*
complete phrase processed in one step, if not fast enough -> rework

init, collect into internal buffer, process, and then feed to
output buffer

init state: INIT ext           ext
state trans:     in hc1  hc2   out

INIT | putItem   =  0    0    +1      | BUSY  -> COLL (put B-SBEG item,
                                                   set do-init to false)

                                    inspace-ok-hc1
                                  needs-more-items-(phrase-or-flush)
COLL1 |getItems -n +n             0 1 | ATOMIC -> PPOSD     (got items,
                                                      if flush set do-init)
COLL2 |getItems -n +n             1 0 | ATOMIC -> PPOSD (got items, forced)
COLL3 |getItems -n +n             1 1 | IDLE          (got items, need more)
COLL4 |getItems  =  =             1 1 | IDLE             (got no items)

PPOSD | posd     = ~n~n               | BUSY     -> PWP     (posd done)
PWP   | lex/g2p  = ~n-n  0+n          | BUSY     -> PPHR    (lex/g2p done)
PPHR  | phr      = -n 0 +m=n          | BUSY     -> PACC    (phr done, m>=n)
PACC  | acc      =  0 0 ~m=n          | BUSY     -> FEED    (acc done)

                                  doinit-flag
FEED | putItems  0  0 0 -m-n  +m  0   | BUSY -> COLL    (put items)
FEED | putItems  0  0 0 -m-n  +m  1   | BUSY -> INIT    (put items)
FEED | putItems  0  0 0 -d-d  +d      | OUT_FULL        (put some items)
*/

static picodata_step_result_t acphStep(register picodata_ProcessingUnit this,
                                     picoos_int16 mode,
                                     picoos_uint16 *numBytesOutput) {
    register acph_subobj_t *acph;
    pico_status_t rv = PICO_OK;
    pico_status_t rvP = PICO_OK;
    picoos_uint16 blen = 0;
    picoos_uint16 clen = 0;
    picoos_uint16 i;


    if (NULL == this || NULL == this->subObj) {
        return PICODATA_PU_ERROR;
    }
    acph = (acph_subobj_t *) this->subObj;
    mode = mode;        /* avoid warning "var not used in this function"*/
    *numBytesOutput = 0;
    while (1) { /* exit via return */
        PICODBG_DEBUG(("doing state %i, hLen|c1Len: %d|%d",
                       acph->procState, acph->headxLen, acph->cbufLen));

        switch (acph->procState) {

            /* *********************************************************/
            /* collect state: get item(s) from charBuf and store in
             * internal buffers, need a complete punctuation-phrase
             */
            case SA_STEPSTATE_COLLECT:

                while (acph->inspaceok && acph->needsmoreitems && (PICO_OK ==
                (rv = picodata_cbGetItem(this->cbIn, acph->tmpbuf,
                                PICODATA_MAX_ITEMSIZE, &blen)))) {
                    rvP = picodata_get_itemparts(acph->tmpbuf,
                    PICODATA_MAX_ITEMSIZE, &(acph->headx[acph->headxLen].head),
                            &(acph->cbuf[acph->cbufLen]), acph->cbufBufSize
                                    - acph->cbufLen, &clen);
                    if (rvP != PICO_OK) {
                        PICODBG_ERROR(("problem getting item parts"));
                        picoos_emRaiseException(this->common->em, rvP,
                        NULL, NULL);
                        return PICODATA_PU_ERROR;
                    }

                    /* if CMD(...FLUSH...) -> PUNC(...FLUSH...),
                     construct PUNC-FLUSH item in headx */
                    if ((acph->headx[acph->headxLen].head.type
                            == PICODATA_ITEM_CMD)
                            && (acph->headx[acph->headxLen].head.info1
                                    == PICODATA_ITEMINFO1_CMD_FLUSH)) {
                        acph->headx[acph->headxLen].head.type
                                = PICODATA_ITEM_PUNC;
                        acph->headx[acph->headxLen].head.info1
                                = PICODATA_ITEMINFO1_PUNC_FLUSH;
                        acph->headx[acph->headxLen].head.info2
                                = PICODATA_ITEMINFO2_PUNC_SENT_T;
                        acph->headx[acph->headxLen].head.len = 0;
                    }

                    /* check/set needsmoreitems */
                    if (acph->headx[acph->headxLen].head.type
                            == PICODATA_ITEM_PUNC) {
                        acph->needsmoreitems = FALSE;
                    }

                    /* check/set inspaceok, keep spare slot for forcing */
                    if ((acph->headxLen >= (PICOACPH_MAXNR_HEADX - 2))
                            || ((acph->cbufBufSize - acph->cbufLen)
                                    < PICODATA_MAX_ITEMSIZE)) {
                        acph->inspaceok = FALSE;
                    }

                    if (clen > 0) {
                        acph->headx[acph->headxLen].cind = acph->cbufLen;
                        acph->cbufLen += clen;
                    } else {
                        acph->headx[acph->headxLen].cind = 0;
                    }
                    acph->headxLen++;
                }

                if (!acph->needsmoreitems) {
                    /* 1, phrase buffered */
                    acph->procState = SA_STEPSTATE_PROCESS_PHR;
                    return PICODATA_PU_ATOMIC;
                } else if (!acph->inspaceok) {
                    /* 2, forced phrase end */
                    /* at least one slot is still free, use it to
                       force a trailing PUNC item */
                    acph->headx[acph->headxLen].head.type = PICODATA_ITEM_PUNC;
                    acph->headx[acph->headxLen].head.info1 =
                        PICODATA_ITEMINFO1_PUNC_PHRASEEND;
                    acph->headx[acph->headxLen].head.info2 =
                        PICODATA_ITEMINFO2_PUNC_PHRASE_FORCED;
                    acph->headx[acph->headxLen].head.len = 0;
                    acph->needsmoreitems = FALSE; /* not really needed for now */
                    acph->headxLen++;
                    PICODBG_WARN(("forcing phrase end, added PUNC_PHRASEEND"));
                    picoos_emRaiseWarning(this->common->em,
                                          PICO_WARN_FALLBACK, NULL,
                                          (picoos_char *)"forced phrase end");
                    acph->procState = SA_STEPSTATE_PROCESS_PHR;
                    return PICODATA_PU_ATOMIC;
                } else if (rv == PICO_EOF) {
                    /* 3, 4 */
                    return PICODATA_PU_IDLE;
                } else if ((rv == PICO_EXC_BUF_UNDERFLOW) ||
                           (rv == PICO_EXC_BUF_OVERFLOW)) {
                    /* error, no valid item in cb (UNDER) */
                    /*        or tmpbuf not large enough, not possible (OVER) */
                    /* no exception raised, left for ctrl to handle */
                    PICODBG_ERROR(("buffer under/overflow, rv: %d", rv));
                    return PICODATA_PU_ERROR;
                } else {
                    /* error, only possible if cbGetItem implementation
                       changes without this function being adapted*/
                    PICODBG_ERROR(("untreated return value, rv: %d", rv));
                    return PICODATA_PU_ERROR;
                }
                break;




            /* *********************************************************/
            /* process phr state: process items in headx and modify
             * headx in place
             */
            case SA_STEPSTATE_PROCESS_PHR:
                /* ensure there is an item in inBuf */
                if (acph->headxLen > 0) {
                    /* we have a phrase in headx, cbuf1 (can be
                       single PUNC item), do phrasing and modify headx */

                    if (PICO_OK != acphSubPhrasing(this, acph)) {
                        picoos_emRaiseException(this->common->em,
                                                PICO_ERR_OTHER, NULL, NULL);
                        return PICODATA_PU_ERROR;
                    }
                    acph->procState = SA_STEPSTATE_PROCESS_ACC;
                } else if (acph->headxLen == 0) {    /* no items in inBuf */
                    PICODBG_WARN(("no items in inBuf"));
                    acph->procState = SA_STEPSTATE_COLLECT;
                    return PICODATA_PU_BUSY;
                }

#if defined (PICO_DEBUG_NOTNEEDED)
                if (1) {
                    picoos_uint8 i, j, ittype;
                    for (i = 0; i < acph->headxLen; i++) {
                        if ((acph->headx[i].boundstrength != 0) &&
                            (acph->headx[i].boundstrength !=
                             PICODATA_ITEMINFO1_BOUND_PHR0)) {
                            PICODBG_INFO(("acph-p: boundstrength '%c', "
                                          "boundtype '%c'",
                                          acph->headx[i].boundstrength,
                                          acph->headx[i].boundtype));
                        }

                        ittype = acph->headx[i].head.type;
                        PICODBG_INFO_CTX();
                        PICODBG_INFO_MSG(("acph-p: ("));
                        PICODBG_INFO_MSG(("'%c',", ittype));
                        if ((32 <= acph->headx[i].head.info1) &&
                            (acph->headx[i].head.info1 < 127) &&
                            (ittype != PICODATA_ITEM_WORDPHON)) {
                            PICODBG_INFO_MSG(("'%c',",acph->headx[i].head.info1));
                        } else {
                            PICODBG_INFO_MSG(("%3d,", acph->headx[i].head.info1));
                        }
                        if ((32 <= acph->headx[i].head.info2) &&
                            (acph->headx[i].head.info2 < 127)) {
                            PICODBG_INFO_MSG(("'%c',",acph->headx[i].head.info2));
                        } else {
                            PICODBG_INFO_MSG(("%3d,", acph->headx[i].head.info2));
                        }
                        PICODBG_INFO_MSG(("%3d)", acph->headx[i].head.len));

                        for (j = 0; j < acph->headx[i].head.len; j++) {
                            if ((ittype == PICODATA_ITEM_CMD)) {
                                PICODBG_INFO_MSG(("%c",
                                        acph->cbuf[acph->headx[i].cind+j]));
                            } else {
                                PICODBG_INFO_MSG(("%4d",
                                        acph->cbuf[acph->headx[i].cind+j]));
                            }
                        }
                        PICODBG_INFO_MSG(("\n"));
                    }
                }
#endif

                break;


            /* *********************************************************/
            /* process acc state: process items in headx and modify
             * headx in place
             */
            case SA_STEPSTATE_PROCESS_ACC:
                /* ensure there is an item in inBuf */
                if (acph->headxLen > 0) {
                    /* we have a phrase in headx, cbuf (can be
                       single PUNC item), do accentuation and modify headx */
                    if (PICO_OK != acphAccentuation(this, acph)) {
                        picoos_emRaiseException(this->common->em,
                                                PICO_ERR_OTHER, NULL, NULL);
                        return PICODATA_PU_ERROR;
                    }
                    acph->procState = SA_STEPSTATE_FEED;
                } else if (acph->headxLen == 0) {    /* no items in inBuf */
                    PICODBG_WARN(("no items in inBuf"));
                    acph->procState = SA_STEPSTATE_COLLECT;
                    return PICODATA_PU_BUSY;
                }
                break;


            /* *********************************************************/
            /* feed state: copy item in internal outBuf to output charBuf */
            case SA_STEPSTATE_FEED: {
                picoos_uint16 indupbound;
                picoos_uint8 dopuoutfull;

                PICODBG_DEBUG(("put out items (bot, len): (%d, %d)",
                               acph->headxBottom, acph->headxLen));

                indupbound = acph->headxBottom + acph->headxLen;
                dopuoutfull = FALSE;

                if (acph->headxBottom == 0) {
                    /* construct first BOUND item in tmpbuf and put item */
                    /* produce BOUND unless it is followed by a term/flush) */
                    if (acph->headx[0].head.info1
                            != PICODATA_ITEMINFO1_PUNC_FLUSH) {
                        if (!acphPutBoundItem(this, acph,
                                acph->headx[0].boundstrength,
                                acph->headx[0].boundtype, &dopuoutfull,
                                numBytesOutput)) {
                            if (dopuoutfull) {
                                PICODBG_DEBUG(("feeding overflow"));
                                return PICODATA_PU_OUT_FULL;
                            } else {
                                /* ERR-msg and exception done in acphPutBoundItem */
                                return PICODATA_PU_ERROR;
                            }
                        }
                    }
                }

                /* for all items in headx, cbuf */
                for (i = acph->headxBottom; i < indupbound; i++) {

                    switch (acph->headx[i].head.type) {
                        case PICODATA_ITEM_PUNC:
                            /* if sentence end, put SEND bound */
                            if ((acph->headx[i].head.info1 ==
                                 PICODATA_ITEMINFO1_PUNC_SENTEND) &&
                                (i == (indupbound - 1))) {
                                /* construct and put BOUND item */
                                if (!acphPutBoundItem(this, acph,
                                            PICODATA_ITEMINFO1_BOUND_SEND,
                                            PICODATA_ITEMINFO2_NA,
                                            &dopuoutfull, numBytesOutput)) {
                                    if (dopuoutfull) {
                                        PICODBG_DEBUG(("feeding overflow"));
                                        return PICODATA_PU_OUT_FULL;
                                    } else {
                                        /* ERR-msg and exception done
                                           in acphPutBoundItem */
                                        return PICODATA_PU_ERROR;
                                    }
                                }
                            } else if ((acph->headx[i].head.info1 ==
                                 PICODATA_ITEMINFO1_PUNC_FLUSH) &&
                                (i == (indupbound - 1))) {
                                /* construct and put BOUND item */
                                if (!acphPutBoundItem(this, acph,
                                            PICODATA_ITEMINFO1_BOUND_TERM,
                                            PICODATA_ITEMINFO2_NA,
                                            &dopuoutfull, numBytesOutput)) {
                                    if (dopuoutfull) {
                                        PICODBG_DEBUG(("feeding overflow"));
                                        return PICODATA_PU_OUT_FULL;
                                    } else {
                                        /* ERR-msg and exception done
                                           in acphPutBoundItem */
                                        return PICODATA_PU_ERROR;
                                    }
                                }
                            }
                            /* else, good-bye PUNC, not needed anymore */
                            break;
                        default:

                            /* PHR2/3 maybe existing, check and add
                               BOUND item now, if needed */
                            if ((acph->headx[i].boundstrength ==
                                 PICODATA_ITEMINFO1_BOUND_PHR2) ||
                                (acph->headx[i].boundstrength ==
                                 PICODATA_ITEMINFO1_BOUND_PHR3)) {
                                if (!acphPutBoundItem(this, acph,
                                            acph->headx[i].boundstrength,
                                            acph->headx[i].boundtype,
                                            &dopuoutfull, numBytesOutput)) {
                                    if (dopuoutfull) {
                                        PICODBG_DEBUG(("feeding overflow"));
                                        return PICODATA_PU_OUT_FULL;
                                    } else {
                                        /* ERR-msg and exception done
                                           in acphPutBoundItem */
                                        return PICODATA_PU_ERROR;
                                    }
                                }
                            }

                            /* copy item unmodified */
                            rv = picodata_put_itemparts(&(acph->headx[i].head),
                                     &(acph->cbuf[acph->headx[i].cind]),
                                     acph->headx[i].head.len,
                                     acph->tmpbuf, PICODATA_MAX_ITEMSIZE,
                                     &blen);

                            rvP = picodata_cbPutItem(this->cbOut, acph->tmpbuf,
                                    PICODATA_MAX_ITEMSIZE, &clen);

                            *numBytesOutput += clen;

                            PICODBG_DEBUG(("put item, status: %d", rvP));

                            if (rvP == PICO_OK) {
                                acph->headxBottom++;
                                acph->headxLen--;
                            } else if (rvP == PICO_EXC_BUF_OVERFLOW) {
                                /* try again next time, but PHR2/3
                                   bound already added if existing,
                                   ensure it's not output a 2nd
                                   time */
                                PICODBG_DEBUG(("feeding overflow"));
                                acph->headx[i].boundstrength = 0;
                                return PICODATA_PU_OUT_FULL;
                            } else {
                                /* error, should never happen */
                                PICODBG_ERROR(("untreated return value, rvP: %d", rvP));
                                return PICODATA_PU_ERROR;
                            }

                            PICODATA_INFO_ITEM(this->voice->kbArray[PICOKNOW_KBID_DBG],
                                               (picoos_uint8 *)"acph: ",
                                               acph->tmpbuf, PICODATA_MAX_ITEMSIZE);

                            break;
                    } /*switch*/
                } /*for*/

                /* reset headx, cbuf */
                acph->headxBottom = 0;
                acph->headxLen = 0;
                acph->cbufLen = 0;
                for (i = 0; i < PICOACPH_MAXNR_HEADX; i++) {
                    acph->headx[i].boundstrength = 0;
                }

                /* reset collect state support variables */
                acph->inspaceok = TRUE;
                acph->needsmoreitems = TRUE;

                acph->procState = SA_STEPSTATE_COLLECT;
                return PICODATA_PU_BUSY;
                break;
            }

            default:
                break;
        } /* switch */

    } /* while */

    /* should be never reached */
    PICODBG_ERROR(("reached end of function"));
    picoos_emRaiseException(this->common->em, PICO_ERR_OTHER, NULL, NULL);
    return PICODATA_PU_ERROR;
}

#ifdef __cplusplus
}
#endif


/* end */