Skip to content

Commit c3a1f9a

Browse files
authored
Merge pull request #120 from kakulesza/set-max-volume-from-ipc-for-volume-module
volume: Set max volume value from IPC for volume module
2 parents e92d096 + d0f427c commit c3a1f9a

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/audio/volume.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static uint64_t vol_work(void *data, uint64_t delay)
106106
vol += VOL_RAMP_STEP;
107107

108108
/* ramp completed ? */
109-
if (vol >= cd->tvolume[i] || vol >= VOL_MAX)
109+
if (vol >= cd->tvolume[i] || vol >= cd->max_volume)
110110
vol_update(cd, i);
111111
else {
112112
cd->volume[i] = vol;
@@ -121,7 +121,7 @@ static uint64_t vol_work(void *data, uint64_t delay)
121121
} else {
122122
/* ramp completed ? */
123123
if (new_vol <= cd->tvolume[i] ||
124-
new_vol <= VOL_MIN) {
124+
new_vol <= cd->min_volume) {
125125
vol_update(cd, i);
126126
} else {
127127
cd->volume[i] = new_vol;
@@ -141,6 +141,23 @@ static uint64_t vol_work(void *data, uint64_t delay)
141141
return 0;
142142
}
143143

144+
/**
145+
* \brief Validates and sets minimum and maximum volume levels.
146+
* \details If max_vol < min_vol or it's equals 0 then set max_vol = VOL_MAX
147+
* \param[in,out] cd Volume component private data.
148+
* \param[in] min_vol Minimum volume level
149+
* \param[in] max_vol Maximum volume level
150+
*/
151+
static void vol_set_min_max_levels(struct comp_data *cd,
152+
uint32_t min_vol, uint32_t max_vol)
153+
{
154+
if (max_vol < min_vol || max_vol == 0)
155+
cd->max_volume = VOL_ZERO_DB;
156+
else
157+
cd->max_volume = max_vol;
158+
cd->min_volume = min_vol;
159+
}
160+
144161
/**
145162
* \brief Creates volume component.
146163
* \param[in,out] data Volume base component device.
@@ -177,10 +194,13 @@ static struct comp_dev *volume_new(struct sof_ipc_comp *comp)
177194

178195
/* set the default volumes */
179196
for (i = 0; i < PLATFORM_MAX_CHANNELS; i++) {
180-
cd->volume[i] = VOL_MAX;
181-
cd->tvolume[i] = VOL_MAX;
197+
cd->volume[i] = VOL_ZERO_DB;
198+
cd->tvolume[i] = VOL_ZERO_DB;
182199
}
183200

201+
/* set volume min/max levels */
202+
vol_set_min_max_levels(cd, ipc_vol->min_value, ipc_vol->max_value);
203+
184204
dev->state = COMP_STATE_READY;
185205
return dev;
186206
}
@@ -234,11 +254,11 @@ static inline void volume_set_chan(struct comp_dev *dev, int chan, uint32_t vol)
234254
* multiplication overflow with the 32 bit value. Non-zero MIN option
235255
* can be useful to prevent totally muted small volume gain.
236256
*/
237-
if (v <= VOL_MIN)
238-
v = VOL_MIN;
257+
if (v <= cd->min_volume)
258+
v = cd->min_volume;
239259

240-
if (v > VOL_MAX)
241-
v = VOL_MAX;
260+
if (v > cd->max_volume)
261+
v = cd->max_volume;
242262

243263
cd->tvolume[chan] = v;
244264
}

src/audio/volume.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
*/
9494
#define VOL_MAX (1 << VOL_QXY_Y)
9595

96+
/** \brief Volume 0dB value. */
97+
#define VOL_ZERO_DB (1 << 16)
98+
9699
/** \brief Volume minimum value. */
97100
#define VOL_MIN 0
98101

@@ -109,6 +112,8 @@ struct comp_data {
109112
uint32_t volume[SOF_IPC_MAX_CHANNELS]; /**< current volume */
110113
uint32_t tvolume[SOF_IPC_MAX_CHANNELS]; /**< target volume */
111114
uint32_t mvolume[SOF_IPC_MAX_CHANNELS]; /**< mute volume */
115+
uint32_t min_volume; /**< minimum volume level */
116+
uint32_t max_volume; /**< maximum volume level */
112117
void (*scale_vol)(struct comp_dev *dev, struct comp_buffer *sink,
113118
struct comp_buffer *source); /**< volume processing function */
114119
struct work volwork; /**< volume scheduled work function */

src/include/uapi/ipc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,8 @@ struct sof_ipc_comp_volume {
690690
struct sof_ipc_comp comp;
691691
struct sof_ipc_comp_config config;
692692
uint32_t channels;
693-
int32_t min_value;
694-
int32_t max_value;
693+
uint32_t min_value;
694+
uint32_t max_value;
695695
enum sof_volume_ramp ramp;
696696
uint32_t initial_ramp; /* ramp space in ms */
697697
} __attribute__((packed));

0 commit comments

Comments
 (0)