[Haskell-cafe] FFI woes!

Sebastian Sylvan sebastian.sylvan at gmail.com
Sat Dec 18 04:39:13 EST 2004

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
> >gone).
> >
> >
> >
> 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
> sample
> 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
> samples
> to remain in memory until the program finishes (like sound effects in a
> game,
> 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.


Sebastian Sylvan
UIN: 44640862

More information about the Haskell-Cafe mailing list