Re: Threads for ongoing processes

From: Billw (notarealemail_at_nowhere.com)
Date: 01/31/05


Date: Mon, 31 Jan 2005 08:35:00 -0500

Actually, if you use a callback to trigger the buffer preparation and
submission, it already will be in a separate thread (the MME thread in
Windows).

Do it like so:

1. In your main thread, prepare your first several buffers and submit them,
setting up MME to call your callback function when done with each. I use
triple buffers, but double is usually sufficient. Start the Wav engine
"paused", so you can unpause it when all buffers have been initialized.
Then unpause to get it going.
2. in the callback function (that is called when a buffer has played),
prepare the replacement buffer. This occurs in the MME thread. But DONT
submit the buffer from the callback function, as you aren't supposed to call
that MME API function from within a callback. Instead, Post (not Send!)
your app (or a thread within it) a Message to tell it that the buffer is
ready. (Or use some other mechanism such as Windows' SetEvent to trigger
the submission outside of your callback).
3. In the message handler (or in whatever code ---outside the callback
thread -- that knows when a buffer is ready) resubmit the newly prepared
buffer.
4. You don't need to "go back' anywhere, since everything is triggered by
callbacks; it will keep itself running. Step 3 is where you would put the
code to close things down when all is finished or the user wants the sound
to stop.

It's not very difficult. Just remember to keep your hands off each buffer
until the callback tells you it is ready again.

"Bram Bos" <bram@mindless.com> wrote in message
news:e386cf42.0501310227.42639d13@posting.google.com...
> I'm working on an application that processes audio and plays
> it back using common MME functions. The process looks like this:
>
> 1. send buffer to playback output
> 2. prepare new buffer with sound data
> 3. callback: "playback is done" > go to step 1.
>
> I wish to do this using a separate thread, because the user
> is messing around in lots of graphical GUI things in the main
> thread. I can't really figure out how to assign this
> callback/process to a thread though. I know how to create
> a thread for a one-time job, but not how to do it for an
> ongoing process (using a callback mechanism)...
>
> can one of the Delphi gurus around here shed some light?
>
> Thanks!
>
> Bram



Relevant Pages

  • Re: server scenario - variables in the right spot?
    ... If I'm correct in what I think you mean by 'data structures' then I also ... steps, the command has, and a way to mark which steps are done or not done, ... Assuming you are using the same receive callback for each BeginReceive, ... that processes the data for the first buffer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Unicast UDP Server
    ... From what I gathered from your post, invoking "connect" with a remote address in a UDP socket will ensure that the receive callback is only invoked with data received from the specified remote address, correct? ... Without the 75 MB receive buffer, we _occasionally_ find the buffer overfilling. ... You haven't made any mention as to what your actual network speed and load is, but I'll reiterate my previous statement: only on the fastest networks, under the highest loads, should a buffer of 75MB be useful. ... If you are seeing delays that long, there's probably a better way to deal with them. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: callback function problem
    ... Hello Tomaz My name is Diego and I 'm from Italy. ... I need to know how to create datagrams of voice through a buffer listening ... Callback function runs in separated thread under main dll thread. ...
    (microsoft.public.dotnet.general)
  • Re: [PATCH 3/3] relay: Add buffer-only channels; useful for early logging.
    ... 20d8b67c06fa5e74f44e80b0a0fd68c8327f7c6a) broke LTTng. ... buffer file creation callback to be called when it calls relay_open. ... Therefore, such channels can be ...
    (Linux-Kernel)
  • Re: Unicast UDP Server
    ... All this time I have been confusing bind and connect. ... I still have to bind to a _local_ IP address for UDP sockets in order for the receive data callback to be invoked. ... From what I gathered from your post, invoking "connect" with a remote address in a UDP socket will ensure that the receive callback is only invoked with data received from the specified remote address, correct? ... Without the 75 MB receive buffer, we _occasionally_ find the buffer overfilling. ...
    (microsoft.public.dotnet.languages.csharp)