1 /* Copyright (c) 2014 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 #include <stdlib.h>
7 #include <sys/param.h>
8
9 #include "cras_audio_area.h"
10 #include "cras_audio_format.h"
11 #include "cras_mix.h"
12
cras_audio_area_create(int num_channels)13 struct cras_audio_area *cras_audio_area_create(int num_channels)
14 {
15 struct cras_audio_area *area;
16 size_t sz;
17
18 sz = sizeof(*area) + num_channels * sizeof(struct cras_channel_area);
19 area = calloc(1, sz);
20 area->num_channels = num_channels;
21
22 return area;
23 }
24
cras_audio_area_copy(const struct cras_audio_area * dst,unsigned int dst_offset,const struct cras_audio_format * dst_fmt,const struct cras_audio_area * src,unsigned int src_offset,float software_gain_scaler)25 unsigned int cras_audio_area_copy(const struct cras_audio_area *dst,
26 unsigned int dst_offset,
27 const struct cras_audio_format *dst_fmt,
28 const struct cras_audio_area *src,
29 unsigned int src_offset,
30 float software_gain_scaler)
31 {
32 unsigned int src_idx, dst_idx;
33 unsigned int ncopy;
34 uint8_t *schan, *dchan;
35
36 ncopy = MIN(src->frames - src_offset, dst->frames - dst_offset);
37
38 /* TODO(dgreid) - this replaces a memcpy, it needs to be way faster. */
39 for (src_idx = 0; src_idx < src->num_channels; src_idx++) {
40 for (dst_idx = 0; dst_idx < dst->num_channels; dst_idx++) {
41 if (!(src->channels[src_idx].ch_set &
42 dst->channels[dst_idx].ch_set))
43 continue;
44
45 schan = src->channels[src_idx].buf +
46 src_offset * src->channels[src_idx].step_bytes;
47 dchan = dst->channels[dst_idx].buf +
48 dst_offset * dst->channels[dst_idx].step_bytes;
49
50 cras_mix_add_scale_stride(
51 dst_fmt->format, dchan, schan, ncopy,
52 dst->channels[dst_idx].step_bytes,
53 src->channels[src_idx].step_bytes,
54 software_gain_scaler);
55 }
56 }
57
58 return ncopy;
59 }
60
cras_audio_area_destroy(struct cras_audio_area * area)61 void cras_audio_area_destroy(struct cras_audio_area *area)
62 {
63 free(area);
64 }
65
cras_audio_area_config_channels(struct cras_audio_area * area,const struct cras_audio_format * fmt)66 void cras_audio_area_config_channels(struct cras_audio_area *area,
67 const struct cras_audio_format *fmt)
68 {
69 unsigned int i, ch;
70
71 /* For mono, config the channel type to match both front
72 * left and front right.
73 * TODO(hychao): add more mapping when we have like {FL, FC}
74 * for mono + kb mic.
75 */
76 if ((fmt->num_channels == 1) &&
77 ((fmt->channel_layout[CRAS_CH_FC] == 0) ||
78 (fmt->channel_layout[CRAS_CH_FL] == 0))) {
79 channel_area_set_channel(area->channels, CRAS_CH_FL);
80 channel_area_set_channel(area->channels, CRAS_CH_FR);
81 return;
82 }
83
84 for (i = 0; i < fmt->num_channels; i++) {
85 area->channels[i].ch_set = 0;
86 for (ch = 0; ch < CRAS_CH_MAX; ch++)
87 if (fmt->channel_layout[ch] == i)
88 channel_area_set_channel(&area->channels[i],
89 ch);
90 }
91 }
92
cras_audio_area_config_buf_pointers(struct cras_audio_area * area,const struct cras_audio_format * fmt,uint8_t * base_buffer)93 void cras_audio_area_config_buf_pointers(struct cras_audio_area *area,
94 const struct cras_audio_format *fmt,
95 uint8_t *base_buffer)
96 {
97 int i;
98 const int sample_size = snd_pcm_format_physical_width(fmt->format) / 8;
99
100 /* TODO(dgreid) - assuming interleaved audio here for now. */
101 for (i = 0; i < area->num_channels; i++) {
102 area->channels[i].step_bytes = cras_get_format_bytes(fmt);
103 area->channels[i].buf = base_buffer + i * sample_size;
104 }
105 }
106