For Programmers: Free Programming Magazines  


Home > Archive > Tcl > April 2007 > Snack, play sound: script behavior in C++ program diifers from the same running under









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Snack, play sound: script behavior in C++ program diifers from the same running under
skulinetz@excite.donbass.com

2007-04-26, 7:08 pm

To help understand problem, first i show all used scripts:

1. To configure Wish:
--------------------
set b1 [button .b1 -text " start "]
set b2 [button .b2 -text " pause "]
set b3 [button .b3 -text " stop "]

place $b1 -in . -relx 0 -rely 0
place $b2 -in . -relx 0 -rely 0 -y 30
place $b3 -in . -relx 0 -rely 0 -y 60

$b1 configure -command { source "tcl/init.tcl"; source "tcl/play.tcl"}
$b2 configure -command {set play_state $play_pause}
$b3 configure -command {set play_state $play_stop}
--------------------
2. Init script (init.tcl):
--------------------
package require snack
set play_none 0
set play_start 1
set play_pause 2
set play_stop 3
set play_state $play_none
--------------------
3. Play script (play.tcl):
--------------------
snack::sound snd1 -file "rain.mp3"
set play_state $play_start
snd1 play -command {set play_state $play_stop}

while {$play_state != $play_stop} {
vwait play_state
if {$play_state == $play_pause} {
snd1 pause
}
if {$play_state == $play_stop} {
snd1 stop
}
}
snd1 destroy
--------------------

First script creates interface window for Wish, so you can easy test
rest scripts.
It does play mp3 file, pause/continue, and stop playing.

Under Wish all works excellent.

In C++ program:
First i run Init script.
Then i create thread, in this thread i run Play script. In task
manager i see 100% CPU usage, but under Wish it is 0%. This is the
first problem.

Next problem - when i run pause/continue/stop - only first command
processed. Next commands not processed. So, if first command is not
"stop" command then application not able to finish thread that runs
playing script.

Program is very short, so i also put here all code (OS Windows):
--------------------
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <tcl.h>
#include <wtypes.h>
#include <winbase.h>
#include <process.h>

using namespace std;

struct tcl_info
{
Tcl_Interp * i;
char const * script;
};

int eval (Tcl_Interp * i, char const * filename)
{
ifstream in (filename);
in.sg (0, ios_base::end);
streamsize size = in.tellg ();
vector <char> script (size + 1);
in.sg (0);
in.read (&script[0], size);
script[size] = 0;

return Tcl_Eval (i, &script[0]);
}

unsigned __stdcall run_script (void * data)
{
cout << "thread started" << endl;

tcl_info * info = static_cast <tcl_info *> (data);
int r = eval (info->i, info->script);

cout << "thread finished, exit code: " << r << endl;
return r;
}

int main(int argc, char *argv[])
{
Tcl_FindExecutable (argv[0]);

Tcl_Interp * i = Tcl_CreateInterp ();
Tcl_Init (i);
int r = eval (i, "tcl/init.tcl");
if (r)
{
cout << "error" << endl;
return 2;
}

tcl_info play = {i, "tcl/play.tcl"};
HANDLE thread = (HANDLE) _beginthreadex (0, 0, run_script, &play,
0, 0);

for (string input;;)
{
getline (cin, input);
if (input == "p")
{
Tcl_Eval (i, "set play_state $play_pause");
}
else if (input == "s")
{
Tcl_Eval (i, "set play_state $play_stop");
}
else if (input == "q")
{
cout << "waiting thread... " << endl;
WaitForSingleObject (thread, INFINITE);
CloseHandle (thread);
break;
}
}

Tcl_Finalize ();
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com