Edinburgh Speech Tools  2.1-release
na_record_main.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: Alan W Black */
34 /* Date : February 1998 */
35 /*-----------------------------------------------------------------------*/
36 /* General recording program */
37 /* */
38 /*=======================================================================*/
39 #include "EST.h"
40 #include "EST_audio.h"
41 #include "EST_cmd_line_options.h"
42 #if defined(WIN32) || defined(__CYGWIN__)
43 #include "windows.h"
44 #include "Mmsystem.h"
45 #endif
46 
47 int record_voxware_wave(EST_Wave &inwave, EST_Option &al);
48 #if defined(WIN32) || defined(__CYGWIN__)
49 int win_record_wave(EST_Wave &wave, EST_Option &al);
50 #endif
51 
52 using namespace std;
53 
54 int main (int argc, char *argv[])
55 {
56  EST_Wave wave;
57  EST_String out_file("-");
58  EST_StrList files;
59  EST_Option al;
60 
62  (argc,argv,
63  EST_String("[options]\n")+
64  "Summary; record waveform from audio device\n"+
65  "use \"-\" to make output files stdout\n"+
66  "-h options help\n"+
67  "-f <int> Input sample rate\n"+
68  "-audiodevice <string> use specified audiodevice if appropriate\n"
69  " for protocol\n"
70  "-time <float> Wave length in seconds\n"+
72  "\n"+
73  "-p <string> audio device protocol. Ths supported types are\n"+
74  " "+options_supported_audio()+"\n",
75  files,al);
76 
77  if (al.present("-f"))
78  al.add_item("-sample_rate", al.val("-f"));
79  else
80  al.add_item("-sample_rate", "16000");
81 
82  if (!al.present("-time"))
83  al.add_item("-time", "10");
84  if (al.present("-o"))
85  out_file = al.val("-o");
86 #if defined(WIN32) || defined(__CYGWIN__)
87  if (win_record_wave(wave,al) != 0)
88 #else
89  if (record_wave(wave,al) != 0)
90 #endif
91  {
92  return -1;
93  }
94 
95  write_wave(wave, out_file, al);
96  return 0;
97 }
98 
99 #if defined(WIN32) || defined(__CYGWIN__)
100 int win_record_wave(EST_Wave &wave, EST_Option &al)
101 {
102  char command_buffer[100]; // This could be more robust - ART
103  MCIERROR audio_error;
104  EST_String out_file("-");
105  // EST_String save_command("save mysound ");
106 
107  if (!al.present("-o"))
108  {
109  cerr << "na_record: for Win32 version, must specify an output file with the -o flag" << endl;
110  return -1;
111  }
112  out_file = al.val("-o");
113  // save_command += al.val("-o");
114 
115  // Should check the audio_error return values in the following - ART
116  // as it only reliable records at 44100 we'll do that and down sample
117 
118  audio_error = mciSendString("open new type waveaudio alias mysound buffer 6",NULL,0,NULL);
119 
120  sprintf(command_buffer,"set mysound time format ms bitspersample 16 samplespersec %d",44100);
121  audio_error = mciSendString(command_buffer,NULL, 0 ,NULL);
122 
123  // In theory, based on the previous command, the integer being
124  // calculated in the next line should be the ending time of the
125  // recording in milliseconds. However, I have tried this program
126  // on a number of Windows machines and have found that the
127  // durations are off by a factor that seems to be assuming a 11025
128  // Hz sample rate. My guess for the additional factor of 2 needed
129  // to make the output file the right duration is that the Win32
130  // multimedia library is probably assuming stereo instead of mono
131  // recording. - ART
132  sprintf(command_buffer,"record mysound from 0 to %d wait",(int)(2*1000*al.fval("-time")*44100)/11025);
133  audio_error = mciSendString(command_buffer,NULL,0,NULL);
134  sprintf(command_buffer,"save mysound %s",(char *)al.val("-o"));
135  audio_error = mciSendString(command_buffer,NULL,0,NULL);
136  // audio_error = mciSendString(save_command,NULL,0,NULL);
137  audio_error = mciSendString("close mysound",NULL,0,NULL);
138 
139  read_wave(wave, out_file, al);
140  wave.resample(al.ival("-sample_rate"));
141 
142  return 0;
143 }
144 #endif
145 
146 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
int main(int argc, char *argv[])
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
int record_wave(EST_Wave &wave, EST_Option &al)
Definition: gen_audio.cc:281
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
NULL
Definition: EST_WFST.cc:55
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int add_item(const K &rkey, const V &rval, int no_search=0)
add key-val pair to list
Definition: EST_TKVL.cc:248
EST_write_status write_wave(EST_Wave &sig, const EST_String &in_file, EST_Option &al)
EST_read_status read_wave(EST_Wave &sig, const EST_String &in_file, EST_Option &al)
EST_String options_wave_output(void)
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
EST_String options_supported_audio(void)
Definition: gen_audio.cc:249
void resample(int rate)
Resample waveform to rate
Definition: EST_Wave.cc:492
EST_String
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101
int record_voxware_wave(EST_Wave &inwave, EST_Option &al)