native_midi.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // Warning: this is a unverified implementation and this comment should be removed if verified
  15. struct _NativeMidiSong
  16. {
  17. std::thread Thread;
  18. std::mutex Mutex;
  19. char *file;
  20. int pid;
  21. volatile bool playing;
  22. bool looping;
  23. };
  24. const char* midi_file = "/tmp/sdlpal.temp.mid";
  25. char* timidity = nullptr;
  26. extern "C" int native_midi_detect()
  27. {
  28. // FIXME!!!
  29. if (timidity)
  30. {
  31. free(timidity);
  32. timidity = nullptr;
  33. }
  34. if (system("timidity -v") == 0)
  35. {
  36. timidity = strdup("/usr/bin/timidity");
  37. return 1;
  38. }
  39. else
  40. {
  41. return 0;
  42. }
  43. }
  44. extern "C" NativeMidiSong *native_midi_loadsong(const char *midifile)
  45. {
  46. struct stat st;
  47. if (0 != stat(midifile, &st)) return NULL;
  48. auto song = new NativeMidiSong;
  49. if (NULL == song) return NULL;
  50. if (NULL == (song->file = new char[strlen(midifile) + 1])) return NULL;
  51. song->pid = -1;
  52. song->playing = false;
  53. song->looping = false;
  54. strcpy(song->file, midifile);
  55. return song;
  56. }
  57. extern "C" NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  58. {
  59. FILE *fp = fopen(midi_file, "wb+");
  60. if (fp)
  61. {
  62. char buf[4096];
  63. size_t bytes;
  64. while(bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf)))
  65. fwrite(buf, sizeof(char), bytes, fp);
  66. fclose(fp);
  67. return native_midi_loadsong(midi_file);
  68. }
  69. return NULL;
  70. }
  71. extern "C" void native_midi_freesong(NativeMidiSong *song)
  72. {
  73. if (song)
  74. {
  75. if (song->file) delete []song->file;
  76. delete song;
  77. }
  78. }
  79. extern "C" void native_midi_start(NativeMidiSong *song, int looping)
  80. {
  81. if (!song) return;
  82. native_midi_stop(song);
  83. song->playing = true;
  84. song->looping = looping ? true : false;
  85. song->Thread = std::move(std::thread([](NativeMidiSong *song)->void {
  86. while(song->looping)
  87. {
  88. auto pid = fork();
  89. int status;
  90. if (0 == pid)
  91. {
  92. char* args[] = { timidity, song->file, NULL };
  93. if (-1 == execv(timidity, args)) exit(-1);
  94. }
  95. else if (-1 == pid)
  96. {
  97. return;
  98. }
  99. song->Mutex.lock();
  100. song->pid = pid;
  101. song->Mutex.unlock();
  102. waitpid(pid, &status, 0);
  103. }
  104. song->playing = false;
  105. }, song));
  106. }
  107. extern "C" void native_midi_stop(NativeMidiSong *song)
  108. {
  109. if (song)
  110. {
  111. song->looping = false;
  112. song->playing = false;
  113. song->Mutex.lock();
  114. kill(song->pid, SIGTERM);
  115. song->Mutex.unlock();
  116. if (song->Thread.joinable())
  117. song->Thread.join();
  118. song->Thread = std::move(std::thread());
  119. }
  120. }
  121. extern "C" int native_midi_active(NativeMidiSong *song)
  122. {
  123. return (song && song->playing) ? 1 : 0;
  124. }
  125. extern "C" void native_midi_setvolume(NativeMidiSong *song, int volume)
  126. {
  127. // TODO
  128. }
  129. extern "C" const char *native_midi_error(NativeMidiSong *song)
  130. {
  131. return "";
  132. }