Edinburgh Speech Tools 2.4-release
 
Loading...
Searching...
No Matches
na_record_main.cc
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
47int record_voxware_wave(EST_Wave &inwave, EST_Option &al);
48#if defined(WIN32) || defined(__CYGWIN__)
49int win_record_wave(EST_Wave &wave, EST_Option &al);
50#endif
51
52/** @name <command>na_record</command> <emphasis>Audio file recording</emphasis>
53 @id na-record-manual
54 * @toc
55 */
56
57//@{
58
59
60/**@name Synopsis
61 */
62//@{
63
64//@synopsis
65
66/**
67
68na_record records wavefors from an audio device. It only supports
69recording for N seconds (default is 10). Specifying the frequency
70defines the recording frequency (if supported by the hardware). This
71currently doesn't support NAS audio in.
72
73 */
74
75//@}
76
77/**@name OPTIONS
78 */
79//@{
80
81//@options
82
83//@}
84
85
86int main (int argc, char *argv[])
87{
88 EST_Wave wave;
89 EST_String out_file("-");
90 EST_StrList files;
91 EST_Option al;
92
93 parse_command_line
94 (argc,argv,
95 EST_String("[options]\n")+
96 "Summary; record waveform from audio device\n"+
97 "use \"-\" to make output files stdout\n"+
98 "-h options help\n"+
99 "-f <int> Input sample rate\n"+
100 "-audiodevice <string> use specified audiodevice if appropriate\n"
101 " for protocol\n"
102 "-time <float> Wave length in seconds\n"+
103 options_wave_output()+
104 "\n"+
105 "-p <string> audio device protocol. Ths supported types are\n"+
106 " "+options_supported_audio()+"\n",
107 files,al);
108
109 if (al.present("-f"))
110 al.add_item("-sample_rate", al.val("-f"));
111 else
112 al.add_item("-sample_rate", "16000");
113
114 if (!al.present("-time"))
115 al.add_item("-time", "10");
116 if (al.present("-o"))
117 out_file = al.val("-o");
118#if defined(WIN32) || defined(__CYGWIN__)
119 if (win_record_wave(wave,al) != 0)
120#else
121 if (record_wave(wave,al) != 0)
122#endif
123 {
124 return -1;
125 }
126
127 write_wave(wave, out_file, al);
128 return 0;
129}
130
131#if defined(WIN32) || defined(__CYGWIN__)
132int win_record_wave(EST_Wave &wave, EST_Option &al)
133{
134 char command_buffer[100]; // This could be more robust - ART
135 MCIERROR audio_error;
136 EST_String out_file("-");
137 // EST_String save_command("save mysound ");
138
139 if (!al.present("-o"))
140 {
141 cerr << "na_record: for Win32 version, must specify an output file with the -o flag" << endl;
142 return -1;
143 }
144 out_file = al.val("-o");
145 // save_command += al.val("-o");
146
147 // Should check the audio_error return values in the following - ART
148 // as it only reliable records at 44100 we'll do that and down sample
149
150 audio_error = mciSendString("open new type waveaudio alias mysound buffer 6",NULL,0,NULL);
151
152 sprintf(command_buffer,"set mysound time format ms bitspersample 16 samplespersec %d",44100);
153 audio_error = mciSendString(command_buffer,NULL, 0 ,NULL);
154
155 // In theory, based on the previous command, the integer being
156 // calculated in the next line should be the ending time of the
157 // recording in milliseconds. However, I have tried this program
158 // on a number of Windows machines and have found that the
159 // durations are off by a factor that seems to be assuming a 11025
160 // Hz sample rate. My guess for the additional factor of 2 needed
161 // to make the output file the right duration is that the Win32
162 // multimedia library is probably assuming stereo instead of mono
163 // recording. - ART
164 sprintf(command_buffer,"record mysound from 0 to %d wait",(int)(2*1000*al.fval("-time")*44100)/11025);
165 audio_error = mciSendString(command_buffer,NULL,0,NULL);
166 sprintf(command_buffer,"save mysound %s",(char *)al.val("-o"));
167 audio_error = mciSendString(command_buffer,NULL,0,NULL);
168 // audio_error = mciSendString(save_command,NULL,0,NULL);
169 audio_error = mciSendString("close mysound",NULL,0,NULL);
170
171 read_wave(wave, out_file, al);
172 wave.resample(al.ival("-sample_rate"));
173
174 return 0;
175}
176#endif
177
178
float fval(const EST_String &rkey, int m=1) const
Definition EST_Option.cc:98
int ival(const EST_String &rkey, int m=1) const
Definition EST_Option.cc:76
int add_item(const K &rkey, const V &rval, int no_search=0)
add key-val pair to list
Definition EST_TKVL.cc:248
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition EST_TKVL.cc:145
const int present(const K &rkey) const
Returns true if key is present.
Definition EST_TKVL.cc:222
void resample(int rate)
Resample waveform to rate
Definition EST_Wave.cc:489