1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 /*
7  * Used to convert from one audio format to another.  Currently only supports
8  * sample rate conversion with the speex backend.
9  */
10 #ifndef CRAS_FMT_CONV_H_
11 #define CRAS_FMT_CONV_H_
12 
13 #include <stdint.h>
14 #include <stdlib.h>
15 
16 #include "cras_types.h"
17 
18 struct cras_audio_format;
19 struct cras_fmt_conv;
20 
21 /* Create and destroy format converters. */
22 struct cras_fmt_conv *cras_fmt_conv_create(const struct cras_audio_format *in,
23 					   const struct cras_audio_format *out,
24 					   size_t max_frames,
25 					   size_t pre_linear_resample);
26 void cras_fmt_conv_destroy(struct cras_fmt_conv *conv);
27 
28 /* Creates the format converter for channel remixing. The conversion takes
29  * a N by N float matrix, to multiply each N-channels sample.
30  * Args:
31  *    num_channels - Number of channels of PCM data.
32  *    coefficient - Float array of length N * N representing the conversion
33  *        matrix, where matrix[i][j] corresponds to coefficient[i * N + j]
34  */
35 struct cras_fmt_conv *cras_channel_remix_conv_create(
36 		unsigned int num_channels,
37 		const float *coefficient);
38 
39 /* Converts nframes of sample from in_buf, using given remix converter.
40  * Args:
41  *    conv - The format converter.
42  *    fmt - The format of the buffer to convert.
43  *    in_buf - The buffer to convert.
44  *    nframes - The number of frames to convert.
45  */
46 void cras_channel_remix_convert(struct cras_fmt_conv *conv,
47 				const struct cras_audio_format *fmt,
48 				uint8_t *in_buf,
49 				size_t nframes);
50 
51 /* Get the input format of the converter. */
52 const struct cras_audio_format *cras_fmt_conv_in_format(
53 		const struct cras_fmt_conv *conv);
54 
55 /* Get the output format of the converter. */
56 const struct cras_audio_format *cras_fmt_conv_out_format(
57 		const struct cras_fmt_conv *conv);
58 
59 /* Get the number of output frames that will result from converting in_frames */
60 size_t cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv *conv,
61 				      size_t in_frames);
62 /* Get the number of input frames that will result from converting out_frames */
63 size_t cras_fmt_conv_out_frames_to_in(struct cras_fmt_conv *conv,
64 				      size_t out_frames);
65 /* Sets the input and output rate to the linear resampler. */
66 void cras_fmt_conv_set_linear_resample_rates(struct cras_fmt_conv *conv,
67 					     float from,
68 					     float to);
69 /* Converts in_frames samples from in_buf, storing the results in out_buf.
70  * Args:
71  *    conv - The format converter returned from cras_fmt_conv_create().
72  *    in_buf - Samples to convert.
73  *    out_buf - Converted samples are placed here.
74  *    in_frames - Number of frames from in_buf to convert.
75  *    out_frames - Maximum number of frames to store in out_buf.  If there isn't
76  *      any format conversion, out_frames must be >= in_frames.  When doing
77  *      format conversion out_frames should be able to hold all the converted
78  *      frames, this can be checked with cras_fmt_conv_in_frames_to_out().
79  * Return number of frames put in out_buf. */
80 size_t cras_fmt_conv_convert_frames(struct cras_fmt_conv *conv,
81 				    const uint8_t *in_buf,
82 				    uint8_t *out_buf,
83 				    unsigned int *in_frames,
84 				    size_t out_frames);
85 
86 /* Checks if format conversion is needed for a fmt converter.
87  * Args:
88  *    conv - The format convert to check.
89  *  Returns:
90  *    Non-zero if a format conversion is needed.
91  */
92 int cras_fmt_conversion_needed(const struct cras_fmt_conv *conv);
93 
94 /* If the server cannot provide the requested format, configures an audio format
95  * converter that handles transforming the input format to the format used by
96  * the server.
97  * Args:
98  *    conv - filled with the new converter if needed.
99  *    dir - the stream direction the new converter used for.
100  *    from - Format to convert from.
101  *    to - Format to convert to.
102  *    frames - size of buffer.
103  */
104 int config_format_converter(struct cras_fmt_conv **conv,
105 			    enum CRAS_STREAM_DIRECTION dir,
106 			    const struct cras_audio_format *from,
107 			    const struct cras_audio_format *to,
108 			    unsigned int frames);
109 
110 #endif /* CRAS_FMT_CONV_H_ */
111