Edinburgh Speech Tools  2.1-release
win32audio.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Richard Caley */
34 /* Date : August 1997 */
35 /*-----------------------------------------------------------------------*/
36 /* Optional 16bit linear support for Windows NT/95 */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdio>
41 #include "EST_cutils.h"
42 #include "EST_Wave.h"
43 #include "EST_Option.h"
44 #include "EST_io_aux.h"
45 #include "EST_Pathname.h"
46 
47 using namespace std;
48 
49 #ifdef SUPPORT_WIN32AUDIO
50 
51 #include <EST_system.h>
52 
53 #ifdef SYSTEM_IS_UNIX
54 
55 // Even if we think we are unix, this must be Win32 if we are asked
56 // to support win32 audio, presumably under cygnus gunwin32 These definitions
57 // aren't in the gnuwin32 headers
58 
59 #undef TRUE
60 #undef FALSE
61 /* Changed for cygwin 1
62  #include <Windows32/BASE.h>
63  #define SND_MEMORY 0x0004
64  #define WAVE_FORMAT_PCM 1
65 */
66 #include <windows.h>
67 extern "C" {
68 WINBOOL STDCALL PlaySoundA(LPCSTR pszSound, HMODULE hmod, DWORD fdwSound);
69 };
70 
71 #define PlaySound PlaySoundA
72 #endif
73 
75 
76 struct riff_header {
77  char riff[4];
78  int file_size;
79  char wave[4];
80  char fmt[4];
81  int header_size;
82  short sample_format;
83  short n_channels;
84  int sample_rate;
85  int bytes_per_second;
86  short block_align;
87  short bits_per_sample;
88  char data[4];
89  int data_size;
90 };
91 
93 {
94  char *buffer = new char[sizeof(riff_header) + inwave.length()*inwave.num_channels() * sizeof(short)];
95 
96  struct riff_header *hdr = (struct riff_header *)buffer;
97  char *data = buffer + sizeof(struct riff_header);
98 
99  strncpy(hdr->riff, "RIFF", 4);
100  hdr->file_size = sizeof(riff_header) + inwave.length()*sizeof(short);
101  strncpy(hdr->wave, "WAVE", 4);
102  strncpy(hdr->fmt, "fmt ", 4);
103  hdr->header_size = 16;
104  hdr->sample_format = WAVE_FORMAT_PCM;
105  hdr->n_channels = inwave.num_channels();
106  hdr->sample_rate = inwave.sample_rate();
107  hdr->bytes_per_second = hdr->sample_rate * hdr->n_channels * 2;
108  hdr->block_align = hdr->n_channels * 2;
109  hdr->bits_per_sample = 16;
110  strncpy(hdr->data, "data", 4);
111  hdr->data_size = hdr->n_channels * 2 * inwave.num_samples();
112 
113  memcpy(data, inwave.values().memory(), hdr->n_channels * 2 * inwave.num_samples());
114  PlaySound( buffer,
115  NULL,
116  SND_MEMORY);
117  delete [] buffer;
118  return 1;
119 }
120 
121 #else
123 
125 {
126  (void)inwave;
127  (void)al;
128  cerr << "Windows win32 audio not supported" << endl;
129  return -1;
130 }
131 
132 #endif
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
int play_win32audio_wave(EST_Wave &inwave, EST_Option &al)
Definition: win32audio.cc:124
#define WAVE_FORMAT_PCM
Definition: EST_wave_io.cc:522
Utility IO Function header file.
ssize_t length() const
return the size of the waveform, i.e. the number of samples.
Definition: EST_Wave.h:151
ssize_t num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
const EST_SMatrix & values() const
Definition: EST_Wave.h:177
#define FALSE
Definition: EST_bool.h:119
NULL
Definition: EST_WFST.cc:55
int win32audio_supported
Definition: win32audio.cc:122
int sample_rate() const
return the sampling rate (frequency)
Definition: EST_Wave.h:147
const T * memory() const
Definition: EST_TVector.h:238
ssize_t num_channels() const
return the number of channels in the waveform
Definition: EST_Wave.h:145
#define TRUE
Definition: EST_bool.h:118