1 /* Copyright 2018 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 #ifndef INPUT_DATA_H_
7 #define INPUT_DATA_H_
8 
9 #include "cras_dsp_pipeline.h"
10 #include "float_buffer.h"
11 
12 /*
13  * Structure holding the information used when a chunk of input buffer
14  * is accessed by multiple streams with different properties and
15  * processing requirements.
16  * Member:
17  *    ext - Provides interface to read and process buffer in dsp pipeline.
18  *    dev_ptr - Pointer to the associated input iodev.
19  *    area - The audio area used for deinterleaved data copy.
20  *    fbuffer - Floating point buffer from input device.
21  */
22 struct input_data {
23 	struct ext_dsp_module ext;
24 	void *dev_ptr;
25 	struct cras_audio_area *area;
26 	struct float_buffer *fbuffer;
27 };
28 
29 /*
30  * Creates an input_data instance for input iodev.
31  * Args:
32  *    dev_ptr - Pointer to the associated input device.
33  */
34 struct input_data *input_data_create(void *dev_ptr);
35 
36 /* Destroys an input_data instance. */
37 void input_data_destroy(struct input_data **data);
38 
39 /* Sets how many frames in buffer has been read by all input streams. */
40 void input_data_set_all_streams_read(struct input_data *data,
41 				     unsigned int nframes);
42 
43 /*
44  * Gets an audio area for |stream| to read data from. An input_data may be
45  * accessed by multiple streams while some requires processing, the
46  * |offsets| arguments helps track the offset value each stream has read
47  * into |data|.
48  * Args:
49  *    data - The input data to get audio area from.
50  *    stream - The stream that reads data.
51  *    offsets - Structure holding the mapping from stream to the offset value
52  *        of how many frames each stream has read into input buffer.
53  *    area - To be filled with a pointer to an audio area struct for stream to
54  *        read data.
55  *    offset - To be filled with the samples offset in |area| that |stream|
56  *        should start reading.
57  */
58 int input_data_get_for_stream(struct input_data *data,
59 			      struct cras_rstream *stream,
60 			      struct buffer_share *offsets,
61 			      struct cras_audio_area **area,
62 			      unsigned int *offset);
63 
64 /*
65  * Marks |frames| of audio data as read by |stream|.
66  * Args:
67  *    data - The input_data to mark frames has been read by |stream|.
68  *    stream - The stream that has read audio data.
69  *    offsets - Structure holding the mapping from stream to the offset value
70  *        of how many frames each stream has read into input buffer.
71  *    frames - Number of frames |stream| has read.
72  */
73 int input_data_put_for_stream(struct input_data *data,
74 			      struct cras_rstream *stream,
75 			      struct buffer_share *offsets,
76 			      unsigned int frames);
77 
78 /*
79  * The software gain scaler of input path consist of two parts:
80  * (1) The device gain scaler used when lack of hardware gain control.
81  * Configured by the IntrinsicSensitivity label in alsa UCM config.
82  * (2) The gain scaler in cras_rstream set by app, for example the AGC
83  * module in Chrome.
84  * Args:
85  *    data - The input data that holds pointer to APM instance.
86  *    idev_sw_agin_scaler - The gain scaler configured on input iodev.
87  *    stream - To provide stream layer software gain.
88  * Returns:
89  *    1.0 if tuned APM in use, otherwise |iodev gain| * |cras_rstream gain|
90  */
91 float input_data_get_software_gain_scaler(struct input_data *data,
92 					  float idev_sw_gain_scaler,
93 					  struct cras_rstream *stream);
94 
95 #endif /* INPUT_DATA_H_ */
96