PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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/**
 21 * Program to test the TiMidity core, without having to worry about decoder
 22 * and/or playsound bugs. It's not meant to be robust or user-friendly.
 23 */
 24
 25#include <stdio.h>
 26#include <stdlib.h>
 27#include "SDL.h"
 28#include "timidity.h"
 29
 30int done_flag = 0;
 31MidiSong *song;
 32    
 33static void audio_callback(void *userdata, Uint8 *stream, int len)
 34{
 35    if (Timidity_PlaySome(song, stream, len) == 0)
 36	done_flag = 1;
 37}
 38
 39int main(int argc, char *argv[])
 40{
 41    SDL_AudioSpec audio;
 42    SDL_RWops *rw;
 43
 44    if (SDL_Init(SDL_INIT_AUDIO) < 0)
 45    {
 46	fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
 47	return 1;
 48    }
 49
 50    atexit(SDL_Quit);
 51
 52    if (argc != 2)
 53    {
 54	fprintf(stderr, "Usage: %s midifile\n", argv[0]);
 55	return 1;
 56    }
 57
 58    audio.freq = 44100;
 59    audio.format = AUDIO_S16SYS;
 60    audio.channels = 2;
 61    audio.samples = 4096;
 62    audio.callback = audio_callback;
 63
 64    if (SDL_OpenAudio(&audio, NULL) < 0)
 65    {
 66	fprintf(stderr, "Couldn't open audio device. %s\n", SDL_GetError());
 67	return 1;
 68    }
 69
 70    if (Timidity_Init() < 0)
 71    {
 72	fprintf(stderr, "Could not initialise TiMidity.\n");
 73	return 1;
 74    }
 75    
 76    rw = SDL_RWFromFile(argv[1], "rb");
 77    if (rw == NULL)
 78    {
 79	fprintf(stderr, "Could not create RWops from MIDI file.\n");
 80	return 1;
 81    }
 82	
 83    song = Timidity_LoadSong(rw, &audio);
 84    SDL_RWclose(rw);
 85    
 86    if (song == NULL)
 87    {
 88	fprintf(stderr, "Could not open MIDI file.\n");
 89	return 1;
 90    }
 91
 92    Timidity_SetVolume(song, 100);
 93    Timidity_Start(song);
 94    
 95    SDL_PauseAudio(0);
 96    while (!done_flag)
 97    {
 98	SDL_Delay(10);
 99    }
100    SDL_PauseAudio(1);
101    Timidity_FreeSong(song);
102    Timidity_Exit();
103    
104    return 0;
105}