All of lore.kernel.org
 help / color / mirror / Atom feed
* Sound with metal effect
@ 2005-03-31 21:28 Laurielle LEA
  2005-04-01  7:39 ` Giuliano Pochini
  0 siblings, 1 reply; 7+ messages in thread
From: Laurielle LEA @ 2005-03-31 21:28 UTC (permalink / raw)
  To: alsa-devel

[-- Attachment #1: Type: text/plain, Size: 704 bytes --]

Hello,

I'm integrating ALSA to my voipclient (softphone) and
my problem is the sound received by the remote user is
with metal effect.
Do you know where it comes from ? From my readBuffer
function ? From my initDevice function ? ...

And where can I see the mail-archive because when I
went to 
http://www.mail-archive.com/alsa-devel@lists.sourceforge.net/
there is no the current mail, the earlier date is in
June 2004.

Thanks in advance.

Regards,
Laurielle LEA.


	

	
		
__________________________________________________________________
Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos mails ! 
Créez votre Yahoo! Mail sur http://fr.mail.yahoo.com/

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: alsa.cpp --]
[-- Type: text/x-c++src; name="alsa.cpp", Size: 4866 bytes --]


#define ALSA_DEVICE	"plughw:0,0"


AudioDriversALSA::AudioDriversALSA(DeviceMode mode) : AudioDrivers () {
	audio_hdl = (snd_pcm_t *) NULL;
	initDevice(mode);

}

AudioDriversALSA::~AudioDriversALSA (void) {
	/* Close the audio handle */
	if (audio_hdl != NULL) snd_pcm_close (audio_hdl);
}


int
AudioDriversALSA::initDevice (DeviceMode mode) {
	int	 err;
		
	if (devstate == DeviceOpened) {
		printf ("ERROR: ALSA Device Already Open !\n");
		return -1;
	}
	
	// Open the audio device
	// Flags : blocking (else have to OR omode with SND_PCM_NONBLOCK).
	switch (mode) {
	case ReadOnly:
		/* Only read sound from the device */
		err = snd_pcm_open (&audio_hdl, ALSA_DEVICE,SND_PCM_STREAM_CAPTURE, 
				SND_PCM_NONBLOCK);
		break;

	case WriteOnly:
		/* Only write sound to the device */
		err = snd_pcm_open (&audio_hdl, ALSA_DEVICE,SND_PCM_STREAM_PLAYBACK,
				SND_PCM_NONBLOCK);	
		break;
	default:
		break;
	}

	if (err < 0) {
		printf ("ERROR: ALSA/snd_pcm_open: Cannot open audio device (%s)\n",
						snd_strerror (err));
		return -1;
	}
	////////////////////////////////////////////////////////////////////////////
	// BEGIN DEVICE SETUP
	////////////////////////////////////////////////////////////////////////////
	// Allocate space for device configuration
	snd_pcm_hw_params_t	*hw_params;

	err = snd_pcm_hw_params_malloc (&hw_params);
	if (err < 0) {
		printf ("Cannot allocate hardware parameter structure (%s)\n",
				 snd_strerror (err));
		return -1;
	}

	// Init hwparams with full configuration space 
	if ((err = snd_pcm_hw_params_any (audio_hdl, hw_params)) < 0) {
		printf ("Cannot initialize hardware parameter structure (%s)\n",
				 snd_strerror (err));
		return -1;
	}

	err = snd_pcm_hw_params_set_access (audio_hdl, hw_params,
					SND_PCM_ACCESS_RW_INTERLEAVED);
	if (err < 0) {
		printf ("Cannot set access type (%s)\n", snd_strerror (err));
	}
	
	// Set sample formats (Signed, 16Bits, little endian)
	err = snd_pcm_hw_params_set_format (audio_hdl, hw_params,
					SND_PCM_FORMAT_S16_LE);
	if (err < 0) {
		printf ("Cannot set sample format (%s)\n", snd_strerror (err));
		return -1;
	} 
	
	unsigned int rate = SAMPLING_RATE;
	unsigned int exact_rate;

	exact_rate = rate;
	// Set sampling rate (8kHz)
	err = snd_pcm_hw_params_set_rate_near (audio_hdl, hw_params, 
			&exact_rate, 0);
	if (err < 0) {
		printf ("Cannot set sample rate (%s)\n", snd_strerror (err));
		return -1;
	}
	if (exact_rate != rate) {
      fprintf(stderr, "The rate %d Hz is not supported by your hardware.\n ==> Using %d Hz instead.\n", rate, exact_rate);
    }
	
	// Set number of channels - Mono(1) or Stereo(2) 
	err = snd_pcm_hw_params_set_channels (audio_hdl, hw_params, MONO);
	if (err < 0) {
		printf ("Cannot set channel count (%s)\n", snd_strerror (err));
		return -1;
	}
	
	// Apply previously setup parameters
	err = snd_pcm_hw_params (audio_hdl, hw_params);
	if (err < 0) {
		printf ("Cannot set parameters (%s)\n", snd_strerror (err));
		return -1;
	}
		
	// Free temp variable used for configuration.
	snd_pcm_hw_params_free (hw_params);
	////////////////////////////////////////////////////////////////////////////
	// END DEVICE SETUP
	////////////////////////////////////////////////////////////////////////////

	// Success
	devstate = DeviceOpened;
	return 0;
}

//////////////////////////////////////////////////////
// For PLAYBACK
// audio_buf is an object of audiobuffer class
// 		getData() : to get the data of the buffer
//		getSize() : to get the size of the buffer
/////////////////////////////////////////////////////
int
AudioDriversALSA::writeBuffer (void) {
	if (devstate != DeviceOpened) {
		printf ("ALSA: writeBuffer(): Device Not Open\n");
		return -1;
	}

	int rc;
	size_t count = audio_buf.getSize()/2;
	short *buf = (short*)audio_buf.getData();
	while (count > 0) {
		rc = snd_pcm_writei(audio_hdl, buf, count);
		if (rc == -EPIPE) {
			snd_pcm_prepare(audio_hdl);
		} else if (rc == -EAGAIN) {
			continue;
		} else if (rc < 0) {
			printf ("ALSA: write(): %s\n", strerror(errno));
			break; 
		}
		buf += rc;
		count -= rc;
	}	
	return rc;
}


//////////////////////////////////////////////////////
// For CAPTURE
/////////////////////////////////////////////////////
int
AudioDriversALSA::readBuffer (void *ptr, int bytes) {
	if( devstate != DeviceOpened ) {
		printf ("ALSA: readBuffer(): Device Not Open\n");
		return -1;
	}

	ssize_t count = bytes/2;
	ssize_t rc;

	do {
		rc = snd_pcm_readi(audio_hdl, (short*)ptr, count);
	} while (rc == -EAGAIN);
	if (rc == -EBADFD) printf ("Read: PCM is not in the right state\n");
	if (rc == -ESTRPIPE) printf ("Read: a suspend event occurred\n");
	if (rc == -EPIPE) {
			printf ("Read: -EPIPE %d\n", rc);
			snd_pcm_prepare(audio_hdl);
	}
	if (rc > 0 && rc != count) {
		printf("Read: warning: asked microphone for %d frames but got %d\n",
			count, rc);
	}

	return rc;
}


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Sound with metal effect
  2005-03-31 21:28 Sound with metal effect Laurielle LEA
@ 2005-04-01  7:39 ` Giuliano Pochini
  2005-04-01 10:07   ` Takashi Iwai
  2005-04-01 16:58   ` Manuel Jander
  0 siblings, 2 replies; 7+ messages in thread
From: Giuliano Pochini @ 2005-04-01  7:39 UTC (permalink / raw)
  To: Laurielle LEA; +Cc: alsa-devel


On 31-Mar-2005 Laurielle LEA wrote:

> I'm integrating ALSA to my voipclient (softphone) and
> my problem is the sound received by the remote user is
> with metal effect.

VoIP uses 8KHz sampling rate IIRC. What did you expect ?



--
Giuliano.


-------------------------------------------------------
This SF.net email is sponsored by Demarc:
A global provider of Threat Management Solutions.
Download our HomeAdmin security software for free today!
http://www.demarc.com/Info/Sentarus/hamr30

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Sound with metal effect
  2005-04-01  7:39 ` Giuliano Pochini
@ 2005-04-01 10:07   ` Takashi Iwai
  2005-04-01 16:58   ` Manuel Jander
  1 sibling, 0 replies; 7+ messages in thread
From: Takashi Iwai @ 2005-04-01 10:07 UTC (permalink / raw)
  To: Giuliano Pochini; +Cc: Laurielle LEA, alsa-devel

At Fri, 01 Apr 2005 09:39:51 +0200 (CEST),
Giuliano Pochini wrote:
> 
> 
> On 31-Mar-2005 Laurielle LEA wrote:
> 
> > I'm integrating ALSA to my voipclient (softphone) and
> > my problem is the sound received by the remote user is
> > with metal effect.
> 
> VoIP uses 8KHz sampling rate IIRC. What did you expect ?

It might be the rate conversion, too.
A better but more CPU-costed rate conversion algorithm would sound
better.


Takashi


-------------------------------------------------------
This SF.net email is sponsored by Demarc:
A global provider of Threat Management Solutions.
Download our HomeAdmin security software for free today!
http://www.demarc.com/Info/Sentarus/hamr30

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Sound with metal effect
@ 2005-04-01 15:17 Laurielle LEA
  2005-04-02 17:41 ` Lee Revell
  0 siblings, 1 reply; 7+ messages in thread
From: Laurielle LEA @ 2005-04-01 15:17 UTC (permalink / raw)
  To: mjander; +Cc: Alsa-devel

In fact, in my read function, 

count = 320;
rc = snd_pcm_readi(audio_hdl, (short*)ptr, count);
I have to have rc = 320 but I receive just 160.

You tell me about other algorithms but I don't
understand everything. Maybe I have to add other init
hardware parameters ?

--- Manuel Jander <manuel.jander@usm.cl> wrote:
> Hi,
> 
> On Fri, 2005-04-01 at 09:39 +0200, Giuliano Pochini
> wrote:
> > On 31-Mar-2005 Laurielle LEA wrote:
> > 
> > > I'm integrating ALSA to my voipclient
> (softphone) and
> > > my problem is the sound received by the remote
> user is
> > > with metal effect.
> > 
> > VoIP uses 8KHz sampling rate IIRC. What did you
> expect ?
> 
> Metal effect indicates bad antialiasing filters.
> That may be caused by
> bad hardware, bad hardware setup or a very bad
> software emulation. 8KHz
> sound obviously does not sound CD-quality, but it
> should not have
> aliasing noise. It should just sound with lower
> bandwidth, specially
> higher frequencies being cutoff. Standard phone
> bandwidth is between
> 300-3400 Hz.
> Software resampling through removing or adding
> samples, and doing some
> mean value cooking is far from optimal. It would be
> better to use a
> recursive Z-tranform (or whatever math tool you
> prefer) synthesised
> algorithm. Even a small IIR filter for
> postprocessing would barely
> consume any CPU power. Such algorithms are well
> known and available.
> 
> Best Regards
> -- 
> Manuel Jander
> Dipl.-Ing. Fachrichtung Elektronik
> 
> 
> 
>
-------------------------------------------------------
> This SF.net email is sponsored by Demarc:
> A global provider of Threat Management Solutions.
> Download our HomeAdmin security software for free
> today!
> http://www.demarc.com/Info/Sentarus/hamr30
> _______________________________________________
> Alsa-devel mailing list
> Alsa-devel@lists.sourceforge.net
>
https://lists.sourceforge.net/lists/listinfo/alsa-devel
> 


	

	
		
__________________________________________________________________
Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos mails ! 
Créez votre Yahoo! Mail sur http://fr.mail.yahoo.com/


-------------------------------------------------------
This SF.net email is sponsored by Demarc:
A global provider of Threat Management Solutions.
Download our HomeAdmin security software for free today!
http://www.demarc.com/Info/Sentarus/hamr30

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Sound with metal effect
  2005-04-01  7:39 ` Giuliano Pochini
  2005-04-01 10:07   ` Takashi Iwai
@ 2005-04-01 16:58   ` Manuel Jander
  1 sibling, 0 replies; 7+ messages in thread
From: Manuel Jander @ 2005-04-01 16:58 UTC (permalink / raw)
  To: alsa-devel

Hi,

On Fri, 2005-04-01 at 09:39 +0200, Giuliano Pochini wrote:
> On 31-Mar-2005 Laurielle LEA wrote:
> 
> > I'm integrating ALSA to my voipclient (softphone) and
> > my problem is the sound received by the remote user is
> > with metal effect.
> 
> VoIP uses 8KHz sampling rate IIRC. What did you expect ?

Metal effect indicates bad antialiasing filters. That may be caused by
bad hardware, bad hardware setup or a very bad software emulation. 8KHz
sound obviously does not sound CD-quality, but it should not have
aliasing noise. It should just sound with lower bandwidth, specially
higher frequencies being cutoff. Standard phone bandwidth is between
300-3400 Hz.
Software resampling through removing or adding samples, and doing some
mean value cooking is far from optimal. It would be better to use a
recursive Z-tranform (or whatever math tool you prefer) synthesised
algorithm. Even a small IIR filter for postprocessing would barely
consume any CPU power. Such algorithms are well known and available.

Best Regards
-- 
Manuel Jander
Dipl.-Ing. Fachrichtung Elektronik



-------------------------------------------------------
This SF.net email is sponsored by Demarc:
A global provider of Threat Management Solutions.
Download our HomeAdmin security software for free today!
http://www.demarc.com/Info/Sentarus/hamr30

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Sound with metal effect
  2005-04-01 15:17 Laurielle LEA
@ 2005-04-02 17:41 ` Lee Revell
  0 siblings, 0 replies; 7+ messages in thread
From: Lee Revell @ 2005-04-02 17:41 UTC (permalink / raw)
  To: Laurielle LEA; +Cc: mjander, Alsa-devel

On Fri, 2005-04-01 at 17:17 +0200, Laurielle LEA wrote:
> In fact, in my read function, 
> 
> count = 320;
> rc = snd_pcm_readi(audio_hdl, (short*)ptr, count);
> I have to have rc = 320 but I receive just 160.
> 
> You tell me about other algorithms but I don't
> understand everything. Maybe I have to add other init
> hardware parameters ?
> 

Your read function takes the third argument in bytes.  But snd_pcm_readi
expects "count" to be in frames.  It looks like you have an 8 bit stereo
stream, or 2 bytes per frame.  So you did get 160 frames (320 bytes).
It looks like you need to set the device to 8 bit mono.

Please see alsa-lib/test/latency.c for an example of the correct usage.

Lee



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Sound with metal effect
@ 2005-04-04 13:48 Laurielle LEA
  0 siblings, 0 replies; 7+ messages in thread
From: Laurielle LEA @ 2005-04-04 13:48 UTC (permalink / raw)
  To: Lee Revell; +Cc: mjander, Alsa-devel


--- Lee Revell <rlrevell@joe-job.com> wrote:
> On Fri, 2005-04-01 at 17:17 +0200, Laurielle LEA
> wrote:
> > In fact, in my read function, 
> > 
> > count = 320;
> > rc = snd_pcm_readi(audio_hdl, (short*)ptr, count);
> > I have to have rc = 320 but I receive just 160.
> > 
> > You tell me about other algorithms but I don't
> > understand everything. Maybe I have to add other
> init
> > hardware parameters ?
> > 
> 
> Your read function takes the third argument in
> bytes.  But snd_pcm_readi
> expects "count" to be in frames.  It looks like you
> have an 8 bit stereo
> stream, or 2 bytes per frame.  So you did get 160
> frames (320 bytes).
> It looks like you need to set the device to 8 bit
> mono.
> 
> Please see alsa-lib/test/latency.c for an example of
> the correct usage.
> 
> Lee
> 

But I have set 
snd_pcm_hw_params_set_channels (audio_hdl,
hw_params,1)

If I write:

int
AudioDriversALSA::readBuffer (void *ptr, int bytes) {
  ssize_t count = bytes/2;   /* bytes=320 */
  ssize_t rc;
  do {
    rc = snd_pcm_readi(audio_hdl, (short*)ptr, count);
  } while (rc == -EAGAIN);
  
  if (rc == -EPIPE) {
    snd_pcm_prepare(audio_hdl);
  }
 
  return rc;
}

I need to write  "rc = rc * 2;" before return rc in
order to send enough data, otherwise I receive metal
sound (microphone required 320 frames but got 160).
Maybe the variable names aren't chosen well.

Can you help me again ?
Does this pb generate  my overrun pb?

Thanks a lot.

Laurielle.


	

	
		
__________________________________________________________________
Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos mails ! 
Créez votre Yahoo! Mail sur http://fr.mail.yahoo.com/


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-04-04 13:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-31 21:28 Sound with metal effect Laurielle LEA
2005-04-01  7:39 ` Giuliano Pochini
2005-04-01 10:07   ` Takashi Iwai
2005-04-01 16:58   ` Manuel Jander
  -- strict thread matches above, loose matches on Subject: below --
2005-04-01 15:17 Laurielle LEA
2005-04-02 17:41 ` Lee Revell
2005-04-04 13:48 Laurielle LEA

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.