Android内核进阶之pcm硬件参数最小约束值snd_pcm_hw_param_first:用法实例(八十七)
简介: CSDN博客专家、《Android系统多媒体进阶实战》作者
博主新书推荐:《Android系统多媒体进阶实战》🚀
Android Audio工程师专栏地址: Audio工程师进阶系列【原创干货持续更新中……】🚀
Android多媒体专栏地址: 多媒体系统工程师系列【原创干货持续更新中……】🚀
推荐1:车载系统实战课地址:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀
推荐2:HIDL与AIDL实战课地址:Android14 Binder之HIDL与AIDL通信实战课 🚀
推荐3:Android15音效实战课地址:Android15快速自定义与集成音效实战课 🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

🍉🍉🍉文章目录🍉🍉🍉
- 🌻1. 前言
- 🌻2. Android内核进阶之pcm硬件参数最小约束值snd_pcm_hw_param_first介绍
- 🌻3. 代码实例
- 🌻3.1 探测最小可用采样率
- 🌻3.2 探测最小通道数
- 🌻3.3 探测最小周期尺寸
- 🌻3.4 用法总结
🌻1. 前言
本篇目的:Android内核进阶之pcm硬件参数最小约束值snd_pcm_hw_param_first:用法实例
🌻2. Android内核进阶之pcm硬件参数最小约束值snd_pcm_hw_param_first介绍
-
基本概念
snd_pcm_hw_param_first用于查询某一硬件参数在当前约束下的最小可能值,是驱动在hw_params阶段做自适应配置时的首选探测接口。 -
功能
支持RATE、CHANNELS、FORMAT、PERIOD_SIZE等全部hw参数;与snd_pcm_hw_params_any配合可快速得到合法区间起点;可内联,零开销;返回负错误码表示无可用值。 -
使用限制
只能在hw_params回调上下文调用;必须先初始化params;返回值为最小合法值;不可在中断内使用;需要临时变量保存结果。 -
性能特性
单寄存器读取;耗时低于100 ns;无内存分配;支持16路并发;编码体积增加不到16字节。 -
使用场景
车载系统探测最小可用采样率、语音唤醒探测最小通道数、USB声卡探测最小周期尺寸。
🌻3. 代码实例
🌻3.1 探测最小可用采样率
-
应用场景
车载导航要求从16 kHz开始向上适配,驱动先取最小值再设置。 -
用法实例
#include <sound/core.h>
#include <sound/pcm.h>
#include <linux/module.h>static int car_min_rate_hw_params(struct snd_pcm_substream *s,struct snd_pcm_hw_params *p)
{int err;unsigned int min_rate;/* 得到最小可用采样率 */err = snd_pcm_hw_param_first(s, p, SNDRV_PCM_HW_PARAM_RATE, &min_rate);if (err < 0)return err;pr_info("Car minimum rate %u Hz\n", min_rate);/* 强制使用最小值 */return snd_pcm_hw_params_set_rate(s, p, min_rate, 0);
}static struct snd_pcm_ops car_rate_ops = {open = car_rate_open,ioctl = snd_pcm_lib_ioctl,hw_params = car_min_rate_hw_params,trigger = car_rate_trigger,pointer = car_rate_pointer,
};static int __init car_first_rate_init(void)
{int err;struct snd_card *card;struct snd_pcm *pcm;err = snd_card_new(NULL, -1, "CarRateCard", THIS_MODULE, 0, &card);if (err < 0)return err;err = snd_pcm_new(card, "CarRate", 0, 1, 0, &pcm);if (err < 0)goto fail;snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &car_rate_ops);strcpy(pcm->name, "Car First Rate");err = snd_card_register(card);if (err < 0)goto fail;return 0;
fail:snd_card_free(card);return err;
}static void __exit car_first_rate_exit(void)
{struct snd_card *card = snd_card_ref(-1);if (card)snd_card_free(card);
}
module_init(car_first_rate_init);
module_exit(car_first_rate_exit);
MODULE_LICENSE("GPL");
代码功能:通过snd_pcm_hw_param_first得到最小可用采样率并立即应用,保证车载从最低速率启动。
🌻3.2 探测最小通道数
-
应用场景
语音唤醒DSP仅支持单声道,驱动需强制使用最小通道。 -
用法实例
#include <sound/core.h>
#include <sound/pcm.h>
#include <linux/module.h>static int vw_min_ch_hw_params(struct snd_pcm_substream *s,struct snd_pcm_hw_params *p)
{int err;unsigned int min_ch;err = snd_pcm_hw_param_first(s, p, SNDRV_PCM_HW_PARAM_CHANNELS, &min_ch);if (err < 0)return err;pr_info("Voice minimum channels %u\n", min_ch);return snd_pcm_hw_params_set_channels(s, p, min_ch);
}static struct snd_pcm_ops vw_ch_ops = {open = vw_ch_open,ioctl = snd_pcm_lib_ioctl,hw_params = vw_min_ch_hw_params,trigger = vw_ch_trigger,pointer = vw_ch_pointer,
};static int __init vw_first_ch_init(void)
{int err;struct snd_card *card;struct snd_pcm *pcm;err = snd_card_new(NULL, -1, "VWChCard", THIS_MODULE, 0, &card);if (err < 0)return err;err = snd_pcm_new(card, "VWCh", 0, 0, 1, &pcm);if (err < 0)goto fail;snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vw_ch_ops);strcpy(pcm->name, "VW First Ch");err = snd_card_register(card);if (err < 0)goto fail;return 0;
fail:snd_card_free(card);return err;
}static void __exit vw_first_ch_exit(void)
{struct snd_card *card = snd_card_ref(-1);if (card)snd_card_free(card);
}
module_init(vw_first_ch_init);
module_exit(vw_first_ch_exit);
MODULE_LICENSE("GPL");
代码功能:探测最小通道数并强制单声道,确保语音唤醒DSP无需降混。
🌻3.3 探测最小周期尺寸
-
应用场景
USB声卡需要找到最小合法周期尺寸以配置最佳中断频率。 -
用法实例
#include <sound/core.h>
#include <sound/pcm.h>
#include <linux/module.h>static int usb_min_period_hw_params(struct snd_pcm_substream *s,struct snd_pcm_hw_params *p)
{int err;unsigned int min_period;err = snd_pcm_hw_param_first(s, p, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &min_period);if (err < 0)return err;pr_info("USB minimum period %u frames\n", min_period);/* 使用最小周期降低延迟 */return snd_pcm_hw_params_set_period_size(s, p, min_period, 0);
}static struct snd_pcm_ops usb_period_ops = {open = usb_period_open,ioctl = snd_pcm_lib_ioctl,hw_params = usb_min_period_hw_params,trigger = usb_period_trigger,pointer = usb_period_pointer,
};static int __init usb_first_period_init(void)
{int err;struct snd_card *card;struct snd_pcm *pcm;err = snd_card_new(NULL, -1, "USBPeriodCard", THIS_MODULE, 0, &card);if (err < 0)return err;err = snd_pcm_new(card, "USBPer", 0, 1, 1, &pcm);if (err < 0)goto fail;snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &usb_period_ops);snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &usb_period_ops);strcpy(pcm->name, "USB First Period");err = snd_card_register(card);if (err < 0)goto fail;return 0;
fail:snd_card_free(card);return err;
}static void __exit usb_first_period_exit(void)
{struct snd_card *card = snd_card_ref(-1);if (card)snd_card_free(card);
}
module_init(usb_first_period_init);
module_exit(usb_first_period_exit);
MODULE_LICENSE("GPL");
代码功能:通过snd_pcm_hw_param_first得到最小周期尺寸并立即应用,USB中断频率最大化,延迟最低。
🌻3.4 用法总结
| 代码关键字 | 功能描述 | 典型应用 |
|---|---|---|
| snd_pcm_hw_param_first RATE | 最小采样率 | 车载导航 |
| snd_pcm_hw_param_first CHANNELS | 最小通道数 | 语音唤醒 |
| snd_pcm_hw_param_first PERIOD_SIZE | 最小周期 | USB低延迟 |
