1 /*---------------------------------------------------------------------------* 2 * riff.h * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 #ifndef RIFF_H 21 #define RIFF_H 22 23 #include "ESR_ReturnCode.h" 24 #include "plog.h" 25 #include "passert.h" 26 #include "pmemory.h" 27 #include "SR_EventLogPrefix.h" 28 29 #define AURORA_BYTES_SEC 5600 30 31 /* standard codec IDs */ 32 #define WAVEFORMAT_PCM 0x01 33 #define WAVEFORMAT_ALAW 0x06 34 #define WAVEFORMAT_MULAW 0x07 35 36 /* swi-specific codec ID */ 37 #define WAVEFORMAT_AURORA 0x99 38 #define WAVEFORMAT_ES_202_050 0x9A 39 40 /** 41 * WAV file format. 42 */ 43 typedef struct 44 { 45 /** 46 * Codec ID. 47 */ 48 unsigned short nFormatTag; 49 /** 50 * The number of channels. 51 */ 52 unsigned short nChannels; 53 /** 54 * sampling rate: sample frames per sec. 55 */ 56 unsigned int nSamplesPerSec; 57 /** 58 * sampling rate * block alignment 59 */ 60 unsigned int nAvgBytesPerSec; 61 /** 62 * number of channels * bytes_per_sample 63 */ 64 unsigned short nBlockAlign; 65 /** 66 * bytes_per_sample * 8 (PCM-specific field) 67 */ 68 unsigned short wBitsPerSample; 69 } 70 WaveFormat; 71 72 /** 73 * Generic start of every RIFF chunk. 74 */ 75 typedef struct 76 { 77 /** 78 * 4-byte signature 79 */ 80 char ckString[4]; 81 /** 82 * Chunk length. 83 */ 84 int ckLength; 85 } 86 ChunkInfoStruct; 87 88 /** 89 * RIFF Header. 90 */ 91 typedef struct 92 { 93 /** 94 * "RIFF" 95 */ 96 char riffString[4]; 97 /** 98 * The length of the RIFF chunk. 99 */ 100 unsigned int riffChunkLength; 101 /** 102 * "WAVE" 103 */ 104 char waveString[4]; 105 /** 106 * "fmt " 107 */ 108 char fmtString[4]; 109 /** 110 * The length of the format chunk. 111 */ 112 unsigned int fmtChunkLength; 113 /** 114 * The audio format. 115 */ 116 WaveFormat waveinfo; 117 /** 118 * "data" 119 */ 120 char dataString[4]; 121 /** 122 * The length of the audio data section. 123 */ 124 unsigned int dataLength; 125 } 126 RiffHeaderStruct; 127 128 /** 129 * An audio segment. 130 */ 131 typedef struct 132 { 133 /** 134 * Position (byte #) where audio segment begins. 135 */ 136 int pos; 137 /** 138 * Length of audio segment. 139 */ 140 int len; 141 /** 142 * SWIrec_PACKET_SUPPRESSED or SWIrec_PACKET_LOST. 143 */ 144 int type; 145 } 146 RiffAudioTuple; 147 148 /** 149 * For "supp" or "lost" chunk. 150 */ 151 typedef struct 152 { 153 /** 154 * The number of audio tuples. 155 */ 156 int num_tuples; 157 /** 158 * The audio tuples. 159 */ 160 RiffAudioTuple *tuples; 161 } 162 SwiRiffAudio; 163 164 /** 165 * Key-value pair. 166 */ 167 typedef struct 168 { 169 /** 170 * e.g. "encoding" 171 */ 172 char *key; 173 /** 174 * e.g. "g723" 175 */ 176 char *value; 177 } 178 RiffKVPair; 179 180 /** 181 * For "kval" chunk. 182 */ 183 typedef struct 184 { 185 /** 186 * The number of key-value pairs. 187 */ 188 int num_pairs; 189 /** 190 * The key-value pairs. 191 */ 192 RiffKVPair *kvpairs; 193 } 194 SwiRiffKeyVals; 195 196 /** 197 * A RIFF audio segment. 198 */ 199 typedef struct 200 { 201 /** 202 * Special audio segments, lost or suppressed 203 */ 204 SwiRiffAudio segs; 205 /** 206 * Key-value pairs. 207 */ 208 SwiRiffKeyVals kvals; 209 } 210 SwiRiffStruct; 211 212 213 SREC_EVENTLOG_API int isLittleEndian(void); 214 215 SREC_EVENTLOG_API ESR_ReturnCode riffReadWave2L16( 216 FILE *f, 217 double from, 218 double to, 219 short **samples, 220 int *rate, 221 int *length, 222 SwiRiffStruct *swichunk); 223 224 SREC_EVENTLOG_API ESR_ReturnCode convertBuf2Riff( 225 unsigned char *waveform, 226 unsigned int num_bytes, 227 wchar_t *audio_type, 228 int rate, 229 int bytes_per_sample, 230 SwiRiffStruct *swichunk, 231 unsigned char **buf, 232 unsigned int *buflen); 233 234 SREC_EVENTLOG_API ESR_ReturnCode readRiff2Buf( 235 FILE *f, 236 void **waveform, 237 unsigned int *num_bytes, 238 const wchar_t **audio_type, 239 SwiRiffStruct *swichunk); 240 241 SREC_EVENTLOG_API int isRiffFile(FILE *fp); 242 SREC_EVENTLOG_API void free_swiRiff(SwiRiffStruct *swichunk); 243 SREC_EVENTLOG_API char *getSwiRiffKVal(SwiRiffStruct *swichunk, char *key); 244 #endif 245 246 247 248 249 250 251 252 253 254 255