libtimidity.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../openttd.h"
00014 #include "../sound_type.h"
00015 #include "../debug.h"
00016 #include "libtimidity.h"
00017 #include <fcntl.h>
00018 #include <sys/types.h>
00019 #include <sys/wait.h>
00020 #include <unistd.h>
00021 #include <signal.h>
00022 #include <sys/stat.h>
00023 #include <errno.h>
00024 #include <timidity.h>
00025 #if defined(PSP)
00026 #include <pspaudiolib.h>
00027 #endif /* PSP */
00028 
00030 enum MidiState {
00031   MIDI_STOPPED = 0,
00032   MIDI_PLAYING = 1,
00033 };
00034 
00035 static struct {
00036   MidIStream *stream;
00037   MidSongOptions options;
00038   MidSong *song;
00039 
00040   MidiState status;
00041   uint32 song_length;
00042   uint32 song_position;
00043 } _midi; 
00044 
00045 #if defined(PSP)
00046 static void AudioOutCallback(void *buf, unsigned int _reqn, void *userdata)
00047 {
00048   memset(buf, 0, _reqn * PSP_NUM_AUDIO_CHANNELS);
00049   if (_midi.status == MIDI_PLAYING) {
00050     mid_song_read_wave(_midi.song, buf, _reqn * PSP_NUM_AUDIO_CHANNELS);
00051   }
00052 }
00053 #endif /* PSP */
00054 
00056 static FMusicDriver_LibTimidity iFMusicDriver_LibTimidity;
00057 
00058 const char *MusicDriver_LibTimidity::Start(const char * const *param)
00059 {
00060   _midi.status = MIDI_STOPPED;
00061   _midi.song = NULL;
00062 
00063   if (mid_init(param == NULL ? NULL : const_cast<char *>(param[0])) < 0) {
00064     /* If init fails, it can be because no configuration was found.
00065      *  If it was not forced via param, try to load it without a
00066      *  configuration. Who knows that works. */
00067     if (param != NULL || mid_init_no_config() < 0) {
00068       return "error initializing timidity";
00069     }
00070   }
00071   DEBUG(driver, 1, "successfully initialised timidity");
00072 
00073   _midi.options.rate = 44100;
00074   _midi.options.format = MID_AUDIO_S16LSB;
00075   _midi.options.channels = 2;
00076 #if defined(PSP)
00077   _midi.options.buffer_size = PSP_NUM_AUDIO_SAMPLES;
00078 #else
00079   _midi.options.buffer_size = _midi.options.rate;
00080 #endif
00081 
00082 #if defined(PSP)
00083   pspAudioInit();
00084   pspAudioSetChannelCallback(_midi.options.channels, &AudioOutCallback, NULL);
00085   pspAudioSetVolume(_midi.options.channels, PSP_VOLUME_MAX, PSP_VOLUME_MAX);
00086 #endif /* PSP */
00087 
00088   return NULL;
00089 }
00090 
00091 void MusicDriver_LibTimidity::Stop()
00092 {
00093   if (_midi.status == MIDI_PLAYING) this->StopSong();
00094   mid_exit();
00095 }
00096 
00097 void MusicDriver_LibTimidity::PlaySong(const char *filename)
00098 {
00099   this->StopSong();
00100 
00101   _midi.stream = mid_istream_open_file(filename);
00102   if (_midi.stream == NULL) {
00103     DEBUG(driver, 0, "Could not open music file");
00104     return;
00105   }
00106 
00107   _midi.song = mid_song_load(_midi.stream, &_midi.options);
00108   mid_istream_close(_midi.stream);
00109   _midi.song_length = mid_song_get_total_time(_midi.song);
00110 
00111   if (_midi.song == NULL) {
00112     DEBUG(driver, 1, "Invalid MIDI file");
00113     return;
00114   }
00115 
00116   mid_song_start(_midi.song);
00117   _midi.status = MIDI_PLAYING;
00118 }
00119 
00120 void MusicDriver_LibTimidity::StopSong()
00121 {
00122   _midi.status = MIDI_STOPPED;
00123   /* mid_song_free cannot handle NULL! */
00124   if (_midi.song != NULL) mid_song_free(_midi.song);
00125   _midi.song = NULL;
00126 }
00127 
00128 bool MusicDriver_LibTimidity::IsSongPlaying()
00129 {
00130   if (_midi.status == MIDI_PLAYING) {
00131     _midi.song_position = mid_song_get_time(_midi.song);
00132     if (_midi.song_position >= _midi.song_length) {
00133       _midi.status = MIDI_STOPPED;
00134       _midi.song_position = 0;
00135     }
00136   }
00137 
00138   return (_midi.status == MIDI_PLAYING);
00139 }
00140 
00141 void MusicDriver_LibTimidity::SetVolume(byte vol)
00142 {
00143   if (_midi.song != NULL) mid_song_set_volume(_midi.song, vol);
00144 }

Generated on Fri May 27 04:19:43 2011 for OpenTTD by  doxygen 1.6.1