Increasing maximum sampling frequency?
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Is it possible to push the maximum sampling frequency higher? If so, where should I start looking?
I tried hacking aplay.c so arecord would accept command line requests up to 256k, but it seems the alsa driver is also limiting the frequency to a maximum of 192k. However, the files generated by arecord *do* have the right number of samples in them.
$ arecord -v -Dplughw:1,0 test.wav -d 1 -r 256
Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono
Plug PCM: Linear conversion PCM (S8)
Its setup is:
stream : CAPTURE
access : RW_INTERLEAVED
format : U8
subformat : STD
channels : 1
rate : 256000
exact rate : 256000 (256000/1)
msbits : 8
buffer_size : 128000
period_size : 32000
period_time : 125000
tstamp_mode : NONE
period_step : 1
avail_min : 32000
period_event : 0
start_threshold : 1
stop_threshold : 128000
silence_threshold: 0
silence_size : 0
boundary : 2097152000
Slave: Hardware PCM card 1 '8-Mic AVR Adaptor' device 0 subdevice 0
Its setup is:
stream : CAPTURE
access : MMAP_INTERLEAVED
format : S8
subformat : STD
channels : 1
rate : 256000
exact rate : 256000 (256000/1)
msbits : 8
buffer_size : 128000
period_size : 32000
period_time : 125000
tstamp_mode : NONE
period_step : 1
avail_min : 32000
period_event : 0
start_threshold : 1
stop_threshold : 128000
silence_threshold: 0
silence_size : 0
boundary : 2097152000
$ aplay -r 256 test.wav
Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono
Warning: rate is not accurate (requested = 256000Hz, got = 192000Hz)
please, try the plug plugin (-Dplug:default)
$ ls -l test.wav
-rw-r--r-- 1 camerons camerons 256044 2009-05-28 11:29 test.wav
$ file test.wav
test.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 8 bit,
mono 256000 Hz
$Quoted 8 lines · Cameron Stone
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Is it possible to push the maximum sampling frequency higher? If so, where should I start looking?
For starters, try adding a 256K sample rate to include/sound/pcm.h:
118 #define SNDRV_PCM_RATE_176400 (1<<11) /* 176400Hz */ 119 #define SNDRV_PCM_RATE_192000 (1<<12) /* 192000Hz */
120
and sound/core/pcm_native.c:
1762
1763 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1764 48000, 64000, 88200, 96000,
176400, 192000 };
1765
Line numbers may vary, mine are from 2.6.27 kernel source.
Lee
Quoted 7 lines · Lee Revell
Quoted 5 lines · Cameron Stone
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Quoted 5 lines
Quoted 2 lines
Is it possible to push the maximum sampling frequency higher? If so, where should I start looking?
For starters, try adding a 256K sample rate to
include/sound/pcm.h:
andsound/core/pcm_native.c:
This is not necessary; drivers can define whatever sample rate they want. 1,792,000 Hz works just fine with the Bt878 driver.
Quoted 7 lines
$ arecord -v -Dplughw:1,0 test.wav -d 1 -r 256 Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono ... Slave: Hardware PCM card 1 '8-Mic AVR Adaptor' device 0 subdevice 0 Its setup is: ... rate : 256000
This indicates that the record PCM stream _does_ use the correct rate.
Quoted 3 lines
$ aplay -r 256 test.wav Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono Warning: rate is not accurate (requested = 256000Hz, got = 192000Hz)
The automatic sample rate converter never uses a frequency higher than 192 kHz.
This maximum frequency is defined in alsa-lib/include/pcm_plugin.h, but you cannot change it without changing LINEAR_DIV and the algorithm in alsa-lib/src/pcm_pcm_rate_linear.c.
Try resampling the .wav file before with sox.
Best regards,
Clemens
At Thu, 28 May 2009 08:52:44 +0200,
Quoted 32 lines · Clemens Ladisch
Quoted 7 lines · Lee Revell
Quoted 5 lines · Cameron Stone
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Quoted 5 lines
Quoted 2 lines
Is it possible to push the maximum sampling frequency higher? If so, where should I start looking?
For starters, try adding a 256K sample rate to
include/sound/pcm.h:
andsound/core/pcm_native.c:This is not necessary; drivers can define whatever sample rate they want. 1,792,000 Hz works just fine with the Bt878 driver.
Quoted 7 lines
$ arecord -v -Dplughw:1,0 test.wav -d 1 -r 256 Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono ... Slave: Hardware PCM card 1 '8-Mic AVR Adaptor' device 0 subdevice 0 Its setup is: ... rate : 256000This indicates that the record PCM stream _does_ use the correct rate.
Quoted 3 lines
$ aplay -r 256 test.wav Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono Warning: rate is not accurate (requested = 256000Hz, got = 192000Hz)
Quoted 6 lines
The automatic sample rate converter never uses a frequency higher than 192 kHz.
This maximum frequency is defined in
alsa-lib/include/pcm_plugin.h, but you cannot change it without changingLINEAR_DIVand the algorithm inalsa-lib/src/pcm_pcm_rate_linear.c.
It's anyway better to user other resampler like pph or samplerate plugins. And these have no restriction, I guess.
It'd be nicer if the rate plugin itself provides the min/max rates optionally. It shouldn't be hard -- just extend the pcm_rate_info_t to contain the new info. A version check would be required to make the backward compatibility, though.
Takashi
At Thu, 28 May 2009 09:28:57 +0200,
Quoted 42 lines · I wrote:
At Thu, 28 May 2009 08:52:44 +0200,
Quoted 32 lines · Clemens Ladisch
Quoted 7 lines · Lee Revell
Quoted 5 lines · Cameron Stone
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Quoted 5 lines
Quoted 2 lines
Is it possible to push the maximum sampling frequency higher? If so, where should I start looking?
For starters, try adding a 256K sample rate to
include/sound/pcm.h:
andsound/core/pcm_native.c:This is not necessary; drivers can define whatever sample rate they want. 1,792,000 Hz works just fine with the Bt878 driver.
Quoted 7 lines
$ arecord -v -Dplughw:1,0 test.wav -d 1 -r 256 Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono ... Slave: Hardware PCM card 1 '8-Mic AVR Adaptor' device 0 subdevice 0 Its setup is: ... rate : 256000This indicates that the record PCM stream _does_ use the correct rate.
Quoted 3 lines
$ aplay -r 256 test.wav Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono Warning: rate is not accurate (requested = 256000Hz, got = 192000Hz)Quoted 6 lines
The automatic sample rate converter never uses a frequency higher than 192 kHz.
This maximum frequency is defined in
alsa-lib/include/pcm_plugin.h, but you cannot change it without changingLINEAR_DIVand the algorithm inalsa-lib/src/pcm_pcm_rate_linear.c.
Quoted 7 lines
It's anyway better to user other resampler like pph or samplerate plugins. And these have no restriction, I guess.
It'd be nicer if the rate plugin itself provides the min/max rates optionally. It shouldn't be hard -- just extend the
pcm_rate_info_tto contain the new info. A version check would be required to make the backward compatibility, though.
OK, here are two patches. One for alsa-lib and one for alsa-plugins. The linear plugin still limits the rate up to 192kHz. But other plugins should work (hopefully).
Takashi
---
include/pcm_rate.h +32 -1
@@ -38,7 +38,7 @@ extern "C" {/** * Protocol version */ -#define SND_PCM_RATE_PLUGIN_VERSION 0x010001 +#define SND_PCM_RATE_PLUGIN_VERSION 0x010002/** hw_params information for a single side */ typedef struct snd_pcm_rate_side_info {
@@ -98,6 +98,17 @@ typedef struct snd_pcm_rate_ops {* compute the frame size for output */ snd_pcm_uframes_t (*output_frames)(void *obj,snd_pcm_uframes_t frames); +/** +* the protocol version the plugin supports; +* new field since version 0x010002 +*/ +unsigned int version; +/** +* return the supported min / max sample rates; +* new ops since version 0x010002 +*/ +int (*get_supported_rates)(void *obj,unsigned int *rate_min, +unsigned int *rate_max); } snd_pcm_rate_ops_t;/** open function type */
@@ -110,6 +121,26 @@ typedef int (*snd_pcm_rate_open_func_t)(unsigned int version, void **objp,#define SND_PCM_RATE_PLUGIN_ENTRY (name) _snd_pcm_rate_##name##_open +#ifndef DOC_HIDDEN +/* old rate_ops for protocol version 0x010001 */ +typedef struct snd_pcm_rate_old_ops { +void (*close)(void *obj); +int (*init)(void *obj,snd_pcm_rate_info_t *info); +void (*free)(void *obj); +void (*reset)(void *obj); +int (*adjust_pitch)(void *obj,snd_pcm_rate_info_t *info); +void (*convert)(void *obj, +const snd_pcm_channel_area_t *dst_areas, +snd_pcm_uframes_t dst_offset,unsigned int dst_frames, +const snd_pcm_channel_area_t *src_areas, +snd_pcm_uframes_t src_offset,unsigned int src_frames); +void (*convert_s16)(void *obj,int16_t *dst,unsigned int dst_frames, +const int16_t *src,unsigned int src_frames); +snd_pcm_uframes_t (*input_frames)(void *obj,snd_pcm_uframes_t frames); +snd_pcm_uframes_t (*output_frames)(void *obj,snd_pcm_uframes_t frames); +}snd_pcm_rate_old_ops_t ; +#endif + #ifdef __cplusplus }#endif
src/pcm/pcm_rate.c +37 -9
76 lines not on this page · raw message
src/pcm/pcm_rate_linear.c +10 -5
@@ -405,6 +405,14 @@ static void linear_close(void *obj)free (obj); } +static int get_supported_rates (void *rate,unsigned int *rate_min, +unsigned int *rate_max) +{ + *rate_min =SND_PCM_PLUGIN_RATE_MIN ; + *rate_max =SND_PCM_PLUGIN_RATE_MAX ; +return 0 ; +} +static const snd_pcm_rate_ops_t linear_ops = { .close = linear_close, .init = linear_init,
@@ -414,17 +422,14 @@ static const snd_pcm_rate_ops_t linear_ops = { .convert = linear_convert, .input_frames = input_frames, .output_frames = output_frames, + .version = SND_PCM_RATE_PLUGIN_VERSION, + .get_supported_rates = get_supported_rates, };int SND_PCM_RATE_PLUGIN_ENTRY (linear ) (unsigned int version,void **objp,snd_pcm_rate_ops_t *ops) {struct rate_linear *rate; -if (version !=SND_PCM_RATE_PLUGIN_VERSION ) { -SNDERR ("Invalid plugin version %x\n" , version); -return -EINVAL ; - } - rate =calloc (1 ,sizeof (*rate));if (! rate)return -ENOMEM ;
pph/rate_speexrate.c +21 -2
@@ -116,6 +116,15 @@ static void pcm_src_close(void *obj)free (obj); } +#if SND_PCM_RATE_PLUGIN_VERSION >=0x010002 +static int get_supported_rates (void *obj,unsigned int *rate_min, +unsigned int *rate_max) +{ + *rate_min = *rate_max =0 ;/* both unlimited */ +return 0 ; +} +#endif +static snd_pcm_rate_ops_t pcm_src_ops = { .close = pcm_src_close, .init = pcm_src_init,
@@ -125,6 +134,10 @@ static snd_pcm_rate_ops_t pcm_src_ops = { .convert_s16 = pcm_src_convert_s16, .input_frames = input_frames, .output_frames = output_frames, +#if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002 + .version =SND_PCM_RATE_PLUGIN_VERSION , + .get_supported_rates = get_supported_rates, +#endif }; static int pcm_src_open(unsigned int version, void **objp,
@@ -132,18 +145,24 @@ static int pcm_src_open(unsigned int version, void **objp, {struct rate_src *rate; +#if SND_PCM_RATE_PLUGIN_VERSION <0x010002 if (version !=SND_PCM_RATE_PLUGIN_VERSION ) {fprintf (stderr,"Invalid rate plugin version %x\n" , version);return -EINVAL ; } - +#endif rate =calloc (1 ,sizeof (*rate));if (! rate)return -ENOMEM ; rate->quality = quality; *objp = rate; - *ops = pcm_src_ops; +#if SND_PCM_RATE_PLUGIN_VERSION >=0x010002 +if (version ==0x010001 ) +memcpy (ops, &pcm_src_ops,sizeof (snd_pcm_rate_old_ops_t)); + else +#endif + *ops = pcm_src_ops;return 0 ; }
rate-lavc/rate_lavcrate.c +21 -2
@@ -202,6 +202,15 @@ static void pcm_src_close(void *obj)pcm_src_free (obj); } +#if SND_PCM_RATE_PLUGIN_VERSION >=0x010002 +static int get_supported_rates (void *obj,unsigned int *rate_min, +unsigned int *rate_max) +{ + *rate_min = *rate_max =0 ;/* both unlimited */ +return 0 ; +} +#endif +static snd_pcm_rate_ops_t pcm_src_ops = { .close = pcm_src_close, .init = pcm_src_init,
@@ -211,6 +220,10 @@ static snd_pcm_rate_ops_t pcm_src_ops = { .convert_s16 = pcm_src_convert_s16, .input_frames = input_frames, .output_frames = output_frames, +#if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002 + .version =SND_PCM_RATE_PLUGIN_VERSION , + .get_supported_rates = get_supported_rates, +#endif };int pcm_src_open (unsigned int version,void **objp,snd_pcm_rate_ops_t *ops)
@@ -218,18 +231,24 @@ int pcm_src_open(unsigned int version, void **objp, snd_pcm_rate_ops_t *ops) {struct rate_src *rate; +#if SND_PCM_RATE_PLUGIN_VERSION <0x010002 if (version !=SND_PCM_RATE_PLUGIN_VERSION ) {fprintf (stderr,"Invalid rate plugin version %x\n" , version);return -EINVAL ; } - +#endif rate =calloc (1 ,sizeof (*rate));if (!rate)return -ENOMEM ; *objp = rate; rate->context =NULL ; - *ops = pcm_src_ops; +#if SND_PCM_RATE_PLUGIN_VERSION >=0x010002 +if (version ==0x010001 ) +memcpy (ops, &pcm_src_ops,sizeof (snd_pcm_rate_old_ops_t)); + else +#endif + *ops = pcm_src_ops;return 0 ; }
rate/rate_samplerate.c +21 -2
@@ -137,6 +137,15 @@ static void pcm_src_close(void *obj)free (obj); } +#if SND_PCM_RATE_PLUGIN_VERSION >=0x010002 +static int get_supported_rates (void *obj,unsigned int *rate_min, +unsigned int *rate_max) +{ + *rate_min = *rate_max =0 ;/* both unlimited */ +return 0 ; +} +#endif +static snd_pcm_rate_ops_t pcm_src_ops = { .close = pcm_src_close, .init = pcm_src_init,
@@ -146,6 +155,10 @@ static snd_pcm_rate_ops_t pcm_src_ops = { .convert_s16 = pcm_src_convert_s16, .input_frames = input_frames, .output_frames = output_frames, +#if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002 + .version =SND_PCM_RATE_PLUGIN_VERSION , + .get_supported_rates = get_supported_rates, +#endif }; static int pcm_src_open(unsigned int version, void **objp,
@@ -153,18 +166,24 @@ static int pcm_src_open(unsigned int version, void **objp, {struct rate_src *rate; +#if SND_PCM_RATE_PLUGIN_VERSION <0x010002 if (version !=SND_PCM_RATE_PLUGIN_VERSION ) {fprintf (stderr,"Invalid rate plugin version %x\n" , version);return -EINVAL ; } - +#endif rate =calloc (1 ,sizeof (*rate));if (! rate)return -ENOMEM ; rate->converter = type; *objp = rate; - *ops = pcm_src_ops; +#if SND_PCM_RATE_PLUGIN_VERSION >=0x010002 +if (version ==0x010001 ) +memcpy (ops, &pcm_src_ops,sizeof (snd_pcm_rate_old_ops_t)); + else +#endif + *ops = pcm_src_ops;return 0 ; }
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Quoted 4 lines · Clemens Ladisch
Quoted 3 lines
$ aplay -r 256 test.wav Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 256000 Hz, Mono Warning: rate is not accurate (requested = 256000Hz, got = 192000Hz)
Quoted 8 lines
The automatic sample rate converter never uses a frequency higher than 192 kHz.
This maximum frequency is defined in
alsa-lib/include/pcm_plugin.h, but you cannot change it without changingLINEAR_DIVand the algorithm inalsa-lib/src/pcm_pcm_rate_linear.c.Try resampling the .wav file before with sox.
This works. Likewise, playing with "/usr/bin/play" from the sox toolkit also works fine.
It seems that the only limiting going on is during playback, so I'm not terribly worried by this, as we're going to be looking at and processing
these samples, not trying to listen to them. Humans can't hear above
20kHz anyway, right?
Thanks to everyone for their help on this.
Cameron.
Quoted 4 lines · Cameron Stone
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Just out of curiosity - which microphone and what kind of codec are you using for such frequencies?
Daniel
Quoted 8 lines · Daniel Mack
Quoted 4 lines · Cameron Stone
I've got a USB microphone application that I want to sample at 256000 8 bit samples per second so I can listen to bats. I know that my device can get close to that rate, but alsa seems to restrict sampling rates to 192kHz.
Just out of curiosity - which microphone and what kind of codec are you using for such frequencies?
I don't know which microphone but I can find out if you're interested. I do know that its sensitivity starts at 10kHz, if that helps.
There is no codec used. I'm using Type I (uncompressed PCM) because it's being powered by an AVR running at 16MHz and there's not enough clock cycles to encode the samples.
Cameron.
A message that is not in the archive (sent off-list or not yet received): [email protected]
The A/D chip is an Analog Devices 7928. It can do 2 mega-samples per second, multiplexed across 8 inputs. The USB audio interface is provided by my firmware code, assisted by the AVR AT90USB647 micro which has some low-level USB support.
HTH.
Cameron.
Quoted 40 lines · Alex Austin
By Codec, I think he's asking about the A/D chip. For example, the TI PCM2902E is a stereo USB audio codec.
- Alex Austin
(651) 238-9273"...and then I visited Wikipedia ...and the next 8 hours are a blur."
On Thu, May 28, 2009 at 8:30 AM, Cameron Stone <[email protected] <mailto:[email protected]>> wrote:
Daniel Mack wrote:
> On Thu, May 28, 2009 at 11:47:34AM +1000, Cameron Stone wrote:
>> I've got a USB microphone application that I want to sample at 256000 8
>> bit samples per second so I can listen to bats. I know that my device >> can get close to that rate, but alsa seems to restrict sampling rates to
>> 192kHz.
>
> Just out of curiosity - which microphone and what kind of codec are you
> using for such frequencies?I don't know which microphone but I can find out if you're interested. I do know that its sensitivity starts at 10kHz, if that helps.
There is no codec used. I'm using Type I (uncompressed PCM) because it's being powered by an AVR running at 16MHz and there's not enough clock cycles to encode the samples.
Cameron.
_______________________________________________
Alsa-devel mailing list
[email protected] <mailto:[email protected]>
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel