native_midi.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/wait.h>
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #include <future>
  8. #include <thread>
  9. #include <condition_variable>
  10. #include <mutex>
  11. #include <atomic>
  12. #include <vector>
  13. #include "native_midi/native_midi.h"
  14. #include "util.h"
  15. #include "palcfg.h"
  16. #define CLIPLAYER_EXECUTABLE (which("timidity"))
  17. struct _NativeMidiSong
  18. {
  19. std::thread Thread;
  20. std::mutex Mutex;
  21. char *file;
  22. int pid;
  23. volatile bool playing;
  24. bool looping;
  25. };
  26. const char* midi_file = "/tmp/sdlpal.temp.mid";
  27. char* cliplayer = nullptr;
  28. static char *which(const char *cmd)
  29. {
  30. char path[PATH_MAX];
  31. FILE *fp = popen(va("which %s", cmd), "r");
  32. if (fp == NULL) {
  33. return NULL;
  34. }else{
  35. fgets(path, sizeof(path)-1, fp);
  36. pclose(fp);
  37. if( path[strlen(path)-1] == '\n' ) path[strlen(path)-1] = '\0';
  38. if( path[strlen(path)-1] == '\r' ) path[strlen(path)-1] = '\0';
  39. return path;
  40. }
  41. }
  42. extern "C" int native_midi_detect()
  43. {
  44. if (cliplayer)
  45. {
  46. free(cliplayer);
  47. cliplayer = nullptr;
  48. }
  49. char *path = (gConfig.pszCLIMIDIPlayerPath ? gConfig.pszCLIMIDIPlayerPath : CLIPLAYER_EXECUTABLE);
  50. if (path && access(path,F_OK) == 0)
  51. {
  52. cliplayer = strdup(path);
  53. return 1;
  54. }
  55. else
  56. {
  57. return 0;
  58. }
  59. }
  60. extern "C" NativeMidiSong *native_midi_loadsong(const char *midifile)
  61. {
  62. struct stat st;
  63. if (0 != stat(midifile, &st)) return NULL;
  64. auto song = new NativeMidiSong;
  65. if (NULL == song) return NULL;
  66. if (NULL == (song->file = new char[strlen(midifile) + 1])) return NULL;
  67. song->pid = -1;
  68. song->playing = false;
  69. song->looping = false;
  70. strcpy(song->file, midifile);
  71. return song;
  72. }
  73. extern "C" NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  74. {
  75. FILE *fp = fopen(midi_file, "wb+");
  76. if (fp)
  77. {
  78. char buf[4096];
  79. size_t bytes;
  80. while((bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf))) > 0)
  81. fwrite(buf, sizeof(char), bytes, fp);
  82. fclose(fp);
  83. return native_midi_loadsong(midi_file);
  84. }
  85. return NULL;
  86. }
  87. extern "C" void native_midi_freesong(NativeMidiSong *song)
  88. {
  89. if (song)
  90. {
  91. if (native_midi_active(song))
  92. native_midi_stop(song);
  93. if (song->file) delete []song->file;
  94. delete song;
  95. }
  96. }
  97. extern "C" void native_midi_start(NativeMidiSong *song, int looping)
  98. {
  99. if (!song) return;
  100. native_midi_stop(song);
  101. song->playing = true;
  102. song->looping = looping ? true : false;
  103. song->Thread = std::move(std::thread([](NativeMidiSong *song)->void {
  104. while(song->looping)
  105. {
  106. auto pid = fork();
  107. int status;
  108. if (0 == pid)
  109. {
  110. char* args[] = { cliplayer, song->file, NULL };
  111. if (-1 == execv(cliplayer, args)) exit(-1);
  112. }
  113. else if (-1 == pid)
  114. {
  115. return;
  116. }
  117. song->Mutex.lock();
  118. song->pid = pid;
  119. song->Mutex.unlock();
  120. waitpid(pid, &status, 0);
  121. }
  122. song->playing = false;
  123. }, song));
  124. }
  125. extern "C" void native_midi_stop(NativeMidiSong *song)
  126. {
  127. if (song)
  128. {
  129. song->looping = false;
  130. song->playing = false;
  131. if (-1 != song->pid)
  132. {
  133. song->Mutex.lock();
  134. kill(song->pid, SIGTERM);
  135. song->Mutex.unlock();
  136. }
  137. if (song->Thread.joinable())
  138. song->Thread.join();
  139. song->Thread = std::move(std::thread());
  140. }
  141. }
  142. extern "C" int native_midi_active(NativeMidiSong *song)
  143. {
  144. return (song && song->playing) ? 1 : 0;
  145. }
  146. extern "C" void native_midi_setvolume(NativeMidiSong *song, int volume)
  147. {
  148. // TODO
  149. }
  150. extern "C" const char *native_midi_error(NativeMidiSong *song)
  151. {
  152. return "";
  153. }