native_midi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "android_jni.h"
  19. #include "SDL_config.h"
  20. #include "SDL.h"
  21. #include "SDL_endian.h"
  22. #include "native_midi/native_midi.h"
  23. /* Native Midi song */
  24. struct _NativeMidiSong
  25. {
  26. int _placeholder;
  27. int playing;
  28. };
  29. static NativeMidiSong *currentsong = NULL;
  30. static int latched_volume = 128;
  31. int native_midi_detect()
  32. {
  33. return 1; /* always available. */
  34. }
  35. NativeMidiSong *native_midi_loadsong(const char *midifile)
  36. {
  37. NativeMidiSong *retval = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
  38. if (retval)
  39. {
  40. memset(retval, 0, sizeof(NativeMidiSong));
  41. JNI_mediaplayer_load(midifile);
  42. }
  43. return retval;
  44. }
  45. NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  46. {
  47. NativeMidiSong *retval = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
  48. if (retval)
  49. {
  50. FILE *fp = fopen(midiInterFile, "wb+");
  51. if (fp)
  52. {
  53. char buf[4096];
  54. size_t bytes;
  55. while(bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf)))
  56. fwrite(buf, sizeof(char), bytes, fp);
  57. fclose(fp);
  58. memset(retval, 0, sizeof(NativeMidiSong));
  59. JNI_mediaplayer_load(midiInterFile);
  60. }
  61. }
  62. return retval;
  63. }
  64. void native_midi_freesong(NativeMidiSong *song)
  65. {
  66. if (song != NULL)
  67. {
  68. if (currentsong == song)
  69. currentsong = NULL;
  70. free(song);
  71. }
  72. }
  73. void native_midi_start(NativeMidiSong *song)
  74. {
  75. native_midi_stop();
  76. if (song != NULL)
  77. {
  78. currentsong = song;
  79. currentsong->playing = 1;
  80. JNI_mediaplayer_play();
  81. }
  82. }
  83. void native_midi_stop()
  84. {
  85. if (currentsong) {
  86. currentsong->playing = 0;
  87. JNI_mediaplayer_stop();
  88. }
  89. }
  90. int native_midi_active()
  91. {
  92. return currentsong ? currentsong->playing : 0;
  93. }
  94. void native_midi_setvolume(int volume)
  95. {
  96. if (latched_volume != volume)
  97. {
  98. JNI_mediaplayer_setvolume(latched_volume = volume);
  99. }
  100. }
  101. const char *native_midi_error(void)
  102. {
  103. return ""; /* !!! FIXME */
  104. }