native_midi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <android/log.h>
  24. #define TAG "sdlpal-jni"
  25. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG,__VA_ARGS__)
  26. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , TAG,__VA_ARGS__)
  27. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO , TAG,__VA_ARGS__)
  28. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN , TAG,__VA_ARGS__)
  29. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , TAG,__VA_ARGS__)
  30. /* Native Midi song */
  31. struct _NativeMidiSong
  32. {
  33. void *player;
  34. int volume;
  35. };
  36. int native_midi_detect()
  37. {
  38. return 1; /* always available. */
  39. }
  40. NativeMidiSong *native_midi_loadsong(const char *midifile)
  41. {
  42. NativeMidiSong *song = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
  43. if (song)
  44. {
  45. song->volume = 127;
  46. song->player = JNI_mediaplayer_load(midifile);
  47. }
  48. return song;
  49. }
  50. NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  51. {
  52. FILE *fp = fopen(midiInterFile, "wb+");
  53. if (fp)
  54. {
  55. char buf[4096];
  56. size_t bytes;
  57. while((bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf))) > 0)
  58. fwrite(buf, sizeof(char), bytes, fp);
  59. fclose(fp);
  60. return native_midi_loadsong(midiInterFile);
  61. }
  62. return NULL;
  63. }
  64. void native_midi_freesong(NativeMidiSong *song)
  65. {
  66. if (song != NULL)
  67. {
  68. JNI_mediaplayer_stop(song->player);
  69. JNI_mediaplayer_free(song->player);
  70. free(song);
  71. }
  72. }
  73. void native_midi_start(NativeMidiSong *song, int looping)
  74. {
  75. if (song != NULL)
  76. {
  77. JNI_mediaplayer_play(song->player, looping);
  78. }
  79. }
  80. void native_midi_stop(NativeMidiSong *song)
  81. {
  82. if (song)
  83. {
  84. JNI_mediaplayer_stop(song->player);
  85. }
  86. }
  87. int native_midi_active(NativeMidiSong *song)
  88. {
  89. return song ? JNI_mediaplayer_isplaying(song->player) : 0;
  90. }
  91. void native_midi_setvolume(NativeMidiSong *song, int volume)
  92. {
  93. if (song)
  94. {
  95. JNI_mediaplayer_setvolume(song->player, song->volume = volume);
  96. }
  97. }
  98. const char *native_midi_error(NativeMidiSong *song)
  99. {
  100. return ""; /* !!! FIXME */
  101. }