native_midi_android.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. native_midi_android: Native Midi support on Android for SDLPal
  3. Copyright (C) 2017 Pal Lockheart
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Pal Lockheart
  16. */
  17. /* This is Android only, using MediaPlayer ( need java part work together ) */
  18. #ifdef __ANDROID__
  19. #include "android_jni.h"
  20. #include "SDL_config.h"
  21. #include "SDL.h"
  22. #include "SDL_endian.h"
  23. #include "native_midi.h"
  24. /* Native Midi song */
  25. struct _NativeMidiSong
  26. {
  27. int _placeholder;
  28. int playing;
  29. };
  30. static NativeMidiSong *currentsong = NULL;
  31. static int latched_volume = 128;
  32. int native_midi_detect()
  33. {
  34. return 1; /* always available. */
  35. }
  36. NativeMidiSong *native_midi_loadsong(const char *midifile)
  37. {
  38. NativeMidiSong *retval = NULL;
  39. SDL_RWops *rw = SDL_RWFromFile(midifile, "rb");
  40. if (rw != NULL) {
  41. retval = native_midi_loadsong_RW(rw);
  42. SDL_RWclose(rw);
  43. }
  44. return retval;
  45. }
  46. NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  47. {
  48. NativeMidiSong *retval = NULL;
  49. void *buf = NULL;
  50. int len = 0;
  51. if (SDL_RWseek(rw, 0, RW_SEEK_END) < 0)
  52. goto fail;
  53. len = SDL_RWtell(rw);
  54. if (len < 0)
  55. goto fail;
  56. if (SDL_RWseek(rw, 0, RW_SEEK_SET) < 0)
  57. goto fail;
  58. buf = malloc(len);
  59. if (buf == NULL)
  60. goto fail;
  61. if (SDL_RWread(rw, buf, len, 1) != 1)
  62. goto fail;
  63. retval = malloc(sizeof(NativeMidiSong));
  64. if (retval == NULL)
  65. goto fail;
  66. memset(retval, '\0', sizeof (*retval));
  67. FILE *fp = fopen(midiInterFile,"wb+");
  68. fwrite(buf,len,1,fp);
  69. fclose(fp);
  70. JNI_mediaplayer_load();
  71. return retval;
  72. fail:
  73. return NULL;
  74. }
  75. void native_midi_freesong(NativeMidiSong *song)
  76. {
  77. if (song != NULL) {
  78. if (currentsong == song)
  79. currentsong = NULL;
  80. free(song);
  81. }
  82. }
  83. void native_midi_start(NativeMidiSong *song)
  84. {
  85. int vol;
  86. if (song == NULL)
  87. return;
  88. currentsong = song;
  89. currentsong->playing = 1;
  90. JNI_mediaplayer_play();
  91. }
  92. void native_midi_stop()
  93. {
  94. if (currentsong) {
  95. currentsong->playing = 0;
  96. JNI_mediaplayer_stop();
  97. }
  98. }
  99. int native_midi_active()
  100. {
  101. if (currentsong == NULL)
  102. return 0;
  103. return currentsong->playing;
  104. }
  105. void native_midi_setvolume(int volume)
  106. {
  107. if (latched_volume == volume)
  108. return;
  109. latched_volume = volume;
  110. JNI_mediaplayer_setvolume(volume);
  111. }
  112. const char *native_midi_error(void)
  113. {
  114. return ""; /* !!! FIXME */
  115. }
  116. #endif