arecord peak meter for S24_3 format
I have an edirol ua-25 (uses S24_3LE format) connected to my powerbook (i.e. big endian).
When I used 'arecord -vv tst.wav' the peak meter is always 0. Using the patch below (against 1.0.10rc1) I get what looks to be good output.
But now I wonder why the other code does not acount for endianess differences between format and cpu?
My alsa kernel driver is still 1.0.8. Not sure if there are any fixes related to this in later version. The actual recording sounds ok when played back on the ua-25 or the built-in powermac chip.
Kinda confused,
Signature · 65 lines
Martin
--- aplay.c.orig 2005-10-08 11:41:18.000000000 +0100
+++ aplay.c 2005-10-08 15:01:44.000000000 +0100
@@ -1099,6 +1100,7 @@
static void compute_max_peak(u_char *data, size_t count)
{
signed int val, max, max_peak = 0, perc;
+ static int run = 0;
size_t ocount = count;
switch (bits_per_sample) {
@@ -1125,6 +1127,33 @@
}
break;
}
+ case 24: {
+ unsigned char *valp = data;
+ signed int mask = snd_pcm_format_silence_32(hwparams.format);
+ int format_little = snd_pcm_format_little_endian(hwparams.format);
+ count /= 3;
+ while (count-- > 0) {
+ if (format_little) {
+ val = valp[0] | (valp[1]<<8) | (valp[2]<<16);
+ //val = __le32_to_cpu(val) >> 8;
+ } else {
+ val = (valp[0]<<16) | (valp[1]<<8) | valp[2];
+ //val = __be32_to_cpu(val) >> 8;
+ }
+ // Correct signed bit in 32-bit value
+ if (val & (1<<(bits_per_sample-1))) {
+ val |= 0xff<<24; // Negate upper bits too
+ val = -val;
+ }
+//printf("0x%lx\t",val);
+
+ val = abs(val) ^ mask;
+ if (max_peak < val)
+ max_peak = val;
+ data += 3;
+ }
+ break;
+ }
case 32: {
signed int *valp = (signed int *)data;
signed int mask = snd_pcm_format_silence_32(hwparams.format);
@@ -1138,7 +1167,11 @@
break;
}
default:
- break;
+ if (run == 0) {
+ fprintf(stderr, "Unsupported bit size %d.\n", bits_per_sample);
+ run = 1;
+ }
+ return;
}
max = 1 << (bits_per_sample-1);
if (max <= 0)
-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmplManaged to do some more testing with the ua-25 in S16_LE mode and with plughw. Seems the 16 and 32 bit code was also not working properly. The patch below fixes these as well, plus some more cleanup in the 24 bit code.
It is still not correct calulating the peak for any unsigned format, but I can't be bothered about that. My guess is the code would become more complicated.
Also, S24_[BL]E (24 bits in 4 bytes) is not working correctly as the sign bit is not handled in the 32 bit code. I could fix this if people feel it's important.
So please review the patch below, and apply if you feel its okay.
Signature · 106 lines
Martin Signed-off-by: Martin Habets <[email protected]> --- --- aplay.c.orig 2005-10-08 11:41:18.000000000 +0100 +++ aplay.c 2005-10-08 17:38:41.000000000 +0100 @@ -43,6 +43,7 @@ #include <sys/uio.h> #include <sys/time.h> #include <sys/signal.h> +#include <asm/byteorder.h> #include "aconfig.h" #include "gettext.h" #include "formats.h" @@ -1099,8 +1100,10 @@ static void compute_max_peak(u_char *data, size_t count) { signed int val, max, max_peak = 0, perc; + static int run = 0; size_t ocount = count; - + int format_little_endian = snd_pcm_format_little_endian(hwparams.format); + switch (bits_per_sample) { case 8: { signed char *valp = (signed char *)data; @@ -1116,12 +1119,39 @@ case 16: { signed short *valp = (signed short *)data; signed short mask = snd_pcm_format_silence_16(hwparams.format); + signed short sval; + count /= 2; while (count-- > 0) { - val = *valp++ ^ mask; - val = abs(val); + if (format_little_endian) + sval = __le16_to_cpu(*valp); + else sval = __be16_to_cpu(*valp); + sval = abs(sval) ^ mask; + if (max_peak < sval) + max_peak = sval; + valp++; + } + break; + } + case 24: { + unsigned char *valp = data; + signed int mask = snd_pcm_format_silence_32(hwparams.format); + + count /= 3; + while (count-- > 0) { + if (format_little_endian) { + val = valp[0] | (valp[1]<<8) | (valp[2]<<16); + } else { + val = (valp[0]<<16) | (valp[1]<<8) | valp[2]; + } + /* Correct signed bit in 32-bit value */ + if (val & (1<<(bits_per_sample-1))) { + val |= 0xff<<24; /* Negate upper bits too */ + } + val = abs(val) ^ mask; if (max_peak < val) max_peak = val; + valp += 3; } break; } @@ -1130,20 +1160,27 @@ signed int mask = snd_pcm_format_silence_32(hwparams.format); count /= 4; while (count-- > 0) { - val = *valp++ ^ mask; - val = abs(val); + if (format_little_endian) + val = __le32_to_cpu(*valp); + else val = __be32_to_cpu(*valp); + val = abs(val) ^ mask; if (max_peak < val) max_peak = val; + valp++; } break; } default: - break; + if (run == 0) { + fprintf(stderr, "Unsupported bit size %d.\n", bits_per_sample); + run = 1; + } + return; } max = 1 << (bits_per_sample-1); if (max <= 0) max = 0x7fffffff; - printf(_("Max peak (%li samples): %05i (0x%04x) "), (long)ocount, max_peak, max_peak); + printf("Max peak (%li samples): 0x%08x ", (long)ocount, max_peak); if (bits_per_sample > 16) perc = max_peak / (max / 100); else ------------------------------------------------------- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl
At Sat, 8 Oct 2005 18:05:57 +0100,
Quoted 6 lines · Martin Habets
Managed to do some more testing with the ua-25 in
S16_LEmode and with plughw. Seems the 16 and 32 bit code was also not working properly. The patch below fixes these as well, plus some more cleanup in the 24 bit code.
Quoted 8 lines
It is still not correct calulating the peak for any unsigned format, but I can't be bothered about that. My guess is the code would become more complicated.
Also, S24_[BL]E (24 bits in 4 bytes) is not working correctly as the sign bit is not handled in the 32 bit code. I could fix this if people feel it's important.So please review the patch below, and apply if you feel its okay.
Thanks, I applied it to CVS now (with a minor modification for gettext).
Takashi
-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl