From: Takashi Iwai Subject: Re: [RFC] TLV entry for linear volume Date: Fri, 25 Aug 2006 12:23:51 +0200 Message-ID: References: <44EC6F16.6090807@superbug.co.uk> <44EC9CED.8040408@superbug.co.uk> <44ECE949.9010207@superbug.co.uk> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: multipart/mixed; boundary="Multipart_Fri_Aug_25_12:23:51_2006-1" Return-path: In-Reply-To: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: alsa-devel-bounces@lists.sourceforge.net Errors-To: alsa-devel-bounces@lists.sourceforge.net To: James Courtier-Dutton Cc: alsa-devel@lists.sourceforge.net List-Id: alsa-devel@alsa-project.org --Multipart_Fri_Aug_25_12:23:51_2006-1 Content-Type: text/plain; charset=US-ASCII At Thu, 24 Aug 2006 11:16:45 +0200, I wrote: > > The difference between us is just how to express and pass the > coefficeints. My suggestion is to pass the values given in the > datasheet as they are, min and max dB. The below is the latest version I have on my local tree. It's against HG tree. Takashi --Multipart_Fri_Aug_25_12:23:51_2006-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="kernel-tlv-fix.diff" Content-Transfer-Encoding: 7bit diff -r 758522076155 include/tlv.h --- a/include/tlv.h Thu Aug 24 12:36:36 2006 +0200 +++ b/include/tlv.h Thu Aug 24 12:49:38 2006 +0200 @@ -33,6 +33,7 @@ #define SNDRV_CTL_TLVT_CONTAINER 0 /* one level down - group of TLVs */ #define SNDRV_CTL_TLVT_DB_SCALE 1 /* dB scale */ +#define SNDRV_CTL_TLVT_DB_LINEAR 2 /* linear volume */ #define DECLARE_TLV_DB_SCALE(name, min, step, mute) \ unsigned int name[] = { \ @@ -40,4 +41,13 @@ unsigned int name[] = { \ (min), ((step) & 0xffff) | ((mute) ? 0x10000 : 0) \ } +/* linear volume between min_dB and max_dB (.01dB unit) */ +#define DECLARE_TLV_DB_LINEAR(name, min_dB, max_dB) \ +unsigned int name[] = { \ + SNDRV_CTL_TLVT_DB_LINEAR, 2 * sizeof(unsigned int), \ + (min_dB), (max_dB) \ +} + +#define TLV_DB_GAIN_MUTE -9999999 + #endif /* __SOUND_TLV_H */ diff -r 758522076155 pci/ymfpci/ymfpci_main.c --- a/pci/ymfpci/ymfpci_main.c Thu Aug 24 12:36:36 2006 +0200 +++ b/pci/ymfpci/ymfpci_main.c Thu Aug 24 12:49:37 2006 +0200 @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -1477,11 +1478,15 @@ static int snd_ymfpci_put_single(struct return change; } +static DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0); + #define YMFPCI_DOUBLE(xname, xindex, reg) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .info = snd_ymfpci_info_double, \ .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \ - .private_value = reg } + .private_value = reg, \ + .tlv = { .p = db_scale_native } } static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { --Multipart_Fri_Aug_25_12:23:51_2006-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="linear-tlv.diff" Content-Transfer-Encoding: 7bit diff -r b64f7b87c52c include/control.h --- a/include/control.h Fri Aug 25 11:56:50 2006 +0200 +++ b/include/control.h Fri Aug 25 12:02:57 2006 +0200 @@ -170,6 +170,11 @@ typedef enum _snd_ctl_event_type { #define SND_CTL_TLVT_CONTAINER 0x0000 /** TLV type - basic dB scale */ #define SND_CTL_TLVT_DB_SCALE 0x0001 +/** TLV type - linear volume */ +#define SND_CTL_TLVT_DB_LINEAR 0x0002 + +/** Mute state */ +#define SND_CTL_TLV_DB_GAIN_MUTE -9999999 /** CTL type */ typedef enum _snd_ctl_type { diff -r b64f7b87c52c src/mixer/simple_none.c --- a/src/mixer/simple_none.c Fri Aug 25 11:56:50 2006 +0200 +++ b/src/mixer/simple_none.c Fri Aug 25 12:03:04 2006 +0200 @@ -1008,6 +1008,13 @@ static int parse_db_range(struct selem_s } break; case SND_CTL_TLVT_DB_SCALE: +#ifndef HAVE_SOFT_FLOAT + case SND_CTL_TLVT_DB_LINEAR: +#endif + if (size < 2 * sizeof(int)) { + SNDERR("Invalid dB_scale TLV size"); + return -EINVAL; + } rec->db_info = malloc(size + sizeof(int) * 2); if (! rec->db_info) return -ENOMEM; @@ -1025,21 +1032,46 @@ static int convert_to_dB(snd_hctl_elem_t static int convert_to_dB(snd_hctl_elem_t *ctl, struct selem_str *rec, long volume, long *db_gain) { - int min, step, mute; - if (init_db_range(ctl, rec) < 0) return -EINVAL; switch (rec->db_info[0]) { - case SND_CTL_TLVT_DB_SCALE: + case SND_CTL_TLVT_DB_SCALE: { + int min, step, mute; min = rec->db_info[2]; step = (rec->db_info[3] & 0xffff); mute = (rec->db_info[3] >> 16) & 1; if (mute && volume == rec->min) - *db_gain = -9999999; + *db_gain = SND_CTL_TLV_DB_GAIN_MUTE; else *db_gain = (volume - rec->min) * step + min; return 0; + } +#ifndef HAVE_SOFT_FLOAT + case SND_CTL_TLVT_DB_LINEAR: { + int mindb = rec->db_info[2]; + int maxdb = rec->db_info[3]; + if (volume <= rec->min || rec->max <= rec->min) + *db_gain = mindb; + else if (volume >= rec->max) + *db_gain = maxdb; + else { + double val = (double)(volume - rec->min) / + (double)(rec->max - rec->min); + if (mindb <= SND_CTL_TLV_DB_GAIN_MUTE) + *db_gain = (long)(100.0 * 20.0 * log10(val)) + + maxdb; + else { + /* FIXME: precalculate and cache these values */ + double lmin = pow(10.0, mindb/20.0); + double lmax = pow(10.0, maxdb/20.0); + val = (lmax - lmin) * val + lmin; + *db_gain = (long)(100.0 * 20.0 * log10(val)); + } + } + return 0; + } +#endif } return -EINVAL; } @@ -1114,6 +1146,10 @@ static int get_dB_range(snd_hctl_elem_t *min = (int)rec->db_info[2]; step = (rec->db_info[3] & 0xffff); *max = *min + (long)(step * (rec->max - rec->min)); + return 0; + case SND_CTL_TLVT_DB_LINEAR: + *min = (int)rec->db_info[2]; + *max = (int)rec->db_info[3]; return 0; } return -EINVAL; @@ -1160,6 +1196,30 @@ static int convert_from_dB(snd_hctl_elem } return 0; } +#ifndef HAVE_SOFT_FLOAT + case SND_CTL_TLVT_DB_LINEAR: { + int min, max; + min = rec->db_info[2]; + max = rec->db_info[3]; + if (db_gain <= min) + *value = rec->min; + else if (db_gain >= max) + *value = rec->max; + else { + /* FIXME: precalculate and cache vmin and vmax */ + double vmin, vmax, v; + vmin = (min <= SND_CTL_TLV_DB_GAIN_MUTE) ? 0.0 : + pow(10.0, (double)min / 20.0); + vmax = !max ? 1.0 : pow(10.0, (double)max / 20.0); + v = pow(10.0, (double)db_gain / 20.0); + v = (v - vmin) * (rec->max - rec->min) / (vmax - vmin); + if (xdir > 0) + v = ceil(v); + *value = (long)v + rec->min; + } + return 0; + } +#endif default: break; } --Multipart_Fri_Aug_25_12:23:51_2006-1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 --Multipart_Fri_Aug_25_12:23:51_2006-1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Alsa-devel mailing list Alsa-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/alsa-devel --Multipart_Fri_Aug_25_12:23:51_2006-1--