recwave.c

Go to the documentation of this file.
00001 /*   
00002  *  recwave.c
00003  *
00004  *  Record sound files in wave format. Only MicroSoft PCM is supported. 
00005  *
00006  *  Michel R. Prevenier.
00007  */
00008 
00009 #include <errno.h>
00010 #include <sys/types.h>
00011 #include <sys/stat.h>
00012 #include <termios.h>
00013 #include <stdlib.h>
00014 #include <fcntl.h>
00015 #include <stdio.h>
00016 #include <unistd.h>
00017 #include <string.h>
00018 #include <signal.h>
00019 #include <sys/ioctl.h>
00020 #include <minix/sound.h>
00021 
00022 _PROTOTYPE (void main, (int argc, char **argv));
00023 _PROTOTYPE (void usage, (void));
00024 _PROTOTYPE ( void write_wave_header, (void));
00025 _PROTOTYPE ( void terminate, (int s));
00026 
00027 
00028 /******* Wave format definitions *********/
00029 
00030 #define RIFF_ID         0x46464952
00031 #define WAVE_ID1        0x45564157
00032 #define WAVE_ID2        0x20746D66
00033 #define DATA_ID         0x61746164
00034 #define MS_PCM_FORMAT   0x0001
00035 
00036 #define WORD    short 
00037 #define DWORD   unsigned long
00038 
00039 struct RIFF_fields
00040 {
00041   DWORD RIFF_id;
00042   DWORD RIFF_len;
00043   DWORD WAVE_id1;
00044   DWORD WAVE_id2;
00045   DWORD data_ptr;
00046 } r_fields; 
00047 
00048 struct common_fields
00049 {
00050   WORD  FormatTag;
00051   WORD  Channels;
00052   DWORD SamplesPerSec;
00053   DWORD AvgBytesPerSec;
00054   WORD  BlockAlign;
00055 } c_fields;
00056 
00057 struct specific_fields
00058 {
00059   WORD BitsPerSample;
00060 } s_fields;
00061 
00062 DWORD data_id;
00063 DWORD data_len;
00064 
00065 /******** End of wave format definitions *********/
00066 
00067 /* Default recording values */
00068 unsigned int sign = 0; 
00069 unsigned int bits = 8; 
00070 unsigned int stereo = 0; 
00071 unsigned int rate = 22050; 
00072 unsigned int time = 10;
00073 
00074 int old_stdin;
00075 struct termios old_tty, new_tty;
00076 int audio, file;
00077 
00078 void usage()
00079 {
00080   fprintf(stderr, "Usage: recwav [-b -s -r] file_name\n");
00081   exit(-1);
00082 }
00083 
00084 void terminate(s)
00085 int s;
00086 {
00087   /* Restore terminal parameters */
00088   tcsetattr(0, TCSANOW, &old_tty);
00089   (void) fcntl(0,F_SETFL,old_stdin);
00090   close(audio);
00091   close(file);
00092   exit(0);              
00093 }
00094 
00095 void write_wave_header()
00096 {
00097   /* RIFF fields */
00098   r_fields.RIFF_id = RIFF_ID;
00099   r_fields.WAVE_id1 = WAVE_ID1;
00100   r_fields.WAVE_id2 = WAVE_ID2;
00101   r_fields.data_ptr = 16;
00102   r_fields.RIFF_len = 20 + r_fields.data_ptr + data_len;
00103 
00104   /* MicroSoft PCM specific fields */
00105   s_fields.BitsPerSample = bits;
00106 
00107   /* Common fields */
00108   c_fields.FormatTag = MS_PCM_FORMAT;
00109   c_fields.Channels = stereo + 1;
00110   c_fields.SamplesPerSec = rate;
00111   c_fields.AvgBytesPerSec =  c_fields.Channels * rate * (bits / 8);
00112   c_fields.BlockAlign = c_fields.Channels * (bits / 8);
00113 
00114   /* Data chunk */
00115   data_id = DATA_ID;
00116 
00117   /* Write wave-file header */
00118   lseek(file, 0L, SEEK_SET);
00119   write(file, &r_fields, 20);
00120   write(file, &c_fields, 14);
00121   write(file, &s_fields, 2);
00122   write(file, &data_id, sizeof(data_id)); 
00123   write(file, &data_len, sizeof(data_len)); 
00124 }
00125 
00126 
00127 void main(argc, argv)
00128 int argc;
00129 char **argv;
00130 {
00131   unsigned int fragment_size;
00132   char *buffer, *file_name;
00133   char c;
00134   int i;
00135 
00136   /* Read parameters */
00137   if (argc < 2) usage();
00138 
00139   i = 1;
00140   while (argv[i][0] == '-' && i < argc)
00141   {
00142     if (strncmp(argv[i], "-b", 2) == 0)
00143       bits = atoi(argv[i] + 2);
00144     else if (strncmp(argv[i], "-s", 2) == 0) 
00145       stereo = atoi(argv[i] + 2);
00146     else if (strncmp(argv[i], "-r", 2) == 0) 
00147       rate = (unsigned int) atol(argv[i] + 2);
00148     else usage();
00149     i++;
00150   }
00151   if (i == argc) usage();
00152 
00153   file_name = argv[i];
00154 
00155   /* Some sanity checks */
00156   if ((bits != 8 && bits != 16) || 
00157       (rate < 4000 || rate > 44100) ||
00158       (stereo != 0 && stereo != 1))
00159   {
00160     fprintf(stderr, "Invalid parameters\n");
00161     exit(-1);
00162   }
00163 
00164   /* Open DSP */
00165   if ((audio = open("/dev/rec", O_RDWR)) < 0) 
00166   {
00167     fprintf(stderr, "Cannot open /dev/rec\n");
00168     exit(-1);
00169   }
00170 
00171   /* Get maximum fragment size and try to allocate a buffer */
00172   ioctl(audio, DSPIOMAX, &fragment_size);
00173   if ((buffer = malloc(fragment_size)) == (char *) 0)
00174   {
00175     fprintf(stderr, "Cannot allocate buffer\n");
00176     exit(-1);
00177   } 
00178 
00179   /* Set sample parameters */
00180   ioctl(audio, DSPIOSIZE, &fragment_size); 
00181   ioctl(audio, DSPIOSTEREO, &stereo); 
00182   ioctl(audio, DSPIORATE, &rate);
00183   ioctl(audio, DSPIOBITS, &bits); 
00184   sign = (bits == 16 ? 1 : 0);
00185   ioctl(audio, DSPIOSIGN, &sign); 
00186 
00187   /* Create sample file */
00188   if ((file = creat(file_name, 511)) < 0) 
00189   {
00190     fprintf(stderr, "Cannot create %s\n", argv[1]);
00191     exit(-1);
00192   } 
00193   /* Skip wave header */
00194   lseek(file, (long)(sizeof(r_fields) + 
00195                      sizeof(c_fields) + 
00196                      sizeof(s_fields) + 
00197                      sizeof(data_id) + 
00198                      sizeof(data_len)), SEEK_SET); 
00199 
00200   printf("\nBits per sample   : %u\n", bits);
00201   printf("Stereo            : %s\n", (stereo == 1 ? "yes" : "no"));
00202   printf("Samples per second: %u\n", rate);
00203 
00204   /* Set terminal parameters and remember the old ones */
00205   tcgetattr(0, &old_tty);
00206   new_tty = old_tty;
00207   new_tty.c_lflag &= ~(ICANON|ECHO);
00208   old_stdin = fcntl(0, F_GETFL);
00209 
00210   /* Catch break signal to be able to restore terminal parameters in case
00211    * of a user interrupt
00212    */
00213   signal(SIGINT, terminate);
00214 
00215   /* Go to non-blocking mode */
00216   tcsetattr(0, TCSANOW, &new_tty);
00217   (void) fcntl(0, F_SETFL, old_stdin | O_NONBLOCK);
00218 
00219   printf("\nPress spacebar to start sampling...\n");
00220   while(!(read(0, &c, 1) == 1 && c == ' '));
00221 
00222   printf("Sampling, press spacebar to stop...\n");
00223   while(!(read(0, &c, 1) == 1 && c == ' '))
00224   {
00225     /* Read sample fragment and write to sample file */
00226     read(audio, buffer, fragment_size);
00227     write(file, buffer, fragment_size);
00228     data_len+= fragment_size;
00229   }
00230   printf("%ld bytes sampled. \n\n", data_len);
00231  
00232   /* Construct the wave header in front of the raw sample data */
00233   write_wave_header();
00234   
00235   /* Restore terminal parameters and exit */
00236   terminate(1);
00237 }

Generated on Fri Apr 14 22:56:58 2006 for minix by  doxygen 1.4.6