How to interact with a process

Hello all, I know how to play a Midi file. I also know how to read the keyboard. But I don't know how to bring the two together such that I can start and stop playing from the keyboard. It is not required (just nice to have) that the console remains responsive while the midi file plays. But it is essential that I can stop the performance. Is this difficult? -- Martin

A 28/01/2013, às 19:36, Martin Drautzburg escreveu:
Hello all,
I know how to play a Midi file. I also know how to read the keyboard. But I don't know how to bring the two together such that I can start and stop playing from the keyboard.
It is not required (just nice to have) that the console remains responsive while the midi file plays. But it is essential that I can stop the performance.
Is this difficult?
I’m also a beginner, so take this with a grain of salt: I suppose the midi playing command doesn’t block until finished playing otherwise you couldn’t stop midway. If that is the case you can do something like main = forever readFromConsole readFromConsole = do in <- getLine processInput in processInput “start” = startMidi processInput “stop” = stopMidi where startMidi and stopMidi are functions. best, Miguel

On Tuesday, 29. January 2013 11:01:30 Miguel Negrao wrote:
main = forever readFromConsole
readFromConsole = do in <- getLine processInput in
processInput “start” = startMidi processInput “stop” = stopMidi
where startMidi and stopMidi are functions.
How to implement these functions is exactly my problem. -- Martin

Then the answer depends on how you are going about playing midi.
1. Is the api call for this playMidiFile :: FilePath -> IO (), where it
waits until the file finishes before it returns?
You may have to spawn a thread (forkIO) before you play, save it's threadId
in some sort of state and then attempt to kill it from the input thread
when the time comes.
2. Does it play the file and return an id you can reference for later
manipulation?
You can simply keep that reference in some sort of state and when the
command comes to kill it, just use that reference.
3. Are you sending external system commands to a sound system like mplayer
or alsa?
Just send the commands and let the system handle it.
On Wed, Jan 30, 2013 at 5:32 PM, Martin Drautzburg wrote: On Tuesday, 29. January 2013 11:01:30 Miguel Negrao wrote: main = forever readFromConsole readFromConsole = do
in <- getLine
processInput in processInput “start” = startMidi
processInput “stop” = stopMidi where startMidi and stopMidi are functions. How to implement these functions is exactly my problem. --
Martin _______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

On Wednesday, 30. January 2013 23:51:03 David McBride wrote:
Then the answer depends on how you are going about playing midi.
1. Is the api call for this playMidiFile :: FilePath -> IO (), where it waits until the file finishes before it returns? You may have to spawn a thread (forkIO) before you play, save it's threadId in some sort of state and then attempt to kill it from the input thread when the time comes.
Yes, my player is a function from Something to IO(), so forkIO is what I was looking for. -- Martin

Okay I tried forkIO in the following way: playBg = do t <- forkIO play putStrLn "playing" And indeed it plays in background. But can't seem to get the killing to work. playBg = do t <- forkIO play putStrLn "playing" c <- getChar putStrLn "interrrupted" killThread t When I enter "asd" while playing I see: *Main> playBg playing asd interrrupted But it just goes on playing. Can a thread refuse to get killed, or am I doing something stupid?
participants (3)
-
David McBride
-
Martin Drautzburg
-
Miguel Negrao