Skip to contentSkip to conversation

Problem with setting period time and buffer time.

DISCUSSION Ignacy Kasperowicz 2 participants 8 messages Feb 18, 2008 – Feb 20, 2008 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hello.

I need to set proper period time for my application. What I need my code to do is to play sine wave with set frequency and for exact time of 1/1200 s (something about 834 us). When I try to set period time for 834 us I've got this error:
Unable to set period time 834 for playback: Invalid argument . Honestly speaking I've got this error message no matter what value I try to set).
The same situation take place when I try to set buffer time for something about 1000us (so one period could fit in my buffer).

The purpose of this program is to generate sine wave for AFSK (audio frequency-shift keying). I want to play wave for period_time (834us) then if needed change frequency and play changed wave again for period_time with phase shifting.

Wave generation with phase shifting for variable frequencies works, but not with period_time I need.

Could anyone look at source and tell me why I can't set this values?

I placed code with syntax highlighting at: http://pygments.org/demo/606/ . (Don't look at prepare_message function, it isn't used right now).

Thanks from advice.

Ignacy.

Signature · 4 lines
################################################
gry świetlicowe drogą do wyższej fazy przemian.
gg:681548 jid: [email protected] usecide.net
################################################
#1 lore raw
Quoted 3 lines · Ignacy Kasperowicz

I need to set proper period time for my application. What I need my code to do is to play sine wave with set frequency and for exact time of 1/1200 s (something about 834 us).

The period time specifies after which interval the sound hardware issues an interrupt, i.e., after which time your application gets waken up when it is waiting for the sound buffer to become non-empty. This has nothing to do with the actual sounds that are being played.

You are not required to write data in period-sized chunks, so you don't actually need to care about the period time.

Quoted 4 lines

When I try to set period time for 834 us I've got this error:
Unable to set period time 834 for playback: Invalid argument . Honestly speaking I've got this error message no matter what value I try to set).

Period (and buffer) times are hardware dependent; you cannot be sure that any specific value is supported by the sound card.

(And you're calling snd_pcm_hw_params_set_period_time_max() which would allow the driver to choose any lower value.)

HTH
Clemens

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 1 line · Ignacy Kasperowicz

But what about buffer_time, why can't I set this time ?

The allowed range of values is hardware dependent, like the period time. Use snd_pcm_hw_params_set_buffer_time_near().

Quoted 1 line

Anyway is there a method to play my wave for a specified short time?

Just write the data to the device.

What is the problem?

Regards,
Clemens

#4 lore raw
Quoted 8 lines

Just write the data to the device.

What is the problem?

Regards,
Clemens

Ok, but when I just write data to the device I don't really know how long it'll be playing. There is no problem when I've infinite loop with writei inside. but I need to change freq of my sound signal with exact 843 us period time.

Thanks for answering Clemens.

Signature · 4 lines
################################################
gry świetlicowe drogą do wyższej fazy przemian.
gg:681548 jid: [email protected] usecide.net
################################################
#5 lore raw
Quoted 4 lines · Ignacy Kasperowicz
Quoted 1 line

Just write the data to the device.

Ok, but when I just write data to the device I don't really know how long it'll be playing.

But you know how many sample you are writing, and you know the sample rate.

Quoted 2 lines

There is no problem when I've infinite loop with writei inside. but I need to change freq of my sound signal with exact 843 us period time.

Assuming a sample rate of 48 kHz, 1/1200 s corresponds to exactly 40 samples. At 44.1 kHz, this would be 36.75 samples, so you'd have to use groups of 37+37+37+36 samples with appropriate phase shifting.

BTW: 1 / 1200 Hz = 833.33... µs

HTH
Clemens
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#6 lore raw

2008/2/19, Clemens Ladisch <[email protected]>:

Quoted 16 lines
Quoted 4 lines · Ignacy Kasperowicz
Quoted 1 line

Just write the data to the device.

Ok, but when I just write data to the device I don't really know how long it'll be playing.

But you know how many sample you are writing, and you know the sample rate.

Quoted 2 lines

There is no problem when I've infinite loop with writei inside. but I need to change freq of my sound signal with exact 843 us period time.

Assuming a sample rate of 48 kHz, 1/1200 s corresponds to exactly 40 samples. At 44.1 kHz, this would be 36.75 samples, so you'd have to use groups of 37+37+37+36 samples with appropriate phase shifting.

Quoted 6 lines

BTW: 1 / 1200 Hz = 833.33... µs

HTH
Clemens

Thanks for correction, I was trying to make this too complicated :P I dunno why I even thought about just setting how many samples to write in writei() :P

Although, when from calculations indeed I should set 40 samples to get sine played for 1/1200Hz, in my code I need to set frames size value for 400. Do You know why? I have 48kHz rate, one channel and 16 bit PCM.

Quoted 1 line

From what I've read there:

https://bugtrack.alsa-project.org/wiki/wikka.php?wakka=FramesPeriods , "A frame is equivalent of one sample being played" so why I need multiply calculation result by 10 ?
I think this is a last rookie question :)

Thanks for answers :)
ignacy

PS. Yeah I know its 833.33...us, I've just rounded it up for experimenting ;)

Signature · 4 lines
################################################
gry świetlicowe drogą do wyższej fazy przemian.
gg:681548 jid: [email protected] usecide.net
################################################
#7 lore raw

Everything works, just ignore post above :)

Thanks for help :)

Best regards.
Ignacy

Signature · 4 lines
################################################
gry świetlicowe drogą do wyższej fazy przemian.
gg:681548 jid: [email protected] usecide.net
################################################
#8 lore raw
In reply to a message not in the archive

Ok, so I misunderstood the idea of a period variable, many thanks for explanation Clemens.

I've just read that buffer_time also isn't what I think it was ;) It's a size of the ring buffer of sound card and it's length depends on hardware, am I right?
So the best way is to depend on default values of hardware?

Anyway is there a method to play my wave for a specified short time? As I can see using timer interface is the only solution but still can't google anything useful :( Small example how to apply timers will be appreciated (I saw example timer.c from alsa-lib, but I'd prefer to read some explanation how the code works).

Thanks from advance for any replays.

Signature · 4 lines
################################################
gry świetlicowe drogą do wyższej fazy przemian.
gg:681548 jid: [email protected] usecide.net
################################################