summaryrefslogtreecommitdiff
path: root/libao/src/src/ao_au.c
blob: f6aad2d42bdfb79614bdae78e2e430c5df3c1c6b (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 *
 *  ao_au.c
 *
 *      Copyright (C) Wil Mahan - May 2001
 *
 *  This file is part of libao, a cross-platform audio output library.  See
 *  README for a history of this source code.
 *
 *  libao is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  libao is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ********************************************************************

 last mod: $Id: ao_au.c 17718 2010-12-06 20:09:29Z xiphmont $

 ********************************************************************/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifndef _MSC_VER
# include <unistd.h>
#endif
#include <ao/ao.h>
#include <ao/plugin.h>

#define AUDIO_FILE_MAGIC ((uint_32)0x2e736e64)  /* ".snd" */

#define AUDIO_UNKNOWN_SIZE (~0) /* (unsigned) -1 */

/* Format codes (not comprehensive) */
#define AUDIO_FILE_ENCODING_LINEAR_8 (2) /* 8-bit linear PCM */
#define AUDIO_FILE_ENCODING_LINEAR_16 (3) /* 16-bit linear PCM */

#define AU_HEADER_LEN (28)

#define DEFAULT_SWAP_BUFFER_SIZE 2048

/* Write a uint_32 in big-endian order. */
#define WRITE_U32(buf, x) \
	*(buf) = (unsigned char)(((x)>>24)&0xff);\
	*((buf)+1) = (unsigned char)(((x)>>16)&0xff);\
	*((buf)+2) = (unsigned char)(((x)>>8)&0xff);\
	*((buf)+3) = (unsigned char)((x)&0xff);

typedef struct Audio_filehdr {
	uint_32 magic; /* magic number */
	uint_32 hdr_size; /* offset of the data */
	uint_32 data_size; /* length of data (optional) */
	uint_32 encoding; /* data format code */
	uint_32 sample_rate; /* samples per second */
	uint_32 channels; /* number of interleaved channels */
	char info[4]; /* optional text information */
} Audio_filehdr;

static char *ao_au_options[] = {"matrix","verbose","quiet","debug"};
static ao_info ao_au_info =
{
	AO_TYPE_FILE,
	"AU file output",
	"au",
	"Wil Mahan <wtm2@duke.edu>",
	"Sends output to a .au file",
	AO_FMT_BIG,
	0,
	ao_au_options,
        sizeof(ao_au_options)/sizeof(*ao_au_options)
};

typedef struct ao_au_internal
{
	Audio_filehdr au;
} ao_au_internal;


static int ao_au_test(void)
{
	return 1; /* File driver always works */
}


static ao_info *ao_au_driver_info(void)
{
	return &ao_au_info;
}


static int ao_au_device_init(ao_device *device)
{
	ao_au_internal *internal;

	internal = (ao_au_internal *) malloc(sizeof(ao_au_internal));

	if (internal == NULL)
		return 0; /* Could not initialize device memory */

	memset(&(internal->au), 0, sizeof(internal->au));

	device->internal = internal;
        device->output_matrix_order = AO_OUTPUT_MATRIX_FIXED;

	return 1; /* Memory alloc successful */
}


static int ao_au_set_option(ao_device *device, const char *key,
			    const char *value)
{
	return 1; /* No options! */
}


static int ao_au_open(ao_device *device, ao_sample_format *format)
{
	ao_au_internal *internal = (ao_au_internal *) device->internal;
	unsigned char buf[AU_HEADER_LEN];

	/* The AU format is big-endian */
	device->driver_byte_format = AO_FMT_BIG;

	/* Fill out the header */
	internal->au.magic = AUDIO_FILE_MAGIC;
	internal->au.channels = device->output_channels;
	if (format->bits == 8)
		internal->au.encoding = AUDIO_FILE_ENCODING_LINEAR_8;
	else if (format->bits == 16)
		internal->au.encoding = AUDIO_FILE_ENCODING_LINEAR_16;
	else {
		/* Only 8 and 16 bits are supported at present. */
		return 0;
	}
	internal->au.sample_rate = format->rate;
	internal->au.hdr_size = AU_HEADER_LEN;

	/* From the AU specification:  "When audio files are passed
	 * through pipes, the 'data_size' field may not be known in
	 * advance.  In such cases, the 'data_size' should be set
	 * to AUDIO_UNKNOWN_SIZE."
	 */
	internal->au.data_size = AUDIO_UNKNOWN_SIZE;
	/* strncpy(state->au.info, "OGG ", 4); */

	/* Write the header in big-endian order */
	WRITE_U32(buf, internal->au.magic);
	WRITE_U32(buf + 4, internal->au.hdr_size);
	WRITE_U32(buf + 8, internal->au.data_size);
	WRITE_U32(buf + 12, internal->au.encoding);
	WRITE_U32(buf + 16, internal->au.sample_rate);
	WRITE_U32(buf + 20, internal->au.channels);
	strncpy (buf + 24, internal->au.info, 4);

	if (fwrite(buf, sizeof(char), AU_HEADER_LEN, device->file)
	    != AU_HEADER_LEN) {
		return 0; /* Error writing header */
	}

        if(!device->inter_matrix){
          /* set up matrix such that users are warned about > stereo playback */
          if(device->output_channels<=2)
            device->inter_matrix=strdup("L,R");
          //else no matrix, which results in a warning
        }


	return 1;
}


/*
 * play the sample to the already opened file descriptor
 */
static int ao_au_play(ao_device *device, const char *output_samples,
		       uint_32 num_bytes)
{
	if (fwrite(output_samples, sizeof(char), num_bytes,
		   device->file) < num_bytes)
		return 0;
	else
		return 1;
}

static int ao_au_close(ao_device *device)
{
	ao_au_internal *internal = (ao_au_internal *) device->internal;

        off_t size;
	unsigned char buf[4];

	/* Try to find the total file length, including header */
	size = ftell(device->file);

	/* It's not a problem if the lseek() fails; the AU
	 * format does not require a file length.  This is
	 * useful for writing to non-seekable files (e.g.
	 * pipes).
	 */
	if (size > 0) {
		internal->au.data_size = size - AU_HEADER_LEN;

		/* Rewind the file */
		if (fseek(device->file, 8 /* offset of data_size */,
					SEEK_SET) < 0)
		{
			return 1; /* Seek failed; that's okay  */
		}

		/* Fill in the file length */
		WRITE_U32 (buf, internal->au.data_size);
		if (fwrite(buf, sizeof(char), 4, device->file) < 4) {
			return 1; /* Header write failed; that's okay */
		}
	}

	return 1;
}


static void ao_au_device_clear(ao_device *device)
{
	ao_au_internal *internal = (ao_au_internal *) device->internal;

	free(internal);
        device->internal=NULL;
}

ao_functions ao_au = {
	ao_au_test,
	ao_au_driver_info,
	ao_au_device_init,
	ao_au_set_option,
	ao_au_open,
	ao_au_play,
	ao_au_close,
	ao_au_device_clear
};