Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

streaming audio

by brillig (Acolyte)
on Jun 05, 2004 at 01:45 UTC ( #361313=perlquestion: print w/replies, xml ) Need Help??

brillig has asked for the wisdom of the Perl Monks concerning the following question:

I found an example of how to set up an http stream client (ie: icecast client) in perl here.

However, for the project i'm working on, I need th audio output to be buffered. I've tried a hundred different ways of doing this, and it always skips or jumps or repeats itself no matter what I do.

Is perl even capable of quickly buffering output like this?
Yes, I understand the concept of buffering. So far I've tried essentially 2 ways:

1. fill a FIFO array with data read from the input stream. print the shifted data off the array to the player stream and get more data in a loop.
2. do the same thing, but with a string instead of an array, and use a fixed output size.

Neither of these work, at least not the way I'm doing it. I had some suggestions about setting the input stream to be non-blocking (using fcntl), which sort of works, but still doesn't solve the underlying issue: the buffer needs to be constantly printing to the player stream, even if we're waiting for input from the input stream.

Replies are listed 'Best First'.
Re: streaming audio
by rcaputo (Chaplain) on Jun 05, 2004 at 06:17 UTC

    First be sure that your network is fast enough to handle the stream. No amount of Perl wizardry will receive data faster than your network can deliver it.

    Second, try increasing the number on line 40 from 512 to some multiple of your operating system's block size. Try 16384, for example. Reading and writing data in larger chunks is more efficient, up to a point.

    Finally, your skipping problem may be caused by data being buffered on the pipe to the handler program. Use the unbuffering trick from lines 19-21 just after HANDLER is opened (line 38). It will help ensure that the handler program doesn't wait for a pipe's buffer to fill up.

    -- Rocco Caputo -

Re: streaming audio
by kragen (Sexton) on Jun 05, 2004 at 03:04 UTC
    By "buffered", I think you mean that you want to ensure that (a) the audio driver's buffer of data never gets empty and (b) the audio stream over the network stays well ahead of the data being played through the speaker. You can go a long way toward (b) by waiting until you have several seconds' worth of data available before you start writing data to the audio driver. (a) may or may not be harder; I don't know enough about audio drivers.

    On a related topic, you may benefit from boosting the scheduling priority of your program.

Re: streaming audio
by chanio (Priest) on Jun 05, 2004 at 03:07 UTC
    Thanks for the address! It looks nice.


    "First of all one desirable feature would be to also store the incoming data into a file as well as passing it along to the handler..."

    If a stream is like the real stream or little river, then a buffer should be like a pond or little lake.

    If you get large chunks of data and you dossify the constant output of it, you have a buffer...

    Perhaps, just by reading from the recorded file, instead of piping, it might work as a buffer.

    As the author says, it is like the 'look ahead' of car's cd players.

    How could you do this? Well, you'ld better try and you'ld learn a lot in the meantime. After that, instead of using your small experience, I bet that you would be able to find a good buffering .PM at cpan.

    But, remember that most mp3 applications, have their own buffer. Like Winamp, that you can tune up at the config. Read about it, also. I remember reading that it works better with a small buffer (measured in seconds).

    _`(___)' __________________________
Re: streaming audio
by Ryszard (Priest) on Jun 06, 2004 at 10:00 UTC
    Here is a cool example that has had a few contributions.

    It does a fork for each connection, and worked "Straight-out-of-the-box (tm)" for me.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://361313]
Approved by ybiC
Front-paged by coreolyn
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-10-02 02:34 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (7 votes). Check out past polls.