作者 fideas_reciteword [actionscript] 2007-05-06 16:26 (点击下载)

  1. #include "sndserv.h"
  2. #include "reciteword.h"
  3. #include <cstdio>
  4. #include <cstdlib>
  5.  
  6. #ifdef G_OS_WIN32
  7. #include "windows.h"
  8. #else
  9. #include <esd.h>
  10. #endif
  11.  
  12. extern CReciteWord* g_pReciteWord;
  13.  
  14. CSndserv::CSndserv()
  15. {
  16. #ifdef G_OS_WIN32
  17. canplay = true;
  18. #else
  19. fd=esd_open_sound(NULL);//"localhost";
  20. canplay = (fd>=0);
  21. #endif
  22. if (!canplay)
  23. g_print("esd initialization failed,no sound will be play!\n");
  24. }
  25.  
  26. CSndserv::~CSndserv()
  27. {
  28. #ifdef G_OS_WIN32
  29. #else
  30. if (fd>=0) esd_close(fd);
  31. #endif
  32. }
  33.  
  34. void
  35. CSndserv::play(const gchar *filename)
  36. {
  37. #ifdef G_OS_WIN32
  38. #else
  39. if (fd>=0)
  40. esd_play_file(NULL,filename,0); // ???
  41. //esd_play_file ("reciteword", filename, 1);
  42. #endif
  43. }
  44.  
  45. //static CSndserv sndserv;
  46.  
  47. static gpointer play_file_mix(gpointer data)
  48. {
  49. g_pReciteWord->sndserv.play((gchar *)data);
  50. g_free(data);
  51. return NULL;
  52. }
  53.  
  54. /*
  55. static void play_file_after_pre(gpointer data,gpointer user_data)
  56. {
  57. g_pReciteWord->sndserv.play((gchar *)user_data);
  58. return NULL;
  59. }
  60. class CThreadPool
  61. {
  62. GThreadPool *pool;
  63. public:
  64. CThreadPool();
  65. ~CThreadPool();
  66. };
  67. CThreadPool::CThreadPool()
  68. {
  69. pool = g_thread_pool_new(play_file_after_pre,NULL,1,FALSE,NULL);
  70. }
  71. CThreadPool::~CThreadPool()
  72. {
  73. g_thread_pool_free(pool,);
  74. }
  75. */
  76.  
  77.  
  78. void play_file(const char *filename,PLAY_METHOD method)
  79. {
  80. if ((!g_pReciteWord->sndserv.canplay)||(g_pReciteWord->sndserv.disable))
  81. return;
  82.  
  83. //esd_play_file ("reciteword", filename, 1); //it can't return quickly :( when in typing,press wrong key will cause freezing.
  84. // the system() function is inefficiency, should change to use thread to call esd_play_file().
  85. /*gchar command[256];
  86. sprintf(command,"esdplay %s &",filename);
  87. system(command);*/
  88. switch (method)
  89. {
  90. case PM_MIX:
  91. {
  92. #ifdef G_OS_WIN32
  93. PlaySound(filename, 0, SND_ASYNC | SND_FILENAME);
  94. #else
  95. GThread * thread;
  96. gchar *dup_filename = g_strdup(filename); //as in the new thread, filename may have already be freeed.
  97. thread = g_thread_create(play_file_mix,(gpointer)dup_filename,false,NULL); //use GThreadPool may be more efficient.
  98. #endif
  99. break;
  100. }
  101. case PM_STOP_PRE: // it is hard to done,may need write my own esd_play_file.
  102. {
  103. break;
  104. }
  105. case PM_AFTER_PRE: // use GThreadPool can do this,but,it does seems very useful before PM_STOP_PRE is done.
  106. {
  107. //GThreadPool *threadpool;
  108. break;
  109. }
  110. }
  111. }

提交下面的校正或者修改. (点击这里开始一个新的帖子)
姓名: 在 cookie 中记住我的名字

屏幕抓图:(jpeg 或 png)