1/*---------------------------------------------------------------------------*
2 *  sample.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 _h_sample_
21#define _h_sample_
22
23
24#include "all_defs.h"
25#ifndef _RTT
26#include "duk_io.h"
27#endif
28#include "front.h"
29
30#ifdef _WIN32
31#include "windows.h"
32#endif
33
34/*  The known wave types here
35*/
36/* #define DEVICE_RAW_PCM 1 */
37/* #define DEVICE_MULAW  2 */
38#define FILE_FORMATTED  3
39
40/*  The known device (op) types here
41*/
42
43#define WAVE_DEVICE_INPUT   1
44#define WAVE_DEVICE_OUTPUT  2
45#define WAVE_FILE_INPUT     3
46#define WAVE_FILE_OUTPUT    4
47
48/*  The known wave-file types are
49** RIFF (R), NIST (N), RAW-PCM (P), RAW-MU-LAW (M)
50*/
51
52#if !defined(_WIN32)     /* TODO: do we want to support RIFF header files? */
53/* Add definition for use in RIFF header r/w */
54#if defined unix || defined PSOS || defined POSIX
55/* VxWorks simulator defines DWORD and WORD already */
56#if !(defined(__vxworks) && (CPU & SIMNT))
57typedef asr_uint32_t DWORD;
58#endif
59typedef unsigned char  BYTE;
60/* following two lines does not help. It only works when WORD is defined by MACRO: #define WORD unsigned short */
61#ifdef WORD
62#undef WORD
63#endif
64#if !(defined(__vxworks) && (CPU & SIMNT))
65typedef asr_uint16_t WORD;
66#endif
67#define WAVE_FORMAT_PCM 0x01
68#endif
69
70//typedef DWORD  FOURCC;         /* a four character code */
71#define MAKEFOURCC(ch0, ch1, ch2, ch3) ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))
72#define mmioFOURCC MAKEFOURCC
73
74/**
75 * @todo document
76 */
77typedef struct
78{
79  WORD  wFormatTag;
80  WORD  nChannels;
81  DWORD nSamplesPerSec;
82  DWORD nAvgBytesPerSec;
83  WORD  nBlockAlign;
84}
85WAVEFORMAT;
86
87/**
88 * @todo document
89 */
90typedef struct
91{
92  WAVEFORMAT wf;
93  WORD       wBitsPerSample;
94}
95PCMWAVEFORMAT;
96#else
97/* disable nameless union/struct warning in mmsytem.h but restore them to
98disallow such code of our own.
99*/
100#pragma warning (push)
101#pragma warning (disable: 4201)
102#include <mmsystem.h>
103#pragma warning (pop)
104#endif
105
106
107#ifndef _RTT
108
109/**
110 * @todo document
111 */
112typedef struct
113{
114  char  typ;  /* R (RIFF), N (NIST), P (RAW PCM) M (MU-LAW) */
115  int   op;      /* read or write */
116  int   endian;  /* 0 is little 1 is big */
117  unsigned long len; /* length of file */
118  PFile* file;  /* pointer to file */
119  char  name[MAX_FILE_NAME]; /* file name */
120}
121wav_file_info;
122
123#endif
124
125/**
126 * @todo document
127 */
128typedef struct
129{
130  char typ;  /* -Undefined as yet- */
131  int  op;      /* read (i/p) or write (o/p) */
132}
133wav_device_info;
134
135/**
136 * @todo document
137 */
138typedef union {
139#ifndef _RTT
140  wav_file_info   file;
141#endif
142  wav_device_info ext;
143} gen_device_info;
144
145#define MAXHISTBITS 33  /* one more than bitrange for signed */
146/* int bit usage - this could be 17 if */
147/* we assume shorts   */
148
149/**
150 * @todo document
151 */
152typedef struct
153{
154  int nsam;
155  int sum;
156  int sum2;
157  int sumsqu;
158  int sumsqu2;
159  int sumabs;
160  int sumabs2;
161  int highclip;
162  int lowclip;
163  int bithist[MAXHISTBITS];
164  samdata highclip_level;
165  samdata lowclip_level;
166  int max_per10000_clip;
167  int max_dc_offset;
168  int high_noise_level_bit;
169  int low_speech_level_bit;
170  int min_samples;
171}
172wave_stats;
173
174/**
175 * @todo document
176 */
177typedef struct
178{
179  int   wave_type;
180  int   device_type;
181  int   samplerate;
182  int   frame_size;
183  int   window_size;
184  int   num_samples;
185  samdata  *income;
186  samdata  *outgo;
187  booldata  initialised;
188  float  scale;
189  int   offset;
190  /* The channel object here is the set of data streams used in making frames.
191      IN CA, it is convenient to store channel as part of CA_Wave (wave_info).
192      It could have a many-to-one relationship with wave_info. */
193  front_channel *channel;
194  gen_device_info     device;
195  wave_stats  stats;
196  booldata  do_stats;
197}
198wave_info;
199
200
201void reset_sig_check(wave_stats *ws);
202void get_sig_check(wave_stats *ws, int *nsam, int *pclowclip, int *pchighclip,
203                   int *dc_offset, int *amp, int *pc5, int *pc95, int* overflow);
204void acc_wave_stats(wave_info* wave);
205
206void create_sample_buffer(wave_info *wave, int frame_size, int window_size);
207void free_sample_buffer(wave_info *wave);
208#ifndef _RTT
209int init_wavfile_stream(wave_info *wave, char *filename, int type);
210int close_wavfile_stream(wave_info *wave);
211int load_wavfile_data(wave_info* wave);
212int save_wavfile_data(wave_info* wave);
213int seek_wavfile_data(wave_info* wave, long offset, int origin);
214int read_riff_header(PFile* waveFile, PCMWAVEFORMAT *pcmWaveFormat, unsigned long *datalen);
215void add_riff_header(PFile* waveFile, int samplerate, int bitspersample);
216void fix_riff_header(PFile* waveFile, int samplerate, int bitspersample);
217#endif
218void copy_wave_data(wave_info* dest, wave_info* src);
219
220
221
222#endif
223