record_wave_main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 (awb@cs.cmu.edu) */
  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. int desired_rate = 16000;
  53. float desired_time = -1;
  54. int still_record = 1;
  55. static const char * const vw_audio_device = "/dev/dsp";
  56. static int audio_set_sample_rate_vw(int afd,int sample_rate)
  57. {
  58. int fmt;
  59. int sfmts;
  60. int stereo=0;
  61. int sstereo=0;
  62. int osample_rate;
  63. int channels=1;
  64. ioctl(afd,SNDCTL_DSP_RESET,0);
  65. sstereo = stereo;
  66. ioctl(afd,SNDCTL_DSP_STEREO,&sstereo);
  67. /* Some devices don't do mono even when you ask them nicely */
  68. if (sstereo != stereo)
  69. osample_rate = sample_rate / 2;
  70. else
  71. osample_rate = sample_rate;
  72. ioctl(afd,SNDCTL_DSP_SPEED,&osample_rate);
  73. ioctl(afd,SNDCTL_DSP_CHANNELS,&channels);
  74. ioctl(afd,SNDCTL_DSP_GETFMTS,&sfmts);
  75. if (sfmts == AFMT_U8)
  76. fmt = AFMT_U8; // its really an 8 bit only device
  77. else if (CST_LITTLE_ENDIAN)
  78. fmt = AFMT_S16_LE;
  79. else
  80. fmt = AFMT_S16_BE;
  81. ioctl(afd,SNDCTL_DSP_SETFMT,&fmt);
  82. if (fmt == AFMT_U8)
  83. return -1;
  84. else
  85. return 0;
  86. }
  87. void sigint_handler(int a)
  88. {
  89. still_record = 0;
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. /* need that cool argument parser */
  94. cst_wave *w;
  95. int r, n, d, i;
  96. int desired_samples;
  97. char * ofile="record.wav";
  98. if (argc == 1)
  99. {
  100. fprintf(stderr,"usage: record_wave -f FREQ -t TIME WAVEFILE\n");
  101. return 1;
  102. }
  103. for (i=1; i<argc; i++)
  104. {
  105. if ((i+1 < argc) && (cst_streq(argv[i],"-f")))
  106. {
  107. desired_rate = atoi(argv[i+1]);
  108. i++;
  109. }
  110. else if ((i+1 < argc) && (cst_streq(argv[i],"-t")))
  111. {
  112. desired_time = atof(argv[i+1]);
  113. i++;
  114. }
  115. else
  116. ofile = argv[i];
  117. }
  118. r = open(vw_audio_device,O_RDONLY);
  119. audio_set_sample_rate_vw(r,desired_rate);
  120. signal(SIGINT,sigint_handler);
  121. w = new_wave();
  122. w->sample_rate = desired_rate;
  123. if (desired_time > 0)
  124. desired_samples = desired_time * desired_rate;
  125. else
  126. desired_samples = 5 * desired_rate;
  127. cst_wave_resize(w,desired_samples,1);
  128. d = 256; n = d;
  129. for (i=0;
  130. still_record && ((desired_time < 0) || (i < desired_samples));
  131. i+=n)
  132. {
  133. if (desired_time < 0)
  134. {
  135. if (i+n > w->num_samples)
  136. cst_wave_resize(w,w->num_samples*1.25,1);
  137. }
  138. else if (i+n > w->num_samples)
  139. d = w->num_samples - i;
  140. n = read(r,&w->samples[i],d*sizeof(short));
  141. n /= 2;
  142. }
  143. cst_wave_resize(w,i,1);
  144. cst_wave_save(w,ofile,"riff");
  145. if (desired_time < 0)
  146. printf("wrote %d samples %f to %s\n",
  147. i,(float)i/(float)desired_rate,ofile);
  148. return 0;
  149. }