release.sh: allow REPO and GITBRANCH env override
[minix.git] / commands / playwave / playwave.c
blob53b92d8dc5f9cc4a303bdbdb175044900b7a763b
1 /*
2 * playwave.c
4 * Play sound files in wave format. Only MicroSoft PCM is supported.
6 * Michel R. Prevenier.
7 */
9 #include <sys/types.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <minix/sound.h>
20 int main(int argc, char **argv);
21 void usage(void);
23 /******* Wave format definitions *********/
25 #define RIFF_ID 0x46464952
26 #define WAVE_ID1 0x45564157
27 #define WAVE_ID2 0x20746D66
28 #define DATA_ID 0x61746164
29 #define MS_PCM_FORMAT 0x0001
31 #define WORD short
32 #define DWORD unsigned long
34 struct RIFF_fields
36 DWORD RIFF_id;
37 DWORD RIFF_len;
38 DWORD WAVE_id1;
39 DWORD WAVE_id2;
40 DWORD data_ptr;
41 } r_fields;
43 struct common_fields
45 WORD FormatTag;
46 WORD Channels;
47 DWORD SamplesPerSec;
48 DWORD AvgBytesPerSec;
49 WORD BlockAlign;
50 } c_fields;
52 struct specific_fields
54 WORD BitsPerSample;
55 } s_fields;
57 DWORD data_id;
58 DWORD data_len;
60 /******** End of wave definitions *********/
63 void usage()
65 fprintf(stderr, "Usage: playwav [-i] file\n");
66 exit(-1);
70 int main ( int argc, char *argv[] )
72 int i, r, audio, file;
73 char *buffer, *file_name = NULL;
74 unsigned int sign;
75 unsigned int fragment_size;
76 unsigned int channels;
77 unsigned int bits;
78 long data_pos;
79 int showinfo = 0;
81 /* Check Parameters */
82 if (argc > 2)
84 if (strncmp(argv[1], "-i", 2) == 0)
86 showinfo = 1;
87 file_name = argv[2];
89 else
90 usage();
92 else file_name = argv[1];
94 /* Open DSP */
95 if ((audio = open("/dev/audio", O_RDWR | O_REOPEN)) < 0)
97 printf("Cannot open /dev/audio: %s\n", strerror(errno));
98 exit(-1);
101 /* Get maximum fragment size and try to allocate a buffer */
102 ioctl(audio, DSPIOMAX, &fragment_size);
103 if ((buffer = malloc(fragment_size)) == (char *)0)
105 fprintf(stderr, "Cannot allocate buffer\n");
106 exit(-1);
108 ioctl(audio, DSPIOSIZE, &fragment_size);
110 /* Open wav file */
111 if((file = open(file_name, O_RDONLY)) < 0)
113 printf("Cannot open %s\n", file_name);
114 exit(-1);
117 /* Check for valid wave format */
118 read(file, &r_fields, 20);
119 if(r_fields.RIFF_id != RIFF_ID)
121 printf("%s not in RIFF format\n", file_name);
122 exit(1);
124 if(r_fields.WAVE_id1 != WAVE_ID1 || r_fields.WAVE_id2 != WAVE_ID2)
126 printf("%s not in WAVE format\n", file_name);
127 exit(1);
130 /* Store data_chunk position */
131 data_pos = lseek(file, 0L, 1) + r_fields.data_ptr;
133 /* Read the common and specific fields */
134 read(file, &c_fields, 14);
135 read(file, &s_fields, 2);
137 /* Check for valid wave format, we can only play MicroSoft PCM */
138 if(c_fields.FormatTag != MS_PCM_FORMAT)
140 printf("%s not in MicroSoft PCM format\n", file_name);
141 exit(1);
144 /* Set DSP parameters */
145 channels = c_fields.Channels;
146 channels--;
147 bits = s_fields.BitsPerSample;
148 ioctl(audio, DSPIOSTEREO, &channels);
149 ioctl(audio, DSPIORATE, &c_fields.SamplesPerSec);
150 ioctl(audio, DSPIOBITS, &bits);
151 sign = (bits == 16 ? 1 : 0);
152 ioctl(audio, DSPIOSIGN, &sign);
154 /* Goto data chunk */
155 lseek(file, data_pos, SEEK_SET);
157 /* Check for valid data chunk */
158 read(file, &data_id, sizeof(data_id));
159 if(data_id != DATA_ID)
161 printf("Invalid data chunk\n");
162 exit(1);
165 /* Get length of data */
166 read(file, &data_len, sizeof(data_len));
168 if (showinfo)
170 printf("\nBits per sample : %d \n", s_fields.BitsPerSample);
171 printf("Stereo : %s \n", (c_fields.Channels == 1 ? "yes" : "no"));
172 printf("Samples per second: %ld \n", c_fields.SamplesPerSec);
173 printf("Average bytes/sec : %ld \n", c_fields.AvgBytesPerSec);
174 printf("Block alignment : %d \n", c_fields.BlockAlign);
175 printf("Datalength (bytes): %ld \n\n", data_len);
178 /* Play data */
179 while(data_len > 0)
181 if (data_len > fragment_size)
183 /* Read next fragment */
184 read(file, buffer, fragment_size);
185 data_len-= fragment_size;
187 else
189 /* Read until end of file and fill rest of buffer with silence,
190 * in PCM this means: fill buffer with last played value
192 read(file, buffer, data_len);
193 for (i = data_len; i< fragment_size; i++)
194 buffer[i] = buffer[(int)data_len-1];
195 data_len = 0;
198 /* Copy data to DSP */
199 r= write(audio, buffer, fragment_size);
200 if (r != fragment_size)
202 if (r < 0)
204 fprintf(stderr, "playwave: write to audio device failed: %s\n",
205 strerror(errno));
207 else
209 fprintf(stderr, "playwave: partial write %d instead of %d\n",
210 r, fragment_size);