| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2001 */ |
| 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 |
/* CARNEGIE MELLON UNIVERSITY 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 CARNEGIE MELLON UNIVERSITY 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 ([email protected]) */ |
| 34 |
/* Date: July 2001 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Record a waveform and save to give file */ |
| 38 |
/* */ |
| 39 |
/* overly specific hack (for linux) first */ |
| 40 |
/* */ |
| 41 |
/*************************************************************************/ |
| 42 |
#include <unistd.h> |
| 43 |
#include <signal.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <sys/types.h> |
| 46 |
#include <sys/stat.h> |
| 47 |
#include <sys/ioctl.h> |
| 48 |
#include <sys/soundcard.h> |
| 49 |
#include <fcntl.h> |
| 50 |
#include "cst_wave.h" |
| 51 |
#include "cst_audio.h" |
| 52 |
|
| 53 |
int desired_rate = 16000; |
| 54 |
float desired_time = -1; |
| 55 |
int still_record = 1; |
| 56 |
|
| 57 |
static const char * const vw_audio_device = "/dev/dsp"; |
| 58 |
static int audio_set_sample_rate_vw(int afd,int sample_rate) |
| 59 |
{ |
| 60 |
int fmt; |
| 61 |
int sfmts; |
| 62 |
int stereo=0; |
| 63 |
int sstereo=0; |
| 64 |
int osample_rate; |
| 65 |
int channels=1; |
| 66 |
|
| 67 |
ioctl(afd,SNDCTL_DSP_RESET,0); |
| 68 |
sstereo = stereo; |
| 69 |
ioctl(afd,SNDCTL_DSP_STEREO,&sstereo); |
| 70 |
/* Some devices don't do mono even when you ask them nicely */ |
| 71 |
if (sstereo != stereo) |
| 72 |
osample_rate = sample_rate / 2; |
| 73 |
else |
| 74 |
osample_rate = sample_rate; |
| 75 |
ioctl(afd,SNDCTL_DSP_SPEED,&osample_rate); |
| 76 |
ioctl(afd,SNDCTL_DSP_CHANNELS,&channels); |
| 77 |
ioctl(afd,SNDCTL_DSP_GETFMTS,&sfmts); |
| 78 |
|
| 79 |
if (sfmts == AFMT_U8) |
| 80 |
fmt = AFMT_U8; // its really an 8 bit only device |
| 81 |
else if (CST_LITTLE_ENDIAN) |
| 82 |
fmt = AFMT_S16_LE; |
| 83 |
else |
| 84 |
fmt = AFMT_S16_BE; |
| 85 |
|
| 86 |
ioctl(afd,SNDCTL_DSP_SETFMT,&fmt); |
| 87 |
|
| 88 |
if (fmt == AFMT_U8) |
| 89 |
return -1; |
| 90 |
else |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
void sigint_handler(int a) |
| 96 |
{ |
| 97 |
still_record = 0; |
| 98 |
} |
| 99 |
|
| 100 |
int main(int argc, char **argv) |
| 101 |
{ |
| 102 |
/* need that cool argument parser */ |
| 103 |
cst_wave *w; |
| 104 |
int r, n, d, i; |
| 105 |
int desired_samples; |
| 106 |
char * ofile="record.wav"; |
| 107 |
|
| 108 |
if (argc == 1) |
| 109 |
{ |
| 110 |
fprintf(stderr,"usage: record_wave -f FREQ -t TIME WAVEFILE\n"); |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
for (i=1; i<argc; i++) |
| 115 |
{ |
| 116 |
if ((i+1 < argc) && (cst_streq(argv[i],"-f"))) |
| 117 |
{ |
| 118 |
desired_rate = atoi(argv[i+1]); |
| 119 |
i++; |
| 120 |
} |
| 121 |
else if ((i+1 < argc) && (cst_streq(argv[i],"-t"))) |
| 122 |
{ |
| 123 |
desired_time = atof(argv[i+1]); |
| 124 |
i++; |
| 125 |
} |
| 126 |
else |
| 127 |
ofile = argv[i]; |
| 128 |
} |
| 129 |
|
| 130 |
r = open(vw_audio_device,O_RDONLY); |
| 131 |
audio_set_sample_rate_vw(r,desired_rate); |
| 132 |
|
| 133 |
signal(SIGINT,sigint_handler); |
| 134 |
|
| 135 |
w = new_wave(); |
| 136 |
w->sample_rate = desired_rate; |
| 137 |
if (desired_time > 0) |
| 138 |
desired_samples = desired_time * desired_rate; |
| 139 |
else |
| 140 |
desired_samples = 5 * desired_rate; |
| 141 |
|
| 142 |
cst_wave_resize(w,desired_samples,1); |
| 143 |
|
| 144 |
d = 256; n = d; |
| 145 |
for (i=0; |
| 146 |
still_record && ((desired_time < 0) || (i < desired_samples)); |
| 147 |
i+=n) |
| 148 |
{ |
| 149 |
if (desired_time < 0) |
| 150 |
{ |
| 151 |
if (i+n > w->num_samples) |
| 152 |
cst_wave_resize(w,w->num_samples*1.25,1); |
| 153 |
} |
| 154 |
else if (i+n > w->num_samples) |
| 155 |
d = w->num_samples - i; |
| 156 |
|
| 157 |
n = read(r,&w->samples[i],d*sizeof(short)); |
| 158 |
n /= 2; |
| 159 |
} |
| 160 |
|
| 161 |
cst_wave_resize(w,i,1); |
| 162 |
cst_wave_save(w,ofile,"riff"); |
| 163 |
|
| 164 |
if (desired_time < 0) |
| 165 |
printf("wrote %d samples %f to %s\n", |
| 166 |
i,(float)i/(float)desired_rate,ofile); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |