summaryrefslogtreecommitdiff
path: root/minix/commands/recwave/recwave.c
blob: cef26fb294f360e0776cb926f520b4cfbe2384a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*   
 *  recwave.c
 *
 *  Record sound files in wave format. Only MicroSoft PCM is supported. 
 *
 *  Michel R. Prevenier.
 */

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <minix/sound.h>

int main(int argc, char **argv);
void usage(void);
void write_wave_header(void);
void terminate(int s);


/******* Wave format definitions *********/

#define RIFF_ID		0x46464952
#define WAVE_ID1	0x45564157
#define WAVE_ID2	0x20746D66
#define DATA_ID		0x61746164
#define MS_PCM_FORMAT	0x0001

#define WORD	short 
#define DWORD   unsigned long

struct RIFF_fields
{
  DWORD RIFF_id;
  DWORD RIFF_len;
  DWORD WAVE_id1;
  DWORD WAVE_id2;
  DWORD data_ptr;
} r_fields; 

struct common_fields
{
  WORD  FormatTag;
  WORD  Channels;
  DWORD SamplesPerSec;
  DWORD AvgBytesPerSec;
  WORD  BlockAlign;
} c_fields;

struct specific_fields
{
  WORD BitsPerSample;
} s_fields;

DWORD data_id;
DWORD data_len;

/******** End of wave format definitions *********/

/* Default recording values */
unsigned int sign = 0; 
unsigned int bits = 8; 
unsigned int stereo = 0; 
unsigned int rate = 22050; 

int old_stdin;
struct termios old_tty, new_tty;
int audio, file;

void usage()
{
  fprintf(stderr, "Usage: recwav [-b -s -r] file_name\n");
  exit(-1);
}

void terminate(int s)
{
  /* Restore terminal parameters */
  tcsetattr(0, TCSANOW, &old_tty);
  (void) fcntl(0,F_SETFL,old_stdin);
  close(audio);
  close(file);
  exit(0);		
}

void write_wave_header()
{
  /* RIFF fields */
  r_fields.RIFF_id = RIFF_ID;
  r_fields.WAVE_id1 = WAVE_ID1;
  r_fields.WAVE_id2 = WAVE_ID2;
  r_fields.data_ptr = 16;
  r_fields.RIFF_len = 20 + r_fields.data_ptr + data_len;

  /* MicroSoft PCM specific fields */
  s_fields.BitsPerSample = bits;

  /* Common fields */
  c_fields.FormatTag = MS_PCM_FORMAT;
  c_fields.Channels = stereo + 1;
  c_fields.SamplesPerSec = rate;
  c_fields.AvgBytesPerSec =  c_fields.Channels * rate * (bits / 8);
  c_fields.BlockAlign = c_fields.Channels * (bits / 8);

  /* Data chunk */
  data_id = DATA_ID;

  /* Write wave-file header */
  lseek(file, 0L, SEEK_SET);
  write(file, &r_fields, 20);
  write(file, &c_fields, 14);
  write(file, &s_fields, 2);
  write(file, &data_id, sizeof(data_id)); 
  write(file, &data_len, sizeof(data_len)); 
}


int main(int argc, char* argv[])
{
  unsigned int fragment_size;
  char *buffer, *file_name;
  char c;
  int i;

  /* Read parameters */
  if (argc < 2) usage();

  i = 1;
  while ((i < argc) && (argv[i][0] == '-'))
  {
    if (strncmp(argv[i], "-b", 2) == 0)
      bits = atoi(argv[i] + 2);
    else if (strncmp(argv[i], "-s", 2) == 0) 
      stereo = atoi(argv[i] + 2);
    else if (strncmp(argv[i], "-r", 2) == 0) 
      rate = (unsigned int) atol(argv[i] + 2);
    else usage();
    i++;
  }
  if (i == argc) usage();

  file_name = argv[i];

  /* Some sanity checks */
  if ((bits != 8 && bits != 16) || 
      (rate < 4000 || rate > 44100) ||
      (stereo != 0 && stereo != 1))
  {
    fprintf(stderr, "Invalid parameters\n");
    exit(-1);
  }

  /* Open DSP */
  if ((audio = open("/dev/rec", O_RDWR)) < 0) 
  {
    fprintf(stderr, "Cannot open /dev/rec\n");
    exit(-1);
  }

  /* Get maximum fragment size and try to allocate a buffer */
  ioctl(audio, DSPIOMAX, &fragment_size);
  if ((buffer = malloc(fragment_size)) == (char *) 0)
  {
    fprintf(stderr, "Cannot allocate buffer\n");
    exit(-1);
  } 

  /* Set sample parameters */
  ioctl(audio, DSPIOSIZE, &fragment_size); 
  ioctl(audio, DSPIOSTEREO, &stereo); 
  ioctl(audio, DSPIORATE, &rate);
  ioctl(audio, DSPIOBITS, &bits); 
  sign = (bits == 16 ? 1 : 0);
  ioctl(audio, DSPIOSIGN, &sign); 

  /* Create sample file */
  if ((file = creat(file_name, 511)) < 0) 
  {
    fprintf(stderr, "Cannot create %s\n", argv[1]);
    exit(-1);
  } 
  /* Skip wave header */
  lseek(file, (long)(sizeof(r_fields) + 
                     sizeof(c_fields) + 
                     sizeof(s_fields) + 
                     sizeof(data_id) + 
                     sizeof(data_len)), SEEK_SET); 

  printf("\nBits per sample   : %u\n", bits);
  printf("Stereo            : %s\n", (stereo == 1 ? "yes" : "no"));
  printf("Samples per second: %u\n", rate);

  /* Set terminal parameters and remember the old ones */
  tcgetattr(0, &old_tty);
  new_tty = old_tty;
  new_tty.c_lflag &= ~(ICANON|ECHO);
  old_stdin = fcntl(0, F_GETFL);

  /* Catch break signal to be able to restore terminal parameters in case
   * of a user interrupt
   */
  signal(SIGINT, terminate);

  /* Go to non-blocking mode */
  tcsetattr(0, TCSANOW, &new_tty);
  (void) fcntl(0, F_SETFL, old_stdin | O_NONBLOCK);

  printf("\nPress spacebar to start sampling...\n");
  while(!(read(0, &c, 1) == 1 && c == ' '));

  printf("Sampling, press spacebar to stop...\n");
  while(!(read(0, &c, 1) == 1 && c == ' '))
  {
    /* Read sample fragment and write to sample file */
    read(audio, buffer, fragment_size);
    write(file, buffer, fragment_size);
    data_len+= fragment_size;
  }
  printf("%ld bytes sampled. \n\n", data_len);
 
  /* Construct the wave header in front of the raw sample data */
  write_wave_header();
  
  /* Restore terminal parameters and exit */
  terminate(1);
}