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(unsigned int num_channels, 36 const float *coefficient); 37 38 /* Converts nframes of sample from in_buf, using given remix converter. 39 * Args: 40 * conv - The format converter. 41 * fmt - The format of the buffer to convert. 42 * in_buf - The buffer to convert. 43 * nframes - The number of frames to convert. 44 */ 45 void cras_channel_remix_convert(struct cras_fmt_conv *conv, 46 const struct cras_audio_format *fmt, 47 uint8_t *in_buf, size_t nframes); 48 49 /* Get the input format of the converter. */ 50 const struct cras_audio_format * 51 cras_fmt_conv_in_format(const struct cras_fmt_conv *conv); 52 53 /* Get the output format of the converter. */ 54 const struct cras_audio_format * 55 cras_fmt_conv_out_format(const struct cras_fmt_conv *conv); 56 57 /* Get the number of output frames that will result from converting in_frames */ 58 size_t cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv *conv, 59 size_t in_frames); 60 /* Get the number of input frames that will result from converting out_frames */ 61 size_t cras_fmt_conv_out_frames_to_in(struct cras_fmt_conv *conv, 62 size_t out_frames); 63 /* Sets the input and output rate to the linear resampler. */ 64 void cras_fmt_conv_set_linear_resample_rates(struct cras_fmt_conv *conv, 65 float from, float to); 66 /* Converts in_frames samples from in_buf, storing the results in out_buf. 67 * Args: 68 * conv - The format converter returned from cras_fmt_conv_create(). 69 * in_buf - Samples to convert. 70 * out_buf - Converted samples are placed here. 71 * in_frames - Number of frames from in_buf to convert. 72 * out_frames - Maximum number of frames to store in out_buf. If there isn't 73 * any format conversion, out_frames must be >= in_frames. When doing 74 * format conversion out_frames should be able to hold all the converted 75 * frames, this can be checked with cras_fmt_conv_in_frames_to_out(). 76 * Return number of frames put in out_buf. */ 77 size_t cras_fmt_conv_convert_frames(struct cras_fmt_conv *conv, 78 const uint8_t *in_buf, uint8_t *out_buf, 79 unsigned int *in_frames, size_t out_frames); 80 81 /* Checks if format conversion is needed for a fmt converter. 82 * Args: 83 * conv - The format convert to check. 84 * Returns: 85 * Non-zero if a format conversion is needed. 86 */ 87 int cras_fmt_conversion_needed(const struct cras_fmt_conv *conv); 88 89 /* If the server cannot provide the requested format, configures an audio format 90 * converter that handles transforming the input format to the format used by 91 * the server. 92 * Args: 93 * conv - filled with the new converter if needed. 94 * dir - the stream direction the new converter used for. 95 * from - Format to convert from. 96 * to - Format to convert to. 97 * frames - size of buffer. 98 */ 99 int config_format_converter(struct cras_fmt_conv **conv, 100 enum CRAS_STREAM_DIRECTION dir, 101 const struct cras_audio_format *from, 102 const struct cras_audio_format *to, 103 unsigned int frames); 104 105 #endif /* CRAS_FMT_CONV_H_ */ 106