Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.   native_midi_mac:  Native Midi support on MacOS for the SDL_mixer library
  3.   Copyright (C) 2001  Max Horn <max@quendi.de>
  4.  
  5.   This software is provided 'as-is', without any express or implied
  6.   warranty.  In no event will the authors be held liable for any damages
  7.   arising from the use of this software.
  8.  
  9.   Permission is granted to anyone to use this software for any purpose,
  10.   including commercial applications, and to alter it and redistribute it
  11.   freely, subject to the following restrictions:
  12.  
  13.   1. The origin of this software must not be misrepresented; you must not
  14.      claim that you wrote the original software. If you use this software
  15.      in a product, an acknowledgment in the product documentation would be
  16.      appreciated but is not required.
  17.   2. Altered source versions must be plainly marked as such, and must not be
  18.      misrepresented as being the original software.
  19.   3. This notice may not be removed or altered from any source distribution.
  20. */
  21. #include "SDL_config.h"
  22. #include "SDL_endian.h"
  23.  
  24. #if __MACOS__ /*|| __MACOSX__ */
  25.  
  26. #include "native_midi.h"
  27. #include "native_midi_common.h"
  28.  
  29. #if __MACOSX__
  30. #include <QuickTime/QuickTimeMusic.h>
  31. #else
  32. #include <QuickTimeMusic.h>
  33. #endif
  34.  
  35. #include <assert.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38.  
  39.  
  40. /* Native Midi song */
  41. struct _NativeMidiSong
  42. {
  43.         Uint32          *tuneSequence;
  44.         Uint32          *tuneHeader;
  45. };
  46.  
  47. enum
  48. {
  49.         /* number of (32-bit) long words in a note request event */
  50.         kNoteRequestEventLength = ((sizeof(NoteRequest)/sizeof(long)) + 2),
  51.  
  52.         /* number of (32-bit) long words in a marker event */
  53.         kMarkerEventLength      = 1,
  54.  
  55.         /* number of (32-bit) long words in a general event, minus its data */
  56.         kGeneralEventLength     = 2
  57. };
  58.  
  59. #define ERROR_BUF_SIZE                  256
  60. #define BUFFER_INCREMENT                5000
  61.  
  62. #define REST_IF_NECESSARY()     do {\
  63.                         int timeDiff = eventPos->time - lastEventTime;  \
  64.                         if(timeDiff)    \
  65.                         {       \
  66.                                 timeDiff = (int)(timeDiff*tick);        \
  67.                                 qtma_StuffRestEvent(*tunePos, timeDiff);        \
  68.                                 tunePos++;      \
  69.                                 lastEventTime = eventPos->time; \
  70.                         }       \
  71.                 } while(0)
  72.  
  73.  
  74. static Uint32 *BuildTuneSequence(MIDIEvent *evntlist, int ppqn, int part_poly_max[32], int part_to_inst[32], int *numParts);
  75. static Uint32 *BuildTuneHeader(int part_poly_max[32], int part_to_inst[32], int numParts);
  76.  
  77. /* The global TunePlayer instance */
  78. static TunePlayer       gTunePlayer = NULL;
  79. static int                      gInstaceCount = 0;
  80. static Uint32           *gCurrentTuneSequence = NULL;
  81. static char                     gErrorBuffer[ERROR_BUF_SIZE] = "";
  82.  
  83.  
  84. /* Check whether QuickTime is available */
  85. int native_midi_detect()
  86. {
  87.         /* TODO */
  88.         return 1;
  89. }
  90.  
  91. NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw, int freerw)
  92. {
  93.         NativeMidiSong  *song = NULL;
  94.         MIDIEvent               *evntlist = NULL;
  95.         int                             part_to_inst[32];
  96.         int                             part_poly_max[32];
  97.         int                             numParts = 0;
  98.         Uint16                  ppqn;
  99.  
  100.         /* Init the arrays */
  101.         memset(part_poly_max,0,sizeof(part_poly_max));
  102.         memset(part_to_inst,-1,sizeof(part_to_inst));
  103.        
  104.         /* Attempt to load the midi file */
  105.         evntlist = CreateMIDIEventList(rw, &ppqn);
  106.         if (!evntlist)
  107.                 goto bail;
  108.  
  109.         /* Allocate memory for the song struct */
  110.         song = malloc(sizeof(NativeMidiSong));
  111.         if (!song)
  112.                 goto bail;
  113.  
  114.         /* Build a tune sequence from the event list */
  115.         song->tuneSequence = BuildTuneSequence(evntlist, ppqn, part_poly_max, part_to_inst, &numParts);
  116.         if(!song->tuneSequence)
  117.                 goto bail;
  118.  
  119.         /* Now build a tune header from the data we collect above, create
  120.            all parts as needed and assign them the correct instrument.
  121.         */
  122.         song->tuneHeader = BuildTuneHeader(part_poly_max, part_to_inst, numParts);
  123.         if(!song->tuneHeader)
  124.                 goto bail;
  125.        
  126.         /* Increment the instance count */
  127.         gInstaceCount++;
  128.         if (gTunePlayer == NULL)
  129.                 gTunePlayer = OpenDefaultComponent(kTunePlayerComponentType, 0);
  130.  
  131.         /* Finally, free the event list */
  132.         FreeMIDIEventList(evntlist);
  133.  
  134.         if (freerw) {
  135.                 SDL_RWclose(rw);
  136.         }
  137.         return song;
  138.        
  139. bail:
  140.         if (evntlist)
  141.                 FreeMIDIEventList(evntlist);
  142.        
  143.         if (song)
  144.         {
  145.                 if(song->tuneSequence)
  146.                         free(song->tuneSequence);
  147.                
  148.                 if(song->tuneHeader)
  149.                         DisposePtr((Ptr)song->tuneHeader);
  150.  
  151.                 free(song);
  152.         }
  153.        
  154.         if (freerw) {
  155.                 SDL_RWclose(rw);
  156.         }
  157.         return NULL;
  158. }
  159.  
  160. void native_midi_freesong(NativeMidiSong *song)
  161. {
  162.         if(!song || !song->tuneSequence)
  163.                 return;
  164.  
  165.         /* If this is the currently playing song, stop it now */       
  166.         if (song->tuneSequence == gCurrentTuneSequence)
  167.                 native_midi_stop();
  168.        
  169.         /* Finally, free the data storage */
  170.         free(song->tuneSequence);
  171.         DisposePtr((Ptr)song->tuneHeader);
  172.         free(song);
  173.  
  174.         /* Increment the instance count */
  175.         gInstaceCount--;
  176.         if ((gTunePlayer != NULL) && (gInstaceCount == 0))
  177.         {
  178.                 CloseComponent(gTunePlayer);
  179.                 gTunePlayer = NULL;
  180.         }
  181. }
  182.  
  183. void native_midi_start(NativeMidiSong *song, int loops)
  184. {
  185.         UInt32          queueFlags = 0;
  186.         ComponentResult tpError;
  187.        
  188.         assert (gTunePlayer != NULL);
  189.  
  190.         /* FIXME: is this code even used anymore? */
  191.         assert (loops == 0);
  192.        
  193.         SDL_PauseAudio(1);
  194.         SDL_UnlockAudio();
  195.    
  196.         /* First, stop the currently playing music */
  197.         native_midi_stop();
  198.        
  199.         /* Set up the queue flags */
  200.         queueFlags = kTuneStartNow;
  201.  
  202.         /* Set the time scale (units per second), we want milliseconds */
  203.         tpError = TuneSetTimeScale(gTunePlayer, 1000);
  204.         if (tpError != noErr)
  205.         {
  206.                 strncpy (gErrorBuffer, "MIDI error during TuneSetTimeScale", ERROR_BUF_SIZE);
  207.                 goto done;
  208.         }
  209.  
  210.         /* Set the header, to tell what instruments are used */
  211.         tpError = TuneSetHeader(gTunePlayer, (UInt32 *)song->tuneHeader);
  212.         if (tpError != noErr)
  213.         {
  214.                 strncpy (gErrorBuffer, "MIDI error during TuneSetHeader", ERROR_BUF_SIZE);
  215.                 goto done;
  216.         }
  217.        
  218.         /* Have it allocate whatever resources are needed */
  219.         tpError = TunePreroll(gTunePlayer);
  220.         if (tpError != noErr)
  221.         {
  222.                 strncpy (gErrorBuffer, "MIDI error during TunePreroll", ERROR_BUF_SIZE);
  223.                 goto done;
  224.         }
  225.  
  226.         /* We want to play at normal volume */
  227.         tpError = TuneSetVolume(gTunePlayer, 0x00010000);
  228.         if (tpError != noErr)
  229.         {
  230.                 strncpy (gErrorBuffer, "MIDI error during TuneSetVolume", ERROR_BUF_SIZE);
  231.                 goto done;
  232.         }
  233.        
  234.         /* Finally, start playing the full song */
  235.         gCurrentTuneSequence = song->tuneSequence;
  236.         tpError = TuneQueue(gTunePlayer, (UInt32 *)song->tuneSequence, 0x00010000, 0, 0xFFFFFFFF, queueFlags, NULL, 0);
  237.         if (tpError != noErr)
  238.         {
  239.                 strncpy (gErrorBuffer, "MIDI error during TuneQueue", ERROR_BUF_SIZE);
  240.                 goto done;
  241.         }
  242.    
  243. done:
  244.         SDL_LockAudio();
  245.         SDL_PauseAudio(0);
  246. }
  247.  
  248. void native_midi_stop()
  249. {
  250.         if (gTunePlayer == NULL)
  251.                 return;
  252.  
  253.         /* Stop music */
  254.         TuneStop(gTunePlayer, 0);
  255.        
  256.         /* Deallocate all instruments */
  257.         TuneUnroll(gTunePlayer);
  258. }
  259.  
  260. int native_midi_active()
  261. {
  262.         if (gTunePlayer != NULL)
  263.         {
  264.                 TuneStatus      ts;
  265.  
  266.                 TuneGetStatus(gTunePlayer,&ts);
  267.                 return ts.queueTime != 0;
  268.         }
  269.         else
  270.                 return 0;
  271. }
  272.  
  273. void native_midi_setvolume(int volume)
  274. {
  275.         if (gTunePlayer == NULL)
  276.                 return;
  277.  
  278.         /* QTMA olume may range from 0.0 to 1.0 (in 16.16 fixed point encoding) */
  279.         TuneSetVolume(gTunePlayer, (0x00010000 * volume)/SDL_MIX_MAXVOLUME);
  280. }
  281.  
  282. const char *native_midi_error(void)
  283. {
  284.         return gErrorBuffer;
  285. }
  286.  
  287. Uint32 *BuildTuneSequence(MIDIEvent *evntlist, int ppqn, int part_poly_max[32], int part_to_inst[32], int *numParts)
  288. {
  289.         int                     part_poly[32];
  290.         int                     channel_to_part[16];
  291.        
  292.         int                     channel_pan[16];
  293.         int                     channel_vol[16];
  294.         int                     channel_pitch_bend[16];
  295.        
  296.         int                     lastEventTime = 0;
  297.         int                     tempo = 500000;
  298.         double          Ippqn = 1.0 / (1000*ppqn);
  299.         double          tick = tempo * Ippqn;
  300.         MIDIEvent       *eventPos = evntlist;
  301.         MIDIEvent       *noteOffPos;
  302.         Uint32          *tunePos, *endPos;
  303.         Uint32          *tuneSequence;
  304.         size_t          tuneSize;
  305.        
  306.         /* allocate space for the tune header */
  307.         tuneSize = 5000;
  308.         tuneSequence = (Uint32 *)malloc(tuneSize * sizeof(Uint32));
  309.         if (tuneSequence == NULL)
  310.                 return NULL;
  311.        
  312.         /* Set starting position in our tune memory */
  313.         tunePos = tuneSequence;
  314.         endPos = tuneSequence + tuneSize;
  315.  
  316.         /* Initialise the arrays */
  317.         memset(part_poly,0,sizeof(part_poly));
  318.        
  319.         memset(channel_to_part,-1,sizeof(channel_to_part));
  320.         memset(channel_pan,-1,sizeof(channel_pan));
  321.         memset(channel_vol,-1,sizeof(channel_vol));
  322.         memset(channel_pitch_bend,-1,sizeof(channel_pitch_bend));
  323.        
  324.         *numParts = 0;
  325.        
  326.         /*
  327.          * Now the major work - iterate over all GM events,
  328.          * and turn them into QuickTime Music format.
  329.          * At the same time, calculate the max. polyphony for each part,
  330.          * and also the part->instrument mapping.
  331.          */
  332.         while(eventPos)
  333.         {
  334.                 int status = (eventPos->status&0xF0)>>4;
  335.                 int channel = eventPos->status&0x0F;
  336.                 int part = channel_to_part[channel];
  337.         int velocity, pitch;
  338.         int value, controller;
  339.         int bend;
  340.         int newInst;
  341.                
  342.                 /* Check if we are running low on space... */
  343.                 if((tunePos+16) > endPos)
  344.                 {
  345.                         /* Resize our data storage. */
  346.                         Uint32          *oldTuneSequence = tuneSequence;
  347.  
  348.                         tuneSize += BUFFER_INCREMENT;
  349.                         tuneSequence = (Uint32 *)realloc(tuneSequence, tuneSize * sizeof(Uint32));
  350.                         if(oldTuneSequence != tuneSequence)
  351.                                 tunePos += tuneSequence - oldTuneSequence;
  352.                         endPos = tuneSequence + tuneSize;
  353.                 }
  354.                
  355.                 switch (status)
  356.                 {
  357.                 case MIDI_STATUS_NOTE_OFF:
  358.                         assert(part>=0 && part<=31);
  359.  
  360.                         /* Keep track of the polyphony of the current part */
  361.                         part_poly[part]--;
  362.                         break;
  363.                 case MIDI_STATUS_NOTE_ON:
  364.                         if (part < 0)
  365.                         {
  366.                                 /* If no part is specified yet, we default to the first instrument, which
  367.                                    is piano (or the first drum kit if we are on the drum channel)
  368.                                 */
  369.                                 int newInst;
  370.                                
  371.                                 if (channel == 9)
  372.                                         newInst = kFirstDrumkit + 1;            /* the first drum kit is the "no drum" kit! */
  373.                                 else
  374.                                         newInst = kFirstGMInstrument;
  375.                                 part = channel_to_part[channel] = *numParts;
  376.                                 part_to_inst[(*numParts)++] = newInst;
  377.                         }
  378.                         /* TODO - add support for more than 32 parts using eXtended QTMA events */
  379.                         assert(part<=31);
  380.                        
  381.                         /* Decode pitch & velocity */
  382.                         pitch = eventPos->data[0];
  383.                         velocity = eventPos->data[1];
  384.                        
  385.                         if (velocity == 0)
  386.                         {
  387.                                 /* was a NOTE OFF in disguise, so we decrement the polyphony */
  388.                                 part_poly[part]--;
  389.                         }
  390.                         else
  391.                         {
  392.                                 /* Keep track of the polyphony of the current part */
  393.                                 int foo = ++part_poly[part];
  394.                                 if (part_poly_max[part] < foo)
  395.                                         part_poly_max[part] = foo;
  396.  
  397.                                 /* Now scan forward to find the matching NOTE OFF event */
  398.                                 for(noteOffPos = eventPos; noteOffPos; noteOffPos = noteOffPos->next)
  399.                                 {
  400.                                         if ((noteOffPos->status&0xF0)>>4 == MIDI_STATUS_NOTE_OFF
  401.                                                 && channel == (eventPos->status&0x0F)
  402.                                                 && pitch == noteOffPos->data[0])
  403.                                                 break;
  404.                                         /* NOTE ON with velocity == 0 is the same as a NOTE OFF */
  405.                                         if ((noteOffPos->status&0xF0)>>4 == MIDI_STATUS_NOTE_ON
  406.                                                 && channel == (eventPos->status&0x0F)
  407.                                                 && pitch == noteOffPos->data[0]
  408.                                                 && 0 == noteOffPos->data[1])
  409.                                                 break;
  410.                                 }
  411.                                
  412.                                 /* Did we find a note off? Should always be the case, but who knows... */
  413.                                 if (noteOffPos)
  414.                                 {
  415.                                         /* We found a NOTE OFF, now calculate the note duration */
  416.                                         int duration = (int)((noteOffPos->time - eventPos->time)*tick);
  417.                                        
  418.                                         REST_IF_NECESSARY();
  419.                                         /* Now we need to check if we get along with a normal Note Event, or if we need an extended one... */
  420.                                         if (duration < 2048 && pitch>=32 && pitch<=95 && velocity>=0 && velocity<=127)
  421.                                         {
  422.                                                 qtma_StuffNoteEvent(*tunePos, part, pitch, velocity, duration);
  423.                                                 tunePos++;
  424.                                         }
  425.                                         else
  426.                                         {
  427.                                                 qtma_StuffXNoteEvent(*tunePos, *(tunePos+1), part, pitch, velocity, duration);
  428.                                                 tunePos+=2;
  429.                                         }
  430.                                 }
  431.                         }
  432.                         break;
  433.                 case MIDI_STATUS_AFTERTOUCH:
  434.                         /* NYI - use kControllerAfterTouch. But how are the parameters to be mapped? */
  435.                         break;
  436.                 case MIDI_STATUS_CONTROLLER:
  437.                         controller = eventPos->data[0];
  438.                         value = eventPos->data[1];
  439.  
  440.                         switch(controller)
  441.                         {
  442.                         case 0: /* bank change - igore for now */
  443.                                 break;
  444.                         case kControllerVolume:
  445.                                 if(channel_vol[channel] != value<<8)
  446.                                 {
  447.                                         channel_vol[channel] = value<<8;
  448.                                         if(part>=0 && part<=31)
  449.                                         {
  450.                                                 REST_IF_NECESSARY();
  451.                                                 qtma_StuffControlEvent(*tunePos, part, kControllerVolume, channel_vol[channel]);
  452.                                                 tunePos++;
  453.                                         }
  454.                                 }
  455.                                 break;
  456.                         case kControllerPan:
  457.                                 if(channel_pan[channel] != (value << 1) + 256)
  458.                                 {
  459.                                         channel_pan[channel] = (value << 1) + 256;
  460.                                         if(part>=0 && part<=31)
  461.                                         {
  462.                                                 REST_IF_NECESSARY();
  463.                                                 qtma_StuffControlEvent(*tunePos, part, kControllerPan, channel_pan[channel]);
  464.                                                 tunePos++;
  465.                                         }
  466.                                 }
  467.                                 break;
  468.                         default:
  469.                                 /* No other controllers implemented yet */;
  470.                                 break;
  471.                         }
  472.                        
  473.                         break;
  474.                 case MIDI_STATUS_PROG_CHANGE:
  475.                         /* Instrument changed */
  476.                         newInst = eventPos->data[0];
  477.                        
  478.                         /* Channel 9 (the 10th channel) is different, it indicates a drum kit */
  479.                         if (channel == 9)
  480.                                 newInst += kFirstDrumkit;
  481.                         else
  482.                                 newInst += kFirstGMInstrument;
  483.                         /* Only if the instrument for this channel *really* changed, add a new part. */
  484.                         if(newInst != part_to_inst[part])
  485.                         {
  486.                                 /* TODO maybe make use of kGeneralEventPartChange here,
  487.                                    to help QT reuse note channels?
  488.                                 */
  489.                                 part = channel_to_part[channel] = *numParts;
  490.                                 part_to_inst[(*numParts)++] = newInst;
  491.  
  492.                                 if(channel_vol[channel] >= 0)
  493.                                 {
  494.                                         REST_IF_NECESSARY();
  495.                                         qtma_StuffControlEvent(*tunePos, part, kControllerVolume, channel_vol[channel]);
  496.                                         tunePos++;
  497.                                 }
  498.                                 if(channel_pan[channel] >= 0)
  499.                                 {
  500.                                         REST_IF_NECESSARY();
  501.                                         qtma_StuffControlEvent(*tunePos, part, kControllerPan, channel_pan[channel]);
  502.                                         tunePos++;
  503.                                 }
  504.                                 if(channel_pitch_bend[channel] >= 0)
  505.                                 {
  506.                                         REST_IF_NECESSARY();
  507.                                         qtma_StuffControlEvent(*tunePos, part, kControllerPitchBend, channel_pitch_bend[channel]);
  508.                                         tunePos++;
  509.                                 }                      
  510.                         }
  511.                         break;
  512.                 case MIDI_STATUS_PRESSURE:
  513.                         /* NYI */
  514.                         break;
  515.                 case MIDI_STATUS_PITCH_WHEEL:
  516.                         /* In the midi spec, 0x2000 = center, 0x0000 = - 2 semitones, 0x3FFF = +2 semitones
  517.                            but for QTMA, we specify it as a 8.8 fixed point of semitones
  518.                            TODO: detect "pitch bend range changes" & honor them!
  519.                         */
  520.                         bend = (eventPos->data[0] & 0x7f) | ((eventPos->data[1] & 0x7f) << 7);
  521.                        
  522.                         /* "Center" the bend */
  523.                         bend -= 0x2000;
  524.                        
  525.                         /* Move it to our format: */
  526.                         bend <<= 4;
  527.                        
  528.                         /* If it turns out the pitch bend didn't change, stop here */
  529.                         if(channel_pitch_bend[channel] == bend)
  530.                                 break;
  531.                        
  532.                         channel_pitch_bend[channel] = bend;
  533.                         if(part>=0 && part<=31)
  534.                         {
  535.                                 /* Stuff a control event */
  536.                                 REST_IF_NECESSARY();
  537.                                 qtma_StuffControlEvent(*tunePos, part, kControllerPitchBend, bend);
  538.                                 tunePos++;
  539.                         }                      
  540.                         break;
  541.                 case MIDI_STATUS_SYSEX:
  542.                         if (eventPos->status == 0xFF && eventPos->data[0] == 0x51) /* Tempo change */
  543.                         {
  544.                                 tempo = (eventPos->extraData[0] << 16) +
  545.                                         (eventPos->extraData[1] << 8) +
  546.                                         eventPos->extraData[2];
  547.                                
  548.                                 tick = tempo * Ippqn;
  549.                         }
  550.                         break;
  551.                 }
  552.                
  553.                 /* on to the next event */
  554.                 eventPos = eventPos->next;
  555.         }
  556.        
  557.         /* Finally, place an end marker */
  558.         *tunePos = kEndMarkerValue;
  559.        
  560.         return tuneSequence;
  561. }
  562.  
  563. Uint32 *BuildTuneHeader(int part_poly_max[32], int part_to_inst[32], int numParts)
  564. {
  565.         Uint32                  *myHeader;
  566.         Uint32                  *myPos1, *myPos2;               /* pointers to the head and tail long words of a music event */
  567.         NoteRequest             *myNoteRequest;
  568.         NoteAllocator   myNoteAllocator;                /* for the NAStuffToneDescription call */
  569.         ComponentResult myErr = noErr;
  570.         int                             part;
  571.  
  572.         myHeader = NULL;
  573.         myNoteAllocator = NULL;
  574.  
  575.         /*
  576.          * Open up the Note Allocator
  577.          */
  578.         myNoteAllocator = OpenDefaultComponent(kNoteAllocatorComponentType,0);
  579.         if (myNoteAllocator == NULL)
  580.                 goto bail;
  581.        
  582.         /*
  583.          * Allocate space for the tune header
  584.          */
  585.         myHeader = (Uint32 *)
  586.                         NewPtrClear((numParts * kNoteRequestEventLength + kMarkerEventLength) * sizeof(Uint32));
  587.         if (myHeader == NULL)
  588.                 goto bail;
  589.        
  590.         myPos1 = myHeader;
  591.        
  592.         /*
  593.          * Loop over all parts
  594.          */
  595.         for(part = 0; part < numParts; ++part)
  596.         {
  597.                 /*
  598.                  * Stuff request for the instrument with the given polyphony
  599.                  */
  600.                 myPos2 = myPos1 + (kNoteRequestEventLength - 1); /* last longword of general event */
  601.                 qtma_StuffGeneralEvent(*myPos1, *myPos2, part, kGeneralEventNoteRequest, kNoteRequestEventLength);
  602.                 myNoteRequest = (NoteRequest *)(myPos1 + 1);
  603.                 myNoteRequest->info.flags = 0;
  604.                 /* I'm told by the Apple people that the Quicktime types were poorly designed and it was
  605.                  * too late to change them. On little endian, the BigEndian(Short|Fixed) types are structs
  606.                  * while on big endian they are primitive types. Furthermore, Quicktime failed to
  607.                  * provide setter and getter functions. To get this to work, we need to case the
  608.                  * code for the two possible situations.
  609.                  * My assumption is that the right-side value was always expected to be BigEndian
  610.                  * as it was written way before the Universal Binary transition. So in the little endian
  611.                  * case, OSSwap is used.
  612.                  */
  613. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  614.                 myNoteRequest->info.polyphony.bigEndianValue = OSSwapHostToBigInt16(part_poly_max[part]);
  615.                 myNoteRequest->info.typicalPolyphony.bigEndianValue = OSSwapHostToBigInt32(0x00010000);
  616. #else
  617.                 myNoteRequest->info.polyphony = part_poly_max[part];
  618.                 myNoteRequest->info.typicalPolyphony = 0x00010000;
  619. #endif
  620.                 myErr = NAStuffToneDescription(myNoteAllocator,part_to_inst[part],&myNoteRequest->tone);
  621.                 if (myErr != noErr)
  622.                         goto bail;
  623.                
  624.                 /* move pointer to beginning of next event */
  625.                 myPos1 += kNoteRequestEventLength;
  626.         }
  627.  
  628.         *myPos1 = kEndMarkerValue;              /* end of sequence marker */
  629.  
  630.  
  631. bail:
  632.         if(myNoteAllocator)
  633.                 CloseComponent(myNoteAllocator);
  634.  
  635.         /* if we encountered an error, dispose of the storage we allocated and return NULL */
  636.         if (myErr != noErr) {
  637.                 DisposePtr((Ptr)myHeader);
  638.                 myHeader = NULL;
  639.         }
  640.  
  641.         return myHeader;
  642. }
  643.  
  644. #endif /* MacOS native MIDI support */
  645.