/project/jni/sdl_sound/decoders/timidity/testmidi.c

https://github.com/aichunyu/FFPlayer · C · 105 lines · 66 code · 17 blank · 22 comment · 12 complexity · 8634ec2dd7c03ef0fb657b95b77620cc MD5 · raw file

  1. /*
  2. * SDL_sound -- An abstract sound format decoding API.
  3. * Copyright (C) 2001 Ryan C. Gordon.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * Program to test the TiMidity core, without having to worry about decoder
  21. * and/or playsound bugs. It's not meant to be robust or user-friendly.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "SDL.h"
  26. #include "timidity.h"
  27. int done_flag = 0;
  28. MidiSong *song;
  29. static void audio_callback(void *userdata, Uint8 *stream, int len)
  30. {
  31. if (Timidity_PlaySome(song, stream, len) == 0)
  32. done_flag = 1;
  33. }
  34. int main(int argc, char *argv[])
  35. {
  36. SDL_AudioSpec audio;
  37. SDL_RWops *rw;
  38. if (SDL_Init(SDL_INIT_AUDIO) < 0)
  39. {
  40. fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  41. return 1;
  42. }
  43. atexit(SDL_Quit);
  44. if (argc != 2)
  45. {
  46. fprintf(stderr, "Usage: %s midifile\n", argv[0]);
  47. return 1;
  48. }
  49. audio.freq = 44100;
  50. audio.format = AUDIO_S16SYS;
  51. audio.channels = 2;
  52. audio.samples = 4096;
  53. audio.callback = audio_callback;
  54. if (SDL_OpenAudio(&audio, NULL) < 0)
  55. {
  56. fprintf(stderr, "Couldn't open audio device. %s\n", SDL_GetError());
  57. return 1;
  58. }
  59. if (Timidity_Init() < 0)
  60. {
  61. fprintf(stderr, "Could not initialise TiMidity.\n");
  62. return 1;
  63. }
  64. rw = SDL_RWFromFile(argv[1], "rb");
  65. if (rw == NULL)
  66. {
  67. fprintf(stderr, "Could not create RWops from MIDI file.\n");
  68. return 1;
  69. }
  70. song = Timidity_LoadSong(rw, &audio);
  71. SDL_RWclose(rw);
  72. if (song == NULL)
  73. {
  74. fprintf(stderr, "Could not open MIDI file.\n");
  75. return 1;
  76. }
  77. Timidity_SetVolume(song, 100);
  78. Timidity_Start(song);
  79. SDL_PauseAudio(0);
  80. while (!done_flag)
  81. {
  82. SDL_Delay(10);
  83. }
  84. SDL_PauseAudio(1);
  85. Timidity_FreeSong(song);
  86. Timidity_Exit();
  87. return 0;
  88. }