123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /*
- native_midi: Hardware Midi support for the SDL_mixer library
- Copyright (C) 2000,2001 Florian 'Proff' Schulze
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public
- License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
- This library 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. See the GNU
- Library General Public License for more details.
- You should have received a copy of the GNU Library General Public
- License along with this library; if not, write to the Free
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- Florian 'Proff' Schulze
- florian.proff.schulze@gmx.net
- */
- #include "native_midi_common.h"
- #include "SDL.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- /* The maximum number of midi tracks that we can handle
- #define MIDI_TRACKS 32 */
- /* A single midi track as read from the midi file */
- typedef struct
- {
- Uint8 *data; /* MIDI message stream */
- int len; /* length of the track data */
- } MIDITrack;
- /* A midi file, stripped down to the absolute minimum - divison & track data */
- typedef struct
- {
- int division; /* number of pulses per quarter note (ppqn) */
- int nTracks; /* number of tracks */
- MIDITrack *track; /* tracks */
- } MIDIFile;
- /* Some macros that help us stay endianess-independant */
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- #define BE_SHORT(x) (x)
- #define BE_LONG(x) (x)
- #else
- #define BE_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
- #define BE_LONG(x) ((((x)&0x0000FF)<<24) | \
- (((x)&0x00FF00)<<8) | \
- (((x)&0xFF0000)>>8) | \
- (((x)>>24)&0xFF))
- #endif
- /* Get Variable Length Quantity */
- static int GetVLQ(MIDITrack *track, int *currentPos)
- {
- int l = 0;
- Uint8 c;
- while(1)
- {
- c = track->data[*currentPos];
- (*currentPos)++;
- l += (c & 0x7f);
- if (!(c & 0x80))
- return l;
- l <<= 7;
- }
- }
- /* Create a single MIDIEvent */
- static MIDIEvent *CreateEvent(Uint32 time, Uint8 event, Uint8 a, Uint8 b)
- {
- MIDIEvent *newEvent;
- newEvent = calloc(1, sizeof(MIDIEvent));
- if (newEvent)
- {
- newEvent->time = time;
- newEvent->status = event;
- newEvent->data[0] = a;
- newEvent->data[1] = b;
- }
- else
- printf("Out of memory");
- return newEvent;
- }
- /* Convert a single midi track to a list of MIDIEvents */
- static MIDIEvent *MIDITracktoStream(MIDITrack *track)
- {
- Uint32 atime = 0;
- Uint32 len = 0;
- Uint8 event,type,a,b;
- Uint8 laststatus = 0;
- Uint8 lastchan = 0;
- int currentPos = 0;
- int end = 0;
- MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */
- MIDIEvent *currentEvent = head;
- while (!end)
- {
- if (currentPos >= track->len)
- break; /* End of data stream reached */
- atime += GetVLQ(track, ¤tPos);
- event = track->data[currentPos++];
-
- /* Handle SysEx seperatly */
- if (((event>>4) & 0x0F) == MIDI_STATUS_SYSEX)
- {
- if (event == 0xFF)
- {
- type = track->data[currentPos];
- currentPos++;
- switch(type)
- {
- case 0x2f: /* End of data marker */
- end = 1;
- case 0x51: /* Tempo change */
- /*
- a=track->data[currentPos];
- b=track->data[currentPos+1];
- c=track->data[currentPos+2];
- AddEvent(song, atime, MEVT_TEMPO, c, b, a);
- */
- break;
- }
- }
- else
- type = 0;
- len = GetVLQ(track, ¤tPos);
-
- /* Create an event and attach the extra data, if any */
- currentEvent->next = CreateEvent(atime, event, type, 0);
- currentEvent = currentEvent->next;
- if (NULL == currentEvent)
- {
- FreeMIDIEventList(head);
- return NULL;
- }
- if (len)
- {
- currentEvent->extraLen = len;
- currentEvent->extraData = malloc(len);
- memcpy(currentEvent->extraData, &(track->data[currentPos]), len);
- currentPos += len;
- }
- }
- else
- {
- a = event;
- if (a & 0x80) /* It's a status byte */
- {
- /* Extract channel and status information */
- lastchan = a & 0x0F;
- laststatus = (a>>4) & 0x0F;
-
- /* Read the next byte which should always be a data byte */
- a = track->data[currentPos++] & 0x7F;
- }
- switch(laststatus)
- {
- case MIDI_STATUS_NOTE_OFF:
- case MIDI_STATUS_NOTE_ON: /* Note on */
- case MIDI_STATUS_AFTERTOUCH: /* Key Pressure */
- case MIDI_STATUS_CONTROLLER: /* Control change */
- case MIDI_STATUS_PITCH_WHEEL: /* Pitch wheel */
- b = track->data[currentPos++] & 0x7F;
- currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, b);
- currentEvent = currentEvent->next;
- if (NULL == currentEvent)
- {
- FreeMIDIEventList(head);
- return NULL;
- }
- break;
- case MIDI_STATUS_PROG_CHANGE: /* Program change */
- case MIDI_STATUS_PRESSURE: /* Channel pressure */
- a &= 0x7f;
- currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, 0);
- currentEvent = currentEvent->next;
- if (NULL == currentEvent)
- {
- FreeMIDIEventList(head);
- return NULL;
- }
- break;
- default: /* Sysex already handled above */
- break;
- }
- }
- }
-
- currentEvent = head->next;
- free(head); /* release the dummy head event */
- return currentEvent;
- }
- /*
- * Convert a midi song, consisting of up to 32 tracks, to a list of MIDIEvents.
- * To do so, first convert the tracks seperatly, then interweave the resulting
- * MIDIEvent-Lists to one big list.
- */
- static MIDIEvent *MIDItoStream(MIDIFile *mididata)
- {
- MIDIEvent **track;
- MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */
- MIDIEvent *currentEvent = head;
- int trackID;
- if (NULL == head)
- return NULL;
-
- track = (MIDIEvent**) calloc(1, sizeof(MIDIEvent*) * mididata->nTracks);
- if (NULL == head)
- return NULL;
-
- /* First, convert all tracks to MIDIEvent lists */
- for (trackID = 0; trackID < mididata->nTracks; trackID++)
- track[trackID] = MIDITracktoStream(&mididata->track[trackID]);
- /* Now, merge the lists. */
- /* TODO */
- while(1)
- {
- Uint32 lowestTime = INT_MAX;
- int currentTrackID = -1;
-
- /* Find the next event */
- for (trackID = 0; trackID < mididata->nTracks; trackID++)
- {
- if (track[trackID] && (track[trackID]->time < lowestTime))
- {
- currentTrackID = trackID;
- lowestTime = track[currentTrackID]->time;
- }
- }
-
- /* Check if we processes all events */
- if (currentTrackID == -1)
- break;
-
- currentEvent->next = track[currentTrackID];
- track[currentTrackID] = track[currentTrackID]->next;
- currentEvent = currentEvent->next;
-
-
- lowestTime = 0;
- }
- /* Make sure the list is properly terminated */
- currentEvent->next = 0;
- currentEvent = head->next;
- free(track);
- free(head); /* release the dummy head event */
- return currentEvent;
- }
- static int ReadMIDIFile(MIDIFile *mididata, SDL_RWops *rw)
- {
- int i = 0;
- Uint32 ID;
- Uint32 size;
- Uint16 format;
- Uint16 tracks;
- Uint16 division;
- if (!mididata)
- return 0;
- if (!rw)
- return 0;
- /* Make sure this is really a MIDI file */
- SDL_RWread(rw, &ID, 1, 4);
- if (BE_LONG(ID) != 'MThd')
- return 0;
-
- /* Header size must be 6 */
- SDL_RWread(rw, &size, 1, 4);
- size = BE_LONG(size);
- if (size != 6)
- return 0;
-
- /* We only support format 0 and 1, but not 2 */
- SDL_RWread(rw, &format, 1, 2);
- format = BE_SHORT(format);
- if (format != 0 && format != 1)
- return 0;
-
- SDL_RWread(rw, &tracks, 1, 2);
- tracks = BE_SHORT(tracks);
- mididata->nTracks = tracks;
-
- /* Allocate tracks */
- mididata->track = (MIDITrack*) calloc(1, sizeof(MIDITrack) * mididata->nTracks);
- if (NULL == mididata->track)
- {
- printf("Out of memory");
- goto bail;
- }
-
- /* Retrieve the PPQN value, needed for playback */
- SDL_RWread(rw, &division, 1, 2);
- mididata->division = BE_SHORT(division);
-
-
- for (i=0; i<tracks; i++)
- {
- SDL_RWread(rw, &ID, 1, 4); /* We might want to verify this is MTrk... */
- SDL_RWread(rw, &size, 1, 4);
- size = BE_LONG(size);
- mididata->track[i].len = size;
- mididata->track[i].data = malloc(size);
- if (NULL == mididata->track[i].data)
- {
- printf("Out of memory");
- goto bail;
- }
- SDL_RWread(rw, mididata->track[i].data, 1, size);
- }
- return 1;
- bail:
- for(;i >= 0; i--)
- {
- if (mididata->track[i].data)
- free(mididata->track[i].data);
- }
- return 0;
- }
- MIDIEvent *CreateMIDIEventList(SDL_RWops *rw, Uint16 *division)
- {
- MIDIFile *mididata = NULL;
- MIDIEvent *eventList;
- int trackID;
-
- mididata = calloc(1, sizeof(MIDIFile));
- if (!mididata)
- return NULL;
- /* Open the file */
- if ( rw != NULL )
- {
- /* Read in the data */
- if ( ! ReadMIDIFile(mididata, rw))
- {
- free(mididata);
- return NULL;
- }
- }
- else
- {
- free(mididata);
- return NULL;
- }
-
- if (division)
- *division = mididata->division;
-
- eventList = MIDItoStream(mididata);
-
- for(trackID = 0; trackID < mididata->nTracks; trackID++)
- {
- if (mididata->track[trackID].data)
- free(mididata->track[trackID].data);
- }
- free(mididata->track);
- free(mididata);
-
- return eventList;
- }
- void FreeMIDIEventList(MIDIEvent *head)
- {
- MIDIEvent *cur, *next;
-
- cur = head;
- while (cur)
- {
- next = cur->next;
- if (cur->extraData)
- free (cur->extraData);
- free (cur);
- cur = next;
- }
- }
|