svox_ssml_parser.h revision 39358f0dacad8cece6c2d3ef1055030f57090c79
1/*
2 * Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * History:
17 * 2009-06-29 -- initial version
18 *
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <libexpat/expat.h>
24
25#ifndef _SVOX_SSML_PARSER_H_
26#define _SVOX_SSML_PARSER_H_
27
28/**
29 * SvoxSsmlParser
30 * Parses SSML 1.0 XML documents and convertes it to Pico compatible text input
31 */
32class SvoxSsmlParser
33{
34 public: /* construction code */
35
36  /**
37     Constructor
38     Creates Expat parser and allocates initial text storage
39  */
40  SvoxSsmlParser();
41
42  /**
43     Destructor
44     Deallocates all resources
45  */
46  ~SvoxSsmlParser();
47
48  /**
49     initSuccessful
50     Verifies that construction was successful
51     return 1 if successful, 0 otherwise
52  */
53  int initSuccessful();
54
55 public: /* public members */
56
57  /**
58     parseDocument
59     Parses SSML 1.0 document passed in as argument
60     @ssmldoc - SSML document, partial document input is supported
61     @isFinal - indicates whether input is a complete or partial document, 1 indicates complete document, 0 indicates partial
62     return Expat status code
63  */
64  int parseDocument(const char* ssmldoc, int isFinal);
65
66  /**
67     getParsedDocument
68     Returns string containing parse result. This can be passed on to Pico for synthesis
69     return parsed string, NULL if error occurred
70  */
71  char* getParsedDocument();
72
73  /**
74     getParsedDocumentLanguage
75     Returns language string specified in xml:lang attribute of the <speak> tag
76     return language code of SSML document, NULL if not set
77  */
78  char* getParsedDocumentLanguage();
79
80 private: /* static callback functions */
81
82  /**
83     starttagHandler
84     Static callback function for Expat start-tag events, internal use only
85  */
86  static void starttagHandler(void* data, const XML_Char* element, const XML_Char** attributes);
87
88  /**
89     endtagHandler
90     Static callback function for Expat end-tag events, internal use only
91  */
92  static void endtagHandler(void* data, const XML_Char* element);
93
94  /**
95     textHandler
96     Static callback function for Expat text events, internal use only
97  */
98  static void textHandler(void* data, const XML_Char* text, int length);
99
100 private: /* element handlers */
101
102  /**
103     startElement
104     Handles start of element, called by starttagHandler.
105  */
106  void startElement(const XML_Char* element, const XML_Char** attributes);
107
108  /**
109     endElement
110     Handles end of element, called by endtagHandler.
111  */
112  void endElement(const XML_Char* element);
113
114  /**
115     textElement
116     Handles text element, called by textHandler.
117  */
118  void textElement(const XML_Char* text, int length);
119
120  /* helper functions */
121
122  /**
123     convertToSvoxPitch
124     Convertes SSML prosody tag pitch values to SVOX Pico pitch values.
125  */
126  char* convertToSvoxPitch(const char* value);
127
128  /**
129     convertToSvoxRate
130     Convertes SSML prosody tag rate values to SVOX Pico speed values.
131  */
132  char* convertToSvoxRate(const char* value);
133
134  /**
135     convertToSvoxVolume
136     Convertes SSML prosody tag volume values to SVOX Pico volume values.
137  */
138  char* convertToSvoxVolume(const char* value);
139
140  /**
141     convertBreakStrengthToTime
142     Convertes SSML break tag strength attribute values to SVOX Pico break time values.
143  */
144  char* convertBreakStrengthToTime(const char* value);
145
146  /**
147     growDataSize
148     Increases size of internal text field.
149  */
150  int growDataSize(int sizeToGrow);
151
152 private: /* data members*/
153
154  char* m_data;          /* internal text field, holds parsed text */
155  int m_datasize;        /* size of internal text field */
156  XML_Parser mParser;    /* Expat XML parser pointer */
157  int m_isInBreak;       /* indicator for handling break tag parsing */
158  char* m_appendix;      /* holds Pico pitch, speed and volume close tags for prosody tag parsing */
159  char* m_docLanguage;   /* language set in speak tag of SSML document */
160};
161
162#endif // _SVOX_SSML_PARSER_H_
163