17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include <stdio.h>
307c478bdstevel@tonic-gate#include <stdlib.h>
317c478bdstevel@tonic-gate#include <stdarg.h>
327c478bdstevel@tonic-gate#include <string.h>
337c478bdstevel@tonic-gate#include <unistd.h>
347c478bdstevel@tonic-gate#include <sys/types.h>
357c478bdstevel@tonic-gate#include <sys/stat.h>
367c478bdstevel@tonic-gate#include <sys/file.h>
377c478bdstevel@tonic-gate#include <sys/param.h>
387c478bdstevel@tonic-gate#include <Audio.h>
397c478bdstevel@tonic-gate#include <AudioFile.h>
407c478bdstevel@tonic-gate#include <AudioPipe.h>
417c478bdstevel@tonic-gate#include <AudioRawPipe.h>
427c478bdstevel@tonic-gate#include <AudioLib.h>
437c478bdstevel@tonic-gate#include <AudioTypePcm.h>
447c478bdstevel@tonic-gate#include <AudioTypeG72X.h>
457c478bdstevel@tonic-gate#include <AudioTypeChannel.h>
467c478bdstevel@tonic-gate#include <AudioTypeMux.h>
477c478bdstevel@tonic-gate#include <AudioTypeSampleRate.h>
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#include <convert.h>
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate// Maximum sizes of buffer to convert, in seconds and bytes
537c478bdstevel@tonic-gate#define	CVTMAXTIME	((double)5.0)
547c478bdstevel@tonic-gate#define	CVTMAXBUF	(64 * 1024)
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate// maintain a list of conversions
577c478bdstevel@tonic-gatestruct conv_list {
587c478bdstevel@tonic-gate	struct conv_list	*next;	// next conversion in chain
597c478bdstevel@tonic-gate	unsigned		bufcnt;	// number of buffers to process
607c478bdstevel@tonic-gate	AudioTypeConvert*	conv;	// conversion class
617c478bdstevel@tonic-gate	AudioHdr		hdr;	// what to convert to
627c478bdstevel@tonic-gate	char			*desc;	// describe conversion (for errs)
637c478bdstevel@tonic-gate};
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate// check if this is a valid conversion. return -1 if not, 0 if OK.
677c478bdstevel@tonic-gateint
687c478bdstevel@tonic-gateverify_conversion(
697c478bdstevel@tonic-gate	AudioHdr	ihdr,
707c478bdstevel@tonic-gate	AudioHdr	ohdr)
717c478bdstevel@tonic-gate{
727c478bdstevel@tonic-gate	char		*enc1;
737c478bdstevel@tonic-gate	char		*enc2;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	if (((ihdr.encoding != ULAW) &&
767c478bdstevel@tonic-gate	    (ihdr.encoding != ALAW) &&
777c478bdstevel@tonic-gate	    (ihdr.encoding != LINEAR) &&
787c478bdstevel@tonic-gate	    (ihdr.encoding != FLOAT) &&
797c478bdstevel@tonic-gate	    (ihdr.encoding != G721) &&
807c478bdstevel@tonic-gate	    (ihdr.encoding != G723)) ||
817c478bdstevel@tonic-gate	    ((ohdr.encoding != ULAW) &&
827c478bdstevel@tonic-gate	    (ohdr.encoding != ALAW) &&
837c478bdstevel@tonic-gate	    (ohdr.encoding != LINEAR) &&
847c478bdstevel@tonic-gate	    (ohdr.encoding != FLOAT) &&
857c478bdstevel@tonic-gate	    (ohdr.encoding != G721) &&
867c478bdstevel@tonic-gate	    (ohdr.encoding != G723))) {
877c478bdstevel@tonic-gate		enc1 = ihdr.EncodingString();
887c478bdstevel@tonic-gate		enc2 = ohdr.EncodingString();
897c478bdstevel@tonic-gate		Err(MGET("can't convert from %s to %s\n"), enc1, enc2);
907c478bdstevel@tonic-gate		delete enc1;
917c478bdstevel@tonic-gate		delete enc2;
927c478bdstevel@tonic-gate		return (-1);
937c478bdstevel@tonic-gate	}
947c478bdstevel@tonic-gate	return (0);
957c478bdstevel@tonic-gate}
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate// check if this conversion is a no-op
987c478bdstevel@tonic-gateint
997c478bdstevel@tonic-gatenoop_conversion(
1007c478bdstevel@tonic-gate	AudioHdr	ihdr,
1017c478bdstevel@tonic-gate	AudioHdr	ohdr,
1027c478bdstevel@tonic-gate	format_type	i_fmt,
1037c478bdstevel@tonic-gate	format_type	o_fmt,
1047c478bdstevel@tonic-gate	off_t		i_offset,
1057c478bdstevel@tonic-gate	off_t		/* o_offset */)
1067c478bdstevel@tonic-gate{
1077c478bdstevel@tonic-gate	if ((ihdr == ohdr) &&
1087c478bdstevel@tonic-gate	    (i_fmt == o_fmt) &&
1097c478bdstevel@tonic-gate	    (i_offset == 0)) {
1107c478bdstevel@tonic-gate		return (1);
1117c478bdstevel@tonic-gate	}
1127c478bdstevel@tonic-gate	return (0);
1137c478bdstevel@tonic-gate}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gate// Conversion list maintenance routines
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate// Return a pointer to the last conversion entry in the list
1197c478bdstevel@tonic-gatestruct conv_list
1207c478bdstevel@tonic-gate*get_last_conv(
1217c478bdstevel@tonic-gate	struct conv_list	*list)
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate	struct conv_list	*lp;
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate	for (lp = list; lp != NULL; lp = lp->next) {
1267c478bdstevel@tonic-gate		if (lp->next == NULL)
1277c478bdstevel@tonic-gate			break;
1287c478bdstevel@tonic-gate	}
1297c478bdstevel@tonic-gate	return (lp);
1307c478bdstevel@tonic-gate}
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate// Release the conversion list
1337c478bdstevel@tonic-gatevoid
1347c478bdstevel@tonic-gatefree_conv_list(
1357c478bdstevel@tonic-gate	struct conv_list	*&list)
1367c478bdstevel@tonic-gate{
1377c478bdstevel@tonic-gate	unsigned int		i;
1387c478bdstevel@tonic-gate	unsigned int		bufs;
1397c478bdstevel@tonic-gate	struct conv_list	*tlp;
1407c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate	while (list != NULL) {
1437c478bdstevel@tonic-gate		bufs = list->bufcnt;
1447c478bdstevel@tonic-gate		conv = list->conv;
1457c478bdstevel@tonic-gate		for (i = 0; i < bufs; i++) {
1467c478bdstevel@tonic-gate			// Delete the conversion string
1477c478bdstevel@tonic-gate			if (list[i].desc != NULL)
1487c478bdstevel@tonic-gate				free(list[i].desc);
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate			// Delete the conversion class if unique
1517c478bdstevel@tonic-gate			if ((list[i].conv != NULL) &&
1527c478bdstevel@tonic-gate			    ((i == 0) || (list[i].conv != conv)))
1537c478bdstevel@tonic-gate				delete(list[i].conv);
1547c478bdstevel@tonic-gate		}
1557c478bdstevel@tonic-gate		tlp = list->next;
1567c478bdstevel@tonic-gate		free((char *)list);
1577c478bdstevel@tonic-gate		list = tlp;
1587c478bdstevel@tonic-gate	}
1597c478bdstevel@tonic-gate}
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gate// Append a new entry on the end of the conversion list
1627c478bdstevel@tonic-gatevoid
1637c478bdstevel@tonic-gateappend_conv_list(
1647c478bdstevel@tonic-gate	struct conv_list	*&list,	// list to modify
1657c478bdstevel@tonic-gate	AudioHdr		tohdr,	// target format
1667c478bdstevel@tonic-gate	unsigned int		bufs,	// number of buffers involved
1677c478bdstevel@tonic-gate	AudioTypeConvert*	conv,	// NULL, if multiple buffers
1687c478bdstevel@tonic-gate	char			*desc)	// string describing the transform
1697c478bdstevel@tonic-gate{
1707c478bdstevel@tonic-gate	unsigned int		i;
1717c478bdstevel@tonic-gate	struct conv_list	*lp;
1727c478bdstevel@tonic-gate	struct conv_list	*nlp;
1737c478bdstevel@tonic-gate	Boolean			B;
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate	nlp = new struct conv_list[bufs];
1767c478bdstevel@tonic-gate	if (nlp == NULL) {
1777c478bdstevel@tonic-gate		Err(MGET("out of memory\n"));
1787c478bdstevel@tonic-gate		exit(1);
1797c478bdstevel@tonic-gate	}
1807c478bdstevel@tonic-gate	B = tohdr.Validate();
1817c478bdstevel@tonic-gate	// Initialize a conversion entry for each expected buffer
1827c478bdstevel@tonic-gate	for (i = 0; i < bufs; i++) {
1837c478bdstevel@tonic-gate		nlp[i].next = NULL;
1847c478bdstevel@tonic-gate		nlp[i].hdr = tohdr;
1857c478bdstevel@tonic-gate		B = nlp[i].hdr.Validate();
1867c478bdstevel@tonic-gate		nlp[i].bufcnt = bufs;
1877c478bdstevel@tonic-gate		nlp[i].conv = conv;
1887c478bdstevel@tonic-gate		if (desc && *desc) {
1897c478bdstevel@tonic-gate			nlp[i].desc = strdup(desc);
1907c478bdstevel@tonic-gate		} else {
1917c478bdstevel@tonic-gate			nlp[i].desc = NULL;
1927c478bdstevel@tonic-gate		}
1937c478bdstevel@tonic-gate	}
1947c478bdstevel@tonic-gate
1957c478bdstevel@tonic-gate	// Link in the new entry
1967c478bdstevel@tonic-gate	if (list == NULL) {
1977c478bdstevel@tonic-gate		list = nlp;
1987c478bdstevel@tonic-gate	} else {
1997c478bdstevel@tonic-gate		lp = get_last_conv(list);
2007c478bdstevel@tonic-gate		lp->next = nlp;
2017c478bdstevel@tonic-gate	}
2027c478bdstevel@tonic-gate}
2037c478bdstevel@tonic-gate
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate// Routines to establish specific conversions.
2067c478bdstevel@tonic-gate// These routines append the proper conversion to the list, and update
2077c478bdstevel@tonic-gate// the audio header structure to reflect the resulting data format.
2087c478bdstevel@tonic-gate
2097c478bdstevel@tonic-gate// Multiplex/Demultiplex interleaved data
2107c478bdstevel@tonic-gate// If the data is multi-channel, demultiplex into multiple buffer streams.
2117c478bdstevel@tonic-gate// If there are multiple buffers, multiplex back into one interleaved stream.
2127c478bdstevel@tonic-gateAudioError
2137c478bdstevel@tonic-gateadd_mux_convert(
2147c478bdstevel@tonic-gate	struct conv_list	*&list,
2157c478bdstevel@tonic-gate	AudioHdr&		ihdr,
2167c478bdstevel@tonic-gate	unsigned int&		bufs)
2177c478bdstevel@tonic-gate{
2187c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
2197c478bdstevel@tonic-gate	unsigned int		n;
2207c478bdstevel@tonic-gate	char			*msg;
2217c478bdstevel@tonic-gate
2227c478bdstevel@tonic-gate	conv = new AudioTypeMux;
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate	// Verify conversion
2257c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr)) {
2267c478bdstevel@tonic-gateerror:		delete conv;
2277c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
2287c478bdstevel@tonic-gate	}
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate	if (bufs == 1) {
2317c478bdstevel@tonic-gate		// Demultiplex multi-channel data
2327c478bdstevel@tonic-gate		n = ihdr.channels;	// save the target number of buffers
2337c478bdstevel@tonic-gate		ihdr.channels = 1;	// each output buffer will be mono
2347c478bdstevel@tonic-gate		msg = MGET("Split multi-channel data");
2357c478bdstevel@tonic-gate	} else {
2367c478bdstevel@tonic-gate		// Multiplex multiple buffers
2377c478bdstevel@tonic-gate		ihdr.channels = bufs;	// set the target interleave
2387c478bdstevel@tonic-gate		n = 1;
2397c478bdstevel@tonic-gate		bufs = 1;		// just one conversion necessary
2407c478bdstevel@tonic-gate		msg = MGET("Interleave multi-channel data");
2417c478bdstevel@tonic-gate	}
2427c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr))
2437c478bdstevel@tonic-gate		goto error;
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gate	append_conv_list(list, ihdr, bufs, conv, msg);
2467c478bdstevel@tonic-gate	bufs = n;
2477c478bdstevel@tonic-gate	return (AUDIO_SUCCESS);
2487c478bdstevel@tonic-gate}
2497c478bdstevel@tonic-gate
2507c478bdstevel@tonic-gate// Convert to PCM (linear, ulaw, alaw)
2517c478bdstevel@tonic-gateAudioError
2527c478bdstevel@tonic-gateadd_pcm_convert(
2537c478bdstevel@tonic-gate	struct conv_list	*&list,
2547c478bdstevel@tonic-gate	AudioHdr&		ihdr,
2557c478bdstevel@tonic-gate	AudioEncoding		tofmt,
2567c478bdstevel@tonic-gate	unsigned int		unitsz,
2577c478bdstevel@tonic-gate	unsigned int&		bufs)
2587c478bdstevel@tonic-gate{
2597c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
2607c478bdstevel@tonic-gate	char			msg[BUFSIZ];
2617c478bdstevel@tonic-gate	char			*infmt;
2627c478bdstevel@tonic-gate	char			*outfmt;
2637c478bdstevel@tonic-gate	AudioError		err;
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gate	conv = new AudioTypePcm;
2667c478bdstevel@tonic-gate
2677c478bdstevel@tonic-gate	// Verify conversion
2687c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr)) {
2697c478bdstevel@tonic-gateerror:		delete conv;
2707c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
2717c478bdstevel@tonic-gate	}
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate	// Set up conversion, get encoding strings
2747c478bdstevel@tonic-gate	infmt = ihdr.EncodingString();
2757c478bdstevel@tonic-gate	ihdr.encoding = tofmt;
2767c478bdstevel@tonic-gate	ihdr.bytes_per_unit = unitsz;
2777c478bdstevel@tonic-gate	ihdr.samples_per_unit = 1;
2787c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr))
2797c478bdstevel@tonic-gate		goto error;
2807c478bdstevel@tonic-gate	outfmt = ihdr.EncodingString();
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	sprintf(msg, MGET("Convert %s to %s"), infmt, outfmt);
2837c478bdstevel@tonic-gate	delete infmt;
2847c478bdstevel@tonic-gate	delete outfmt;
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate	append_conv_list(list, ihdr, bufs, conv, msg);
2877c478bdstevel@tonic-gate	return (AUDIO_SUCCESS);
2887c478bdstevel@tonic-gate}
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate// Convert multi-channel data to mono, or vice versa
2917c478bdstevel@tonic-gateAudioError
2927c478bdstevel@tonic-gateadd_channel_convert(
2937c478bdstevel@tonic-gate	struct conv_list	*&list,
2947c478bdstevel@tonic-gate	AudioHdr&		ihdr,
2957c478bdstevel@tonic-gate	unsigned int		tochans,
2967c478bdstevel@tonic-gate	unsigned int&		bufs)
2977c478bdstevel@tonic-gate{
2987c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
2997c478bdstevel@tonic-gate	char			msg[BUFSIZ];
3007c478bdstevel@tonic-gate	char			*inchans;
3017c478bdstevel@tonic-gate	char			*outchans;
3027c478bdstevel@tonic-gate	AudioError		err;
3037c478bdstevel@tonic-gate
3047c478bdstevel@tonic-gate	// Make sure we're converting to/from mono with an interleaved buffer
3057c478bdstevel@tonic-gate	if (((ihdr.channels != 1) && (tochans != 1)) || (bufs != 1))
3067c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
3077c478bdstevel@tonic-gate
3087c478bdstevel@tonic-gate	conv = new AudioTypeChannel;
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate	// Verify conversion; if no good, try converting to 16-bit pcm first
3117c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr) || (ihdr.channels != 1)) {
3127c478bdstevel@tonic-gate		if (err = add_pcm_convert(list, ihdr, LINEAR, 2, bufs)) {
3137c478bdstevel@tonic-gate			delete conv;
3147c478bdstevel@tonic-gate			return (err);
3157c478bdstevel@tonic-gate		}
3167c478bdstevel@tonic-gate		if (!conv->CanConvert(ihdr)) {
3177c478bdstevel@tonic-gateerror:			delete conv;
3187c478bdstevel@tonic-gate			return (AUDIO_ERR_FORMATLOCK);
3197c478bdstevel@tonic-gate		}
3207c478bdstevel@tonic-gate	}
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate	// Set up conversion, get channel strings
3237c478bdstevel@tonic-gate	inchans = ihdr.ChannelString();
3247c478bdstevel@tonic-gate	ihdr.channels = tochans;
3257c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr))
3267c478bdstevel@tonic-gate		goto error;
3277c478bdstevel@tonic-gate	outchans = ihdr.ChannelString();
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate	sprintf(msg, MGET("Convert %s to %s"), inchans, outchans);
3307c478bdstevel@tonic-gate	delete inchans;
3317c478bdstevel@tonic-gate	delete outchans;
3327c478bdstevel@tonic-gate
3337c478bdstevel@tonic-gate	append_conv_list(list, ihdr, bufs, conv, msg);
3347c478bdstevel@tonic-gate	return (AUDIO_SUCCESS);
3357c478bdstevel@tonic-gate}
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gate// Compress data
3387c478bdstevel@tonic-gateAudioError
3397c478bdstevel@tonic-gateadd_compress(
3407c478bdstevel@tonic-gate	struct conv_list	*&list,
3417c478bdstevel@tonic-gate	AudioHdr&		ihdr,
3427c478bdstevel@tonic-gate	AudioEncoding		tofmt,
3437c478bdstevel@tonic-gate	unsigned int		unitsz,
3447c478bdstevel@tonic-gate	unsigned int&		bufs)
3457c478bdstevel@tonic-gate{
3467c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
3477c478bdstevel@tonic-gate	char			msg[BUFSIZ];
3487c478bdstevel@tonic-gate	char			*infmt;
3497c478bdstevel@tonic-gate	char			*outfmt;
3507c478bdstevel@tonic-gate	struct conv_list	*lp;
3517c478bdstevel@tonic-gate	int			i;
3527c478bdstevel@tonic-gate	AudioError		err;
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gate	// Make sure we're converting something we understand
3557c478bdstevel@tonic-gate	if ((tofmt != G721) && (tofmt != G723))
3567c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate	conv = new AudioTypeG72X;
3597c478bdstevel@tonic-gate
3607c478bdstevel@tonic-gate	// Verify conversion; if no good, try converting to 16-bit pcm first
3617c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr)) {
3627c478bdstevel@tonic-gate		if (err = add_pcm_convert(list, ihdr, LINEAR, 2, bufs)) {
3637c478bdstevel@tonic-gate			delete conv;
3647c478bdstevel@tonic-gate			return (err);
3657c478bdstevel@tonic-gate		}
3667c478bdstevel@tonic-gate		if (!conv->CanConvert(ihdr)) {
3677c478bdstevel@tonic-gateerror:			delete conv;
3687c478bdstevel@tonic-gate			return (AUDIO_ERR_FORMATLOCK);
3697c478bdstevel@tonic-gate		}
3707c478bdstevel@tonic-gate	}
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	// Set up conversion, get encoding strings
3737c478bdstevel@tonic-gate	infmt = ihdr.EncodingString();
3747c478bdstevel@tonic-gate	ihdr.encoding = tofmt;
3757c478bdstevel@tonic-gate	switch (tofmt) {
3767c478bdstevel@tonic-gate	case G721:
3777c478bdstevel@tonic-gate		ihdr.bytes_per_unit = unitsz;
3787c478bdstevel@tonic-gate		ihdr.samples_per_unit = 2;
3797c478bdstevel@tonic-gate		break;
3807c478bdstevel@tonic-gate	case G723:
3817c478bdstevel@tonic-gate		ihdr.bytes_per_unit = unitsz;
3827c478bdstevel@tonic-gate		ihdr.samples_per_unit = 8;
3837c478bdstevel@tonic-gate		break;
3847c478bdstevel@tonic-gate	}
3857c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr))
3867c478bdstevel@tonic-gate		goto error;
3877c478bdstevel@tonic-gate	outfmt = ihdr.EncodingString();
3887c478bdstevel@tonic-gate
3897c478bdstevel@tonic-gate	sprintf(msg, MGET("Convert %s to %s"), infmt, outfmt);
3907c478bdstevel@tonic-gate	delete infmt;
3917c478bdstevel@tonic-gate	delete outfmt;
3927c478bdstevel@tonic-gate
3937c478bdstevel@tonic-gate	append_conv_list(list, ihdr, bufs, NULL, msg);
3947c478bdstevel@tonic-gate
3957c478bdstevel@tonic-gate	// Need a separate converter instantiation for each channel
3967c478bdstevel@tonic-gate	lp = get_last_conv(list);
3977c478bdstevel@tonic-gate	for (i = 0; i < bufs; i++) {
3987c478bdstevel@tonic-gate		if (i == 0)
3997c478bdstevel@tonic-gate			lp[i].conv = conv;
4007c478bdstevel@tonic-gate		else
4017c478bdstevel@tonic-gate			lp[i].conv = new AudioTypeG72X;
4027c478bdstevel@tonic-gate	}
4037c478bdstevel@tonic-gate	return (AUDIO_SUCCESS);
4047c478bdstevel@tonic-gate}
4057c478bdstevel@tonic-gate
4067c478bdstevel@tonic-gate// Decompress data
4077c478bdstevel@tonic-gateAudioError
4087c478bdstevel@tonic-gateadd_decompress(
4097c478bdstevel@tonic-gate	struct conv_list	*&list,
4107c478bdstevel@tonic-gate	AudioHdr&		ihdr,
4117c478bdstevel@tonic-gate	AudioEncoding		tofmt,
4127c478bdstevel@tonic-gate	unsigned int		unitsz,
4137c478bdstevel@tonic-gate	unsigned int&		bufs)
4147c478bdstevel@tonic-gate{
4157c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
4167c478bdstevel@tonic-gate	char			msg[BUFSIZ];
4177c478bdstevel@tonic-gate	char			*infmt;
4187c478bdstevel@tonic-gate	char			*outfmt;
4197c478bdstevel@tonic-gate	struct conv_list	*lp;
4207c478bdstevel@tonic-gate	int			i;
4217c478bdstevel@tonic-gate	AudioError		err;
4227c478bdstevel@tonic-gate
4237c478bdstevel@tonic-gate	// Make sure we're converting something we understand
4247c478bdstevel@tonic-gate	if ((ihdr.encoding != G721) && (ihdr.encoding != G723))
4257c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate	conv = new AudioTypeG72X;
4287c478bdstevel@tonic-gate
4297c478bdstevel@tonic-gate	// Verify conversion
4307c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr)) {
4317c478bdstevel@tonic-gateerror:		delete conv;
4327c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
4337c478bdstevel@tonic-gate	}
4347c478bdstevel@tonic-gate
4357c478bdstevel@tonic-gate	// Set up conversion, get encoding strings
4367c478bdstevel@tonic-gate	infmt = ihdr.EncodingString();
4377c478bdstevel@tonic-gate	ihdr.encoding = tofmt;
4387c478bdstevel@tonic-gate	ihdr.bytes_per_unit = unitsz;
4397c478bdstevel@tonic-gate	ihdr.samples_per_unit = 1;
4407c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr)) {
4417c478bdstevel@tonic-gate		// Try converting to 16-bit linear
4427c478bdstevel@tonic-gate		ihdr.encoding = LINEAR;
4437c478bdstevel@tonic-gate		ihdr.bytes_per_unit = 2;
4447c478bdstevel@tonic-gate		if (!conv->CanConvert(ihdr))
4457c478bdstevel@tonic-gate			goto error;
4467c478bdstevel@tonic-gate	}
4477c478bdstevel@tonic-gate	outfmt = ihdr.EncodingString();
4487c478bdstevel@tonic-gate
4497c478bdstevel@tonic-gate	sprintf(msg, MGET("Convert %s to %s"), infmt, outfmt);
4507c478bdstevel@tonic-gate	delete infmt;
4517c478bdstevel@tonic-gate	delete outfmt;
4527c478bdstevel@tonic-gate
4537c478bdstevel@tonic-gate	append_conv_list(list, ihdr, bufs, NULL, msg);
4547c478bdstevel@tonic-gate
4557c478bdstevel@tonic-gate	// Need a separate converter instantiation for each channel
4567c478bdstevel@tonic-gate	lp = get_last_conv(list);
4577c478bdstevel@tonic-gate	for (i = 0; i < bufs; i++) {
4587c478bdstevel@tonic-gate		if (i == 0)
4597c478bdstevel@tonic-gate			lp[i].conv = conv;
4607c478bdstevel@tonic-gate		else
4617c478bdstevel@tonic-gate			lp[i].conv = new AudioTypeG72X;
4627c478bdstevel@tonic-gate	}
4637c478bdstevel@tonic-gate	return (AUDIO_SUCCESS);
4647c478bdstevel@tonic-gate}
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate// Sample rate conversion
4677c478bdstevel@tonic-gateAudioError
4687c478bdstevel@tonic-gateadd_rate_convert(
4697c478bdstevel@tonic-gate	struct conv_list	*&list,
4707c478bdstevel@tonic-gate	AudioHdr&		ihdr,
4717c478bdstevel@tonic-gate	unsigned int		torate,
4727c478bdstevel@tonic-gate	unsigned int&		bufs)
4737c478bdstevel@tonic-gate{
4747c478bdstevel@tonic-gate	AudioTypeConvert*	conv;
4757c478bdstevel@tonic-gate	unsigned int		fromrate;
4767c478bdstevel@tonic-gate	char			msg[BUFSIZ];
4777c478bdstevel@tonic-gate	char			*inrate;
4787c478bdstevel@tonic-gate	char			*outrate;
4797c478bdstevel@tonic-gate	struct conv_list	*lp;
4807c478bdstevel@tonic-gate	int			i;
4817c478bdstevel@tonic-gate	AudioError		err;
4827c478bdstevel@tonic-gate
4837c478bdstevel@tonic-gate	fromrate = ihdr.sample_rate;
4847c478bdstevel@tonic-gate	conv = new AudioTypeSampleRate(fromrate, torate);
4857c478bdstevel@tonic-gate
4867c478bdstevel@tonic-gate	// Verify conversion; if no good, try converting to 16-bit pcm first
4877c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr)) {
4887c478bdstevel@tonic-gate		if (err = add_pcm_convert(list, ihdr, LINEAR, 2, bufs)) {
4897c478bdstevel@tonic-gate			delete conv;
4907c478bdstevel@tonic-gate			return (err);
4917c478bdstevel@tonic-gate		}
4927c478bdstevel@tonic-gate		if (!conv->CanConvert(ihdr)) {
4937c478bdstevel@tonic-gateerror:			delete conv;
4947c478bdstevel@tonic-gate			return (AUDIO_ERR_FORMATLOCK);
4957c478bdstevel@tonic-gate		}
4967c478bdstevel@tonic-gate	}
4977c478bdstevel@tonic-gate
4987c478bdstevel@tonic-gate	// Set up conversion, get encoding strings
4997c478bdstevel@tonic-gate	inrate = ihdr.RateString();
5007c478bdstevel@tonic-gate	ihdr.sample_rate = torate;
5017c478bdstevel@tonic-gate	if (!conv->CanConvert(ihdr))
5027c478bdstevel@tonic-gate		goto error;
5037c478bdstevel@tonic-gate	outrate = ihdr.RateString();
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate	sprintf(msg, MGET("Convert %s to %s"), inrate, outrate);
5067c478bdstevel@tonic-gate	delete inrate;
5077c478bdstevel@tonic-gate	delete outrate;
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gate	append_conv_list(list, ihdr, bufs, NULL, msg);
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gate	// Need a separate converter instantiation for each channel
5127c478bdstevel@tonic-gate	lp = get_last_conv(list);
5137c478bdstevel@tonic-gate	for (i = 0; i < bufs; i++) {
5147c478bdstevel@tonic-gate		if (i == 0)
5157c478bdstevel@tonic-gate			lp[i].conv = conv;
5167c478bdstevel@tonic-gate		else
5177c478bdstevel@tonic-gate			lp[i].conv = new AudioTypeSampleRate(fromrate, torate);
5187c478bdstevel@tonic-gate	}
5197c478bdstevel@tonic-gate	return (AUDIO_SUCCESS);
5207c478bdstevel@tonic-gate}
5217c478bdstevel@tonic-gate
5227c478bdstevel@tonic-gate// Returns TRUE if the specified header has a pcm type encoding
5237c478bdstevel@tonic-gateBoolean
5247c478bdstevel@tonic-gatepcmtype(
5257c478bdstevel@tonic-gate	AudioHdr&	hdr)
5267c478bdstevel@tonic-gate{
5277c478bdstevel@tonic-gate	if (hdr.samples_per_unit != 1)
5287c478bdstevel@tonic-gate		return (FALSE);
5297c478bdstevel@tonic-gate	switch (hdr.encoding) {
5307c478bdstevel@tonic-gate	case LINEAR:
5317c478bdstevel@tonic-gate	case FLOAT:
5327c478bdstevel@tonic-gate	case ULAW:
5337c478bdstevel@tonic-gate	case ALAW:
5347c478bdstevel@tonic-gate		return (TRUE);
5357c478bdstevel@tonic-gate	}
5367c478bdstevel@tonic-gate	return (FALSE);
5377c478bdstevel@tonic-gate}
5387c478bdstevel@tonic-gate
5397c478bdstevel@tonic-gate#define	IS_PCM(ihp)		(pcmtype(ihp))
5407c478bdstevel@tonic-gate#define	IS_MONO(ihp)		(ihp.channels == 1)
5417c478bdstevel@tonic-gate#define	RATE_CONV(ihp, ohp)	(ihp.sample_rate != ohp.sample_rate)
5427c478bdstevel@tonic-gate#define	ENC_CONV(ihp, ohp)	((ihp.encoding != ohp.encoding) ||	\
5437c478bdstevel@tonic-gate				    (ihp.samples_per_unit !=		\
5447c478bdstevel@tonic-gate				    ohp.samples_per_unit) ||		\
5457c478bdstevel@tonic-gate				    (ihp.bytes_per_unit != ohp.bytes_per_unit))
5467c478bdstevel@tonic-gate#define	CHAN_CONV(ihp, ohp)	(ihp.channels != ohp.channels)
5477c478bdstevel@tonic-gate
5487c478bdstevel@tonic-gate
5497c478bdstevel@tonic-gate// Build the conversion list to get from input to output format
5507c478bdstevel@tonic-gateAudioError
5517c478bdstevel@tonic-gatebuild_conversion_list(
5527c478bdstevel@tonic-gate	struct conv_list	*&list,
5537c478bdstevel@tonic-gate	AudioStream*		ifp,
5547c478bdstevel@tonic-gate	AudioStream*		ofp)
5557c478bdstevel@tonic-gate{
5567c478bdstevel@tonic-gate	AudioHdr		ihdr;
5577c478bdstevel@tonic-gate	AudioHdr		ohdr;
5587c478bdstevel@tonic-gate	unsigned int		bufs;
5597c478bdstevel@tonic-gate	AudioError		err;
5607c478bdstevel@tonic-gate
5617c478bdstevel@tonic-gate	ihdr = ifp->GetHeader();
5627c478bdstevel@tonic-gate	ohdr = ofp->GetHeader();
5637c478bdstevel@tonic-gate	bufs = 1;
5647c478bdstevel@tonic-gate
5657c478bdstevel@tonic-gate	// Each pass, add another conversion, until there's no more to do
5667c478bdstevel@tonic-gate	while (((ihdr != ohdr) || (bufs != 1)) && !err) {
5677c478bdstevel@tonic-gate
5687c478bdstevel@tonic-gate		// First off, if the target is mono, convert the source to mono
5697c478bdstevel@tonic-gate		// before doing harder stuff, like sample rate conversion.
5707c478bdstevel@tonic-gate		if (IS_MONO(ohdr)) {
5717c478bdstevel@tonic-gate			if (!IS_MONO(ihdr)) {
5727c478bdstevel@tonic-gate				if (IS_PCM(ihdr)) {
5737c478bdstevel@tonic-gate					// If multi-channel pcm,
5747c478bdstevel@tonic-gate					// mix the channels down to one
5757c478bdstevel@tonic-gate					err = add_channel_convert(list,
5767c478bdstevel@tonic-gate					    ihdr, 1, bufs);
5777c478bdstevel@tonic-gate				} else {
5787c478bdstevel@tonic-gate					// If not pcm, demultiplex in order
5797c478bdstevel@tonic-gate					// to decompress
5807c478bdstevel@tonic-gate					err = add_mux_convert(list, ihdr, bufs);
5817c478bdstevel@tonic-gate				}
5827c478bdstevel@tonic-gate				continue;
5837c478bdstevel@tonic-gate			} else if (bufs != 1) {
5847c478bdstevel@tonic-gate				// Multi-channel data was demultiplexed
5857c478bdstevel@tonic-gate				if (IS_PCM(ihdr)) {
5867c478bdstevel@tonic-gate					// If multi-channel pcm, recombine them
5877c478bdstevel@tonic-gate					// for mixing down to one
5887c478bdstevel@tonic-gate					err = add_mux_convert(list, ihdr, bufs);
5897c478bdstevel@tonic-gate				} else {
5907c478bdstevel@tonic-gate					// If not pcm, decompress it
5917c478bdstevel@tonic-gate					err = add_decompress(list, ihdr,
5927c478bdstevel@tonic-gate					    ohdr.encoding, ohdr.bytes_per_unit,
5937c478bdstevel@tonic-gate					    bufs);
5947c478bdstevel@tonic-gate				}
5957c478bdstevel@tonic-gate				continue;
5967c478bdstevel@tonic-gate			}
5977c478bdstevel@tonic-gate			// At this point, input and output are both mono
5987c478bdstevel@tonic-gate
5997c478bdstevel@tonic-gate		} else if (ihdr.channels != 1) {
6007c478bdstevel@tonic-gate			// Here if input and output are both multi-channel.
6017c478bdstevel@tonic-gate			// If sample rate conversion or compression,
6027c478bdstevel@tonic-gate			// split into multiple streams
6037c478bdstevel@tonic-gate			if (RATE_CONV(ihdr, ohdr) ||
6047c478bdstevel@tonic-gate			    (ENC_CONV(ihdr, ohdr) &&
6057c478bdstevel@tonic-gate			    (!IS_PCM(ihdr) || !IS_PCM(ohdr)))) {
6067c478bdstevel@tonic-gate				err = add_mux_convert(list, ihdr, bufs);
6077c478bdstevel@tonic-gate				continue;
6087c478bdstevel@tonic-gate			}
6097c478bdstevel@tonic-gate		}
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate		// Input is either mono, split into multiple buffers, or
6127c478bdstevel@tonic-gate		// this is a conversion that can be handled multi-channel.
6137c478bdstevel@tonic-gate		if (RATE_CONV(ihdr, ohdr)) {
6147c478bdstevel@tonic-gate			// Decompress before sample-rate conversion
6157c478bdstevel@tonic-gate			if (!IS_PCM(ihdr)) {
6167c478bdstevel@tonic-gate				err = add_decompress(list, ihdr,
6177c478bdstevel@tonic-gate				    ohdr.encoding, ohdr.bytes_per_unit,
6187c478bdstevel@tonic-gate				    bufs);
6197c478bdstevel@tonic-gate			} else {
6207c478bdstevel@tonic-gate				err = add_rate_convert(list, ihdr,
6217c478bdstevel@tonic-gate				    ohdr.sample_rate, bufs);
6227c478bdstevel@tonic-gate			}
6237c478bdstevel@tonic-gate			continue;
6247c478bdstevel@tonic-gate		}
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate		if (ENC_CONV(ihdr, ohdr)) {
6277c478bdstevel@tonic-gate			// Encoding is changing:
6287c478bdstevel@tonic-gate			if (!IS_PCM(ihdr)) {
6297c478bdstevel@tonic-gate				// if we start compressed, decompress
6307c478bdstevel@tonic-gate				err = add_decompress(list, ihdr,
6317c478bdstevel@tonic-gate				    ohdr.encoding, ohdr.bytes_per_unit,
6327c478bdstevel@tonic-gate				    bufs);
6337c478bdstevel@tonic-gate			} else if (IS_PCM(ohdr)) {
6347c478bdstevel@tonic-gate				// we should be able to convert to PCM now
6357c478bdstevel@tonic-gate				err = add_pcm_convert(list, ihdr,
6367c478bdstevel@tonic-gate				    ohdr.encoding, ohdr.bytes_per_unit,
6377c478bdstevel@tonic-gate				    bufs);
6387c478bdstevel@tonic-gate			} else {
6397c478bdstevel@tonic-gate				// we should be able to compress now
6407c478bdstevel@tonic-gate				err = add_compress(list, ihdr,
6417c478bdstevel@tonic-gate				    ohdr.encoding, ohdr.bytes_per_unit,
6427c478bdstevel@tonic-gate				    bufs);
6437c478bdstevel@tonic-gate			}
6447c478bdstevel@tonic-gate			continue;
6457c478bdstevel@tonic-gate		}
6467c478bdstevel@tonic-gate
6477c478bdstevel@tonic-gate		// The sample rate and encoding match.
6487c478bdstevel@tonic-gate		// All that's left to do is get the channels right
6497c478bdstevel@tonic-gate		if (bufs > 1) {
6507c478bdstevel@tonic-gate			// Combine channels back into an interleaved stream
6517c478bdstevel@tonic-gate			err = add_mux_convert(list, ihdr, bufs);
6527c478bdstevel@tonic-gate			continue;
6537c478bdstevel@tonic-gate		}
6547c478bdstevel@tonic-gate		if (!IS_MONO(ohdr)) {
6557c478bdstevel@tonic-gate			// If multi-channel output, try to accomodate
6567c478bdstevel@tonic-gate			err = add_channel_convert(list,
6577c478bdstevel@tonic-gate			    ihdr, ohdr.channels, bufs);
6587c478bdstevel@tonic-gate			continue;
6597c478bdstevel@tonic-gate		}
6607c478bdstevel@tonic-gate
6617c478bdstevel@tonic-gate		// Everything should be done at this point.
6627c478bdstevel@tonic-gate		// XXX - this should never be reached
6637c478bdstevel@tonic-gate		return (AUDIO_ERR_FORMATLOCK);
6647c478bdstevel@tonic-gate	}
6657c478bdstevel@tonic-gate	return (err);
6667c478bdstevel@tonic-gate}
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate// Set up the conversion list and execute it
6697c478bdstevel@tonic-gateint
6707c478bdstevel@tonic-gatedo_convert(
6717c478bdstevel@tonic-gate	AudioStream*	ifp,
6727c478bdstevel@tonic-gate	AudioStream*	ofp)
6737c478bdstevel@tonic-gate{
6747c478bdstevel@tonic-gate	struct conv_list *list = NULL;
6757c478bdstevel@tonic-gate	struct conv_list *lp;
6767c478bdstevel@tonic-gate	AudioBuffer* 	obuf;
6777c478bdstevel@tonic-gate	AudioBuffer** 	multibuf;
6787c478bdstevel@tonic-gate	AudioError	err;
6797c478bdstevel@tonic-gate	AudioHdr	ihdr;
6807c478bdstevel@tonic-gate	AudioHdr	ohdr;
6817c478bdstevel@tonic-gate	Double		pos = 0.0;
6827c478bdstevel@tonic-gate	size_t		len;
6837c478bdstevel@tonic-gate	unsigned int	i;
6847c478bdstevel@tonic-gate	Double		cvtlen;
6857c478bdstevel@tonic-gate	char		*msg1;
6867c478bdstevel@tonic-gate	char		*msg2;
6877c478bdstevel@tonic-gate
6887c478bdstevel@tonic-gate	ihdr = ifp->GetHeader();
6897c478bdstevel@tonic-gate	ohdr = ofp->GetHeader();
6907c478bdstevel@tonic-gate
6917c478bdstevel@tonic-gate	// create conversion list
6927c478bdstevel@tonic-gate	if ((err = build_conversion_list(list, ifp, ofp)) != AUDIO_SUCCESS) {
6937c478bdstevel@tonic-gate		free_conv_list(list);
6947c478bdstevel@tonic-gate		msg1 = ohdr.FormatString();
6957c478bdstevel@tonic-gate		Err(MGET("Cannot convert %s to %s\n"), ifp->GetName(), msg1);
6967c478bdstevel@tonic-gate		delete msg1;
6977c478bdstevel@tonic-gate		return (-1);
6987c478bdstevel@tonic-gate	}
6997c478bdstevel@tonic-gate
7007c478bdstevel@tonic-gate	// Print warnings for exceptional conditions
7017c478bdstevel@tonic-gate	if ((ohdr.sample_rate < 8000) || (ohdr.sample_rate > 48000)) {
7027c478bdstevel@tonic-gate		msg1 = ohdr.RateString();
7037c478bdstevel@tonic-gate		Err(MGET("Warning: converting %s to %s\n"),
7047c478bdstevel@tonic-gate		    ifp->GetName(), msg1);
7057c478bdstevel@tonic-gate		delete msg1;
7067c478bdstevel@tonic-gate	}
7077c478bdstevel@tonic-gate	if (ohdr.channels > 2) {
7087c478bdstevel@tonic-gate		msg1 = ohdr.ChannelString();
7097c478bdstevel@tonic-gate		Err(MGET("Warning: converting %s to %s\n"),
7107c478bdstevel@tonic-gate		    ifp->GetName(), msg1);
7117c478bdstevel@tonic-gate		delete msg1;
7127c478bdstevel@tonic-gate	}
7137c478bdstevel@tonic-gate
7147c478bdstevel@tonic-gate	if (Debug) {
7157c478bdstevel@tonic-gate		msg1 = ihdr.FormatString();
7167c478bdstevel@tonic-gate		msg2 = ohdr.FormatString();
7177c478bdstevel@tonic-gate		Err(MGET("Converting %s:\n\t\tfrom: %s\n\t\tto: %s\n"),
7187c478bdstevel@tonic-gate		    ifp->GetName(), msg1, msg2);
7197c478bdstevel@tonic-gate		delete msg1;
7207c478bdstevel@tonic-gate		delete msg2;
7217c478bdstevel@tonic-gate
7227c478bdstevel@tonic-gate		// Print each entry in the conversion list
7237c478bdstevel@tonic-gate		for (lp = list; lp; lp = lp->next) {
7247c478bdstevel@tonic-gate			(void) fprintf(stderr, MGET("\t%s  %s\n"), lp->desc,
7257c478bdstevel@tonic-gate			    (lp->bufcnt == 1) ? "" : MGET("(multi-channel)"));
7267c478bdstevel@tonic-gate		}
7277c478bdstevel@tonic-gate	}
7287c478bdstevel@tonic-gate
7297c478bdstevel@tonic-gate	// Calculate buffer size, obeying maximums
7307c478bdstevel@tonic-gate	cvtlen = ihdr.Bytes_to_Time(CVTMAXBUF);
7317c478bdstevel@tonic-gate	if (cvtlen > CVTMAXTIME)
7327c478bdstevel@tonic-gate		cvtlen = CVTMAXTIME;
7337c478bdstevel@tonic-gate	if (cvtlen > ohdr.Bytes_to_Time(CVTMAXBUF * 4))
7347c478bdstevel@tonic-gate		cvtlen = ohdr.Bytes_to_Time(CVTMAXBUF * 4);
7357c478bdstevel@tonic-gate
7367c478bdstevel@tonic-gate	// create output buf
7377c478bdstevel@tonic-gate	if (!(obuf = new AudioBuffer(cvtlen, MGET("Audio Convert Buffer")))) {
7387c478bdstevel@tonic-gate		Err(MGET("Can't create conversion buffer\n"));
7397c478bdstevel@tonic-gate		exit(1);
7407c478bdstevel@tonic-gate	}
7417c478bdstevel@tonic-gate
7427c478bdstevel@tonic-gate	while (1) {
7437c478bdstevel@tonic-gate		// Reset length
7447c478bdstevel@tonic-gate		len = (size_t)ihdr.Time_to_Bytes(cvtlen);
7457c478bdstevel@tonic-gate		if ((err = obuf->SetHeader(ihdr)) != AUDIO_SUCCESS) {
7467c478bdstevel@tonic-gate			Err(MGET("Can't set buffer header: %s\n"), err.msg());
7477c478bdstevel@tonic-gate			return (-1);
7487c478bdstevel@tonic-gate		}
7497c478bdstevel@tonic-gate		// If growing buffer, free the old one rather than copy data
7507c478bdstevel@tonic-gate		if (obuf->GetSize() < cvtlen)
7517c478bdstevel@tonic-gate			obuf->SetSize(0.);
7527c478bdstevel@tonic-gate		obuf->SetSize(cvtlen);
7537c478bdstevel@tonic-gate
7547c478bdstevel@tonic-gate		// Read a chunk of input and set the real length of buffer
7557c478bdstevel@tonic-gate		// XXX - Use Copy() method??  Check for errors?
7567c478bdstevel@tonic-gate		if (err = ifp->ReadData(obuf->GetAddress(), len, pos))
7577c478bdstevel@tonic-gate			break;
7587c478bdstevel@tonic-gate		obuf->SetLength(ihdr.Bytes_to_Time(len));
7597c478bdstevel@tonic-gate
7607c478bdstevel@tonic-gate		// Process each entry in the conversion list
7617c478bdstevel@tonic-gate		for (lp = list; lp; lp = lp->next) {
7627c478bdstevel@tonic-gate			if (lp->conv) {
7637c478bdstevel@tonic-gate				// If multiple buffers, make multiple calls
7647c478bdstevel@tonic-gate				if (lp->bufcnt == 1) {
7657c478bdstevel@tonic-gate					err = lp->conv->Convert(obuf, lp->hdr);
7667c478bdstevel@tonic-gate				} else {
7677c478bdstevel@tonic-gate					multibuf = (AudioBuffer**)obuf;
7687c478bdstevel@tonic-gate					for (i = 0; i < lp->bufcnt; i++) {
7697c478bdstevel@tonic-gate						err = lp[i].conv->Convert(
7707c478bdstevel@tonic-gate						    multibuf[i], lp[i].hdr);
7717c478bdstevel@tonic-gate						if (err)
7727c478bdstevel@tonic-gate							break;
7737c478bdstevel@tonic-gate					}
7747c478bdstevel@tonic-gate				}
7757c478bdstevel@tonic-gate				if (err) {
7767c478bdstevel@tonic-gate					Err(MGET(
7777c478bdstevel@tonic-gate					    "Conversion failed: %s (%s)\n"),
7787c478bdstevel@tonic-gate					    lp->desc ? lp->desc : MGET("???"),
7797c478bdstevel@tonic-gate					    err.msg());
7807c478bdstevel@tonic-gate					return (-1);
7817c478bdstevel@tonic-gate				}
7827c478bdstevel@tonic-gate			}
7837c478bdstevel@tonic-gate		}
7847c478bdstevel@tonic-gate
7857c478bdstevel@tonic-gate		if ((err = write_output(obuf, ofp)) != AUDIO_SUCCESS) {
7867c478bdstevel@tonic-gate			Err(MGET("Error writing to output file %s (%s)\n"),
7877c478bdstevel@tonic-gate			    ofp->GetName(), err.msg());
7887c478bdstevel@tonic-gate			return (-1);
7897c478bdstevel@tonic-gate		}
7907c478bdstevel@tonic-gate	}
7917c478bdstevel@tonic-gate
7927c478bdstevel@tonic-gate	// Now flush any left overs from conversions w/state
7937c478bdstevel@tonic-gate	obuf->SetLength(0.0);
7947c478bdstevel@tonic-gate	for (lp = list; lp; lp = lp->next) {
7957c478bdstevel@tonic-gate		if (lp->conv) {
7967c478bdstevel@tonic-gate			// First check if there's any residual to convert.
7977c478bdstevel@tonic-gate			// If not, just set the header to this type.
7987c478bdstevel@tonic-gate			// If multiple buffers, make multiple calls
7997c478bdstevel@tonic-gate			if (lp->bufcnt == 1) {
8007c478bdstevel@tonic-gate				err = lp->conv->Convert(obuf, lp->hdr);
8017c478bdstevel@tonic-gate				if (!err)
8027c478bdstevel@tonic-gate					err = lp->conv->Flush(obuf);
8037c478bdstevel@tonic-gate			} else {
8047c478bdstevel@tonic-gate				multibuf = (AudioBuffer**)obuf;
8057c478bdstevel@tonic-gate				for (i = 0; i < lp->bufcnt; i++) {
8067c478bdstevel@tonic-gate					err = lp[i].conv->Convert(
8077c478bdstevel@tonic-gate					    multibuf[i], lp[i].hdr);
8087c478bdstevel@tonic-gate					if (!err) {
8097c478bdstevel@tonic-gate						err = lp[i].conv->Flush(
8107c478bdstevel@tonic-gate						    multibuf[i]);
8117c478bdstevel@tonic-gate					}
8127c478bdstevel@tonic-gate					if (err)
8137c478bdstevel@tonic-gate						break;
8147c478bdstevel@tonic-gate				}
8157c478bdstevel@tonic-gate			}
8167c478bdstevel@tonic-gate			if (err) {
8177c478bdstevel@tonic-gate				Err(MGET(
8187c478bdstevel@tonic-gate				    "Warning: Flush of final bytes failed: "
8197c478bdstevel@tonic-gate				    "%s (%s)\n"),
8207c478bdstevel@tonic-gate				    lp->desc ? lp->desc : MGET("???"),
8217c478bdstevel@tonic-gate				    err.msg());
8227c478bdstevel@tonic-gate
8237c478bdstevel@tonic-gate				/* return (-1); ignore errors for now */
8247c478bdstevel@tonic-gate				break;
8257c478bdstevel@tonic-gate			}
8267c478bdstevel@tonic-gate		}
8277c478bdstevel@tonic-gate	}
8287c478bdstevel@tonic-gate
8297c478bdstevel@tonic-gate	if (obuf->GetLength() > 0.0) {
8307c478bdstevel@tonic-gate		if ((err = write_output(obuf, ofp)) != AUDIO_SUCCESS) {
8317c478bdstevel@tonic-gate			Err(MGET("Warning: Final write to %s failed (%s)\n"),
8327c478bdstevel@tonic-gate			    ofp->GetName(), err.msg());
8337c478bdstevel@tonic-gate			/* return (-1); ignore errors for now */
8347c478bdstevel@tonic-gate		}
8357c478bdstevel@tonic-gate	}
8367c478bdstevel@tonic-gate
8377c478bdstevel@tonic-gate	delete obuf;
8387c478bdstevel@tonic-gate	free_conv_list(list);
8397c478bdstevel@tonic-gate	return (0);
8407c478bdstevel@tonic-gate}
841