[Haskell-cafe] FFI woes!

Keean Schupke k.schupke at imperial.ac.uk
Sat Dec 18 05:33:25 EST 2004

I still think you need to use a StablePtr, so the sample will not get
garbage collected.

You then need to free the sample explicitly when playback has finished...

Ideally you want to add a callback to do the free-ing. In my opinion if the
library you are using does not provide this callback then the library is
broken - and you should try and find a different library which does things

If you cannot find an alternate library I guess you have to poll playback of
the sample before freeing.

    with_channel 1 $ do
        play_sample my_sample
        free my_sample

If you wish to support multiple channels, you would fork a thread to
deal with each channel...

There are too many unknowns to say much more than this... If you
queue two samples to the same channel, how do you detemine
when the first sample has finished (as polling the channed will
tell you it is still playing).

I get the feeling the traditional way to manage this is to load _all_ the
samples for a song/game level in one go, and keep them until the whole
song or game-level is finished.


Sebastian Sylvan wrote:

>On Sat, 18 Dec 2004 09:07:12 +0000, Keean Schupke
><k.schupke at imperial.ac.uk> wrote:
>>Sebastian Sylvan wrote:
>>>What do you mean?
>>>Yes the sample is copied to memory when loaded, but I still have to
>>>"worry" because if it's garbage colleced (ie, a "free" call is called
>>>on it) then the sound can not be played anymore (because the data is
>>I mean the sound card has its own, independant memory, so it should be
>>okay to let the sample be garbage collected. I suggest this because if the
>>sound library has no way to tell if the sample has finished playing (they
>>provide a callback in some cases but not this case) then the library author
>>must have considered it not necessary for some reason. The only reason I
>>can think of that it would not be necessary to be notified of when the
>>has been finished with is if the 'play' call makes an internal copy of
>>the sample
>>in the sound-card buffer, and it is safe to delete the sample
>>immediately play
>>returns. The other possibility is that the library author intended all
>>to remain in memory until the program finishes (like sound effects in a
>>you load them once and just keep them).
>If the sound data is removed, then the playback will stop because the
>data is gone.
>There is a way to find if a sound data is playing, but only if you
>know which channels you have loaded it into. Then you can see if that
>channel is currently playing something.
>When the sound data is garbage collected the sound data will be
>removed via a call to the C library function "free". This causes all
>playback of that sound to stop, which exactly the problem I'm trying
>to solve!
>I want a "channel" object (spawned with a call to "play") with a
>reference to the sound data, so that the sound data will be kept alive
>as long as the channel is kept alive. That's easy and already solved.
>Then, however, I also want to defer collection of the channel object
>while it's playing something - which I have not been able to solve
>I've tried to create a ForeignPtr to this Haskell value (StablePtr ->
>Ptr -> ForeignPtr) and attatch a finalizer to it, which spawns a
>keep-alive loop when the RTS tries to collect the channel.
>I've tried weak pointers to attatch a finalizer which also spawns a
>keep-alive loop.
>So far nothing's worked.

More information about the Haskell-Cafe mailing list