diff options
author | Max Horn | 2002-07-07 19:06:48 +0000 |
---|---|---|
committer | Max Horn | 2002-07-07 19:06:48 +0000 |
commit | 42ca9ea3a96f9f5146d8c07335dc3662ef6ec34e (patch) | |
tree | dc71aff8f1caa207b97baa31daf69276f32050ac /simon/midi.cpp | |
parent | 2b97c496c2d2f2de51580708cf5d5d8e6e0360f1 (diff) | |
download | scummvm-rg350-42ca9ea3a96f9f5146d8c07335dc3662ef6ec34e.tar.gz scummvm-rg350-42ca9ea3a96f9f5146d8c07335dc3662ef6ec34e.tar.bz2 scummvm-rg350-42ca9ea3a96f9f5146d8c07335dc3662ef6ec34e.zip |
indent run
svn-id: r4476
Diffstat (limited to 'simon/midi.cpp')
-rw-r--r-- | simon/midi.cpp | 215 |
1 files changed, 117 insertions, 98 deletions
diff --git a/simon/midi.cpp b/simon/midi.cpp index c4acc1390a..b9cf3ac867 100644 --- a/simon/midi.cpp +++ b/simon/midi.cpp @@ -27,57 +27,64 @@ #include "../sound/mixer.h" #include "simon.h" -void MidiPlayer::read_from_file(void *dst, uint size) { +void MidiPlayer::read_from_file(void *dst, uint size) +{ if (fread(dst, size, 1, _input) != 1) error("Midi read error"); } -byte MidiPlayer::read_byte_from_file() { +byte MidiPlayer::read_byte_from_file() +{ byte num; read_from_file(&num, 1); return num; } -uint32 MidiPlayer::read_uint32_from_file() { +uint32 MidiPlayer::read_uint32_from_file() +{ uint32 num; read_from_file(&num, 4); return READ_BE_UINT32(&num); } -uint16 MidiPlayer::read_uint16_from_file() { +uint16 MidiPlayer::read_uint16_from_file() +{ uint16 num; read_from_file(&num, 2); return READ_BE_UINT16(&num); } -void MidiPlayer::read_all_songs(FILE *in) { - uint i,num; - +void MidiPlayer::read_all_songs(FILE *in) +{ + uint i, num; + _input = in; _midi_cur_song_ptr = _midi_songs; num = read_byte_from_file(); - for(i=0; i!=num; i++) { + for (i = 0; i != num; i++) { read_one_song(&_midi_songs[i]); } } -void MidiPlayer::read_all_songs_old(FILE *in) { - uint i,num; - +void MidiPlayer::read_all_songs_old(FILE *in) +{ + uint i, num; + _input = in; _midi_cur_song_ptr = _midi_songs; num = 1; - for(i=0; i!=num; i++) { + for (i = 0; i != num; i++) { read_one_song(&_midi_songs[i]); } } -void MidiPlayer::read_mthd(Song *s, bool old) { +void MidiPlayer::read_mthd(Song *s, bool old) +{ Track *t; uint i; @@ -96,15 +103,15 @@ void MidiPlayer::read_mthd(Song *s, bool old) { read_byte_from_file(); } - s->tracks = t = (Track*)calloc(s->num_tracks, sizeof(Track)); + s->tracks = t = (Track *)calloc(s->num_tracks, sizeof(Track)); if (t == NULL) error("Out of memory when allocating MIDI tracks"); - - for(i=0; i!=s->num_tracks; i++, t++) { + + for (i = 0; i != s->num_tracks; i++, t++) { if (!old) { if (read_uint32_from_file() != 'MTrk') error("Midi track has no 'MTrk'"); - + t->data_size = read_uint32_from_file(); } else { uint32 pos = ftell(_input); @@ -114,7 +121,7 @@ void MidiPlayer::read_mthd(Song *s, bool old) { t->data_size = end - pos; } - t->data_ptr = (byte*)calloc(t->data_size,1); + t->data_ptr = (byte *)calloc(t->data_size, 1); if (t->data_ptr == NULL) error("Out of memory when allocating MIDI track data"); @@ -126,19 +133,20 @@ void MidiPlayer::read_mthd(Song *s, bool old) { t->a = 0; t->last_cmd = 0; t->delay = 0; - + if (t->data_cur_size == 0) { t->a |= 1; continue; } - + t->delay = track_read_gamma(t); } } -void MidiPlayer::read_one_song(Song *s) { +void MidiPlayer::read_one_song(Song *s) +{ _midi_var10 = 0; - + s->ppqn = 0; s->midi_format = 0; s->num_tracks = 0; @@ -146,7 +154,7 @@ void MidiPlayer::read_one_song(Song *s) { uint32 id = read_uint32_from_file(); - switch(id) { + switch (id) { case 'MThd': read_mthd(s, false); break; @@ -162,35 +170,38 @@ void MidiPlayer::read_one_song(Song *s) { } -uint32 MidiPlayer::track_read_gamma(Track *t) { +uint32 MidiPlayer::track_read_gamma(Track *t) +{ uint32 sum; byte b; sum = 0; do { b = track_read_byte(t); - sum = (sum<<7) | (b & 0x7F); + sum = (sum << 7) | (b & 0x7F); } while (b & 0x80); - return sum; + return sum; } -byte MidiPlayer::track_read_byte(Track *t) { +byte MidiPlayer::track_read_byte(Track *t) +{ if (t->a & 1) error("Trying to read byte from MIDI stream when end reached"); - + if (!--t->data_cur_size) { - t->a|=1; + t->a |= 1; } return *t->data_cur_ptr++; } -void MidiPlayer::initialize() { +void MidiPlayer::initialize() +{ int res; int i; - for(i=0; i!=16; i++) + for (i = 0; i != 16; i++) _midi_volume_table[i] = 100; _midi_5 = 0; @@ -199,31 +210,32 @@ void MidiPlayer::initialize() { res = _md->open(MidiDriver::MO_STREAMING); if (res != 0) error("MidiPlayer::initializer, got %s", MidiDriver::get_error_name(res)); - + _midi_var9 = true; } -int MidiPlayer::fill(MidiEvent *me, int num_event) { - uint32 best,j; - Track *best_track,*t; +int MidiPlayer::fill(MidiEvent *me, int num_event) +{ + uint32 best, j; + Track *best_track, *t; bool did_reset; NoteRec midi_tmp_note_rec; int i = 0; did_reset = false; - - for(i=0; i!=num_event;) { + + for (i = 0; i != num_event;) { best_track = NULL; best = 0xFFFFFFFF; /* Locate which track that's next */ t = _midi_cur_song_ptr->tracks; - for(j=_midi_cur_song_ptr->num_tracks; j; j--,t++) { - if (!(t->a&1)) { + for (j = _midi_cur_song_ptr->num_tracks; j; j--, t++) { + if (!(t->a & 1)) { if (t->delay < best) { best = t->delay; best_track = t; - } + } } } @@ -242,70 +254,75 @@ int MidiPlayer::fill(MidiEvent *me, int num_event) { if (fill_helper(&midi_tmp_note_rec, me + i)) i++; - if (midi_tmp_note_rec.sysex_data) free(midi_tmp_note_rec.sysex_data); + if (midi_tmp_note_rec.sysex_data) + free(midi_tmp_note_rec.sysex_data); } return i; } -int MidiPlayer::on_fill(void *param, MidiEvent *ev, int num) { - MidiPlayer *mp = (MidiPlayer*)param; +int MidiPlayer::on_fill(void *param, MidiEvent *ev, int num) +{ + MidiPlayer *mp = (MidiPlayer *) param; return mp->fill(ev, num); } -bool MidiPlayer::fill_helper(NoteRec *nr, MidiEvent *me) { +bool MidiPlayer::fill_helper(NoteRec *nr, MidiEvent *me) +{ uint b; b = nr->delay - _midi_var10; _midi_var10 = nr->delay; - - if (nr->cmd<0xF0) { + + if (nr->cmd < 0xF0) { me->delta = b; - me->event = nr->cmd | (nr->param_1<<8) | (nr->param_2<<16); - - if ((nr->cmd&0xF0) == 0xB0 && nr->param_1 == 7) { - _midi_volume_table[nr->cmd&0xF] = nr->param_2; + me->event = nr->cmd | (nr->param_1 << 8) | (nr->param_2 << 16); + + if ((nr->cmd & 0xF0) == 0xB0 && nr->param_1 == 7) { + _midi_volume_table[nr->cmd & 0xF] = nr->param_2; nr->param_1 = 0x76; - me->event = nr->cmd | (nr->param_1<<8) | (nr->param_2<<16)/* | MEVT_F_CALLBACK*/; + me->event = nr->cmd | (nr->param_1 << 8) | (nr->param_2 << 16) /* | MEVT_F_CALLBACK */ ; } return true; } - - if (nr->cmd==0xF0 || nr->cmd==0xF7 || nr->param_1 != 0x51) { + + if (nr->cmd == 0xF0 || nr->cmd == 0xF7 || nr->param_1 != 0x51) { return false; } - - _midi_tempo = nr->sysex_data[2] | (nr->sysex_data[1]<<8) | (nr->sysex_data[0]<<16); - _midi_var8 = (_midi_cur_song_ptr->ppqn*60000) / _midi_tempo; + + _midi_tempo = nr->sysex_data[2] | (nr->sysex_data[1] << 8) | (nr->sysex_data[0] << 16); + _midi_var8 = (_midi_cur_song_ptr->ppqn * 60000) / _midi_tempo; me->delta = b; - me->event = (MidiDriver::ME_TEMPO << 24) | _midi_tempo; + me->event = (MidiDriver::ME_TEMPO << 24)|_midi_tempo; return true; } -void MidiPlayer::reset_tracks() { +void MidiPlayer::reset_tracks() +{ Track *t; uint i; _midi_var10 = 0; - for(i=0,t=_midi_cur_song_ptr->tracks; i!=_midi_cur_song_ptr->num_tracks; i++,t++) { + for (i = 0, t = _midi_cur_song_ptr->tracks; i != _midi_cur_song_ptr->num_tracks; i++, t++) { t->data_cur_size = t->data_size; t->data_cur_ptr = t->data_ptr; t->a = 0; t->last_cmd = 0; t->delay = 0; - if (t->data_cur_size==0) { - t->a|=1; + if (t->data_cur_size == 0) { + t->a |= 1; } else { t->delay = track_read_gamma(t); } } } -void MidiPlayer::read_next_note(Track *t, NoteRec *nr) { +void MidiPlayer::read_next_note(Track *t, NoteRec *nr) +{ byte cmd_byte; uint i; @@ -315,94 +332,96 @@ void MidiPlayer::read_next_note(Track *t, NoteRec *nr) { nr->param_2 = 0; nr->cmd_length = 0; nr->sysex_data = NULL; - - if(t->a&1 || t->data_cur_size==0) + + if (t->a & 1 || t->data_cur_size == 0) error("read next note when track ended"); /* read next midi byte, but skip any pitch bends. */ - for(;;) { + for (;;) { cmd_byte = track_read_byte(t); - if ((cmd_byte&0xF0) != 0xE0) + if ((cmd_byte & 0xF0) != 0xE0) break; track_read_byte(t); track_read_byte(t); - } - + } + if (!(cmd_byte & 0x80)) { /* running status? */ - if (t->last_cmd==0) + if (t->last_cmd == 0) error("Last cmd = 0"); nr->cmd = t->last_cmd; nr->param_1 = cmd_byte; - cmd_byte = nr->cmd&0xF0; + cmd_byte = nr->cmd & 0xF0; nr->cmd_length = 2; - if (cmd_byte!=0xC0 && cmd_byte!=0xD0) { + if (cmd_byte != 0xC0 && cmd_byte != 0xD0) { nr->param_2 = track_read_byte(t); nr->cmd_length++; } - } else if ((cmd_byte&0xF0)!=0xF0) { + } else if ((cmd_byte & 0xF0) != 0xF0) { nr->cmd = cmd_byte; t->last_cmd = cmd_byte; cmd_byte &= 0xF0; - nr->cmd_length = (cmd_byte==0xC0 || cmd_byte==0xD0) ? 2 : 3; - - if (t->data_cur_size < nr->cmd_length-1) { + nr->cmd_length = (cmd_byte == 0xC0 || cmd_byte == 0xD0) ? 2 : 3; + + if (t->data_cur_size < nr->cmd_length - 1) { error("read_next_note: end of stream"); } - + nr->param_1 = track_read_byte(t); - if (nr->cmd_length==3) + if (nr->cmd_length == 3) nr->param_2 = track_read_byte(t); - } else if (cmd_byte==0xF0 || cmd_byte==0xF7) { + } else if (cmd_byte == 0xF0 || cmd_byte == 0xF7) { nr->cmd = cmd_byte; nr->cmd_length = track_read_gamma(t); if (t->data_cur_size < nr->cmd_length) error("read_next_note: end of stream 2"); - nr->sysex_data = (byte*)malloc(nr->cmd_length); - if (nr->sysex_data==NULL) + nr->sysex_data = (byte *)malloc(nr->cmd_length); + if (nr->sysex_data == NULL) error("read_next_note: out of memory"); - for(i=0; i!=nr->cmd_length; i++) + for (i = 0; i != nr->cmd_length; i++) nr->sysex_data[i] = track_read_byte(t); - } else if (cmd_byte==0xFF) { - + } else if (cmd_byte == 0xFF) { + nr->cmd = cmd_byte; nr->param_1 = track_read_byte(t); nr->cmd_length = track_read_gamma(t); if (nr->cmd_length) { if (t->data_cur_size < nr->cmd_length) error("read_next_note: end of stream 3"); - nr->sysex_data = (byte*)malloc(nr->cmd_length); - if (nr->sysex_data==NULL) + nr->sysex_data = (byte *)malloc(nr->cmd_length); + if (nr->sysex_data == NULL) error("read_next_note: out of memory"); - for(i=0; i!=nr->cmd_length; i++) + for (i = 0; i != nr->cmd_length; i++) nr->sysex_data[i] = track_read_byte(t); } - if (nr->param_1==0x2F) - t->a|=1; + if (nr->param_1 == 0x2F) + t->a |= 1; } else { error("Invalid sysex cmd"); } nr->delay = t->delay; - if (!(t->a&1)) { + if (!(t->a & 1)) { t->delay += track_read_gamma(t); } } -void MidiPlayer::shutdown() { +void MidiPlayer::shutdown() +{ _md->close(); unload(); } -void MidiPlayer::unload() { - uint i,j; +void MidiPlayer::unload() +{ + uint i, j; Song *s; Track *t; - for(i=0,s=_midi_songs; i!=8; i++,s++) { + for (i = 0, s = _midi_songs; i != 8; i++, s++) { if (s->tracks) { - for(j=0,t=s->tracks; j!=s->num_tracks;j++,t++) { + for (j = 0, t = s->tracks; j != s->num_tracks; j++, t++) { if (t->data_ptr) free(t->data_ptr); } @@ -412,14 +431,14 @@ void MidiPlayer::unload() { } } -void MidiPlayer::play() { +void MidiPlayer::play() +{ _md->pause(false); } -void MidiPlayer::set_driver(MidiDriver *md) { +void MidiPlayer::set_driver(MidiDriver *md) +{ _md = md; md->set_stream_callback(this, on_fill); } - - |