Skip to contentSkip to conversation

Starting the device manually

DISCUSSION Eric Peters 3 participants 7 messages Jul 24, 2006 – Aug 4, 2006

lore
Share & tools
lore
#0 lore raw

Dear all,

I'm a bit confused by the alsa documentation. I want to fill up a buffer of a alsa device with snd_pcm_writei and then if the buffer is full, I want to be able to start the playback manually. By looking to the documentation, I found the method snd_pcm_sw_params_set_start_threshold. In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically. Here are the lines of code, I'm setting the buffer_size and period_size and the start threshhold:

/* determine the buffer size */
if (buffer_time == 0 && buffer_frames == 0) {
if( snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0)

< 0) {
     return -1;
   }
   if (buffer_time > 500000) {
     buffer_time = 500000;

}
}

/* calculating the period size depending on the buffer size */ if (period_time == 0 && period_frames == 0) {

   if (buffer_time > 0) {
     period_time = buffer_time / 4;
   } else {
     period_frames = buffer_frames / 4;

}
}

/* setting the period size according to the buffer size */ if (period_time > 0) {
if (snd_pcm_hw_params_set_period_time_near(pcm_handle, hwparams,

&period_time, 0) < 0) {
     return -1;
   }
} else {
   if ( snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams,
&period_frames, 0) < 0) {
     return -1;

}
}

/* setting the buffer size */
if (buffer_time > 0) {
if (snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams,

&buffer_time, 0) < 0) {
     return -1;
   }
} else {
   if ( snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams,
&buffer_frames) < 0) {
     return -1;

}
}

/* calculating the number of frames of a buffer and a period */ buffer_frames = buffer_time * sample_rate; period_frames = period_time * sample_rate;

/* setting the start threshhold in frames */ int err = 0;
/*Init swparams */
if ((err = snd_pcm_sw_params_malloc (&swparams)) < 0) {
return -1;
}

if ((err = snd_pcm_sw_params_current(pcm_handle, swparams)) < 0) {
return -1;
}

if ((err = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 2*buffer_frames)) < 0) {
return FAILURE;
}

err = snd_pcm_sw_params(pcm_handle, swparams);
if (err < 0) {
return -1;
}

Here are some specifications of the system I'm using:

- SuSE 9.3 with all updates and patches
- ALSA version 1.0.9a
- sound board Audigy 4 PRO [SB0380]
- chip SigmaTel STAC9750,51

Now some questions:
- Where is the failure in my implementation?
- I think, if I don't call snd_pcm_start there should be no output if the start threshold is larger than the max_buffer_size. Is this correct?
- Is it possible that the starting behaviour depends on the hardware I'm using?
- Do you have other hints so I can start my playback manually on a timepoint I want?

Best regards,
Eric

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#1 lore raw
Quoted 8 lines · Eric Peters

I'm a bit confused by the alsa documentation. I want to fill up a buffer of a alsa device with snd_pcm_writei and then if the buffer is full, I want to be able to start the playback manually. By looking to the documentation, I found the method snd_pcm_sw_params_set_start_threshold. In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically.

What are the contents of /proc/asound/card?/pcm0p/sub0/sw_params during playback?

Quoted 3 lines

/* calculating the number of frames of a buffer and a period */ buffer_frames = buffer_time * sample_rate; period_frames = period_time * sample_rate;

Is this correct? Aren't the xxx_time variables in microseconds?

Quoted 1 line

snd_pcm_sw_params_set_start_threshold(..., 2*buffer_frames)

This is OK, but it is common to use just the boundary value instead of some explicitly calculated value.

Quoted 2 lines

- I think, if I don't call snd_pcm_start there should be no output if the start threshold is larger than the max_buffer_size. Is this correct?

... if the start threshold is larger than the current buffer size, in frames. Yes.

Quoted 2 lines

- Is it possible that the starting behaviour depends on the hardware I'm using?

No, the software parameters are hardware independent.

Regards,
Clemens

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#2 lore raw
Quoted 10 lines · Clemens Ladisch
Quoted 8 lines · Eric Peters

I'm a bit confused by the alsa documentation. I want to fill up a buffer of a alsa device with snd_pcm_writei and then if the buffer is full, I want to be able to start the playback manually. By looking to the documentation, I found the method snd_pcm_sw_params_set_start_threshold. In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically.

Quoted 2 lines

What are the contents of /proc/asound/card?/pcm0p/sub0/sw_params during playback?

tstamp_mode: NONE
period_step: 1
sleep_min: 0
avail_min: 1
xfer_align: 4096
start_threshold: 2703204728
stop_threshold: 16384
silence_threshold: 0
silence_size: 0
boundary: 1073741824

Can you explain me these values and what they're for? I think, I know the sense of start and stop threshold. But the other values...

Quoted 6 lines
Quoted 4 lines

/* calculating the number of frames of a buffer and a period */ buffer_frames = buffer_time * sample_rate; period_frames = period_time * sample_rate;

Is this correct? Aren't the xxx_time variables in microseconds?

I don't know. ;-)
How is a frame defined?

Quoted 5 lines
Quoted 2 lines

snd_pcm_sw_params_set_start_threshold(..., 2*buffer_frames)

This is OK, but it is common to use just the boundary value instead of some explicitly calculated value.

The boundary value in the output above? How can I determine this value in my application?

Quoted 7 lines
Quoted 3 lines

- I think, if I don't call snd_pcm_start there should be no output if the start threshold is larger than the max_buffer_size. Is this correct?

... if the start threshold is larger than the current buffer size, in frames. Yes.

Quoted 8 lines
Quoted 2 lines

- Is it possible that the starting behaviour depends on the hardware I'm using?

No, the software parameters are hardware independent.

Regards,
Clemens

Thanks Clemens for your fast answer.

Best regards,

Eric

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#4 lore raw
In reply to Eric Peters
Quoted 13 lines · Eric Peters

Dear all,

I'm a bit confused by the alsa documentation. I want to fill up a buffer of a alsa device with snd_pcm_writei and then if the buffer is full, I want to be able to start the playback manually. By looking to the documentation, I found the method snd_pcm_sw_params_set_start_threshold. In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically. Here are the lines of code, I'm setting the buffer_size and period_size and the start threshhold:

Why would you actually want to do what you describe. (You certainly can do it with ALSA, but as to why you would want to, is another thing entirely) It is normally better to get the buffers running, sending silence samples to the buffer, and then introduce your sound when you want to. One can accurately determine the delay or latency of the buffer, so accurately timing samples is not a problem.
One uses a callback approach to send sound to the card. A good example of this callback approach is an application called jackd.
http://jackaudio.org/

James

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#6 lore raw
In reply to Eric Peters
Quoted 13 lines · Eric Peters

Dear all,

I'm a bit confused by the alsa documentation. I want to fill up a buffer of a alsa device with snd_pcm_writei and then if the buffer is full, I want to be able to start the playback manually. By looking to the documentation, I found the method snd_pcm_sw_params_set_start_threshold. In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically. Here are the lines of code, I'm setting the buffer_size and period_size and the start threshhold:

Why would you actually want to do what you describe. (You certainly can do it with ALSA, but as to why you would want to, is another thing entirely) It is normally better to get the buffers running, sending silence samples to the buffer, and then introduce your sound when you want to. One can accurately determine the delay or latency of the buffer, so accurately timing samples is not a problem.
One uses a callback approach to send sound to the card. A good example of this callback approach is an application called jackd.
http://jackaudio.org/

James

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#5 lore raw
In reply to James Courtier-Dutton
Quoted 25 lines · James Courtier-Dutton
Quoted 13 lines · Eric Peters

Dear all,

I'm a bit confused by the alsa documentation. I want to fill up a buffer of a alsa device with snd_pcm_writei and then if the buffer is full, I want to be able to start the playback manually. By looking to the documentation, I found the method snd_pcm_sw_params_set_start_threshold. In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically. Here are the lines of code, I'm setting the buffer_size and period_size and the start threshhold:

Why would you actually want to do what you describe. (You certainly can do it with ALSA, but as to why you would want to, is another thing entirely)
It is normally better to get the buffers running, sending silence samples to the buffer, and then introduce your sound when you want to. One can accurately determine the delay or latency of the buffer, so accurately timing samples is not a problem.
One uses a callback approach to send sound to the card. A good example of this callback approach is an application called jackd.
http://jackaudio.org/

Quoted 2 lines

James

Thank you James for your answer,

but unfortunately it isn't a real simple application. So it wouldn't be possible to start the device with silence samples. But I will take this approach into account to make it better. :-)

Best regards,
Eric

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#3 lore raw
In reply to Eric Peters
Quoted 11 lines · Eric Peters
Quoted 8 lines · Clemens Ladisch
Quoted 4 lines · Eric Peters

In some other messages on this list there was the hint to set this threshhold larger than the max_buffer_size and then the device shouldn't start automatically. So I've done. But unfortunately the playback starts again automatically.

What are the contents of /proc/asound/card?/pcm0p/sub0/sw_params during playback?

start_threshold: 2703204728

This is way too large. In fact, it's so large that, as a signed value, it's negative.

Just use the boundary value, which is appropriate for 'almost infinity' values like this.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

/* calculating the number of frames of a buffer and a period */ buffer_frames = buffer_time * sample_rate; period_frames = period_time * sample_rate;

Is this correct? Aren't the xxx_time variables in microseconds?

I don't know. ;-)
How is a frame defined?

It's the amount of data that is transferred in one sample period. It's more than one sample when there is more than one channel.

The buffer_time/period_time variables are in microseconds, while the sample_rate uses seconds (Hz = 1/s), so you would have to divide by 1000000 for the calculation to make sense.

Quoted 5 lines
Quoted 2 lines

..., but it is common to use just the boundary value instead of some explicitly calculated value.

The boundary value in the output above? How can I determine this value in my application?

snd_pcm_sw_params_get_boundary()

HTH
Clemens

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV