* Distorted playback with libsndfile
@ 2005-07-14 19:13 Jan Buchholz
0 siblings, 0 replies; only message in thread
From: Jan Buchholz @ 2005-07-14 19:13 UTC (permalink / raw)
To: alsa-devel
Hi!
I am currently working on a small app, which uses
the libsndfile to load audio files. But I cannot
seem to get the playback working properly.
The snd_pcm_t instance is set up and configured
in the following method:
int audioCtrl::setFormat(unsigned channels, unsigned rate,
int bufferSize){
snd_pcm_hw_params_t *params;
snd_pcm_format_t fmt;
int actualChannels = channels;
int err;
bool errorOccured = false;
// Allocating parameter handle
if ((err = snd_pcm_hw_params_malloc (¶ms)) < 0) {
printf("Malloc");
errorOccured = true;
}
// Opening the sound device
if ((err = snd_pcm_open (&outputHandle, deviceName,
SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Open");
errorOccured = true;
}
// Check if valid parameters are available
if ((err = snd_pcm_hw_params_any (outputHandle, params)) < 0) {
printf("Any");
errorOccured = true;
}
// Set access method; Interleaved - alternating channel data
if ((err = snd_pcm_hw_params_set_access (outputHandle, params,
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
printf("Access");
errorOccured = true;
}
// Probing the device for the most common data formats
if (snd_pcm_hw_params_set_format (outputHandle, params,
SND_PCM_FORMAT_S16) == 0) {
fmt = SND_PCM_FORMAT_S16;
} else if (snd_pcm_hw_params_set_format (outputHandle, params,
SND_PCM_FORMAT_S32_LE) == 0) {
fmt = SND_PCM_FORMAT_S32_LE;
} else if (snd_pcm_hw_params_set_format (outputHandle, params,
SND_PCM_FORMAT_S8) == 0) {
fmt = SND_PCM_FORMAT_S8;
} else if (snd_pcm_hw_params_set_format (outputHandle, params,
SND_PCM_FORMAT_FLOAT_LE) == 0) {
fmt = SND_PCM_FORMAT_FLOAT_LE;
} else {
printf("Format");
errorOccured = true;
}
// Probing the device for the samplerate of the sample
if ((err = snd_pcm_hw_params_set_rate_near (outputHandle, params, &rate,
0)) < 0) {
printf("Rate");
errorOccured = true;
}
// setting the number of channels
if ((err = snd_pcm_hw_params_set_channels (outputHandle, params,
channels)) < 0) {
printf("Channels");
errorOccured = true;
}
// Setting the parameters
if ((err = snd_pcm_hw_params (outputHandle, params)) < 0) {
printf("Params");
errorOccured = true;
}
// Preparing the device for playback
if ((err = snd_pcm_prepare (outputHandle)) < 0) {
printf("Prepare");
errorOccured = true;
}
snd_pcm_hw_params_free (params);
if(errorOccured) return -1;
else if(fmt == SND_PCM_FORMAT_S8) return 8;
else if(fmt == SND_PCM_FORMAT_S16) return 16;
else if(fmt == SND_PCM_FORMAT_S32_LE) return 32;
else if(fmt == SND_PCM_FORMAT_FLOAT) return 1;
else return 0;
}
After this initialisation, a separate thread gets buffers from
the sndfile instance and writes them to the device using the
following method:
bool audioCtrl::playBuffer(void *buffer, int size){
int err;
if ((err = snd_pcm_writei (outputHandle, buffer, size)) != size)
return false;
else return true;
}
For 16 bit playback, I have tried using buffers from both sf_read_short
and sf_read_int with different results:
buffers from sf_read_short sound totally distorted and with buffers from
sf_read_int (stereo file) data from both channels are played only on the
right channel in half speed (I suppose the first 16 bits of the ints are
0s, thus only silence is written to the left channel).
Is there an error in the initialisation method? Did I forget to set a
parameter? I did not fully grasp the concept of periods, so maybe that
could be the problem!? I guess the sndfile buffers should work without
conversion.
Greets,
Jan
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2005-07-14 19:13 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-14 19:13 Distorted playback with libsndfile Jan Buchholz
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.