int SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname) { wavinfo_t info; midi *handle; unsigned char *local_buffer; unsigned long int local_buffer_size = Qfilesize (file); if (!midi_intiialized) { if (midi_init ()) { return -1; } } local_buffer = malloc (local_buffer_size); Qread (file, local_buffer, local_buffer_size); Qclose (file); // WildMidi takes ownership, so be damned if you touch it handle = WildMidi_OpenBuffer (local_buffer, local_buffer_size); if (handle == NULL) return -1; info = midi_get_info (handle); WildMidi_Close (handle); Sys_MaskPrintf (SYS_DEV, "stream %s\n", realname); // we init stream here cause we will only ever stream SND_SFX_Stream (sfx, realname, info, midi_stream_open); return 0; }
static void midi_stream_close (sfx_t *sfx) { sfxstream_t *stream = sfx->data.stream; midi_file_t *mf = (midi_file_t *) stream->file; WildMidi_Close (mf->handle); free (mf); SND_SFX_StreamClose (sfx); }
void stop() { Sound::stopMusic(); LOCK(); if (s_song && s_midiFormat == MFMT_GUS_PATCH) { WildMidi_Close(s_song); } else if (s_song && s_midiFormat == MFMT_SOUND_FONT) { fluid_player_stop(s_fluidPlayer); s_fluidPlaying = false; } s_song = NULL; UNLOCK(); }
static GstStateChangeReturn gst_wildmidi_change_state (GstElement * element, GstStateChange transition) { GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; GstWildmidi *wildmidi = GST_WILDMIDI (element); switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY: break; case GST_STATE_CHANGE_READY_TO_PAUSED: wildmidi->offset = 0; wildmidi->state = GST_WILDMIDI_STATE_LOAD; wildmidi->discont = FALSE; break; case GST_STATE_CHANGE_PAUSED_TO_PLAYING: break; default: break; } ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); switch (transition) { case GST_STATE_CHANGE_PLAYING_TO_PAUSED: break; case GST_STATE_CHANGE_PAUSED_TO_READY: GST_OBJECT_LOCK (wildmidi); if (wildmidi->song) WildMidi_Close (wildmidi->song); wildmidi->song = NULL; GST_OBJECT_UNLOCK (wildmidi); gst_adapter_clear (wildmidi->adapter); break; case GST_STATE_CHANGE_READY_TO_NULL: break; default: break; } return ret; }
QList<FileInfo *> DecoderWildMidiFactory::createPlayList(const QString &fileName, bool useMetaData, QStringList *) { Q_UNUSED(useMetaData); QList <FileInfo*> list; FileInfo *info = new FileInfo(fileName); if(WildMidiHelper::instance()->initialize() && WildMidiHelper::instance()->sampleRate()) { void *midi_ptr = WildMidi_Open (fileName.toLocal8Bit()); if(midi_ptr) { WildMidiHelper::instance()->addPtr(midi_ptr); _WM_Info *wm_info = WildMidi_GetInfo(midi_ptr); info->setLength((qint64)wm_info->approx_total_samples / WildMidiHelper::instance()->sampleRate()); WildMidi_Close(midi_ptr); WildMidiHelper::instance()->removePtr(midi_ptr); } } list << info; return list; }
void MidiPlayerThread::play() { qDebug() << "MidiPlayerThread::play"; const unsigned long int buf_size = 4096; int output_result = 0; unsigned long int count_diff; unsigned long int cur_sec, prev_sec; m_inPause = false; m_received_end_of_file = false; cur_sec = 0; QByteArray buf; buf.resize( buf_size ); _WM_Info *wm_info = WildMidi_GetInfo( m_midiPtr ); m_md->list(); emit receivedStartingTime( 0 ); emit backendFullyLoaded(); emit receivedNoVideo(); qDebug() << "approx_total_samples is " << wm_info->approx_total_samples; while ( 1 ) { if ( m_isStop ) { qDebug() << "MidiPlayerThread::play: m_isStop = " << m_isStop; break; } count_diff = wm_info->approx_total_samples - wm_info->current_sample; if ( count_diff == 0 ) { qDebug() << "MidiPlayerThread::play: it is count_diff == 0"; break; } if ( m_inPause ) { usleep( 1000 ); continue; } buf.fill( 0 ); if ( count_diff < buf_size ) { buf.resize( count_diff * 4 ); output_result = WildMidi_GetOutput ( m_midiPtr, buf.data(), ( count_diff * 4 ) ); } else { output_result = WildMidi_GetOutput ( m_midiPtr, buf.data(), buf_size ); } if ( output_result <= 0 ) { qDebug() << "MidiPlayerThread::play: output_result <= 0"; break; } prev_sec = wm_info->current_sample / m_sample_rate; if ( prev_sec != cur_sec ) { cur_sec = prev_sec; emit receivedCurrentSec( cur_sec ); // qDebug() << "cur_sec is " << cur_sec; } send_output( buf.data(), output_result ); wm_info = WildMidi_GetInfo( m_midiPtr ); } // buf.fill( 0, 16384 ); // send_output( buf.data(), 16384); // buf.fill( 0, 16384 ); // send_output( buf.data(), 16384); // send_output( buf.data(), 16384); // usleep( 5000 ); if ( m_midiPtr ) { // file was played if ( WildMidi_Close( m_midiPtr ) == ( -1 ) ) { qDebug() << "oops!"; } m_midiPtr = 0; } WildMidi_Shutdown(); close_output(); // emit processExited(); m_received_end_of_file = true; qDebug() << "MidiPlayerThread::play: end of play"; }
static void wildmidi_file_decode(struct decoder *decoder, const char *path_fs) { static const struct audio_format audio_format = { .sample_rate = WILDMIDI_SAMPLE_RATE, .format = SAMPLE_FORMAT_S16, .channels = 2, }; midi *wm; const struct _WM_Info *info; enum decoder_command cmd; wm = WildMidi_Open(path_fs); if (wm == NULL) return; info = WildMidi_GetInfo(wm); if (info == NULL) { WildMidi_Close(wm); return; } decoder_initialized(decoder, &audio_format, true, info->approx_total_samples / WILDMIDI_SAMPLE_RATE); do { char buffer[4096]; int len; info = WildMidi_GetInfo(wm); if (info == NULL) break; len = WildMidi_GetOutput(wm, buffer, sizeof(buffer)); if (len <= 0) break; cmd = decoder_data(decoder, NULL, buffer, len, 0); if (cmd == DECODE_COMMAND_SEEK) { unsigned long seek_where = WILDMIDI_SAMPLE_RATE * decoder_seek_where(decoder); WildMidi_SampledSeek(wm, &seek_where); decoder_command_finished(decoder); cmd = DECODE_COMMAND_NONE; } } while (cmd == DECODE_COMMAND_NONE); WildMidi_Close(wm); } static struct tag * wildmidi_tag_dup(const char *path_fs) { midi *wm; const struct _WM_Info *info; struct tag *tag; wm = WildMidi_Open(path_fs); if (wm == NULL) return NULL; info = WildMidi_GetInfo(wm); if (info == NULL) { WildMidi_Close(wm); return NULL; } tag = tag_new(); tag->time = info->approx_total_samples / WILDMIDI_SAMPLE_RATE; WildMidi_Close(wm); return tag; }