Click sound when playing "silence" buffer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello! I'm completely new to ALSA and I tried to do a simply buzzer using it.

During the debug I noticed a strange behaviour: I used
SND_PCM_FORMAT_S8 format, tried to play the buffer filled with zero
bytes and I heard click sound when my program starts and end playing
the buffer. When I filled the buffer with 0x80 bytes I didn't hear any
clicks. The closer the bytes value to 0x80, the quieter the click is.

I tried snd_pcm_format_set_silence() instead of memset() and got
clicks too. Why I got them?

Does it mean what when I use some formats, the values in buffer for
zero output voltage are not 0x00?

My source code is in attachment, my sound card is Intel HDA.
#define ALSA_PCM_NEW_HW_PARAMS_API

#include <alsa/asoundlib.h>

int main()
{
int errCode;
unsigned int val;
int dir;
size_t size;
unsigned long loops;
char* buffer;

snd_pcm_t* hPCM;
snd_pcm_hw_params_t* params;
snd_pcm_uframes_t frames;
	
	errCode = snd_pcm_open(&hPCM, "default", SND_PCM_STREAM_PLAYBACK, 0);
	if (errCode < 0){
		printf ("Error snd_pcm_open: %s\n", snd_strerror(errCode));
		return 1;
	}
	
	snd_pcm_hw_params_alloca(&params);
	snd_pcm_hw_params_any(hPCM, params);
	snd_pcm_hw_params_set_access(hPCM, params, SND_PCM_ACCESS_RW_INTERLEAVED);
	//snd_pcm_hw_params_set_format(hPCM, params, SND_PCM_FORMAT_S16_LE);
	snd_pcm_hw_params_set_format(hPCM, params, SND_PCM_FORMAT_S8);
	snd_pcm_hw_params_set_channels(hPCM, params, 2);
	
	val = 44100;
	snd_pcm_hw_params_set_rate_near(hPCM, params, &val, &dir);
	
	frames = 256;
	snd_pcm_hw_params_set_period_size_near(hPCM, params, &frames, &dir);
	
	errCode = snd_pcm_hw_params(hPCM, params);
	if (errCode < 0){
		printf ("Error snd_pcm_hw_params: %s\n", snd_strerror(errCode));
	}
	
	snd_pcm_hw_params_get_period_size(params, &frames, &dir);
	size = frames * 2 * 4;
	buffer = (char*)malloc(size);
		
	memset(buffer, 0x00, size);
	
	snd_pcm_hw_params_get_period_time(params, &val, &dir);
	loops = 1000000 / val;
	
	while(loops > 0){
		loops--;
		errCode = snd_pcm_writei(hPCM, buffer, frames);
	}
		
	snd_pcm_drain(hPCM);
	snd_pcm_close(hPCM);
	free(buffer);
	return 0;
}
_______________________________________________
Alsa-devel mailing list
Alsa-devel@xxxxxxxxxxxxxxxx
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

[Index of Archives]     [ALSA User]     [Linux Audio Users]     [Kernel Archive]     [Asterisk PBX]     [Photo Sharing]     [Linux Sound]     [Video 4 Linux]     [Gimp]     [Yosemite News]

  Powered by Linux