Re: Redirecting Mic Input in software (no arecord / aplay involved)

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

 



Thierry Bouchard wrote:
> Im trying to capture the Microphone input and redirect it right away into
> speakers. The problem is that whenever I use "snd_pcm_readi"
> and "snd_pcm_writei" one after each other in my capture loop, the pipe
> becomes broken on the output side, i.e. every call to "snd_pcm_writei" fails
> after it succeeded for like 2 passes.

In the time that snd_pcm_readi needs to wait for a block of data to
become available, the data written by the last snd_pcm_writei call is
played by the sound card.  Just when snd_pcm_readi returns, the playback
buffer has become completely empty, and even the slightest delay in
writing the next data to it will result in an underrun.

To ensure that the playback buffer contains enough samples to adjust for
timing variations, do not start playing when the first block of data is
written to it but when the buffer is (almost) completely filled.

The buffer fullness at which a playback device is started is called the
"start threshold".  The default value is 1, i.e., after at least one
sample has been written to the buffer; you can change it with the
snd_pcm_sw_params_start_threshold function.

The following is from the implementation of snd_pcm_set_params:

	snd_pcm_sw_params_t *swparams;

	snd_pcm_sw_params_alloca(&swparams);
	...
	err = snd_pcm_sw_params_current(pcm, swparams);
	if (err < 0) {
		SNDERR("Unable to determine current swparams for %s: %s", s, snd_strerror(err));
		return err;
	}
	/* start the transfer when the buffer is almost full: */
	/* (buffer_size / avail_min) * avail_min */
	err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (buffer_size / period_size) * period_size);
	if (err < 0) {
		SNDERR("Unable to set start threshold mode for %s: %s", s, snd_strerror(err));
		return err;
	}
	/* allow the transfer when at least period_size samples can be processed */
	err = snd_pcm_sw_params_set_avail_min(pcm, swparams, period_size);
	if (err < 0) {
		SNDERR("Unable to set avail min for %s: %s", s, snd_strerror(err));
		return err;
	}
	/* write the parameters to the playback device */
	err = snd_pcm_sw_params(pcm, swparams);
	if (err < 0) {
		SNDERR("Unable to set sw params for %s: %s", s, snd_strerror(err));
		return err;
	}


However, an even better solution for your problem would be to enable the
input monitoring function of your sound card.  This should be supported
by the CMI8788 chip, but it isn't supported by the driver because so far
I have not been able to find out how it works.  I'll look into it.


Regards,
Clemens

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Alsa-user mailing list
Alsa-user@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/alsa-user

[Index of Archives]     [ALSA Devel]     [Linux Audio Users]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]

  Powered by Linux