xref: /titanic_50/usr/src/cmd/audio/utilities/AudioTypeSampleRate.cc (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <memory.h>
31 #include <math.h>
32 
33 #include <AudioDebug.h>
34 #include <AudioTypeSampleRate.h>
35 
36 // This is the first stab at a conversion class for Sample Rate conversions
37 
38 // class AudioTypeSampleRate methods
39 
40 // Constructor
41 AudioTypeSampleRate::
AudioTypeSampleRate(int inrate,int outrate)42 AudioTypeSampleRate(int inrate, int outrate) :
43 	resampler(inrate, outrate), input_rate(inrate), output_rate(outrate)
44 {
45 }
46 
47 // Destructor
48 AudioTypeSampleRate::
~AudioTypeSampleRate()49 ~AudioTypeSampleRate()
50 {
51 }
52 
53 // Test conversion possibilities.
54 // Return TRUE if conversion to/from the specified type is possible.
55 Boolean AudioTypeSampleRate::
CanConvert(AudioHdr h) const56 CanConvert(
57 	AudioHdr	h) const		// target header
58 {
59 	if ((input_rate <= 0) || (output_rate <= 0))
60 		return (FALSE);
61 	if ((h.encoding != LINEAR) ||
62 	    ((h.sample_rate != output_rate) && (h.sample_rate != input_rate)) ||
63 	    (h.bytes_per_unit != 2) ||
64 	    (h.channels != 1)) {
65 		return (FALSE);
66 	}
67 	return (TRUE);
68 }
69 
70 
71 // Convert buffer to the specified type
72 // May replace the buffer with a new one, if necessary
73 AudioError AudioTypeSampleRate::
Convert(AudioBuffer * & inbuf,AudioHdr outhdr)74 Convert(
75 	AudioBuffer*&	inbuf,			// data buffer to process
76 	AudioHdr	outhdr)			// target header
77 {
78 	AudioBuffer*	outbuf;
79 	AudioHdr	inhdr;
80 	Double		length;
81 	int		i;
82 	size_t		nsamps;
83 	size_t		insamps;
84 	AudioError	err;
85 
86 	inhdr = inbuf->GetHeader();
87 	length = inbuf->GetLength();
88 
89 	if (Undefined(length)) {
90 		return (AUDIO_ERR_BADARG);
91 	}
92 
93 	// Make sure we're not being asked to do the impossible
94 	// XXX - need a better error code
95 	if ((err = inhdr.Validate()) || (err = outhdr.Validate())) {
96 		return (err);
97 	}
98 
99 	// If the requested conversion is different than what was initially
100 	// established, then return an error.
101 	// XXX - Maybe one day flush and re-init the filter
102 	if ((inhdr.sample_rate != input_rate) ||
103 	    (outhdr.sample_rate != output_rate)) {
104 		return (AUDIO_ERR_BADARG);
105 	}
106 
107 	// If conversion is a no-op, just return success
108 	if (inhdr.sample_rate == outhdr.sample_rate) {
109 		return (AUDIO_SUCCESS);
110 	}
111 
112 	// If nothing in the buffer, do the simple thing
113 	if (length == 0.) {
114 		inbuf->SetHeader(outhdr);
115 		return (AUDIO_SUCCESS);
116 	}
117 
118 	// Add some padding to the output buffer
119 	i = 4 * ((input_rate / output_rate) + (output_rate / input_rate));
120 	length += outhdr.Samples_to_Time(i);
121 
122 	// Allocate a new buffer
123 	outbuf = new AudioBuffer(length, "(SampleRate conversion buffer)");
124 	if (outbuf == 0)
125 		return (AUDIO_UNIXERROR);
126 	if (err = outbuf->SetHeader(outhdr)) {
127 		delete outbuf;
128 		return (err);
129 	}
130 
131 	// here's where the guts go ...
132 	nsamps = resampler.filter((short *)inbuf->GetAddress(),
133 		    (int)inbuf->GetHeader().Time_to_Samples(inbuf->GetLength()),
134 		    (short *)outbuf->GetAddress());
135 
136 	// do a sanity check. did we write more bytes then we had
137 	// available in the output buffer?
138 	insamps = (unsigned int)
139 		outbuf->GetHeader().Time_to_Samples(outbuf->GetSize());
140 
141 	AUDIO_DEBUG((2, "TypeResample: after filter, insamps=%d, outsamps=%d\n",
142 		    insamps, nsamps));
143 
144 	if (nsamps > outbuf->GetHeader().Time_to_Samples(outbuf->GetSize())) {
145 		AudioStderrMsg(outbuf, AUDIO_NOERROR, Fatal,
146 		    (char *)"resample filter corrupted the heap");
147 	}
148 
149 	// set output size appropriately
150 	outbuf->SetLength(outbuf->GetHeader().Samples_to_Time(nsamps));
151 
152 	// This will delete the buffer
153 	inbuf->Reference();
154 	inbuf->Dereference();
155 
156 	inbuf = outbuf;
157 	return (AUDIO_SUCCESS);
158 }
159 
160 AudioError AudioTypeSampleRate::
Flush(AudioBuffer * & outbuf)161 Flush(
162 	AudioBuffer*&	outbuf)
163 {
164 	AudioHdr	h;
165 	Double		pos;
166 	int		nsamp;
167 	size_t		cnt;
168 	AudioError	err;
169 	unsigned char	*tmpbuf;
170 
171 	if (outbuf == NULL)
172 		return (AUDIO_SUCCESS);
173 	h = outbuf->GetHeader();
174 
175 	nsamp = resampler.getFlushSize();
176 	if (nsamp > 0) {
177 		cnt = (size_t)nsamp * h.bytes_per_unit;
178 		tmpbuf = new unsigned char[cnt];
179 
180 		// this does a flush
181 		nsamp = resampler.filter(NULL, 0, (short *)tmpbuf);
182 
183 		// Copy to the supplied buffer
184 		if (nsamp > 0) {
185 			cnt = (size_t)nsamp * h.bytes_per_unit;
186 			pos = outbuf->GetLength();
187 			err = outbuf->AppendData(tmpbuf, cnt, pos);
188 			if (err)
189 				return (err);
190 		}
191 		delete tmpbuf;
192 	}
193 	return (AUDIO_SUCCESS);
194 }
195