source: trunk/minix/commands/ibm/playwave.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.3 KB
Line 
1/*
2 * playwave.c
3 *
4 * Play sound files in wave format. Only MicroSoft PCM is supported.
5 *
6 * Michel R. Prevenier.
7 */
8
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>
19
20_PROTOTYPE( void main, (int argc, char **argv));
21_PROTOTYPE( void usage, (void));
22
23/******* Wave format definitions *********/
24
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
30
31#define WORD short
32#define DWORD unsigned long
33
34struct RIFF_fields
35{
36 DWORD RIFF_id;
37 DWORD RIFF_len;
38 DWORD WAVE_id1;
39 DWORD WAVE_id2;
40 DWORD data_ptr;
41} r_fields;
42
43struct common_fields
44{
45 WORD FormatTag;
46 WORD Channels;
47 DWORD SamplesPerSec;
48 DWORD AvgBytesPerSec;
49 WORD BlockAlign;
50} c_fields;
51
52struct specific_fields
53{
54 WORD BitsPerSample;
55} s_fields;
56
57DWORD data_id;
58DWORD data_len;
59
60/******** End of wave definitions *********/
61
62
63void usage()
64{
65 fprintf(stderr, "Usage: playwav [-i] file\n");
66 exit(-1);
67}
68
69
70void main ( int argc, char *argv[] )
71{
72 int i, audio, file;
73 char *buffer, *file_name;
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;
80
81 /* Check Parameters */
82 if (argc > 2)
83 {
84 if (strncmp(argv[1], "-i", 2) == 0)
85 {
86 showinfo = 1;
87 file_name = argv[2];
88 }
89 else
90 usage();
91 }
92 else file_name = argv[1];
93
94 /* Open DSP */
95 if ((audio = open("/dev/audio", O_RDWR)) < 0)
96 {
97 printf("Cannot open /dev/audio\n");
98 exit(-1);
99 }
100
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)
104 {
105 fprintf(stderr, "Cannot allocate buffer\n");
106 exit(-1);
107 }
108 ioctl(audio, DSPIOSIZE, &fragment_size);
109
110 /* Open wav file */
111 if((file = open(file_name, O_RDONLY)) < 0)
112 {
113 printf("Cannot open %s\n", file_name);
114 exit(-1);
115 }
116
117 /* Check for valid wave format */
118 read(file, &r_fields, 20);
119 if(r_fields.RIFF_id != RIFF_ID)
120 {
121 printf("%s not in RIFF format\n", file_name);
122 exit(1);
123 }
124 if(r_fields.WAVE_id1 != WAVE_ID1 || r_fields.WAVE_id2 != WAVE_ID2)
125 {
126 printf("%s not in WAVE format\n", file_name);
127 exit(1);
128 }
129
130 /* Store data_chunk position */
131 data_pos = lseek(file, 0L, 1) + r_fields.data_ptr;
132
133 /* Read the common and specific fields */
134 read(file, &c_fields, 14);
135 read(file, &s_fields, 2);
136
137 /* Check for valid wave format, we can only play MicroSoft PCM */
138 if(c_fields.FormatTag != MS_PCM_FORMAT)
139 {
140 printf("%s not in MicroSoft PCM format\n", file_name);
141 exit(1);
142 }
143
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);
153
154 /* Goto data chunk */
155 lseek(file, data_pos, SEEK_SET);
156
157 /* Check for valid data chunk */
158 read(file, &data_id, sizeof(data_id));
159 if(data_id != DATA_ID)
160 {
161 printf("Invalid data chunk\n");
162 exit(1);
163 }
164
165 /* Get length of data */
166 read(file, &data_len, sizeof(data_len));
167
168 if (showinfo)
169 {
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);
176 }
177
178 /* Play data */
179 while(data_len > 0)
180 {
181 if (data_len > fragment_size)
182 {
183 /* Read next fragment */
184 read(file, buffer, fragment_size);
185 data_len-= fragment_size;
186 }
187 else
188 {
189 /* Read until end of file and fill rest of buffer with silence,
190 * in PCM this means: fill buffer with last played value
191 */
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;
196 }
197
198 /* Copy data to DSP */
199 write(audio, buffer, fragment_size);
200 }
201}
Note: See TracBrowser for help on using the repository browser.