Linux – ALC888 – SIP/VoIP – reduce white noise in the ALSA sound library with libspeex

With the following patch you could reduce the white noise in the alsa sound library using libspeex:

diff -r alsa-lib-1.0.21a/configure alsa-lib-1.0.21a.working/configure
9731c9731
< LIBS="-ldl  $LIBS"
---
> LIBS="-lspeexdsp -ldl  $LIBS"
20938c20938
<     ALSA_DEPLIBS="$ALSA_DEPLIBS -ldl"
---
>     ALSA_DEPLIBS="$ALSA_DEPLIBS -lspeexdsp -ldl"
diff -r alsa-lib-1.0.21a/src/pcm/pcm.c alsa-lib-1.0.21a.working/src/pcm/pcm.c
641a642,644
> #include <speex/speex_preprocess.h>
> SpeexPreprocessState *speex_state = NULL;
>
693a697,700
> 	if( speex_state != NULL ) {
> 		speex_preprocess_state_destroy( speex_state );
> 		speex_state = NULL;
> 	}
1300a1308
> 	snd_pcm_sframes_t sframest;
1311c1319,1336
< 	return _snd_pcm_readi(pcm, buffer, size);
---
> 	sframest = _snd_pcm_readi(pcm, buffer, size);
>
> 	if( speex_state == NULL ){
> 		speex_state = speex_preprocess_state_init( (int) size, 8000 );
> 		if( speex_state != NULL ){
> 			int denoise = 1;
> 			speex_preprocess_ctl( speex_state, SPEEX_PREPROCESS_SET_DENOISE, &denoise );
> 			int noise_suppress = 2555;
> 			speex_preprocess_ctl( speex_state, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noise_suppress);
> 			int agc_enabled = 0;
> 			speex_preprocess_ctl( speex_state, SPEEX_PREPROCESS_SET_AGC, &agc_enabled);
> 		}
> 	}
> 	if( speex_state != NULL ){
> 		speex_preprocess_run( speex_state, (short*)buffer );
> 	}
>
> 	return sframest;

Below is just a reminder of the call stack for one PCM read request:

./linphone-3.1.2/mediastreamer2/src# 
vim alsa.c

...
result = alsa_read( fd, buf, count );
...
int alsa_read( snd_pcm_t* handle, buf, count){
	result = snd_pcm_readi(fd, buf, count);
}

/lib/modules/2.6.29/build/audio/realtek-linux-audiopack-5.13/alsa-lib-1.0.21a# 
Library: /lib/modules/2.6.29/build/audio/realtek-linux-audiopack-5.13/alsa-lib-1.0.21a/src/pcm/libpcm.a
vim ./src/pcm/pcm_hw.c


snd_pcm_frames_t snd_pcm_readi( fd, buf, count){

	struct sndrv_xferi xferi;
	xferi.buf = buf,
	xferi.frames = count;
	xferi.result = 0;
	err = ioctl( fd,  SNDRV_PCM_IOCTL_READI_FRAMES, &xferi );
	if( err < 0 )
		return err;
	return xferi.result;
}

/lib/modules/2.6.29/build/audio/realtek-linux-audiopack-5.13/alsa-driver-1.0.21# 

Library: /lib/modules/2.6.29/kernel/sound/acore/snd-pcm.ko
vim ./alsa-kernel/core/pcm_native.c


2643static int snd_pcm_capture_ioctl1(struct file *file,
2644                                  struct snd_pcm_substream *substream,
2645                                  unsigned int cmd, void __user *arg)

case SNDRV_PCM_IOCTL_READI_FRAMES:
{
	result = snd_pcm_lib_read(substream,buf,count)
}

case SNDRV_PCM_IOCTL_READN_FRAMES:
{
	result = snd_pcm_lib_readv(substream,buf,frames)
}

ssize_t snd_pcm_read( fd, buf, count, offset){
	result = snd_pcm_lib_read(substream,buf,count)
}

vim ./alsa-kernel/core/pcm_lib.c

Library: /lib/modules/2.6.29/kernel/sound/acore/snd-pcm.ko

snd_pcm_sframes_t   snd_pcm_lib_read( substream, buf, count ){
	snd_pcm_lib_read1( substream, buf, count, nonblock, snd_pcm_lib_reader_transfer);
}

snd_pcm_sframes_t   snd_pcm_lib_read1( substream, buf, count, nonblock, transfer){
	transfer( substream, appl_ofs, buf, offset, frames );
  	??? ---> ???
}

vim ./alsa-kernel/pci/hda/patch_realtek.c		
Library: /lib/modules/2.6.29/kernel/sound/pci/hda/snd-hda-codec-realtek.ko
	printk(KERN_INFO "hda_codec3: %s: BIOS AUTO-PROBING3.\n",

Advertisements
This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s