summaryrefslogtreecommitdiff
path: root/minix/commands/playwave/playwave.c
blob: 3a59e780a179edc8f2b94255e4e698ed6c9adc40 (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
/*   
 *  playwave.c
 *
 *  Play sound files in wave format. Only MicroSoft PCM is supported. 
 *
 *  Michel R. Prevenier.
 */

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

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

/******* 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 definitions *********/


void usage()
{
  fprintf(stderr, "Usage: playwav [-i] file\n");
  exit(-1);
}

int open_audio(unsigned int *fragment_size, unsigned int channels,
	unsigned int samples_per_sec, unsigned int bits)
{
  unsigned int sign;
  int audio;

  /* Open DSP */
  if ((audio = open("/dev/audio", O_RDWR)) < 0)
  {
    printf("Cannot open /dev/audio: %s\n", strerror(errno));
    exit(-1);
  }

  ioctl(audio, DSPIOMAX, fragment_size); /* Get maximum fragment size. */

  /* Set DSP parameters (should check return values..) */
  ioctl(audio, DSPIOSIZE, fragment_size);	/* Use max. fragment size. */
  ioctl(audio, DSPIOSTEREO, &channels);
  ioctl(audio, DSPIORATE, &samples_per_sec);
  ioctl(audio, DSPIOBITS, &bits);
  sign = (bits == 16 ? 1 : 0);
  ioctl(audio, DSPIOSIGN, &sign);
  return audio;
}

int main ( int argc, char *argv[] )
{
  int i, r, audio, file;
  char *buffer, *file_name = NULL;
  unsigned int fragment_size, fragment_size2;
  long data_pos;  
  int showinfo = 0;

  /* Check Parameters */
  if (argc > 2)
  {
    if (strncmp(argv[1], "-i", 2) == 0)
    {
      showinfo = 1;
      file_name = argv[2];
    }
    else
      usage();
  }
  else file_name = argv[1];

  /* Open wav file */
  if((file = open(file_name, O_RDONLY)) < 0)
  {
    printf("Cannot open %s\n", file_name);
    exit(-1);
  }

  /* Check for valid wave format */
  read(file, &r_fields, 20);
  if(r_fields.RIFF_id != RIFF_ID)
  {
      printf("%s not in RIFF format\n", file_name);
      exit(1);
  }
  if(r_fields.WAVE_id1 != WAVE_ID1 || r_fields.WAVE_id2 != WAVE_ID2)
  {
      printf("%s not in WAVE format\n", file_name);
      exit(1);
  }

  /* Store data_chunk position */
  data_pos = lseek(file, 0L, 1) + r_fields.data_ptr;

  /* Read the common and specific fields */
  read(file, &c_fields, 14);
  read(file, &s_fields, 2);

  /* Check for valid wave format, we can only play MicroSoft PCM */
  if(c_fields.FormatTag != MS_PCM_FORMAT)
  {
    printf("%s not in MicroSoft PCM format\n", file_name);
    exit(1);
  }

  /* Open audio device and set DSP parameters */
  audio = open_audio(&fragment_size, c_fields.Channels - 1,
	c_fields.SamplesPerSec, s_fields.BitsPerSample);

  if ((buffer = malloc(fragment_size)) == (char *)0)
  {
    fprintf(stderr, "Cannot allocate buffer\n");
    exit(-1);
  }

  /* Goto data chunk */
  lseek(file, data_pos, SEEK_SET);

  /* Check for valid data chunk */
  read(file, &data_id, sizeof(data_id));
  if(data_id != DATA_ID)
  {
    printf("Invalid data chunk\n");
    exit(1);
  }

  /* Get length of data */
  read(file, &data_len, sizeof(data_len));

  if (showinfo)
  {
    printf("\nBits per sample   : %d \n", s_fields.BitsPerSample);
    printf("Stereo            : %s \n", (c_fields.Channels == 1 ? "yes" : "no"));
    printf("Samples per second: %ld \n", c_fields.SamplesPerSec); 
    printf("Average bytes/sec : %ld \n", c_fields.AvgBytesPerSec);
    printf("Block alignment   : %d \n", c_fields.BlockAlign);
    printf("Datalength (bytes): %ld \n\n", data_len);
  }
    
  /* Play data */
  while(data_len > 0)
  {
    if (data_len > fragment_size) 
    {
      /* Read next fragment */
      read(file, buffer, fragment_size); 
      data_len-= fragment_size;
    }
    else 
    { 
      /* Read until end of file and fill rest of buffer with silence,
       * in PCM this means: fill buffer with last played value
       */
      read(file, buffer, data_len); 
      for (i = data_len; i< fragment_size; i++) 
        buffer[i] = buffer[(int)data_len-1];
      data_len = 0;
    }

    /* Copy data to DSP */
    r= write(audio, buffer,  fragment_size);
    if (r != fragment_size)
    {
	if (r < 0)
	{
		fprintf(stderr, "playwave: write to audio device failed: %s\n",
			strerror(errno));

		/* If we get EIO, the driver might have restarted. Reopen the
		 * audio device.
		 */
		if (errno == EIO) {
			close(audio);
			audio = open_audio(&fragment_size2,
				c_fields.Channels - 1, c_fields.SamplesPerSec,
				s_fields.BitsPerSample);
			if (fragment_size2 != fragment_size) {
			    fprintf(stderr, "Fragment size has changed\n");
			    exit(1);
			}
		}
	}
	else
	{
		fprintf(stderr, "playwave: partial write %d instead of %d\n",
			r, fragment_size);
	}
    }
  }
}