native_midi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. void *player;
  27. int volume;
  28. };
  29. int native_midi_detect()
  30. {
  31. return 1; /* always available. */
  32. }
  33. NativeMidiSong *native_midi_loadsong(const char *midifile)
  34. {
  35. NativeMidiSong *song = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
  36. if (song)
  37. {
  38. song->volume = 127;
  39. song->player = JNI_mediaplayer_load(midifile);
  40. }
  41. return song;
  42. }
  43. NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  44. {
  45. FILE *fp = fopen(midiInterFile, "wb+");
  46. if (fp)
  47. {
  48. char buf[4096];
  49. size_t bytes;
  50. while(bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf)))
  51. fwrite(buf, sizeof(char), bytes, fp);
  52. fclose(fp);
  53. return native_midi_loadsong(midiInterFile);
  54. }
  55. return NULL;
  56. }
  57. void native_midi_freesong(NativeMidiSong *song)
  58. {
  59. if (song != NULL)
  60. {
  61. JNI_mediaplayer_stop(song->player);
  62. JNI_mediaplayer_free(song->player);
  63. free(song);
  64. }
  65. }
  66. void native_midi_start(NativeMidiSong *song, int looping)
  67. {
  68. if (song != NULL)
  69. {
  70. JNI_mediaplayer_play(song->player, looping);
  71. }
  72. }
  73. void native_midi_stop(NativeMidiSong *song)
  74. {
  75. if (song)
  76. {
  77. JNI_mediaplayer_stop(song->player);
  78. }
  79. }
  80. int native_midi_active(NativeMidiSong *song)
  81. {
  82. return song ? JNI_mediaplayer_isplaying(song->player) : 0;
  83. }
  84. void native_midi_setvolume(NativeMidiSong *song, int volume)
  85. {
  86. if (song)
  87. {
  88. JNI_mediaplayer_setvolume(song->player, song->volume = volume);
  89. }
  90. }
  91. const char *native_midi_error(NativeMidiSong *song)
  92. {
  93. return ""; /* !!! FIXME */
  94. }